* posix/glob.h (__glob_opendir_hook, __glob_readdir_hook,
[glibc.git] / sysdeps / mach / hurd / ioctl.c
blob24bfbed9430e106774f885b154b14848b0454b79
1 /* Copyright (C) 1992, 1993, 1994, 1995, 1996 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <sys/ioctl.h>
22 #include <hurd.h>
23 #include <hurd/fd.h>
24 #include <hurd/signal.h>
25 #include <stdarg.h>
26 #include <mach/notify.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <hurd/ioctl.h>
32 #define typesize(type) (1 << (type))
35 /* Perform the I/O control operation specified by REQUEST on FD.
36 The actual type and use of ARG and the return value depend on REQUEST. */
37 int
38 DEFUN(__ioctl, (fd, request),
39 int fd AND unsigned long int request DOTS)
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 -1 };
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 struct
54 mig_reply_header_t header;
55 char data[3 * sizeof (mach_msg_type_t) +
56 _IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type)) +
57 _IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type)) +
58 _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
59 } msg;
60 mach_msg_header_t *const m = &msg.header.Head;
61 mach_msg_type_t *t = &msg.header.RetCodeType;
62 mach_msg_id_t msgid;
63 unsigned int reply_size;
65 void *arg;
67 error_t err;
69 /* Send the RPC already packed up in MSG to IOPORT
70 and decode the return value. */
71 error_t send_rpc (io_t ioport)
73 error_t err;
75 m->msgh_size = (char *) t - (char *) &msg;
76 m->msgh_remote_port = ioport;
77 m->msgh_local_port = __mig_get_reply_port ();
78 m->msgh_seqno = 0;
79 m->msgh_id = msgid;
80 m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
81 MACH_MSG_TYPE_MAKE_SEND_ONCE);
82 err = _hurd_intr_rpc_mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
83 m->msgh_size, sizeof (msg),
84 m->msgh_local_port,
85 MACH_MSG_TIMEOUT_NONE,
86 MACH_PORT_NULL);
87 switch (err)
89 case MACH_MSG_SUCCESS:
90 break;
91 case MACH_SEND_INVALID_REPLY:
92 case MACH_RCV_INVALID_NAME:
93 __mig_dealloc_reply_port (m->msgh_local_port);
94 default:
95 return err;
98 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
100 /* Allow no ports or VM. */
101 __mach_msg_destroy (m);
102 /* Want to return a different error below for a different msgid. */
103 if (m->msgh_id == msgid + 100)
104 return MIG_TYPE_ERROR;
107 if (m->msgh_id != msgid + 100)
108 return (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
109 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
111 if (m->msgh_size != reply_size &&
112 m->msgh_size != sizeof (mig_reply_header_t))
113 return MIG_TYPE_ERROR;
115 if (*(int *) &msg.header.RetCodeType !=
116 ((union { mach_msg_type_t t; int i; })
117 { t: io2mach_type (1, _IOTS (sizeof msg.header.RetCode)) }).i)
118 return MIG_TYPE_ERROR;
119 return msg.header.RetCode;
122 va_list ap;
124 va_start (ap, request);
125 arg = va_arg (ap, void *);
126 va_end (ap);
129 /* Check for a registered handler for REQUEST. */
130 ioctl_handler_t handler = _hurd_lookup_ioctl_handler (request);
131 if (handler)
133 /* This handler groks REQUEST. Se lo puntamonos. */
134 int save = errno;
135 int result = (*handler) (fd, request, arg);
136 if (result != -1 || errno != ENOTTY)
137 return result;
139 /* The handler doesn't really grok this one.
140 Try the normal RPC translation. */
141 errno = save;
145 /* Compute the Mach message ID for the RPC from the group and command
146 parts of the ioctl request. */
147 msgid = 100000 + ((_IOC_GROUP (request) - 'f') * 4000); /* Base subsystem */
148 /* Because of MiG's poorly chosen algorithm of adding 100 to a request
149 msgid to produce the reply msgid, we cannot just add the command part
150 of the ioctl request to the subsystem base msgid. For ioctl requests
151 past 99, we must skip blocks of 100 msgids to allow for the reply
152 msgids corresponding to the earlier requests. */
153 if (_IOC_COMMAND (request) >= 100)
154 msgid += 100;
155 if (_IOC_COMMAND (request) >= 200)
156 msgid += 100;
157 msgid += _IOC_COMMAND (request);
159 if (_IOC_INOUT (request) & IOC_IN)
161 /* Pack an argument into the message buffer. */
162 void in (unsigned int count, enum __ioctl_datum type)
164 if (count > 0)
166 void *p = &t[1];
167 const size_t len = count * typesize ((unsigned int) type);
168 *t = io2mach_type (count, type);
169 memcpy (p, arg, len);
170 arg += len;
171 p += len;
172 p = (void *) (((unsigned long int) p + sizeof (*t) - 1)
173 & ~(sizeof (*t) - 1));
174 t = p;
178 /* Pack the argument data. */
179 in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
180 in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
181 in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
183 else if (_IOC_INOUT (request) == IOC_VOID)
185 /* The RPC takes a single integer_t argument.
186 Rather than pointing to the value, ARG is the value itself. */
187 *t++ = io2mach_type (1, _IOTS (int));
188 *((int *) t)++ = (int) arg;
191 /* Compute the expected size of the reply. There is a standard header
192 consisting of the message header and the reply code. Then, for out
193 and in/out ioctls, there come the data with their type headers. */
194 reply_size = sizeof (mig_reply_header_t);
196 if (_IOC_INOUT (request) & IOC_OUT)
198 inline void figure_reply (unsigned int count, enum __ioctl_datum type)
200 if (count > 0)
202 /* Add the size of the type and data. */
203 reply_size += sizeof (mach_msg_type_t) + typesize (type) * count;
204 /* Align it to word size. */
205 reply_size += sizeof (mach_msg_type_t) - 1;
206 reply_size &= ~(sizeof (mach_msg_type_t) - 1);
209 figure_reply (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
210 figure_reply (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
211 figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
214 err = HURD_DPORT_USE (fd, _hurd_ctty_output (port, ctty, send_rpc));
216 t = (mach_msg_type_t *) msg.data;
217 switch (err)
219 /* Unpack the message buffer into the argument location. */
220 int out (unsigned int count, unsigned int type,
221 void *store, void **update)
223 if (count > 0)
225 const size_t len = count * typesize (type);
226 union { mach_msg_type_t t; int i; } ipctype;
227 ipctype.t = io2mach_type (count, type);
228 if (*(int *) t != ipctype.i)
229 return 1;
230 ++t;
231 memcpy (store, t, len);
232 if (update != NULL)
233 *update += len;
234 t = (void *) (((unsigned long int) t + len + sizeof (*t) - 1)
235 & ~(sizeof (*t) - 1));
237 return 0;
240 case 0:
241 if (m->msgh_size != reply_size ||
242 ((_IOC_INOUT (request) & IOC_OUT) &&
243 (out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
244 out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
245 out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))))
246 return __hurd_fail (MIG_TYPE_ERROR);
247 return 0;
249 case MIG_BAD_ID:
250 case EOPNOTSUPP:
251 /* The server didn't understand the RPC. */
252 err = ENOTTY;
253 default:
254 return __hurd_fail (err);
258 weak_alias (__ioctl, ioctl)