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