Update.
[glibc.git] / sysdeps / mach / hurd / ioctl.c
blobac8e8040ee439699e4963f2fe1adb434e246339f
1 /* Copyright (C) 1992, 93, 94, 95, 96, 97, 99 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 <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. */
38 int
39 __ioctl (int fd, unsigned long int request, ...)
41 /* Map individual type fields to Mach IPC types. */
42 static const int mach_types[] =
43 { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
44 MACH_MSG_TYPE_INTEGER_64 };
45 #define io2mach_type(count, type) \
46 ((mach_msg_type_t) { mach_types[type], typesize (type) * 8, count, 1, 0, 0 })
48 /* Extract the type information encoded in the request. */
49 unsigned int type = _IOC_TYPE (request);
51 /* Message buffer. */
52 #define msg_align(x) \
53 (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1))
54 struct
56 mig_reply_header_t header;
57 char data[3 * sizeof (mach_msg_type_t) +
58 msg_align (_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type))) +
59 msg_align (_IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type))) +
60 _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
61 } msg;
62 mach_msg_header_t *const m = &msg.header.Head;
63 mach_msg_type_t *t;
64 mach_msg_id_t msgid;
65 unsigned int reply_size;
67 void *arg;
69 error_t err;
71 /* Send the RPC already packed up in MSG to IOPORT
72 and decode the return value. */
73 error_t send_rpc (io_t ioport)
75 error_t err;
76 mach_msg_type_t *t = &msg.header.RetCodeType;
78 /* Marshal the request arguments into the message buffer.
79 We must redo this work each time we retry the RPC after a SIGTTOU,
80 because the reply message containing the EBACKGROUND error code
81 clobbers the same message buffer also used for the request. */
83 if (_IOC_INOUT (request) & IOC_IN)
85 /* Pack an argument into the message buffer. */
86 void in (unsigned int count, enum __ioctl_datum type)
88 if (count > 0)
90 void *p = &t[1];
91 const size_t len = count * typesize ((unsigned int) type);
92 *t = io2mach_type (count, type);
93 memcpy (p, arg, len);
94 arg += len;
95 p += len;
96 p = (void *) (((unsigned long int) p + sizeof (*t) - 1)
97 & ~(sizeof (*t) - 1));
98 t = p;
102 /* Pack the argument data. */
103 in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
104 in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
105 in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
107 else if (_IOC_INOUT (request) == IOC_VOID)
109 /* The RPC takes a single integer_t argument.
110 Rather than pointing to the value, ARG is the value itself. */
111 *t++ = io2mach_type (1, _IOTS (int));
112 *((int *) t)++ = (int) arg;
115 m->msgh_size = (char *) t - (char *) &msg;
116 m->msgh_remote_port = ioport;
117 m->msgh_local_port = __mig_get_reply_port ();
118 m->msgh_seqno = 0;
119 m->msgh_id = msgid;
120 m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
121 MACH_MSG_TYPE_MAKE_SEND_ONCE);
122 err = _hurd_intr_rpc_mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
123 m->msgh_size, sizeof (msg),
124 m->msgh_local_port,
125 MACH_MSG_TIMEOUT_NONE,
126 MACH_PORT_NULL);
127 switch (err)
129 case MACH_MSG_SUCCESS:
130 break;
131 case MACH_SEND_INVALID_REPLY:
132 case MACH_RCV_INVALID_NAME:
133 __mig_dealloc_reply_port (m->msgh_local_port);
134 default:
135 return err;
138 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
140 /* Allow no ports or VM. */
141 __mach_msg_destroy (m);
142 /* Want to return a different error below for a different msgid. */
143 if (m->msgh_id == msgid + 100)
144 return MIG_TYPE_ERROR;
147 if (m->msgh_id != msgid + 100)
148 return (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
149 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
151 if (m->msgh_size != reply_size &&
152 m->msgh_size != sizeof (mig_reply_header_t))
153 return MIG_TYPE_ERROR;
155 if (*(int *) &msg.header.RetCodeType !=
156 ((union { mach_msg_type_t t; int i; })
157 { t: io2mach_type (1, _IOTS (sizeof msg.header.RetCode)) }).i)
158 return MIG_TYPE_ERROR;
159 return msg.header.RetCode;
162 va_list ap;
164 va_start (ap, request);
165 arg = va_arg (ap, void *);
166 va_end (ap);
169 /* Check for a registered handler for REQUEST. */
170 ioctl_handler_t handler = _hurd_lookup_ioctl_handler (request);
171 if (handler)
173 /* This handler groks REQUEST. Se lo puntamonos. */
174 int save = errno;
175 int result = (*handler) (fd, request, arg);
176 if (result != -1 || errno != ENOTTY)
177 return result;
179 /* The handler doesn't really grok this one.
180 Try the normal RPC translation. */
181 errno = save;
185 /* Compute the Mach message ID for the RPC from the group and command
186 parts of the ioctl request. */
187 msgid = IOC_MSGID (request);
189 /* Compute the expected size of the reply. There is a standard header
190 consisting of the message header and the reply code. Then, for out
191 and in/out ioctls, there come the data with their type headers. */
192 reply_size = sizeof (mig_reply_header_t);
194 if (_IOC_INOUT (request) & IOC_OUT)
196 inline void figure_reply (unsigned int count, enum __ioctl_datum type)
198 if (count > 0)
200 /* Add the size of the type and data. */
201 reply_size += sizeof (mach_msg_type_t) + typesize (type) * count;
202 /* Align it to word size. */
203 reply_size += sizeof (mach_msg_type_t) - 1;
204 reply_size &= ~(sizeof (mach_msg_type_t) - 1);
207 figure_reply (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
208 figure_reply (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
209 figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
212 /* Marshal the arguments into the request message and make the RPC.
213 This wrapper function handles EBACKGROUND returns, turning them
214 into either SIGTTOU or EIO. */
215 err = HURD_DPORT_USE (fd, _hurd_ctty_output (port, ctty, send_rpc));
217 t = (mach_msg_type_t *) msg.data;
218 switch (err)
220 /* Unpack the message buffer into the argument location. */
221 int out (unsigned int count, unsigned int type,
222 void *store, void **update)
224 if (count > 0)
226 const size_t len = count * typesize (type);
227 union { mach_msg_type_t t; int i; } ipctype;
228 ipctype.t = io2mach_type (count, type);
229 if (*(int *) t != ipctype.i)
230 return 1;
231 ++t;
232 memcpy (store, t, len);
233 if (update != NULL)
234 *update += len;
235 t = (void *) (((unsigned long int) t + len + sizeof (*t) - 1)
236 & ~(sizeof (*t) - 1));
238 return 0;
241 case 0:
242 if (m->msgh_size != reply_size ||
243 ((_IOC_INOUT (request) & IOC_OUT) &&
244 (out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
245 out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
246 out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))))
247 return __hurd_fail (MIG_TYPE_ERROR);
248 return 0;
250 case MIG_BAD_ID:
251 case EOPNOTSUPP:
252 /* The server didn't understand the RPC. */
253 err = ENOTTY;
254 default:
255 return __hurd_fail (err);
259 weak_alias (__ioctl, ioctl)