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. */
20 #include <sys/ioctl.h>
23 #include <hurd/signal.h>
25 #include <mach/notify.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. */
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);
52 #define msg_align(x) \
53 (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1))
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))];
62 mach_msg_header_t *const m = &msg.header.Head;
63 mach_msg_type_t *t = &msg.header.RetCodeType;
65 unsigned int reply_size;
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)
77 m->msgh_size = (char *) t - (char *) &msg;
78 m->msgh_remote_port = ioport;
79 m->msgh_local_port = __mig_get_reply_port ();
82 m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
83 MACH_MSG_TYPE_MAKE_SEND_ONCE);
84 err = _hurd_intr_rpc_mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
85 m->msgh_size, sizeof (msg),
87 MACH_MSG_TIMEOUT_NONE,
91 case MACH_MSG_SUCCESS:
93 case MACH_SEND_INVALID_REPLY:
94 case MACH_RCV_INVALID_NAME:
95 __mig_dealloc_reply_port (m->msgh_local_port);
100 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
102 /* Allow no ports or VM. */
103 __mach_msg_destroy (m);
104 /* Want to return a different error below for a different msgid. */
105 if (m->msgh_id == msgid + 100)
106 return MIG_TYPE_ERROR;
109 if (m->msgh_id != msgid + 100)
110 return (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
111 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
113 if (m->msgh_size != reply_size &&
114 m->msgh_size != sizeof (mig_reply_header_t))
115 return MIG_TYPE_ERROR;
117 if (*(int *) &msg.header.RetCodeType !=
118 ((union { mach_msg_type_t t; int i; })
119 { t: io2mach_type (1, _IOTS (sizeof msg.header.RetCode)) }).i)
120 return MIG_TYPE_ERROR;
121 return msg.header.RetCode;
126 va_start (ap, request);
127 arg = va_arg (ap, void *);
131 /* Check for a registered handler for REQUEST. */
132 ioctl_handler_t handler = _hurd_lookup_ioctl_handler (request);
135 /* This handler groks REQUEST. Se lo puntamonos. */
137 int result = (*handler) (fd, request, arg);
138 if (result != -1 || errno != ENOTTY)
141 /* The handler doesn't really grok this one.
142 Try the normal RPC translation. */
147 /* Compute the Mach message ID for the RPC from the group and command
148 parts of the ioctl request. */
149 msgid = IOC_MSGID (request);
151 if (_IOC_INOUT (request) & IOC_IN)
153 /* Pack an argument into the message buffer. */
154 void in (unsigned int count, enum __ioctl_datum type)
159 const size_t len = count * typesize ((unsigned int) type);
160 *t = io2mach_type (count, type);
161 memcpy (p, arg, len);
164 p = (void *) (((unsigned long int) p + sizeof (*t) - 1)
165 & ~(sizeof (*t) - 1));
170 /* Pack the argument data. */
171 in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
172 in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
173 in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
175 else if (_IOC_INOUT (request) == IOC_VOID)
177 /* The RPC takes a single integer_t argument.
178 Rather than pointing to the value, ARG is the value itself. */
179 *t++ = io2mach_type (1, _IOTS (int));
180 *((int *) t)++ = (int) arg;
183 /* Compute the expected size of the reply. There is a standard header
184 consisting of the message header and the reply code. Then, for out
185 and in/out ioctls, there come the data with their type headers. */
186 reply_size = sizeof (mig_reply_header_t);
188 if (_IOC_INOUT (request) & IOC_OUT)
190 inline void figure_reply (unsigned int count, enum __ioctl_datum type)
194 /* Add the size of the type and data. */
195 reply_size += sizeof (mach_msg_type_t) + typesize (type) * count;
196 /* Align it to word size. */
197 reply_size += sizeof (mach_msg_type_t) - 1;
198 reply_size &= ~(sizeof (mach_msg_type_t) - 1);
201 figure_reply (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
202 figure_reply (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
203 figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
206 err = HURD_DPORT_USE (fd, _hurd_ctty_output (port, ctty, send_rpc));
208 t = (mach_msg_type_t *) msg.data;
211 /* Unpack the message buffer into the argument location. */
212 int out (unsigned int count, unsigned int type,
213 void *store, void **update)
217 const size_t len = count * typesize (type);
218 union { mach_msg_type_t t; int i; } ipctype;
219 ipctype.t = io2mach_type (count, type);
220 if (*(int *) t != ipctype.i)
223 memcpy (store, t, len);
226 t = (void *) (((unsigned long int) t + len + sizeof (*t) - 1)
227 & ~(sizeof (*t) - 1));
233 if (m->msgh_size != reply_size ||
234 ((_IOC_INOUT (request) & IOC_OUT) &&
235 (out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
236 out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
237 out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))))
238 return __hurd_fail (MIG_TYPE_ERROR);
243 /* The server didn't understand the RPC. */
246 return __hurd_fail (err);
250 weak_alias (__ioctl, ioctl)