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>
27 /* Symbol set of ioctl handler lists. If there are user-registered
28 handlers, one of these lists will contain them. The other lists are
29 handlers built into the library. */
33 struct ioctl_handler *v[0];
34 } _hurd_ioctl_handler_lists;
37 /* Perform the I/O control operation specified by REQUEST on FD.
38 The actual type and use of ARG and the return value depend on REQUEST. */
40 DEFUN(__ioctl, (fd, request),
41 int fd AND unsigned long int request DOTS)
43 /* Map individual type fields to Mach IPC types. */
44 static const int mach_types[] =
45 { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
49 char msg[sizeof (mach_msg_header_t) + /* Header. */
50 sizeof (mach_msg_type_t) + 4 + /* Return code. */
51 3 * (sizeof (mach_msg_type_t) + (4 * 32))]; /* Argument data. */
52 mach_msg_header_t *m = (mach_msg_header_t *) msg;
53 mach_msg_type_t *t = (mach_msg_type_t *) &m[1];
61 #define io2mach_type(count, type) \
62 ((mach_msg_type_t) { mach_types[type], (type << 1) * 8, count, 1, 0, 0 })
64 /* Pack an argument into the message buffer. */
65 inline void in (unsigned int count, unsigned int type)
69 void *const p = &t[1];
70 const size_t len = count * (type << 1);
71 *t = io2mach_type (count, type);
74 t = p + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof (*t));
78 /* Unpack the message buffer into the argument location. */
79 inline int out (unsigned int count, unsigned int type,
80 void *store, void **update)
84 const size_t len = count * (type << 1);
85 union { mach_msg_type_t t; int i; } ipctype;
86 ipctype.t = io2mach_type (count, type);
87 if (*(int *) t != ipctype.i)
90 memcpy (t, store, len);
93 t = (void *) t + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof *t);
100 va_start (ap, request);
101 arg = va_arg (ap, void *);
105 /* Check for a registered handler for REQUEST. */
108 const struct ioctl_handler *h;
110 for (i = 0; i < _hurd_ioctl_handler_lists.n; ++i)
111 for (h = _hurd_ioctl_handler_lists.v[i]; h != NULL; h = h->next)
112 if (request >= h->first_request && request <= h->last_request)
113 /* This handler groks REQUEST. Se lo puntamonos. */
114 return (*h->handler) (fd, request, arg);
118 /* Pack the argument data. */
121 msgid = 100000 + ((r.__s.group << 2) * 1000) + r.__s.command;
123 in (r.__t.count0, r.__t.type0);
124 in (r.__t.count1, r.__t.type1);
125 in (r.__t.count2, r.__t.type2);
130 m->msgh_size = (char *) t - msg;
131 m->msgh_remote_port = port;
132 m->msgh_local_port = __mig_get_reply_port ();
136 m->msgh_bits = ?; /* XXX */
138 HURD_EINTR_RPC (port, __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
139 m->msgh_size, sizeof (msg),
141 MACH_MSG_TIMEOUT_NONE,
147 case MACH_MSG_SUCCESS:
149 case MACH_SEND_INVALID_REPLY:
150 case MACH_RCV_INVALID_NAME:
151 __mig_dealloc_reply_port ();
153 return __hurd_fail (err);
156 if (m->msgh_id != msgid + 100)
157 return __hurd_fail (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
158 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
160 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX) || /* XXX ? */
161 m->msgh_size != (char *) t - msg)
162 return __hurd_fail (MIG_TYPE_ERROR);
164 t = (mach_msg_type_t *) &m[1];
165 if (out (1, _IOTS (sizeof (error_t)), &err, NULL) ||
166 out (r.__t.count0, r.__t.type0, arg, &arg) ||
167 out (r.__t.count1, r.__t.type2, arg, &arg) ||
168 out (r.__t.count2, r.__t.type2, arg, &arg))
169 return __hurd_fail (MIG_TYPE_ERROR);
172 return __hurd_fail (err);