Update.
[glibc.git] / sysdeps / mach / hurd / ioctl.c
blob5c6138fc0b0a8642b9bec037736ff861d3fd3719
1 /* Copyright (C) 1992,93,94,95,96,97,99,2000 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
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>
32 #include <hurd/ioctls.defs>
34 #define typesize(type) (1 << (type))
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, ...)
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 })
49 /* Extract the type information encoded in the request. */
50 unsigned int type = _IOC_TYPE (request);
52 /* Message buffer. */
53 #define msg_align(x) \
54 (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1))
55 struct
57 mig_reply_header_t header;
58 char data[3 * sizeof (mach_msg_type_t) +
59 msg_align (_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type))) +
60 msg_align (_IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type))) +
61 _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
62 } msg;
63 mach_msg_header_t *const m = &msg.header.Head;
64 mach_msg_type_t *t;
65 mach_msg_id_t msgid;
66 unsigned int reply_size;
68 void *arg;
70 error_t err;
72 /* Send the RPC already packed up in MSG to IOPORT
73 and decode the return value. */
74 error_t send_rpc (io_t ioport)
76 error_t err;
77 mach_msg_type_t *t = &msg.header.RetCodeType;
79 /* Marshal the request arguments into the message buffer.
80 We must redo this work each time we retry the RPC after a SIGTTOU,
81 because the reply message containing the EBACKGROUND error code
82 clobbers the same message buffer also used for the request. */
84 if (_IOC_INOUT (request) & IOC_IN)
86 /* We don't want to advance ARG since it will be used to copy out
87 too if IOC_OUT is also set. */
88 void *argptr = arg;
90 /* Pack an argument into the message buffer. */
91 void in (unsigned int count, enum __ioctl_datum type)
93 if (count > 0)
95 void *p = &t[1];
96 const size_t len = count * typesize ((unsigned int) type);
97 *t = io2mach_type (count, type);
98 p = __mempcpy (p, argptr, len);
99 argptr += len;
100 p = (void *) (((uintptr_t) p + sizeof (*t) - 1)
101 & ~(sizeof (*t) - 1));
102 t = p;
106 /* Pack the argument data. */
107 in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
108 in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
109 in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
111 else if (_IOC_INOUT (request) == IOC_VOID)
113 /* The RPC takes a single integer_t argument.
114 Rather than pointing to the value, ARG is the value itself. */
115 *t++ = io2mach_type (1, _IOTS (int));
116 *((int *) t)++ = (int) arg;
119 m->msgh_size = (char *) t - (char *) &msg;
120 m->msgh_remote_port = ioport;
121 m->msgh_local_port = __mig_get_reply_port ();
122 m->msgh_seqno = 0;
123 m->msgh_id = msgid;
124 m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
125 MACH_MSG_TYPE_MAKE_SEND_ONCE);
126 err = _hurd_intr_rpc_mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
127 m->msgh_size, sizeof (msg),
128 m->msgh_local_port,
129 MACH_MSG_TIMEOUT_NONE,
130 MACH_PORT_NULL);
131 switch (err)
133 case MACH_MSG_SUCCESS:
134 break;
135 case MACH_SEND_INVALID_REPLY:
136 case MACH_RCV_INVALID_NAME:
137 __mig_dealloc_reply_port (m->msgh_local_port);
138 default:
139 return err;
142 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
144 /* Allow no ports or VM. */
145 __mach_msg_destroy (m);
146 /* Want to return a different error below for a different msgid. */
147 if (m->msgh_id == msgid + 100)
148 return MIG_TYPE_ERROR;
151 if (m->msgh_id != msgid + 100)
152 return (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
153 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
155 if (m->msgh_size != reply_size &&
156 m->msgh_size != sizeof (mig_reply_header_t))
157 return MIG_TYPE_ERROR;
159 if (*(int *) &msg.header.RetCodeType !=
160 ((union { mach_msg_type_t t; int i; })
161 { t: io2mach_type (1, _IOTS (sizeof msg.header.RetCode)) }).i)
162 return MIG_TYPE_ERROR;
163 return msg.header.RetCode;
166 va_list ap;
168 va_start (ap, request);
169 arg = va_arg (ap, void *);
170 va_end (ap);
173 /* Check for a registered handler for REQUEST. */
174 ioctl_handler_t handler = _hurd_lookup_ioctl_handler (request);
175 if (handler)
177 /* This handler groks REQUEST. Se lo puntamonos. */
178 int save = errno;
179 int result = (*handler) (fd, request, arg);
180 if (result != -1 || errno != ENOTTY)
181 return result;
183 /* The handler doesn't really grok this one.
184 Try the normal RPC translation. */
185 errno = save;
189 /* Compute the Mach message ID for the RPC from the group and command
190 parts of the ioctl request. */
191 msgid = IOC_MSGID (request);
193 /* Compute the expected size of the reply. There is a standard header
194 consisting of the message header and the reply code. Then, for out
195 and in/out ioctls, there come the data with their type headers. */
196 reply_size = sizeof (mig_reply_header_t);
198 if (_IOC_INOUT (request) & IOC_OUT)
200 inline void figure_reply (unsigned int count, enum __ioctl_datum type)
202 if (count > 0)
204 /* Add the size of the type and data. */
205 reply_size += sizeof (mach_msg_type_t) + typesize (type) * count;
206 /* Align it to word size. */
207 reply_size += sizeof (mach_msg_type_t) - 1;
208 reply_size &= ~(sizeof (mach_msg_type_t) - 1);
211 figure_reply (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
212 figure_reply (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
213 figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
216 /* Marshal the arguments into the request message and make the RPC.
217 This wrapper function handles EBACKGROUND returns, turning them
218 into either SIGTTOU or EIO. */
219 err = HURD_DPORT_USE (fd, _hurd_ctty_output (port, ctty, send_rpc));
221 t = (mach_msg_type_t *) msg.data;
222 switch (err)
224 /* Unpack the message buffer into the argument location. */
225 int out (unsigned int count, unsigned int type,
226 void *store, void **update)
228 if (count > 0)
230 const size_t len = count * typesize (type);
231 union { mach_msg_type_t t; int i; } ipctype;
232 ipctype.t = io2mach_type (count, type);
233 if (*(int *) t != ipctype.i)
234 return 1;
235 ++t;
236 memcpy (store, t, len);
237 if (update != NULL)
238 *update += len;
239 t = (void *) (((uintptr_t) t + len + sizeof (*t) - 1)
240 & ~(sizeof (*t) - 1));
242 return 0;
245 case 0:
246 if (m->msgh_size != reply_size ||
247 ((_IOC_INOUT (request) & IOC_OUT) &&
248 (out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
249 out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
250 out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))))
251 return __hurd_fail (MIG_TYPE_ERROR);
252 return 0;
254 case MIG_BAD_ID:
255 case EOPNOTSUPP:
256 /* The server didn't understand the RPC. */
257 err = ENOTTY;
258 default:
259 return __hurd_fail (err);
263 weak_alias (__ioctl, ioctl)