1 /* ioctl commands which must be done in the C library.
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
22 #include <sys/ioctl.h>
23 #include <hurd/ioctl.h>
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)
32 /* Look up REQUEST in the set of handlers. */
34 _hurd_lookup_ioctl_handler (int request)
37 const struct ioctl_handler *h;
39 for (ptr = symbol_set_first_element (_hurd_ioctl_handler_lists);
40 !symbol_set_end_p (_hurd_ioctl_handler_lists, ptr);
42 for (h = *ptr; h != NULL; h = h->next)
43 if (request >= h->first_request && request <= h->last_request)
51 /* Find out how many bytes may be read from FD without blocking. */
60 *(volatile int *) arg = *arg;
70 mach_msg_type_number_t navail;
71 err = HURD_DPORT_USE (fd, __io_readable (port, &navail));
78 err = HURD_DPORT_USE (fd, (*arg ?
79 __io_set_some_openmodes :
80 __io_clear_some_openmodes)
85 err = HURD_DPORT_USE (fd, (*arg ?
86 __io_set_some_openmodes :
87 __io_clear_some_openmodes)
92 err = HURD_DPORT_USE (fd, __io_mod_owner (port, *arg));
96 err = HURD_DPORT_USE (fd, __io_get_owner (port, arg));
100 return err ? __hurd_fail (err) : 0;
103 _HURD_HANDLE_IOCTLS (fioctl, FIOGETOWN, FIONREAD);
115 return __hurd_fail (EGRATUITOUS);
124 return __fcntl (fd, F_SETFD, flag);
126 _HURD_HANDLE_IOCTLS (fioclex, FIOCLEX, FIONCLEX);
128 #include <hurd/term.h>
131 rectty_dtable (mach_port_t cttyid)
136 __mutex_lock (&_hurd_dtable_lock);
138 for (i = 0; i < _hurd_dtablesize; ++i)
140 struct hurd_fd *const d = _hurd_dtable[i];
144 /* Nothing to do for an unused descriptor cell. */
147 if (cttyid == MACH_PORT_NULL)
148 /* We now have no controlling tty at all. */
149 newctty = MACH_PORT_NULL;
151 HURD_PORT_USE (&d->port,
153 /* Get the io object's cttyid port. */
154 if (! __term_getctty (port, &id))
156 if (id == cttyid && /* Is it ours? */
157 /* Get the ctty io port. */
158 __term_open_ctty (port,
159 _hurd_pid, _hurd_pgrp,
161 /* XXX it is our ctty but the call failed? */
162 newctty = MACH_PORT_NULL;
163 __mach_port_deallocate
164 (__mach_task_self (), (mach_port_t) id);
167 newctty = MACH_PORT_NULL;
171 /* Install the new ctty port. */
172 _hurd_port_set (&d->ctty, newctty);
175 __mutex_unlock (&_hurd_dtable_lock);
180 /* Called when we have received a message saying to use a new ctty ID port. */
183 _hurd_setcttyid (mach_port_t cttyid)
187 if (cttyid != MACH_PORT_NULL)
189 /* Give the new send right a user reference.
190 This is a good way to check that it is valid. */
191 if (err = __mach_port_mod_refs (__mach_task_self (), cttyid,
192 MACH_PORT_RIGHT_SEND, 1))
196 /* Install the port, consuming the reference we just created. */
197 _hurd_port_set (&_hurd_ports[INIT_PORT_CTTYID], cttyid);
199 /* Reset all the ctty ports in all the descriptors. */
200 __USEPORT (CTTYID, (rectty_dtable (cttyid), 0));
206 /* Make FD be the controlling terminal.
207 This function is called for `ioctl (fd, TCIOSCTTY)'. */
211 int request) /* Always TIOCSCTTY. */
216 /* Get FD's cttyid port, unless it is already ours. */
217 err = HURD_DPORT_USE (fd, ctty != MACH_PORT_NULL ? EADDRINUSE :
218 __term_getctty (port, &cttyid));
219 if (err == EADDRINUSE)
220 /* FD is already the ctty. Nothing to do. */
223 return __hurd_fail (err);
225 /* Make it our own. */
226 _hurd_port_set (&_hurd_ports[INIT_PORT_CTTYID], cttyid);
228 /* Reset all the ctty ports in all the descriptors. */
229 __USEPORT (CTTYID, (rectty_dtable (cttyid), 0));
233 _HURD_HANDLE_IOCTL (tiocsctty, TIOCSCTTY);
235 /* Dissociate from the controlling terminal. */
239 int request) /* Always TIOCNOTTY. */
241 mach_port_t fd_cttyid;
244 if (err = HURD_DPORT_USE (fd, __term_getctty (port, &fd_cttyid)))
245 return __hurd_fail (err);
247 if (__USEPORT (CTTYID, port != fd_cttyid))
250 __mach_port_deallocate (__mach_task_self (), fd_cttyid);
253 return __hurd_fail (err);
255 /* Clear our cttyid port cell. */
256 _hurd_port_set (&_hurd_ports[INIT_PORT_CTTYID], MACH_PORT_NULL);
258 /* Reset all the ctty ports in all the descriptors. */
260 __USEPORT (CTTYID, (rectty_dtable (MACH_PORT_NULL), 0));
264 _HURD_HANDLE_IOCTL (tiocnotty, TIOCNOTTY);