]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/ioctl.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / mach / hurd / ioctl.c
1 /* Copyright (C) 1992-2021 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 <https://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 #include <sysdep-cancel.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 union
61 {
62 mig_reply_header_t header;
63 struct
64 {
65 mach_msg_header_t Head;
66 int RetCodeType;
67 kern_return_t RetCode;
68 } header_typecheck;
69 };
70 char data[3 * sizeof (mach_msg_type_t)
71 + msg_align (_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type)))
72 + msg_align (_IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type)))
73 + _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
74 #else /* Untyped Mach IPC format. */
75 mig_reply_error_t header;
76 char data[_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type))
77 + _IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type))
78 + _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
79 mach_msg_trailer_t trailer;
80 #endif
81 } msg;
82 mach_msg_header_t *const m = &msg.header.Head;
83 mach_msg_id_t msgid;
84 unsigned int reply_size;
85 #ifdef MACH_MSG_TYPE_BIT
86 mach_msg_type_t *t;
87 #else
88 void *p;
89 #endif
90
91 void *arg = NULL;
92
93 error_t err;
94
95 /* Send the RPC already packed up in MSG to IOPORT
96 and decode the return value. */
97 error_t send_rpc (io_t ioport)
98 {
99 error_t err;
100 #ifdef MACH_MSG_TYPE_BIT
101 mach_msg_type_t *t = &msg.header.RetCodeType;
102 #else
103 void *p = &msg.header.RetCode;
104 #endif
105
106 /* Marshal the request arguments into the message buffer.
107 We must redo this work each time we retry the RPC after a SIGTTOU,
108 because the reply message containing the EBACKGROUND error code
109 clobbers the same message buffer also used for the request. */
110
111 if (_IOC_INOUT (request) & IOC_IN)
112 {
113 /* We don't want to advance ARG since it will be used to copy out
114 too if IOC_OUT is also set. */
115 void *argptr = arg;
116
117 /* Pack an argument into the message buffer. */
118 void in (unsigned int count, enum __ioctl_datum type)
119 {
120 if (count > 0)
121 {
122 const size_t len = count * typesize ((unsigned int) type);
123 #ifdef MACH_MSG_TYPE_BIT
124 void *p = &t[1];
125 *t = io2mach_type (count, type);
126 p = __mempcpy (p, argptr, len);
127 p = (void *) (((uintptr_t) p + sizeof (*t) - 1)
128 & ~(sizeof (*t) - 1));
129 t = p;
130 #else
131 p = __mempcpy (p, argptr, len);
132 #endif
133 argptr += len;
134 }
135 }
136
137 /* Pack the argument data. */
138 in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
139 in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
140 in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
141 }
142 else if (_IOC_INOUT (request) == IOC_VOID && _IOT_COUNT0 (type) != 0)
143 {
144 /* The RPC takes a single integer_t argument.
145 Rather than pointing to the value, ARG is the value itself. */
146 #ifdef MACH_MSG_TYPE_BIT
147 *t++ = io2mach_type (1, _IOTS (integer_t));
148 *(integer_t *) t = (integer_t) arg;
149 t = (void *) t + sizeof (integer_t);
150 #else
151 *(integer_t *) p = (integer_t) arg;
152 p = (void *) p + sizeof (integer_t);
153 #endif
154 }
155
156 memset (m, 0, sizeof *m); /* Clear unused fields. */
157 m->msgh_size = (
158 #ifdef MACH_MSG_TYPE_BIT
159 (char *) t
160 #else
161 (char *) p
162 #endif
163 - (char *) &msg);
164 m->msgh_remote_port = ioport;
165 m->msgh_local_port = __mig_get_reply_port ();
166 m->msgh_id = msgid;
167 m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
168 MACH_MSG_TYPE_MAKE_SEND_ONCE);
169 err = _hurd_intr_rpc_mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
170 m->msgh_size, sizeof (msg),
171 m->msgh_local_port,
172 MACH_MSG_TIMEOUT_NONE,
173 MACH_PORT_NULL);
174 switch (err)
175 {
176 case MACH_MSG_SUCCESS:
177 break;
178 case MACH_SEND_INVALID_REPLY:
179 case MACH_RCV_INVALID_NAME:
180 __mig_dealloc_reply_port (m->msgh_local_port);
181 /* Fall through. */
182 default:
183 return err;
184 }
185
186 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
187 {
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;
193 }
194
195 if (m->msgh_id != msgid + 100)
196 return (m->msgh_id == MACH_NOTIFY_SEND_ONCE
197 ? MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
198
199 if (m->msgh_size != reply_size
200 && m->msgh_size != sizeof msg.header)
201 return MIG_TYPE_ERROR;
202
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;
210 }
211
212 if (_IOT_COUNT0 (type) != 0)
213 {
214 /* Data need either be sent, received, or even both. */
215 va_list ap;
216
217 va_start (ap, request);
218 arg = va_arg (ap, void *);
219 va_end (ap);
220 }
221
222 {
223 /* Check for a registered handler for REQUEST. */
224 ioctl_handler_t handler = _hurd_lookup_ioctl_handler (request);
225 if (handler)
226 {
227 /* This handler groks REQUEST. Se lo puntamonos. */
228 int save = errno;
229 int result = (*handler) (fd, request, arg);
230 if (result != -1 || errno != ENOTTY)
231 return result;
232
233 /* The handler doesn't really grok this one.
234 Try the normal RPC translation. */
235 errno = save;
236 }
237 }
238
239 /* Compute the Mach message ID for the RPC from the group and command
240 parts of the ioctl request. */
241 msgid = IOC_MSGID (request);
242
243 /* Compute the expected size of the reply. There is a standard header
244 consisting of the message header and the reply code. Then, for out
245 and in/out ioctls, there come the data with their type headers. */
246 reply_size = sizeof msg.header;
247
248 if (_IOC_INOUT (request) & IOC_OUT)
249 {
250 inline void figure_reply (unsigned int count, enum __ioctl_datum type)
251 {
252 if (count > 0)
253 {
254 #ifdef MACH_MSG_TYPE_BIT
255 /* Add the size of the type and data. */
256 reply_size += sizeof (mach_msg_type_t) + typesize (type) * count;
257 /* Align it to word size. */
258 reply_size += sizeof (mach_msg_type_t) - 1;
259 reply_size &= ~(sizeof (mach_msg_type_t) - 1);
260 #else
261 reply_size += typesize (type) * count;
262 #endif
263 }
264 }
265 figure_reply (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
266 figure_reply (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
267 figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
268 }
269
270 /* Marshal the arguments into the request message and make the RPC.
271 This wrapper function handles EBACKGROUND returns, turning them
272 into either SIGTTOU or EIO. */
273 if (request == TIOCDRAIN)
274 {
275 /* This is a cancellation point. */
276 int cancel_oldtype = LIBC_CANCEL_ASYNC();
277 err = HURD_DPORT_USE_CANCEL (fd, _hurd_ctty_output (port, ctty, send_rpc));
278 LIBC_CANCEL_RESET (cancel_oldtype);
279 }
280 else
281 err = HURD_DPORT_USE (fd, _hurd_ctty_output (port, ctty, send_rpc));
282
283 #ifdef MACH_MSG_TYPE_BIT
284 t = (mach_msg_type_t *) msg.data;
285 #else
286 p = (void *) msg.data;
287 #endif
288 switch (err)
289 {
290 /* Unpack the message buffer into the argument location. */
291 int out (unsigned int count, unsigned int type,
292 void *store, void **update)
293 {
294 if (count > 0)
295 {
296 const size_t len = count * typesize (type);
297 #ifdef MACH_MSG_TYPE_BIT
298 union { mach_msg_type_t t; int i; } ipctype;
299 ipctype.t = io2mach_type (count, type);
300 if (*(int *) t != ipctype.i)
301 return 1;
302 ++t;
303 memcpy (store, t, len);
304 if (update != NULL)
305 *update += len;
306 t = (void *) (((uintptr_t) t + len + sizeof (*t) - 1)
307 & ~(sizeof (*t) - 1));
308 #else
309 memcpy (store, p, len);
310 p += len;
311 if (update != NULL)
312 *update += len;
313 #endif
314 }
315 return 0;
316 }
317
318 case 0:
319 if (m->msgh_size != reply_size
320 || ((_IOC_INOUT (request) & IOC_OUT)
321 && (out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg)
322 || out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg)
323 || out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))))
324 return __hurd_fail (MIG_TYPE_ERROR);
325 return 0;
326
327 case MIG_BAD_ID:
328 case EOPNOTSUPP:
329 /* The server didn't understand the RPC. */
330 err = ENOTTY;
331 /* Fall through. */
332 default:
333 return __hurd_fail (err);
334 }
335 }
336
337 libc_hidden_def (__ioctl)
338 weak_alias (__ioctl, ioctl)