]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | /* ioctl commands which must be done in the C library. |
56f778c9 | 2 | Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc. |
28f540f4 RM |
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 | |
17 | not, write to the Free Software Foundation, Inc., 675 Mass Ave, | |
18 | Cambridge, MA 02139, USA. */ | |
19 | ||
20 | #include <hurd.h> | |
21 | #include <hurd/fd.h> | |
22 | #include <sys/ioctl.h> | |
23 | #include <hurd/ioctl.h> | |
24 | ||
25 | ||
26 | ||
27 | /* Symbol set of ioctl handler lists. If there are user-registered | |
28 | handlers, one of these lists will contain them. The other lists are | |
29 | handlers built into the library. */ | |
30 | symbol_set_define (_hurd_ioctl_handler_lists) | |
31 | ||
32 | /* Look up REQUEST in the set of handlers. */ | |
33 | ioctl_handler_t | |
34 | _hurd_lookup_ioctl_handler (int request) | |
35 | { | |
36 | void *const *ptr; | |
37 | const struct ioctl_handler *h; | |
38 | ||
39 | for (ptr = symbol_set_first_element (_hurd_ioctl_handler_lists); | |
40 | !symbol_set_end_p (_hurd_ioctl_handler_lists, ptr); | |
41 | ++ptr) | |
42 | for (h = *ptr; h != NULL; h = h->next) | |
43 | if (request >= h->first_request && request <= h->last_request) | |
44 | return h->handler; | |
45 | ||
46 | return NULL; | |
47 | } | |
48 | \f | |
49 | #include <fcntl.h> | |
50 | ||
51 | /* Find out how many bytes may be read from FD without blocking. */ | |
52 | ||
53 | static int | |
54 | fioctl (int fd, | |
55 | int request, | |
56 | int *arg) | |
57 | { | |
58 | error_t err; | |
59 | ||
60 | *(volatile int *) arg = *arg; | |
61 | ||
62 | switch (request) | |
63 | { | |
64 | default: | |
65 | err = EGRATUITOUS; | |
66 | break; | |
67 | ||
68 | case FIONREAD: | |
69 | { | |
70 | mach_msg_type_number_t navail; | |
71 | err = HURD_DPORT_USE (fd, __io_readable (port, &navail)); | |
72 | if (!err) | |
73 | *arg = (int) navail; | |
74 | } | |
75 | break; | |
76 | ||
77 | case FIONBIO: | |
78 | err = HURD_DPORT_USE (fd, (*arg ? | |
79 | __io_set_some_openmodes : | |
80 | __io_clear_some_openmodes) | |
81 | (port, O_NONBLOCK)); | |
82 | break; | |
83 | ||
84 | case FIOASYNC: | |
85 | err = HURD_DPORT_USE (fd, (*arg ? | |
86 | __io_set_some_openmodes : | |
87 | __io_clear_some_openmodes) | |
88 | (port, O_ASYNC)); | |
89 | break; | |
90 | ||
91 | case FIOSETOWN: | |
92 | err = HURD_DPORT_USE (fd, __io_mod_owner (port, *arg)); | |
93 | break; | |
94 | ||
95 | case FIOGETOWN: | |
96 | err = HURD_DPORT_USE (fd, __io_get_owner (port, arg)); | |
97 | break; | |
98 | } | |
99 | ||
100 | return err ? __hurd_fail (err) : 0; | |
101 | } | |
102 | ||
103 | _HURD_HANDLE_IOCTLS (fioctl, FIOGETOWN, FIONREAD); | |
104 | ||
105 | ||
106 | static int | |
107 | fioclex (int fd, | |
108 | int request) | |
109 | { | |
110 | int flag; | |
111 | ||
112 | switch (request) | |
113 | { | |
114 | default: | |
115 | return __hurd_fail (EGRATUITOUS); | |
116 | case FIOCLEX: | |
117 | flag = FD_CLOEXEC; | |
118 | break; | |
119 | case FIONCLEX: | |
120 | flag = 0; | |
121 | break; | |
122 | } | |
123 | ||
124 | return __fcntl (fd, F_SETFD, flag); | |
125 | } | |
126 | _HURD_HANDLE_IOCTLS (fioclex, FIOCLEX, FIONCLEX); | |
127 | \f | |
128 | #include <hurd/term.h> | |
56f778c9 | 129 | #include <hurd/tioctl.h> |
28f540f4 RM |
130 | |
131 | static void | |
132 | rectty_dtable (mach_port_t cttyid) | |
133 | { | |
134 | int i; | |
56f778c9 | 135 | |
28f540f4 RM |
136 | HURD_CRITICAL_BEGIN; |
137 | __mutex_lock (&_hurd_dtable_lock); | |
138 | ||
139 | for (i = 0; i < _hurd_dtablesize; ++i) | |
140 | { | |
141 | struct hurd_fd *const d = _hurd_dtable[i]; | |
142 | mach_port_t newctty; | |
143 | ||
144 | if (d == NULL) | |
145 | /* Nothing to do for an unused descriptor cell. */ | |
146 | continue; | |
147 | ||
148 | if (cttyid == MACH_PORT_NULL) | |
149 | /* We now have no controlling tty at all. */ | |
150 | newctty = MACH_PORT_NULL; | |
151 | else | |
152 | HURD_PORT_USE (&d->port, | |
153 | ({ mach_port_t id; | |
154 | /* Get the io object's cttyid port. */ | |
155 | if (! __term_getctty (port, &id)) | |
156 | { | |
157 | if (id == cttyid && /* Is it ours? */ | |
158 | /* Get the ctty io port. */ | |
159 | __term_open_ctty (port, | |
160 | _hurd_pid, _hurd_pgrp, | |
161 | &newctty)) | |
162 | /* XXX it is our ctty but the call failed? */ | |
163 | newctty = MACH_PORT_NULL; | |
164 | __mach_port_deallocate | |
165 | (__mach_task_self (), (mach_port_t) id); | |
166 | } | |
167 | else | |
168 | newctty = MACH_PORT_NULL; | |
169 | 0; | |
170 | })); | |
171 | ||
172 | /* Install the new ctty port. */ | |
173 | _hurd_port_set (&d->ctty, newctty); | |
174 | } | |
175 | ||
176 | __mutex_unlock (&_hurd_dtable_lock); | |
177 | HURD_CRITICAL_END; | |
178 | } | |
179 | ||
180 | ||
181 | /* Called when we have received a message saying to use a new ctty ID port. */ | |
182 | ||
183 | error_t | |
184 | _hurd_setcttyid (mach_port_t cttyid) | |
185 | { | |
186 | error_t err; | |
187 | ||
188 | if (cttyid != MACH_PORT_NULL) | |
189 | { | |
190 | /* Give the new send right a user reference. | |
191 | This is a good way to check that it is valid. */ | |
192 | if (err = __mach_port_mod_refs (__mach_task_self (), cttyid, | |
193 | MACH_PORT_RIGHT_SEND, 1)) | |
194 | return err; | |
195 | } | |
196 | ||
197 | /* Install the port, consuming the reference we just created. */ | |
198 | _hurd_port_set (&_hurd_ports[INIT_PORT_CTTYID], cttyid); | |
199 | ||
200 | /* Reset all the ctty ports in all the descriptors. */ | |
201 | __USEPORT (CTTYID, (rectty_dtable (cttyid), 0)); | |
202 | ||
203 | return 0; | |
204 | } | |
205 | ||
206 | ||
207 | /* Make FD be the controlling terminal. | |
208 | This function is called for `ioctl (fd, TCIOSCTTY)'. */ | |
209 | ||
210 | static int | |
211 | tiocsctty (int fd, | |
212 | int request) /* Always TIOCSCTTY. */ | |
213 | { | |
214 | mach_port_t cttyid; | |
215 | error_t err; | |
216 | ||
217 | /* Get FD's cttyid port, unless it is already ours. */ | |
218 | err = HURD_DPORT_USE (fd, ctty != MACH_PORT_NULL ? EADDRINUSE : | |
219 | __term_getctty (port, &cttyid)); | |
220 | if (err == EADDRINUSE) | |
221 | /* FD is already the ctty. Nothing to do. */ | |
222 | return 0; | |
223 | else if (err) | |
224 | return __hurd_fail (err); | |
225 | ||
56f778c9 RM |
226 | /* Change the terminal's pgrp to ours. */ |
227 | err = HURD_DPORT_USE (fd, __tioctl_tiocspgrp (port, _hurd_pgrp)); | |
228 | if (err) | |
229 | return __hurd_fail (err); | |
230 | ||
28f540f4 RM |
231 | /* Make it our own. */ |
232 | _hurd_port_set (&_hurd_ports[INIT_PORT_CTTYID], cttyid); | |
233 | ||
234 | /* Reset all the ctty ports in all the descriptors. */ | |
235 | __USEPORT (CTTYID, (rectty_dtable (cttyid), 0)); | |
236 | ||
237 | return 0; | |
238 | } | |
239 | _HURD_HANDLE_IOCTL (tiocsctty, TIOCSCTTY); | |
240 | ||
241 | /* Dissociate from the controlling terminal. */ | |
242 | ||
243 | static int | |
244 | tiocnotty (int fd, | |
245 | int request) /* Always TIOCNOTTY. */ | |
246 | { | |
247 | mach_port_t fd_cttyid; | |
248 | error_t err; | |
249 | ||
250 | if (err = HURD_DPORT_USE (fd, __term_getctty (port, &fd_cttyid))) | |
251 | return __hurd_fail (err); | |
252 | ||
253 | if (__USEPORT (CTTYID, port != fd_cttyid)) | |
254 | err = EINVAL; | |
255 | ||
256 | __mach_port_deallocate (__mach_task_self (), fd_cttyid); | |
257 | ||
258 | if (err) | |
259 | return __hurd_fail (err); | |
260 | ||
261 | /* Clear our cttyid port cell. */ | |
262 | _hurd_port_set (&_hurd_ports[INIT_PORT_CTTYID], MACH_PORT_NULL); | |
263 | ||
264 | /* Reset all the ctty ports in all the descriptors. */ | |
56f778c9 | 265 | |
28f540f4 RM |
266 | __USEPORT (CTTYID, (rectty_dtable (MACH_PORT_NULL), 0)); |
267 | ||
268 | return 0; | |
269 | } | |
270 | _HURD_HANDLE_IOCTL (tiocnotty, TIOCNOTTY); |