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