chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / hurd / hurd / fd.h
1 /* File descriptors.
2    Copyright (C) 1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2006,2007
3         Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #ifndef _HURD_FD_H
22
23 #define _HURD_FD_H      1
24 #include <features.h>
25
26 #include <cthreads.h>
27
28 #include <hurd/hurd_types.h>
29 #include <hurd/port.h>
30 #include <sys/socket.h>
31
32
33 /* Structure representing a file descriptor.  */
34
35 struct hurd_fd
36   {
37     struct hurd_port port;      /* io server port.  */
38     int flags;                  /* fcntl flags; locked by port.lock.  */
39
40     /* Normal port to the ctty.  When `port' is our ctty, this is a port to
41        the same io object but which never returns EBACKGROUND; when not,
42        this is nil.  */
43     struct hurd_port ctty;
44   };
45
46
47 /* Current file descriptor table.  */
48
49 extern int _hurd_dtablesize;
50 extern struct hurd_fd **_hurd_dtable;
51 extern struct mutex _hurd_dtable_lock; /* Locks those two variables.  */
52 \f
53 #include <hurd/signal.h>
54
55 #ifndef _HURD_FD_H_EXTERN_INLINE
56 #define _HURD_FD_H_EXTERN_INLINE __extern_inline
57 #endif
58
59 /* Returns the descriptor cell for FD.  If FD is invalid or unused, return
60    NULL.  The cell is unlocked; when ready to use it, lock it and check for
61    it being unused.  */
62
63 _HURD_FD_H_EXTERN_INLINE struct hurd_fd *
64 _hurd_fd_get (int fd)
65 {
66   struct hurd_fd *descriptor;
67
68   __mutex_lock (&_hurd_dtable_lock);
69   if (fd < 0 || fd >= _hurd_dtablesize)
70     descriptor = NULL;
71   else
72     {
73       struct hurd_fd *cell = _hurd_dtable[fd];
74       if (cell == NULL)
75         /* No descriptor allocated at this index.  */
76         descriptor = NULL;
77       else
78         {
79           __spin_lock (&cell->port.lock);
80           if (cell->port.port == MACH_PORT_NULL)
81             /* The descriptor at this index has no port in it.
82                This happens if it existed before but was closed.  */
83             descriptor = NULL;
84           else
85             descriptor = cell;
86           __spin_unlock (&cell->port.lock);
87         }
88     }
89   __mutex_unlock (&_hurd_dtable_lock);
90
91   return descriptor;
92 }
93
94
95 /* Evaluate EXPR with the variable `descriptor' bound to a pointer to the
96    file descriptor structure for FD.   */
97
98 #define HURD_FD_USE(fd, expr)                                                 \
99   ({ struct hurd_fd *descriptor = _hurd_fd_get (fd);                          \
100      descriptor == NULL ? EBADF : (expr); })
101
102 /* Evaluate EXPR with the variable `port' bound to the port to FD, and
103    `ctty' bound to the ctty port.  */
104
105 #define HURD_DPORT_USE(fd, expr) \
106   HURD_FD_USE ((fd), HURD_FD_PORT_USE (descriptor, (expr)))
107
108 /* Likewise, but FD is a pointer to the file descriptor structure.  */
109
110 #define HURD_FD_PORT_USE(fd, expr)                                            \
111   ({ error_t __result;                                                        \
112      struct hurd_fd *const __d = (fd);                                        \
113      struct hurd_userlink __ulink, __ctty_ulink;                              \
114      io_t port, ctty;                                                         \
115      void *crit = _hurd_critical_section_lock ();                             \
116      __spin_lock (&__d->port.lock);                                           \
117      if (__d->port.port == MACH_PORT_NULL)                                    \
118        {                                                                      \
119          __spin_unlock (&__d->port.lock);                                     \
120          _hurd_critical_section_unlock (crit);                                \
121          __result = EBADF;                                                    \
122        }                                                                      \
123      else                                                                     \
124        {                                                                      \
125          ctty = _hurd_port_get (&__d->ctty, &__ctty_ulink);                   \
126          port = _hurd_port_locked_get (&__d->port, &__ulink);                 \
127          _hurd_critical_section_unlock (crit);                                \
128          __result = (expr);                                                   \
129          _hurd_port_free (&__d->port, &__ulink, port);                        \
130          if (ctty != MACH_PORT_NULL)                                          \
131            _hurd_port_free (&__d->ctty, &__ctty_ulink, ctty);                 \
132        }                                                                      \
133      __result; })
134 \f
135 #include <errno.h>
136
137 /* Check if ERR should generate a signal.
138    Returns the signal to take, or zero if none.  */
139
140 _HURD_FD_H_EXTERN_INLINE int
141 _hurd_fd_error_signal (error_t err)
142 {
143   switch (err)
144     {
145     case EMACH_SEND_INVALID_DEST:
146     case EMIG_SERVER_DIED:
147       /* The server has disappeared!  */
148       return SIGLOST;
149     case EPIPE:
150       return SIGPIPE;
151     default:
152       /* Having a default case avoids -Wenum-switch warnings.  */
153       return 0;
154     }
155 }
156
157 /* Handle an error from an RPC on a file descriptor's port.  You should
158    always use this function to handle errors from RPCs made on file
159    descriptor ports.  Some errors are translated into signals.  */
160
161 _HURD_FD_H_EXTERN_INLINE error_t
162 _hurd_fd_error (int fd, error_t err)
163 {
164   int signo = _hurd_fd_error_signal (err);
165   if (signo)
166     {
167       const struct hurd_signal_detail detail
168         = { code: fd, error: err, exc: 0 };
169       _hurd_raise_signal (NULL, signo, &detail);
170     }
171   return err;
172 }
173
174 /* Handle error code ERR from an RPC on file descriptor FD's port.
175    Set `errno' to the appropriate error code, and always return -1.  */
176
177 _HURD_FD_H_EXTERN_INLINE int
178 __hurd_dfail (int fd, error_t err)
179 {
180   errno = _hurd_fd_error (fd, err);
181   return -1;
182 }
183
184 /* Likewise, but do not raise SIGPIPE on EPIPE if flags contain
185    MSG_NOSIGNAL.  */
186
187 _HURD_FD_H_EXTERN_INLINE int
188 __hurd_sockfail (int fd, int flags, error_t err)
189 {
190   if (!(flags & MSG_NOSIGNAL) || err != EPIPE)
191     err = _hurd_fd_error (fd, err);
192   errno = err;
193   return -1;
194 }
195 \f
196 /* Set up *FD to have PORT its server port, doing appropriate ctty magic.
197    Does no locking or unlocking.  */
198
199 extern void _hurd_port2fd (struct hurd_fd *fd, io_t port, int flags);
200
201 /* Allocate a new file descriptor and install PORT in it (doing any
202    appropriate ctty magic); consumes a user reference on PORT.  FLAGS are
203    as for `open'; only O_IGNORE_CTTY is meaningful, but all are saved.
204
205    If the descriptor table is full, set errno, and return -1.
206    If DEALLOC is nonzero, deallocate PORT first.  */
207
208 extern int _hurd_intern_fd (io_t port, int flags, int dealloc);
209
210 /* Allocate a new file descriptor in the table and return it, locked.  The
211    new descriptor number will be no less than FIRST_FD.  If the table is
212    full, set errno to EMFILE and return NULL.  If FIRST_FD is negative or
213    bigger than the size of the table, set errno to EINVAL and return NULL.  */
214
215 extern struct hurd_fd *_hurd_alloc_fd (int *fd_ptr, int first_fd);
216
217 /* Allocate a new file descriptor structure and initialize its port cells
218    with PORT and CTTY.  (This does not affect the descriptor table.)  */
219
220 extern struct hurd_fd *_hurd_new_fd (io_t port, io_t ctty);
221
222 /* Close a file descriptor, making it available for future reallocation.  */
223
224 extern error_t _hurd_fd_close (struct hurd_fd *fd);
225
226 /* Read and write data from a file descriptor; just like `read' and `write'
227    if OFFSET is -1, or like `pread' and `pwrite' if OFFSET is not -1.
228    If successful, stores the amount actually read or written in *NBYTES.  */
229
230 extern error_t _hurd_fd_read (struct hurd_fd *fd,
231                               void *buf, size_t *nbytes, loff_t offset);
232 extern error_t _hurd_fd_write (struct hurd_fd *fd,
233                                const void *buf, size_t *nbytes, loff_t offset);
234
235
236 /* Call *RPC on PORT and/or CTTY; if a call on CTTY returns EBACKGROUND,
237    generate SIGTTIN/SIGTTOU or EIO as appropriate.  */
238
239 extern error_t _hurd_ctty_input (io_t port, io_t ctty, error_t (*rpc) (io_t));
240 extern error_t _hurd_ctty_output (io_t port, io_t ctty, error_t (*rpc) (io_t));
241
242
243 /* The guts of `select' and `poll'.  Check the first NFDS descriptors
244    either in POLLFDS (if nonnull) or in each of READFDS, WRITEFDS,
245    EXCEPTFDS that is nonnull.  If TIMEOUT is not NULL, time out after
246    waiting the interval specified therein.  If SIGMASK is nonnull,
247    the set of blocked signals is temporarily set to that during this call.
248    Returns the number of ready descriptors, or -1 for errors.  */
249 struct pollfd;
250 struct timespec;
251 extern int _hurd_select (int nfds, struct pollfd *pollfds,
252                          fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
253                          const struct timespec *timeout,
254                          const sigset_t *sigmask);
255
256 /* Variant of file_name_lookup used in *at function implementations.
257    AT_FLAGS may only contain AT_SYMLINK_FOLLOW or AT_SYMLINK_NOFOLLOW,
258    which will remove and add O_NOLINK from FLAGS respectively.
259    Other bits cause EINVAL.  */
260 extern file_t __file_name_lookup_at (int fd, int at_flags,
261                                      const char *file_name,
262                                      int flags, mode_t mode);
263
264 /* Variant of file_name_split used in *at function implementations.  */
265 extern file_t __file_name_split_at (int fd, const char *file_name,
266                                     char **name);
267
268 /* Variant of directory_name_split used in *at function implementations.  */
269 extern file_t __directory_name_split_at (int fd, const char *directory_name,
270                                          char **name);
271
272
273
274 #endif  /* hurd/fd.h */