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