1 /* Copyright (C) 1992, 1993, 1994 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. */
21 #include <sys/ioctl.h>
25 #include <mach/notify.h>
28 /* Symbol set of ioctl handler lists. If there are user-registered
29 handlers, one of these lists will contain them. The other lists are
30 handlers built into the library. The definition of the set comes from
35 struct ioctl_handler *v[0];
36 } _hurd_ioctl_handler_lists;
39 /* Perform the I/O control operation specified by REQUEST on FD.
40 The actual type and use of ARG and the return value depend on REQUEST. */
42 DEFUN(__ioctl, (fd, request),
43 int fd AND unsigned long int request DOTS)
45 /* Map individual type fields to Mach IPC types. */
46 static const int mach_types[] =
47 { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
50 /* Extract the type information encoded in the request. */
51 unsigned int type = _IOC_TYPE (request);
56 mig_reply_header_t header;
57 char data[_IOT_COUNT0 (type) * (_IOT_TYPE0 (request) << 1) +
58 _IOT_COUNT1 (type) * (_IOT_TYPE1 (request) << 1) +
59 _IOT_COUNT2 (type) * (_IOT_TYPE2 (request) << 1)];
61 mach_msg_header_t *const m = &msg.header.Head;
62 mach_msg_type_t *t = &msg.header.RetCodeType;
64 unsigned int reply_size;
70 #define io2mach_type(count, type) \
71 ((mach_msg_type_t) { mach_types[type], (type << 1) * 8, count, 1, 0, 0 })
73 /* Pack an argument into the message buffer. */
74 inline void in (unsigned int count, enum __ioctl_datum type)
78 void *const p = &t[1];
79 const size_t len = count * ((unsigned int) type << 1);
80 *t = io2mach_type (count, type);
83 t = p + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof (*t));
87 /* Unpack the message buffer into the argument location. */
88 inline int out (unsigned int count, unsigned int type,
89 void *store, void **update)
93 const size_t len = count * (type << 1);
94 union { mach_msg_type_t t; int i; } ipctype;
95 ipctype.t = io2mach_type (count, type);
96 if (*(int *) t != ipctype.i)
99 memcpy (store, t, len);
102 t = (void *) t + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof *t);
109 va_start (ap, request);
110 arg = va_arg (ap, void *);
114 /* Check for a registered handler for REQUEST. */
117 const struct ioctl_handler *h;
119 for (i = 0; i < _hurd_ioctl_handler_lists.n; ++i)
120 for (h = _hurd_ioctl_handler_lists.v[i]; h != NULL; h = h->next)
121 if (request >= h->first_request && request <= h->last_request)
122 /* This handler groks REQUEST. Se lo puntamonos. */
123 return (*h->handler) (fd, request, arg);
126 /* Compute the Mach message ID for the RPC from the group and command
127 parts of the ioctl request. */
129 ((_IOC_GROUP (request) - 'f') * 4000) + _IOC_COMMAND (request));
131 if (_IOC_INOUT (request) & IOC_IN)
133 /* Pack the argument data. */
134 in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
135 in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
136 in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
139 /* Compute the expected size of the reply. There is a standard header
140 consisting of the message header and the reply code. Then, for out
141 and in/out ioctls, there come the data with their type headers. */
142 reply_size = sizeof (mig_reply_header_t);
144 if (_IOC_INOUT (request) & IOC_OUT)
146 inline void figure_reply (unsigned int count, enum __ioctl_datum type)
150 /* Add the size of the type and data. */
151 reply_size += sizeof (mach_msg_type_t) + (type << 1) * count;
152 /* Align it to word size. */
153 reply_size += sizeof (mach_msg_type_t) - 1;
154 reply_size &= ~(sizeof (mach_msg_type_t) - 1);
157 figure_reply (_IOT_COUNT0 (request), _IOT_TYPE0 (request));
158 figure_reply (_IOT_COUNT1 (request), _IOT_TYPE1 (request));
159 figure_reply (_IOT_COUNT2 (request), _IOT_TYPE2 (request));
167 m->msgh_size = (char *) t - (char *) &msg;
168 m->msgh_remote_port = port;
169 m->msgh_local_port = __mig_get_reply_port ();
172 m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
173 MACH_MSG_TYPE_MAKE_SEND_ONCE);
175 HURD_EINTR_RPC (port, __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
176 m->msgh_size, sizeof (msg),
178 MACH_MSG_TIMEOUT_NONE,
181 __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG, m->msgh_size, sizeof (msg),
182 m->msgh_local_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
188 case MACH_MSG_SUCCESS:
190 case MACH_SEND_INVALID_REPLY:
191 case MACH_RCV_INVALID_NAME:
192 __mig_dealloc_reply_port ();
194 return __hurd_fail (err);
197 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
199 /* Allow no ports or VM. */
200 __mach_msg_destroy (m);
201 /* Want to return a different error below for a different msgid. */
202 if (m->msgh_id == msgid + 100)
203 return __hurd_fail (MIG_TYPE_ERROR);
206 if (m->msgh_id != msgid + 100)
207 return __hurd_fail (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
208 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
210 if (m->msgh_size != reply_size &&
211 m->msgh_size != sizeof (mig_reply_header_t))
212 return __hurd_fail (MIG_TYPE_ERROR);
214 if (*(int *) &msg.header.RetCodeType !=
215 ((union { mach_msg_type_t t; int i; })
216 { t: io2mach_type (1, _IOTS (sizeof msg.header.RetCode)) }).i)
217 return __hurd_fail (MIG_TYPE_ERROR);
218 err = msg.header.RetCode;
220 t = (mach_msg_type_t *) msg.data;
224 if (m->msgh_size != reply_size ||
225 out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
226 out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
227 out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))
228 return __hurd_fail (MIG_TYPE_ERROR);
233 /* The server didn't understand the RPC. */
236 return __hurd_fail (err);