-/* Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
-This file is part of the GNU C Library.
+/* Copyright (C) 1992, 93, 94, 95, 96, 97, 99 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
-The GNU C Library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-Library General Public License for more details.
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA. */
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
-#include <ansidecl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <hurd.h>
#include <assert.h>
#include <string.h>
#include <hurd/ioctl.h>
+#include <mach/mig_support.h>
+#include <hurd/ioctls.defs>
#define typesize(type) (1 << (type))
/* Perform the I/O control operation specified by REQUEST on FD.
The actual type and use of ARG and the return value depend on REQUEST. */
int
-DEFUN(__ioctl, (fd, request),
- int fd AND unsigned long int request DOTS)
+__ioctl (int fd, unsigned long int request, ...)
{
/* Map individual type fields to Mach IPC types. */
static const int mach_types[] =
{ MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
- -1 };
+ MACH_MSG_TYPE_INTEGER_64 };
#define io2mach_type(count, type) \
((mach_msg_type_t) { mach_types[type], typesize (type) * 8, count, 1, 0, 0 })
unsigned int type = _IOC_TYPE (request);
/* Message buffer. */
+#define msg_align(x) \
+ (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1))
struct
{
mig_reply_header_t header;
char data[3 * sizeof (mach_msg_type_t) +
- _IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type)) +
- _IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type)) +
+ msg_align (_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type))) +
+ msg_align (_IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type))) +
_IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
} msg;
mach_msg_header_t *const m = &msg.header.Head;
- mach_msg_type_t *t = &msg.header.RetCodeType;
+ mach_msg_type_t *t;
mach_msg_id_t msgid;
unsigned int reply_size;
error_t err;
- struct hurd_sigstate *ss;
- int noctty;
-
/* Send the RPC already packed up in MSG to IOPORT
and decode the return value. */
- inline error_t send_rpc (io_t ioport)
+ error_t send_rpc (io_t ioport)
{
error_t err;
+ mach_msg_type_t *t = &msg.header.RetCodeType;
+
+ /* Marshal the request arguments into the message buffer.
+ We must redo this work each time we retry the RPC after a SIGTTOU,
+ because the reply message containing the EBACKGROUND error code
+ clobbers the same message buffer also used for the request. */
+
+ if (_IOC_INOUT (request) & IOC_IN)
+ {
+ /* Pack an argument into the message buffer. */
+ void in (unsigned int count, enum __ioctl_datum type)
+ {
+ if (count > 0)
+ {
+ void *p = &t[1];
+ const size_t len = count * typesize ((unsigned int) type);
+ *t = io2mach_type (count, type);
+ memcpy (p, arg, len);
+ arg += len;
+ p += len;
+ p = (void *) (((unsigned long int) p + sizeof (*t) - 1)
+ & ~(sizeof (*t) - 1));
+ t = p;
+ }
+ }
+
+ /* Pack the argument data. */
+ in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
+ in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
+ in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
+ }
+ else if (_IOC_INOUT (request) == IOC_VOID)
+ {
+ /* The RPC takes a single integer_t argument.
+ Rather than pointing to the value, ARG is the value itself. */
+ *t++ = io2mach_type (1, _IOTS (int));
+ *((int *) t)++ = (int) arg;
+ }
m->msgh_size = (char *) t - (char *) &msg;
m->msgh_remote_port = ioport;
m->msgh_id = msgid;
m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
MACH_MSG_TYPE_MAKE_SEND_ONCE);
- err = HURD_EINTR_RPC (ioport, __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
- m->msgh_size, sizeof (msg),
- m->msgh_local_port,
- MACH_MSG_TIMEOUT_NONE,
- MACH_PORT_NULL));
+ err = _hurd_intr_rpc_mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
+ m->msgh_size, sizeof (msg),
+ m->msgh_local_port,
+ MACH_MSG_TIMEOUT_NONE,
+ MACH_PORT_NULL);
switch (err)
{
case MACH_MSG_SUCCESS:
/* Check for a registered handler for REQUEST. */
ioctl_handler_t handler = _hurd_lookup_ioctl_handler (request);
if (handler)
- /* This handler groks REQUEST. Se lo puntamonos. */
- return (*handler) (fd, request, arg);
+ {
+ /* This handler groks REQUEST. Se lo puntamonos. */
+ int save = errno;
+ int result = (*handler) (fd, request, arg);
+ if (result != -1 || errno != ENOTTY)
+ return result;
+
+ /* The handler doesn't really grok this one.
+ Try the normal RPC translation. */
+ errno = save;
+ }
}
/* Compute the Mach message ID for the RPC from the group and command
parts of the ioctl request. */
- msgid = 100000 + ((_IOC_GROUP (request) - 'f') * 4000); /* Base subsystem */
- /* Because of MiG's poorly chosen algorithm of adding 100 to a request
- msgid to produce the reply msgid, we cannot just add the command part
- of the ioctl request to the subsystem base msgid. For ioctl requests
- past 99, we must skip blocks of 100 msgids to allow for the reply
- msgids corresponding to the earlier requests. */
- if (_IOC_COMMAND (request) >= 100)
- msgid += 100;
- if (_IOC_COMMAND (request) >= 200)
- msgid += 100;
- msgid += _IOC_COMMAND (request);
-
- if (_IOC_INOUT (request) & IOC_IN)
- {
- /* Pack an argument into the message buffer. */
- void in (unsigned int count, enum __ioctl_datum type)
- {
- if (count > 0)
- {
- void *p = &t[1];
- const size_t len = count * typesize ((unsigned int) type);
- *t = io2mach_type (count, type);
- memcpy (p, arg, len);
- arg += len;
- p += len;
- p = (void *) (((unsigned long int) p + sizeof (*t) - 1)
- & ~(sizeof (*t) - 1));
- t = p;
- }
- }
-
- /* Pack the argument data. */
- in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
- in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
- in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
- }
+ msgid = IOC_MSGID (request);
/* Compute the expected size of the reply. There is a standard header
consisting of the message header and the reply code. Then, for out
figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
}
- /* Note that fd-write.c implements the same SIGTTOU behavior.
- Any changes here should be done there as well. */
-
- /* Don't use the ctty io port if we are blocking or ignoring SIGTTOU. */
- ss = _hurd_self_sigstate ();
- noctty = (__sigismember (&ss->blocked, SIGTTOU) ||
- ss->actions[SIGTTOU].sa_handler == SIG_IGN);
- __mutex_unlock (&ss->lock);
-
- err = HURD_DPORT_USE
- (fd,
- ({
- const io_t ioport = (!noctty && ctty != MACH_PORT_NULL) ? ctty : port;
- do
- {
- /* The actual hair to send the RPC is in the inline `send_rpc'
- function (above), to avoid horrendous indentation. */
- err = send_rpc (ioport);
- if (ioport == ctty && err == EBACKGROUND)
- {
- if (_hurd_orphaned)
- /* Our process group is orphaned, so we never generate a
- signal; we just fail. */
- err = EIO;
- else
- {
- /* Send a SIGTTOU signal to our process group. */
- err = __USEPORT (CTTYID, _hurd_sig_post (0, SIGTTOU, port));
- /* XXX what to do if error here? */
- /* At this point we should have just run the handler for
- SIGTTOU or resumed after being stopped. Now this is
- still a "system call", so check to see if we should
- restart it. */
- __mutex_lock (&ss->lock);
- if (!(ss->actions[SIGTTOU].sa_flags & SA_RESTART))
- err = EINTR;
- __mutex_unlock (&ss->lock);
- }
- }
- } while (err == EBACKGROUND);
- err;
- }));
+ /* Marshal the arguments into the request message and make the RPC.
+ This wrapper function handles EBACKGROUND returns, turning them
+ into either SIGTTOU or EIO. */
+ err = HURD_DPORT_USE (fd, _hurd_ctty_output (port, ctty, send_rpc));
t = (mach_msg_type_t *) msg.data;
switch (err)