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 } *const _hurd_ioctl_handlers;
39 #define typesize(type) (1 << (type))
42 /* Perform the I/O control operation specified by REQUEST on FD.
43 The actual type and use of ARG and the return value depend on REQUEST. */
45 DEFUN(__ioctl, (fd, request),
46 int fd AND unsigned long int request DOTS)
48 /* Map individual type fields to Mach IPC types. */
49 static const int mach_types[] =
50 { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
53 /* Extract the type information encoded in the request. */
54 unsigned int type = _IOC_TYPE (request);
59 mig_reply_header_t header;
60 char data[_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (request)) +
61 _IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (request)) +
62 _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (request))];
64 mach_msg_header_t *const m = &msg.header.Head;
65 mach_msg_type_t *t = &msg.header.RetCodeType;
67 unsigned int reply_size;
73 #define io2mach_type(count, type) \
74 ((mach_msg_type_t) { mach_types[type], typesize (type) * 8, count, 1, 0, 0 })
78 va_start (ap, request);
79 arg = va_arg (ap, void *);
83 /* Check for a registered handler for REQUEST. */
86 const struct ioctl_handler *h;
88 for (i = 0; i < _hurd_ioctl_handlers->n; ++i)
89 for (h = _hurd_ioctl_handlers->v[i]; h != NULL; h = h->next)
90 if (request >= h->first_request && request <= h->last_request)
91 /* This handler groks REQUEST. Se lo puntamonos. */
92 return (*h->handler) (fd, request, arg);
95 /* Compute the Mach message ID for the RPC from the group and command
96 parts of the ioctl request. */
98 ((_IOC_GROUP (request) - 'f') * 4000) + _IOC_COMMAND (request));
100 if (_IOC_INOUT (request) & IOC_IN)
102 /* Pack an argument into the message buffer. */
103 void in (unsigned int count, enum __ioctl_datum type)
108 const size_t len = count * typesize ((unsigned int) type);
109 *t = io2mach_type (count, type);
110 memcpy (p, arg, len);
113 p = (void *) (((unsigned long int) p + sizeof (*t) - 1)
114 & ~(sizeof (*t) - 1));
119 /* Pack the argument data. */
120 in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
121 in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
122 in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
125 /* Compute the expected size of the reply. There is a standard header
126 consisting of the message header and the reply code. Then, for out
127 and in/out ioctls, there come the data with their type headers. */
128 reply_size = sizeof (mig_reply_header_t);
130 if (_IOC_INOUT (request) & IOC_OUT)
132 inline void figure_reply (unsigned int count, enum __ioctl_datum type)
136 /* Add the size of the type and data. */
137 reply_size += sizeof (mach_msg_type_t) + typesize (type) * count;
138 /* Align it to word size. */
139 reply_size += sizeof (mach_msg_type_t) - 1;
140 reply_size &= ~(sizeof (mach_msg_type_t) - 1);
143 figure_reply (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
144 figure_reply (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
145 figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
153 m->msgh_size = (char *) t - (char *) &msg;
154 m->msgh_remote_port = port;
155 m->msgh_local_port = __mig_get_reply_port ();
158 m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
159 MACH_MSG_TYPE_MAKE_SEND_ONCE);
161 HURD_EINTR_RPC (port, __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
162 m->msgh_size, sizeof (msg),
164 MACH_MSG_TIMEOUT_NONE,
167 __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG, m->msgh_size, sizeof (msg),
168 m->msgh_local_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
174 case MACH_MSG_SUCCESS:
176 case MACH_SEND_INVALID_REPLY:
177 case MACH_RCV_INVALID_NAME:
178 __mig_dealloc_reply_port ();
180 return __hurd_fail (err);
183 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
185 /* Allow no ports or VM. */
186 __mach_msg_destroy (m);
187 /* Want to return a different error below for a different msgid. */
188 if (m->msgh_id == msgid + 100)
189 return __hurd_fail (MIG_TYPE_ERROR);
192 if (m->msgh_id != msgid + 100)
193 return __hurd_fail (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
194 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
196 if (m->msgh_size != reply_size &&
197 m->msgh_size != sizeof (mig_reply_header_t))
198 return __hurd_fail (MIG_TYPE_ERROR);
200 if (*(int *) &msg.header.RetCodeType !=
201 ((union { mach_msg_type_t t; int i; })
202 { t: io2mach_type (1, _IOTS (sizeof msg.header.RetCode)) }).i)
203 return __hurd_fail (MIG_TYPE_ERROR);
204 err = msg.header.RetCode;
206 t = (mach_msg_type_t *) msg.data;
209 /* Unpack the message buffer into the argument location. */
210 int out (unsigned int count, unsigned int type,
211 void *store, void **update)
215 const size_t len = count * typesize (type);
216 union { mach_msg_type_t t; int i; } ipctype;
217 ipctype.t = io2mach_type (count, type);
218 if (*(int *) t != ipctype.i)
221 memcpy (store, t, len);
224 t = (void *) (((unsigned long int) t + len + sizeof (*t) - 1)
225 & ~(sizeof (*t) - 1));
231 if (m->msgh_size != reply_size ||
232 ((_IOC_INOUT (request) & IOC_OUT) &&
233 out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
234 out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
235 out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg)))
236 return __hurd_fail (MIG_TYPE_ERROR);
241 /* The server didn't understand the RPC. */
244 return __hurd_fail (err);