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