Update copyright notices with scripts/update-copyrights
[jlayton/glibc.git] / sysdeps / mach / hurd / ioctl.c
1 /* Copyright (C) 1992-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #include <errno.h>
19 #include <sys/ioctl.h>
20 #include <hurd.h>
21 #include <hurd/fd.h>
22 #include <hurd/signal.h>
23 #include <stdarg.h>
24 #include <mach/notify.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <hurd/ioctl.h>
29 #include <mach/mig_support.h>
30
31 #include <hurd/ioctls.defs>
32
33 #define typesize(type)  (1 << (type))
34
35
36 /* Perform the I/O control operation specified by REQUEST on FD.
37    The actual type and use of ARG and the return value depend on REQUEST.  */
38 int
39 __ioctl (int fd, unsigned long int request, ...)
40 {
41 #ifdef MACH_MSG_TYPE_CHAR
42   /* Map individual type fields to Mach IPC types.  */
43   static const int mach_types[] =
44     { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
45       MACH_MSG_TYPE_INTEGER_64 };
46 #define io2mach_type(count, type) \
47   ((mach_msg_type_t) { mach_types[type], typesize (type) * 8, count, 1, 0, 0 })
48 #endif
49
50   /* Extract the type information encoded in the request.  */
51   unsigned int type = _IOC_TYPE (request);
52
53   /* Message buffer.  */
54 #define msg_align(x) \
55   (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1))
56   struct
57   {
58 #ifdef MACH_MSG_TYPE_BIT
59     union
60     {
61       mig_reply_header_t header;
62       struct
63       {
64         mach_msg_header_t       Head;
65         int                     RetCodeType;
66         kern_return_t           RetCode;
67       } header_typecheck;
68     };
69     char data[3 * sizeof (mach_msg_type_t) +
70              msg_align (_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type))) +
71              msg_align (_IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type))) +
72              _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
73 #else  /* Untyped Mach IPC format.  */
74     mig_reply_error_t header;
75     char data[_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type)) +
76              _IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type)) +
77              _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
78     mach_msg_trailer_t trailer;
79 #endif
80   } msg;
81   mach_msg_header_t *const m = &msg.header.Head;
82   mach_msg_id_t msgid;
83   unsigned int reply_size;
84 #ifdef MACH_MSG_TYPE_BIT
85   mach_msg_type_t *t;
86 #else
87   void *p;
88 #endif
89
90   void *arg = NULL;
91
92   error_t err;
93
94   /* Send the RPC already packed up in MSG to IOPORT
95      and decode the return value.  */
96   error_t send_rpc (io_t ioport)
97     {
98       error_t err;
99 #ifdef MACH_MSG_TYPE_BIT
100       mach_msg_type_t *t = &msg.header.RetCodeType;
101 #else
102       void *p = &msg.header.RetCode;
103 #endif
104
105       /* Marshal the request arguments into the message buffer.
106          We must redo this work each time we retry the RPC after a SIGTTOU,
107          because the reply message containing the EBACKGROUND error code
108          clobbers the same message buffer also used for the request.  */
109
110       if (_IOC_INOUT (request) & IOC_IN)
111         {
112           /* We don't want to advance ARG since it will be used to copy out
113              too if IOC_OUT is also set.  */
114           void *argptr = arg;
115
116           /* Pack an argument into the message buffer.  */
117           void in (unsigned int count, enum __ioctl_datum type)
118             {
119               if (count > 0)
120                 {
121                   const size_t len = count * typesize ((unsigned int) type);
122 #ifdef MACH_MSG_TYPE_BIT
123                   void *p = &t[1];
124                   *t = io2mach_type (count, type);
125                   p = __mempcpy (p, argptr, len);
126                   p = (void *) (((uintptr_t) p + sizeof (*t) - 1)
127                                 & ~(sizeof (*t) - 1));
128                   t = p;
129 #else
130                   p = __mempcpy (p, argptr, len);
131 #endif
132                   argptr += len;
133                 }
134             }
135
136           /* Pack the argument data.  */
137           in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
138           in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
139           in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
140         }
141       else if (_IOC_INOUT (request) == IOC_VOID && _IOT_COUNT0 (type) != 0)
142         {
143           /* The RPC takes a single integer_t argument.
144              Rather than pointing to the value, ARG is the value itself.  */
145 #ifdef MACH_MSG_TYPE_BIT
146           *t++ = io2mach_type (1, _IOTS (integer_t));
147           *(integer_t *) t = (integer_t) arg;
148           t = (void *) t + sizeof (integer_t);
149 #else
150           *(integer_t *) p = (integer_t) arg;
151           p = (void *) p + sizeof (integer_t);
152 #endif
153         }
154
155       memset (m, 0, sizeof *m); /* Clear unused fields.  */
156       m->msgh_size = (
157 #ifdef MACH_MSG_TYPE_BIT
158                       (char *) t
159 #else
160                       (char *) p
161 #endif
162                       - (char *) &msg);
163       m->msgh_remote_port = ioport;
164       m->msgh_local_port = __mig_get_reply_port ();
165       m->msgh_id = msgid;
166       m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
167                                      MACH_MSG_TYPE_MAKE_SEND_ONCE);
168       err = _hurd_intr_rpc_mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
169                                      m->msgh_size, sizeof (msg),
170                                      m->msgh_local_port,
171                                      MACH_MSG_TIMEOUT_NONE,
172                                      MACH_PORT_NULL);
173       switch (err)
174         {
175         case MACH_MSG_SUCCESS:
176           break;
177         case MACH_SEND_INVALID_REPLY:
178         case MACH_RCV_INVALID_NAME:
179           __mig_dealloc_reply_port (m->msgh_local_port);
180         default:
181           return err;
182         }
183
184       if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
185         {
186           /* Allow no ports or VM.  */
187           __mach_msg_destroy (m);
188           /* Want to return a different error below for a different msgid.  */
189           if (m->msgh_id == msgid + 100)
190             return MIG_TYPE_ERROR;
191         }
192
193       if (m->msgh_id != msgid + 100)
194         return (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
195                 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
196
197       if (m->msgh_size != reply_size &&
198           m->msgh_size != sizeof msg.header)
199         return MIG_TYPE_ERROR;
200
201 #ifdef MACH_MSG_TYPE_BIT
202       if (msg.header_typecheck.RetCodeType !=
203           ((union { mach_msg_type_t t; int i; })
204            { t: io2mach_type (1, _IOTS (msg.header.RetCode)) }).i)
205         return MIG_TYPE_ERROR;
206 #endif
207       return msg.header.RetCode;
208     }
209
210   if (_IOT_COUNT0 (type) != 0)
211     {
212       /* Data need either be sent, received, or even both.  */
213       va_list ap;
214
215       va_start (ap, request);
216       arg = va_arg (ap, void *);
217       va_end (ap);
218     }
219
220   {
221     /* Check for a registered handler for REQUEST.  */
222     ioctl_handler_t handler = _hurd_lookup_ioctl_handler (request);
223     if (handler)
224       {
225         /* This handler groks REQUEST.  Se lo puntamonos.  */
226         int save = errno;
227         int result = (*handler) (fd, request, arg);
228         if (result != -1 || errno != ENOTTY)
229           return result;
230
231         /* The handler doesn't really grok this one.
232            Try the normal RPC translation.  */
233         errno = save;
234       }
235   }
236
237   /* Compute the Mach message ID for the RPC from the group and command
238      parts of the ioctl request.  */
239   msgid = IOC_MSGID (request);
240
241   /* Compute the expected size of the reply.  There is a standard header
242      consisting of the message header and the reply code.  Then, for out
243      and in/out ioctls, there come the data with their type headers.  */
244   reply_size = sizeof msg.header;
245
246   if (_IOC_INOUT (request) & IOC_OUT)
247     {
248       inline void figure_reply (unsigned int count, enum __ioctl_datum type)
249         {
250           if (count > 0)
251             {
252 #ifdef MACH_MSG_TYPE_BIT
253               /* Add the size of the type and data.  */
254               reply_size += sizeof (mach_msg_type_t) + typesize (type) * count;
255               /* Align it to word size.  */
256               reply_size += sizeof (mach_msg_type_t) - 1;
257               reply_size &= ~(sizeof (mach_msg_type_t) - 1);
258 #else
259               reply_size += typesize (type) * count;
260 #endif
261             }
262         }
263       figure_reply (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
264       figure_reply (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
265       figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
266     }
267
268   /* Marshal the arguments into the request message and make the RPC.
269      This wrapper function handles EBACKGROUND returns, turning them
270      into either SIGTTOU or EIO.  */
271   err = HURD_DPORT_USE (fd, _hurd_ctty_output (port, ctty, send_rpc));
272
273 #ifdef MACH_MSG_TYPE_BIT
274   t = (mach_msg_type_t *) msg.data;
275 #else
276   p = (void *) msg.data;
277 #endif
278   switch (err)
279     {
280       /* Unpack the message buffer into the argument location.  */
281       int out (unsigned int count, unsigned int type,
282                void *store, void **update)
283         {
284           if (count > 0)
285             {
286               const size_t len = count * typesize (type);
287 #ifdef MACH_MSG_TYPE_BIT
288               union { mach_msg_type_t t; int i; } ipctype;
289               ipctype.t = io2mach_type (count, type);
290               if (*(int *) t != ipctype.i)
291                 return 1;
292               ++t;
293               memcpy (store, t, len);
294               if (update != NULL)
295                 *update += len;
296               t = (void *) (((uintptr_t) t + len + sizeof (*t) - 1)
297                             & ~(sizeof (*t) - 1));
298 #else
299               memcpy (store, p, len);
300               p += len;
301               if (update != NULL)
302                 *update += len;
303 #endif
304             }
305           return 0;
306         }
307
308     case 0:
309       if (m->msgh_size != reply_size ||
310           ((_IOC_INOUT (request) & IOC_OUT) &&
311            (out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
312             out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
313             out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))))
314         return __hurd_fail (MIG_TYPE_ERROR);
315       return 0;
316
317     case MIG_BAD_ID:
318     case EOPNOTSUPP:
319       /* The server didn't understand the RPC.  */
320       err = ENOTTY;
321     default:
322       return __hurd_fail (err);
323     }
324 }
325
326 weak_alias (__ioctl, ioctl)