1 /* Copyright (C) 1992-2018 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 <http://www.gnu.org/licenses/>. */
19 #include <sys/ioctl.h>
22 #include <hurd/signal.h>
24 #include <mach/notify.h>
28 #include <hurd/ioctl.h>
29 #include <mach/mig_support.h>
31 #include <hurd/ioctls.defs>
33 #define typesize(type) (1 << (type))
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. */
39 __ioctl (int fd
, unsigned long int request
, ...)
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 })
50 /* Extract the type information encoded in the request. */
51 unsigned int type
= _IOC_TYPE (request
);
54 #define msg_align(x) \
55 (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1))
58 #ifdef MACH_MSG_TYPE_BIT
61 mig_reply_header_t header
;
64 mach_msg_header_t Head
;
66 kern_return_t RetCode
;
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
;
81 mach_msg_header_t
*const m
= &msg
.header
.Head
;
83 unsigned int reply_size
;
84 #ifdef MACH_MSG_TYPE_BIT
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
)
99 #ifdef MACH_MSG_TYPE_BIT
100 mach_msg_type_t
*t
= &msg
.header
.RetCodeType
;
102 void *p
= &msg
.header
.RetCode
;
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. */
110 if (_IOC_INOUT (request
) & IOC_IN
)
112 /* We don't want to advance ARG since it will be used to copy out
113 too if IOC_OUT is also set. */
116 /* Pack an argument into the message buffer. */
117 void in (unsigned int count
, enum __ioctl_datum type
)
121 const size_t len
= count
* typesize ((unsigned int) type
);
122 #ifdef MACH_MSG_TYPE_BIT
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));
130 p
= __mempcpy (p
, argptr
, len
);
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
));
141 else if (_IOC_INOUT (request
) == IOC_VOID
&& _IOT_COUNT0 (type
) != 0)
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
);
150 *(integer_t
*) p
= (integer_t
) arg
;
151 p
= (void *) p
+ sizeof (integer_t
);
155 memset (m
, 0, sizeof *m
); /* Clear unused fields. */
157 #ifdef MACH_MSG_TYPE_BIT
163 m
->msgh_remote_port
= ioport
;
164 m
->msgh_local_port
= __mig_get_reply_port ();
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
),
171 MACH_MSG_TIMEOUT_NONE
,
175 case MACH_MSG_SUCCESS
:
177 case MACH_SEND_INVALID_REPLY
:
178 case MACH_RCV_INVALID_NAME
:
179 __mig_dealloc_reply_port (m
->msgh_local_port
);
184 if ((m
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
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
;
193 if (m
->msgh_id
!= msgid
+ 100)
194 return (m
->msgh_id
== MACH_NOTIFY_SEND_ONCE
?
195 MIG_SERVER_DIED
: MIG_REPLY_MISMATCH
);
197 if (m
->msgh_size
!= reply_size
&&
198 m
->msgh_size
!= sizeof msg
.header
)
199 return MIG_TYPE_ERROR
;
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
;
207 return msg
.header
.RetCode
;
210 if (_IOT_COUNT0 (type
) != 0)
212 /* Data need either be sent, received, or even both. */
215 va_start (ap
, request
);
216 arg
= va_arg (ap
, void *);
221 /* Check for a registered handler for REQUEST. */
222 ioctl_handler_t handler
= _hurd_lookup_ioctl_handler (request
);
225 /* This handler groks REQUEST. Se lo puntamonos. */
227 int result
= (*handler
) (fd
, request
, arg
);
228 if (result
!= -1 || errno
!= ENOTTY
)
231 /* The handler doesn't really grok this one.
232 Try the normal RPC translation. */
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
);
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
;
246 if (_IOC_INOUT (request
) & IOC_OUT
)
248 inline void figure_reply (unsigned int count
, enum __ioctl_datum type
)
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);
259 reply_size
+= typesize (type
) * count
;
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
));
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
));
273 #ifdef MACH_MSG_TYPE_BIT
274 t
= (mach_msg_type_t
*) msg
.data
;
276 p
= (void *) msg
.data
;
280 /* Unpack the message buffer into the argument location. */
281 int out (unsigned int count
, unsigned int type
,
282 void *store
, void **update
)
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
)
293 memcpy (store
, t
, len
);
296 t
= (void *) (((uintptr_t) t
+ len
+ sizeof (*t
) - 1)
297 & ~(sizeof (*t
) - 1));
299 memcpy (store
, p
, len
);
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
);
319 /* The server didn't understand the RPC. */
322 return __hurd_fail (err
);
326 libc_hidden_def (__ioctl
)
327 weak_alias (__ioctl
, ioctl
)