Merge branch 'elf-move'
[platform/upstream/glibc.git] / hurd / hurdioctl.c
1 /* ioctl commands which must be done in the C library.
2    Copyright (C) 1994,95,96,97,99,2001,2002,2009,2010
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, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <hurd.h>
21 #include <hurd/fd.h>
22 #include <sys/ioctl.h>
23 #include <hurd/ioctl.h>
24 #include <string.h>
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   /* Mask off the type bits, so that we see requests in a single group as a
40      contiguous block of values.  */
41   request = _IOC_NOTYPE (request);
42
43   for (ptr = symbol_set_first_element (_hurd_ioctl_handler_lists);
44        !symbol_set_end_p (_hurd_ioctl_handler_lists, ptr);
45        ++ptr)
46     for (h = *ptr; h != NULL; h = h->next)
47       if (request >= h->first_request && request <= h->last_request)
48         return h->handler;
49
50   return NULL;
51 }
52 \f
53 #include <fcntl.h>
54
55 /* Find out how many bytes may be read from FD without blocking.  */
56
57 static int
58 fioctl (int fd,
59         int request,
60         int *arg)
61 {
62   error_t err;
63
64   *(volatile int *) arg = *arg;
65
66   switch (request)
67     {
68     default:
69       err = ENOTTY;
70       break;
71
72     case FIONREAD:
73       {
74         mach_msg_type_number_t navail;
75         err = HURD_DPORT_USE (fd, __io_readable (port, &navail));
76         if (!err)
77           *arg = (int) navail;
78       }
79       break;
80
81     case FIONBIO:
82       err = HURD_DPORT_USE (fd, (*arg ?
83                                  __io_set_some_openmodes :
84                                  __io_clear_some_openmodes)
85                             (port, O_NONBLOCK));
86       break;
87
88     case FIOASYNC:
89       err = HURD_DPORT_USE (fd, (*arg ?
90                                  __io_set_some_openmodes :
91                                  __io_clear_some_openmodes)
92                             (port, O_ASYNC));
93       break;
94
95     case FIOSETOWN:
96       err = HURD_DPORT_USE (fd, __io_mod_owner (port, *arg));
97       break;
98
99     case FIOGETOWN:
100       err = HURD_DPORT_USE (fd, __io_get_owner (port, arg));
101       break;
102     }
103
104   return err ? __hurd_dfail (fd, err) : 0;
105 }
106
107 _HURD_HANDLE_IOCTLS (fioctl, FIOGETOWN, FIONREAD);
108
109
110 static int
111 fioclex (int fd,
112          int request)
113 {
114   int flag;
115
116   switch (request)
117     {
118     default:
119       return __hurd_fail (ENOTTY);
120     case FIOCLEX:
121       flag = FD_CLOEXEC;
122       break;
123     case FIONCLEX:
124       flag = 0;
125       break;
126     }
127
128   return __fcntl (fd, F_SETFD, flag);
129 }
130 _HURD_HANDLE_IOCTLS (fioclex, FIOCLEX, FIONCLEX);
131 \f
132 #include <hurd/term.h>
133 #include <hurd/tioctl.h>
134
135 /* Install a new CTTYID port, atomically updating the dtable appropriately.
136    This consumes the send right passed in.  */
137
138 void
139 _hurd_locked_install_cttyid (mach_port_t cttyid)
140 {
141   mach_port_t old;
142   struct hurd_port *const port = &_hurd_ports[INIT_PORT_CTTYID];
143   struct hurd_userlink ulink;
144   int i;
145
146   /* Install the new cttyid port, and preserve it with a ulink.
147      We unroll the _hurd_port_set + _hurd_port_get here so that
148      there is no window where the cell is unlocked and CTTYID could
149      be changed by another thread.  (We also delay the deallocation
150      of the old port until the end, to minimize the duration of the
151      critical section.)
152
153      It is important that changing the cttyid port is only ever done by
154      holding the dtable lock continuously while updating the port cell and
155      re-ctty'ing the dtable; dtable.c assumes we do this.  Otherwise, the
156      pgrp-change notification code in dtable.c has to worry about racing
157      against us here in odd situations.  The one exception to this is
158      setsid, which holds the dtable lock while changing the pgrp and
159      clearing the cttyid port, and then unlocks the dtable lock to allow
160
161
162   */
163
164   __spin_lock (&port->lock);
165   old = _hurd_userlink_clear (&port->users) ? port->port : MACH_PORT_NULL;
166   port->port = cttyid;
167   cttyid = _hurd_port_locked_get (port, &ulink);
168
169   for (i = 0; i < _hurd_dtablesize; ++i)
170     {
171       struct hurd_fd *const d = _hurd_dtable[i];
172       mach_port_t newctty = MACH_PORT_NULL;
173
174       if (d == NULL)
175         /* Nothing to do for an unused descriptor cell.  */
176         continue;
177
178       if (cttyid != MACH_PORT_NULL)
179         /* We do have some controlling tty.  */
180         HURD_PORT_USE (&d->port,
181                        ({ mach_port_t id;
182                           /* Get the io object's cttyid port.  */
183                           if (! __term_getctty (port, &id))
184                             {
185                               if (id == cttyid /* Is it ours?  */
186                                   /* Get the ctty io port.  */
187                                   && __term_open_ctty (port,
188                                                        _hurd_pid, _hurd_pgrp,
189                                                        &newctty))
190                                 /* XXX it is our ctty but the call failed? */
191                                 newctty = MACH_PORT_NULL;
192                               __mach_port_deallocate (__mach_task_self (), id);
193                             }
194                           0;
195                         }));
196
197       /* Install the new ctty port.  */
198       _hurd_port_set (&d->ctty, newctty);
199     }
200
201   __mutex_unlock (&_hurd_dtable_lock);
202
203   if (old != MACH_PORT_NULL)
204     __mach_port_deallocate (__mach_task_self (), old);
205   _hurd_port_free (port, &ulink, cttyid);
206 }
207
208 static void
209 install_ctty (mach_port_t cttyid)
210 {
211   HURD_CRITICAL_BEGIN;
212   __mutex_lock (&_hurd_dtable_lock);
213   _hurd_locked_install_cttyid (cttyid);
214   HURD_CRITICAL_END;
215 }
216
217
218 /* Called when we have received a message saying to use a new ctty ID port.  */
219
220 error_t
221 _hurd_setcttyid (mach_port_t cttyid)
222 {
223   error_t err;
224
225   if (cttyid != MACH_PORT_NULL)
226     {
227       /* Give the new send right a user reference.
228          This is a good way to check that it is valid.  */
229       if (err = __mach_port_mod_refs (__mach_task_self (), cttyid,
230                                       MACH_PORT_RIGHT_SEND, 1))
231         return err;
232     }
233
234   /* Install the port, consuming the reference we just created.  */
235   install_ctty (cttyid);
236
237   return 0;
238 }
239
240
241 static inline error_t
242 do_tiocsctty (io_t port, io_t ctty)
243 {
244   mach_port_t cttyid;
245   error_t err;
246
247   if (ctty != MACH_PORT_NULL)
248     /* PORT is already the ctty.  Nothing to do.  */
249     return 0;
250
251   /* Get PORT's cttyid port.  */
252   err = __term_getctty (port, &cttyid);
253   if (err)
254     return err;
255
256   /* Change the terminal's pgrp to ours.  */
257   err = __tioctl_tiocspgrp (port, _hurd_pgrp);
258   if (err)
259     __mach_port_deallocate (__mach_task_self (), cttyid);
260   else
261     /* Make it our own.  */
262     install_ctty (cttyid);
263
264   return err;
265 }
266
267 /* Make FD be the controlling terminal.
268    This function is called for `ioctl (fd, TCIOSCTTY)'.  */
269
270 static int
271 tiocsctty (int fd,
272            int request)         /* Always TIOCSCTTY.  */
273 {
274   return __hurd_fail (HURD_DPORT_USE (fd, do_tiocsctty (port, ctty)));
275 }
276 _HURD_HANDLE_IOCTL (tiocsctty, TIOCSCTTY);
277
278 /* Dissociate from the controlling terminal.  */
279
280 static int
281 tiocnotty (int fd,
282            int request)         /* Always TIOCNOTTY.  */
283 {
284   mach_port_t fd_cttyid;
285   error_t err;
286
287   if (err = HURD_DPORT_USE (fd, __term_getctty (port, &fd_cttyid)))
288     return __hurd_fail (err);
289
290   if (__USEPORT (CTTYID, port != fd_cttyid))
291     err = EINVAL;
292
293   __mach_port_deallocate (__mach_task_self (), fd_cttyid);
294
295   if (err)
296     return __hurd_fail (err);
297
298   /* Clear our cttyid port.  */
299   install_ctty (MACH_PORT_NULL);
300
301   return 0;
302 }
303 _HURD_HANDLE_IOCTL (tiocnotty, TIOCNOTTY);
304 \f
305 #include <hurd/pfinet.h>
306 #include <net/if.h>
307 #include <netinet/in.h>
308
309 /* Fill in the buffer IFC->IFC_BUF of length IFC->IFC_LEN with a list
310    of ifr structures, one for each network interface.  */
311 static int
312 siocgifconf (int fd, int request, struct ifconf *ifc)
313 {
314   error_t err;
315   size_t data_len = ifc->ifc_len;
316   char *data = ifc->ifc_buf;
317
318   if (data_len <= 0)
319     return 0;
320
321   err = HURD_DPORT_USE (fd, __pfinet_siocgifconf (port, ifc->ifc_len,
322                                                   &data, &data_len));
323   if (data_len < ifc->ifc_len)
324     ifc->ifc_len = data_len;
325   if (data != ifc->ifc_buf)
326     {
327       memcpy (ifc->ifc_buf, data, ifc->ifc_len);
328       __vm_deallocate (__mach_task_self (), (vm_address_t) data, data_len);
329     }
330   return err ? __hurd_dfail (fd, err) : 0;
331 }
332 _HURD_HANDLE_IOCTL (siocgifconf, SIOCGIFCONF);