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
21 #include <sys/ioctl.h>
24 #include <hurd/signal.h>
26 #include <mach/notify.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. */
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 })
52 /* Extract the type information encoded in the request. */
53 unsigned int type
= _IOC_TYPE (request
);
56 #define msg_align(x) \
57 (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1))
60 #ifdef MACH_MSG_TYPE_BIT
63 mig_reply_header_t header
;
66 mach_msg_header_t Head
;
68 kern_return_t RetCode
;
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
;
83 mach_msg_header_t
*const m
= &msg
.header
.Head
;
85 unsigned int reply_size
;
86 #ifdef MACH_MSG_TYPE_BIT
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
)
101 #ifdef MACH_MSG_TYPE_BIT
102 mach_msg_type_t
*t
= &msg
.header
.RetCodeType
;
104 void *p
= &msg
.header
.RetCode
;
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. */
118 /* Pack an argument into the message buffer. */
119 void in (unsigned int count
, enum __ioctl_datum type
)
123 const size_t len
= count
* typesize ((unsigned int) type
);
124 #ifdef MACH_MSG_TYPE_BIT
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));
132 p
= __mempcpy (p
, 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
);
152 *(integer_t
*) p
= (integer_t
) arg
;
153 p
= (void *) p
+ sizeof (integer_t
);
157 memset (m
, 0, sizeof *m
); /* Clear unused fields. */
159 #ifdef MACH_MSG_TYPE_BIT
165 m
->msgh_remote_port
= ioport
;
166 m
->msgh_local_port
= __mig_get_reply_port ();
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
),
173 MACH_MSG_TIMEOUT_NONE
,
177 case MACH_MSG_SUCCESS
:
179 case MACH_SEND_INVALID_REPLY
:
180 case MACH_RCV_INVALID_NAME
:
181 __mig_dealloc_reply_port (m
->msgh_local_port
);
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
;
209 return msg
.header
.RetCode
;
214 va_start (ap
, request
);
215 arg
= va_arg (ap
, void *);
219 /* Check for a registered handler for REQUEST. */
220 ioctl_handler_t handler
= _hurd_lookup_ioctl_handler (request
);
223 /* This handler groks REQUEST. Se lo puntamonos. */
225 int result
= (*handler
) (fd
, request
, arg
);
226 if (result
!= -1 || errno
!= ENOTTY
)
229 /* The handler doesn't really grok this one.
230 Try the normal RPC translation. */
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
)
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);
257 reply_size
+= typesize (type
) * count
;
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
;
274 p
= (void *) msg
.data
;
278 /* Unpack the message buffer into the argument location. */
279 int out (unsigned int count
, unsigned int type
,
280 void *store
, void **update
)
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
)
291 memcpy (store
, t
, len
);
294 t
= (void *) (((uintptr_t) t
+ len
+ sizeof (*t
) - 1)
295 & ~(sizeof (*t
) - 1));
297 memcpy (store
, p
, len
);
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
);
317 /* The server didn't understand the RPC. */
320 return __hurd_fail (err
);
324 weak_alias (__ioctl
, ioctl
)