1 /* Copyright (C) 1992-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <libc-pointer-arith.h>
20 #include <sys/ioctl.h>
23 #include <hurd/signal.h>
25 #include <mach/notify.h>
29 #include <hurd/ioctl.h>
30 #include <mach/mig_support.h>
32 #include <sysdep-cancel.h>
34 #include <hurd/ioctls.defs>
36 #define msg_align(x) ALIGN_UP (x, __alignof__ (uintptr_t))
37 #define typesize(type) (1 << (type))
39 /* Perform the I/O control operation specified by REQUEST on FD.
40 The actual type and use of ARG and the return value depend on REQUEST. */
42 __ioctl (int fd
, unsigned long int request
, ...)
44 #ifdef MACH_MSG_TYPE_BIT
45 /* Map individual type fields to Mach IPC types. */
46 static const int mach_types
[] =
47 { MACH_MSG_TYPE_CHAR
, MACH_MSG_TYPE_INTEGER_16
, MACH_MSG_TYPE_INTEGER_32
,
48 MACH_MSG_TYPE_INTEGER_64
};
49 #define io2mach_type(count, type) \
50 ((mach_msg_type_t) { \
51 .msgt_name = mach_types[type], \
52 .msgt_size = typesize(type) * 8, \
53 .msgt_number = count, \
54 .msgt_inline = TRUE, \
55 .msgt_longform = FALSE, \
56 .msgt_deallocate = FALSE, \
61 /* Extract the type information encoded in the request. */
62 unsigned int type
= _IOC_TYPE (request
);
67 #ifdef MACH_MSG_TYPE_BIT
70 mig_reply_header_t header
;
73 mach_msg_header_t Head
;
74 mach_msg_type_t RetCodeType
;
75 kern_return_t RetCode
;
78 char data
[3 * sizeof (mach_msg_type_t
)
79 + msg_align (_IOT_COUNT0 (type
) * typesize (_IOT_TYPE0 (type
)))
80 + msg_align (_IOT_COUNT1 (type
) * typesize (_IOT_TYPE1 (type
)))
81 + msg_align (_IOT_COUNT2 (type
) * typesize (_IOT_TYPE2 (type
)))];
82 #else /* Untyped Mach IPC format. */
83 mig_reply_error_t header
;
84 char data
[_IOT_COUNT0 (type
) * typesize (_IOT_TYPE0 (type
))
85 + _IOT_COUNT1 (type
) * typesize (_IOT_TYPE1 (type
))
86 + _IOT_COUNT2 (type
) * typesize (_IOT_TYPE2 (type
))];
87 mach_msg_trailer_t trailer
;
90 mach_msg_header_t
*const m
= &msg
.header
.Head
;
92 unsigned int reply_size
;
93 #ifdef MACH_MSG_TYPE_BIT
103 /* Send the RPC already packed up in MSG to IOPORT
104 and decode the return value. */
105 error_t
send_rpc (io_t ioport
)
108 #ifdef MACH_MSG_TYPE_BIT
109 mach_msg_type_t
*t
= &msg
.header
.RetCodeType
;
111 void *p
= &msg
.header
.RetCode
;
114 /* Marshal the request arguments into the message buffer.
115 We must redo this work each time we retry the RPC after a SIGTTOU,
116 because the reply message containing the EBACKGROUND error code
117 clobbers the same message buffer also used for the request. */
119 if (_IOC_INOUT (request
) & IOC_IN
)
121 /* We don't want to advance ARG since it will be used to copy out
122 too if IOC_OUT is also set. */
126 if (request
== TIOCFLUSH
&& !argptr
)
129 /* Pack an argument into the message buffer. */
130 void in (unsigned int count
, enum __ioctl_datum type
)
134 const size_t len
= count
* typesize ((unsigned int) type
);
135 #ifdef MACH_MSG_TYPE_BIT
137 *t
= io2mach_type (count
, type
);
138 p
= __mempcpy (p
, argptr
, len
);
139 p
= (void *) msg_align ((uintptr_t) p
);
142 p
= __mempcpy (p
, argptr
, len
);
148 /* Pack the argument data. */
149 in (_IOT_COUNT0 (type
), _IOT_TYPE0 (type
));
150 in (_IOT_COUNT1 (type
), _IOT_TYPE1 (type
));
151 in (_IOT_COUNT2 (type
), _IOT_TYPE2 (type
));
153 else if (_IOC_INOUT (request
) == IOC_VOID
&& _IOT_COUNT0 (type
) != 0)
155 /* The RPC takes a single integer_t argument.
156 Rather than pointing to the value, ARG is the value itself. */
157 #ifdef MACH_MSG_TYPE_BIT
158 *t
++ = io2mach_type (1, _IOTS (integer_t
));
159 *(integer_t
*) t
= (integer_t
) (intptr_t) arg
;
160 t
= (void *) msg_align ((uintptr_t) t
+ sizeof (integer_t
));
162 *(integer_t
*) p
= (integer_t
) (intptr_t) arg
;
163 p
= (void *) p
+ sizeof (integer_t
);
167 memset (m
, 0, sizeof *m
); /* Clear unused fields. */
169 #ifdef MACH_MSG_TYPE_BIT
175 m
->msgh_remote_port
= ioport
;
176 m
->msgh_local_port
= __mig_get_reply_port ();
178 m
->msgh_bits
= MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND
,
179 MACH_MSG_TYPE_MAKE_SEND_ONCE
);
180 err
= _hurd_intr_rpc_mach_msg (m
, MACH_SEND_MSG
|MACH_RCV_MSG
,
181 m
->msgh_size
, sizeof (msg
),
183 MACH_MSG_TIMEOUT_NONE
,
187 case MACH_MSG_SUCCESS
:
189 case MACH_SEND_INVALID_REPLY
:
190 case MACH_RCV_INVALID_NAME
:
191 __mig_dealloc_reply_port (m
->msgh_local_port
);
197 if ((m
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
199 /* Allow no ports or VM. */
200 __mach_msg_destroy (m
);
201 /* Want to return a different error below for a different msgid. */
202 if (m
->msgh_id
== msgid
+ 100)
203 return MIG_TYPE_ERROR
;
206 if (m
->msgh_id
!= msgid
+ 100)
207 return (m
->msgh_id
== MACH_NOTIFY_SEND_ONCE
208 ? MIG_SERVER_DIED
: MIG_REPLY_MISMATCH
);
210 if (m
->msgh_size
!= reply_size
211 && m
->msgh_size
!= sizeof msg
.header
)
212 return MIG_TYPE_ERROR
;
214 #ifdef MACH_MSG_TYPE_BIT
215 mach_msg_type_t ipctype
= io2mach_type(1, _IOTS (msg
.header
.RetCode
));
216 if (BAD_TYPECHECK (&msg
.header_typecheck
.RetCodeType
, &ipctype
))
217 return MIG_TYPE_ERROR
;
219 return msg
.header
.RetCode
;
222 if (_IOT_COUNT0 (type
) != 0)
224 /* Data need either be sent, received, or even both. */
227 va_start (ap
, request
);
228 arg
= va_arg (ap
, void *);
233 /* Check for a registered handler for REQUEST. */
234 ioctl_handler_t handler
= _hurd_lookup_ioctl_handler (request
);
237 /* This handler groks REQUEST. Se lo puntamonos. */
239 int result
= (*handler
) (fd
, request
, arg
);
240 if (result
!= -1 || errno
!= ENOTTY
)
243 /* The handler doesn't really grok this one.
244 Try the normal RPC translation. */
249 /* Compute the Mach message ID for the RPC from the group and command
250 parts of the ioctl request. */
251 msgid
= IOC_MSGID (request
);
253 /* Compute the expected size of the reply. There is a standard header
254 consisting of the message header and the reply code. Then, for out
255 and in/out ioctls, there come the data with their type headers. */
256 reply_size
= sizeof msg
.header
;
258 if (_IOC_INOUT (request
) & IOC_OUT
)
260 inline void figure_reply (unsigned int count
, enum __ioctl_datum type
)
264 #ifdef MACH_MSG_TYPE_BIT
265 /* Add the size of the type and data. */
266 reply_size
+= sizeof (mach_msg_type_t
) + typesize (type
) * count
;
267 /* Align it to word size. */
268 reply_size
= msg_align (reply_size
);
270 reply_size
+= typesize (type
) * count
;
274 figure_reply (_IOT_COUNT0 (type
), _IOT_TYPE0 (type
));
275 figure_reply (_IOT_COUNT1 (type
), _IOT_TYPE1 (type
));
276 figure_reply (_IOT_COUNT2 (type
), _IOT_TYPE2 (type
));
279 /* Marshal the arguments into the request message and make the RPC.
280 This wrapper function handles EBACKGROUND returns, turning them
281 into either SIGTTOU or EIO. */
282 if (request
== TIOCDRAIN
)
284 /* This is a cancellation point. */
285 int cancel_oldtype
= LIBC_CANCEL_ASYNC();
286 err
= HURD_DPORT_USE_CANCEL (fd
, _hurd_ctty_output (port
, ctty
, send_rpc
));
287 LIBC_CANCEL_RESET (cancel_oldtype
);
290 err
= HURD_DPORT_USE (fd
, _hurd_ctty_output (port
, ctty
, send_rpc
));
292 #ifdef MACH_MSG_TYPE_BIT
293 t
= (mach_msg_type_t
*) msg
.data
;
295 p
= (void *) msg
.data
;
299 /* Unpack the message buffer into the argument location. */
300 int out (unsigned int count
, unsigned int type
,
301 void *store
, void **update
)
305 const size_t len
= count
* typesize (type
);
306 #ifdef MACH_MSG_TYPE_BIT
307 const mach_msg_type_t ipctype
= io2mach_type(count
, type
);
308 if (BAD_TYPECHECK (t
, &ipctype
))
311 memcpy (store
, t
, len
);
314 t
= (mach_msg_type_t
*) msg_align ((uintptr_t) t
+ len
);
316 memcpy (store
, p
, len
);
326 if (m
->msgh_size
!= reply_size
327 || ((_IOC_INOUT (request
) & IOC_OUT
)
328 && (out (_IOT_COUNT0 (type
), _IOT_TYPE0 (type
), arg
, &arg
)
329 || out (_IOT_COUNT1 (type
), _IOT_TYPE1 (type
), arg
, &arg
)
330 || out (_IOT_COUNT2 (type
), _IOT_TYPE2 (type
), arg
, &arg
))))
331 return __hurd_fail (MIG_TYPE_ERROR
);
336 /* The server didn't understand the RPC. */
340 return __hurd_fail (err
);
344 libc_hidden_def (__ioctl
)
345 weak_alias (__ioctl
, ioctl
)