1 /* Copyright (C) 1992 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>
24 /* Perform the I/O control operation specified by REQUEST on FD.
25 The actual type and use of ARG and the return value depend on REQUEST. */
27 DEFUN(__ioctl, (fd, request, arg),
28 int fd AND int request AND PTR arg)
30 /* Map individual type fields to Mach IPC types. */
31 static const int mach_types[] =
32 { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
36 char msg[sizeof (mach_msg_header_t) + /* Header. */
37 sizeof (mach_msg_type_t) + 4 /* Return code. */
38 3 * (sizeof (mach_msg_type_t) + (4 * 32))]; /* Argument data. */
39 mach_msg_header_t *m = (mach_msg_header_t *) msg;
40 mach_msg_type_t *t = (mach_msg_type_t *) &m[1];
46 #define io2mach_type(count, type) \
48 { mach_types[type], (type << 1) * 8, count, 1, 0, 0 })
50 /* Pack an argument into the message buffer. */
51 inline void in (unsigned int count, unsigned int type)
55 void *const p = &t[1];
56 const size_t len = count * (type << 1);
57 *t = io2mach_type (count, type);
60 t = p + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof (*t));
64 /* Unpack the message buffer into the argument location. */
65 inline int out (unsigned int count, unsigned int type,
66 void *store, void **update)
70 const size_t len = count * (type << 1);
71 union { mach_msg_type_t t; int i; } ipctype;
72 ipctype.t = io2mach_type (count, type);
73 if (*(int *) t != ipctype.i)
76 memcpy (t, store, len);
79 t = (void *) t + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof *t);
84 /* Pack the argument data. */
87 msgid = 100000 + ((r.s.group << 2) * 1000) + r.s.command;
89 in (r.s.count0, r.s.type0);
90 in (r.s.count1, r.s.type1);
91 in (r.s.count2, r.s.type2);
96 m->msgh_size = (char *) t - msg;
97 m->msgh_request_port = port;
98 m->msgh_reply_port = __mig_reply_port ();
101 m->msgh_bits = ?; /* XXX */
102 _HURD_EINTR_RPC (port, __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
103 m->msgh_size, sizeof (msg),
105 MACH_MSG_TIMEOUT_NONE,
111 case MACH_MSG_SUCCESS:
113 case MACH_SEND_INVALID_REPLY:
114 case MACH_RCV_INVALID_NAME:
115 __mig_dealloc_reply_port ();
117 return __hurd_fail (err);
120 if (m->msgh_id != msgid + 100)
121 return __hurd_fail (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
122 MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
124 if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX) ||
125 m->msgh_size != (char *) t - msg)
126 return __hurd_fail (MIG_TYPE_ERROR);
128 t = (mach_msg_type_t) &m[1];
129 if (out (1, _IOTS (sizeof (error_t)), &err, NULL) ||
130 out (r.s.count0, r.s.type0, arg, &arg) ||
131 out (r.s.count1, r.s.type2, arg, &arg) ||
132 out (r.s.count2, r.s.type2, arg, &arg))
133 return __hurd_fail (MIG_TYPE_ERROR);
136 return __hurd_fail (err);