Back to index

glibc  2.9
ioctl.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,93,94,95,96,97,99,2000,2002,2005
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <sys/ioctl.h>
00022 #include <hurd.h>
00023 #include <hurd/fd.h>
00024 #include <hurd/signal.h>
00025 #include <stdarg.h>
00026 #include <mach/notify.h>
00027 #include <assert.h>
00028 #include <string.h>
00029 #include <stdint.h>
00030 #include <hurd/ioctl.h>
00031 #include <mach/mig_support.h>
00032 
00033 #include <hurd/ioctls.defs>
00034 
00035 #define typesize(type)      (1 << (type))
00036 
00037 
00038 /* Perform the I/O control operation specified by REQUEST on FD.
00039    The actual type and use of ARG and the return value depend on REQUEST.  */
00040 int
00041 __ioctl (int fd, unsigned long int request, ...)
00042 {
00043 #ifdef MACH_MSG_TYPE_CHAR
00044   /* Map individual type fields to Mach IPC types.  */
00045   static const int mach_types[] =
00046     { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
00047       MACH_MSG_TYPE_INTEGER_64 };
00048 #define io2mach_type(count, type) \
00049   ((mach_msg_type_t) { mach_types[type], typesize (type) * 8, count, 1, 0, 0 })
00050 #endif
00051 
00052   /* Extract the type information encoded in the request.  */
00053   unsigned int type = _IOC_TYPE (request);
00054 
00055   /* Message buffer.  */
00056 #define msg_align(x) \
00057   (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1))
00058   struct
00059   {
00060 #ifdef MACH_MSG_TYPE_BIT
00061     union
00062     {
00063       mig_reply_header_t header;
00064       struct
00065       {
00066        mach_msg_header_t    Head;
00067        int                  RetCodeType;
00068        kern_return_t        RetCode;
00069       } header_typecheck;
00070     };
00071     char data[3 * sizeof (mach_msg_type_t) +
00072             msg_align (_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type))) +
00073             msg_align (_IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type))) +
00074             _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
00075 #else  /* Untyped Mach IPC format.  */
00076     mig_reply_error_t header;
00077     char data[_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type)) +
00078             _IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type)) +
00079             _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))];
00080     mach_msg_trailer_t trailer;
00081 #endif
00082   } msg;
00083   mach_msg_header_t *const m = &msg.header.Head;
00084   mach_msg_id_t msgid;
00085   unsigned int reply_size;
00086 #ifdef MACH_MSG_TYPE_BIT
00087   mach_msg_type_t *t;
00088 #else
00089   void *p;
00090 #endif
00091 
00092   void *arg;
00093 
00094   error_t err;
00095 
00096   /* Send the RPC already packed up in MSG to IOPORT
00097      and decode the return value.  */
00098   error_t send_rpc (io_t ioport)
00099     {
00100       error_t err;
00101 #ifdef MACH_MSG_TYPE_BIT
00102       mach_msg_type_t *t = &msg.header.RetCodeType;
00103 #else
00104       void *p = &msg.header.RetCode;
00105 #endif
00106 
00107       /* Marshal the request arguments into the message buffer.
00108         We must redo this work each time we retry the RPC after a SIGTTOU,
00109         because the reply message containing the EBACKGROUND error code
00110         clobbers the same message buffer also used for the request.  */
00111 
00112       if (_IOC_INOUT (request) & IOC_IN)
00113        {
00114          /* We don't want to advance ARG since it will be used to copy out
00115              too if IOC_OUT is also set.  */
00116          void *argptr = arg;
00117 
00118          /* Pack an argument into the message buffer.  */
00119          void in (unsigned int count, enum __ioctl_datum type)
00120            {
00121              if (count > 0)
00122               {
00123                 const size_t len = count * typesize ((unsigned int) type);
00124 #ifdef MACH_MSG_TYPE_BIT
00125                 void *p = &t[1];
00126                 *t = io2mach_type (count, type);
00127                 p = __mempcpy (p, argptr, len);
00128                 p = (void *) (((uintptr_t) p + sizeof (*t) - 1)
00129                             & ~(sizeof (*t) - 1));
00130                 t = p;
00131 #else
00132                 p = __mempcpy (p, argptr, len);
00133 #endif
00134                 argptr += len;
00135               }
00136            }
00137 
00138          /* Pack the argument data.  */
00139          in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
00140          in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
00141          in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
00142        }
00143       else if (_IOC_INOUT (request) == IOC_VOID)
00144        {
00145          /* The RPC takes a single integer_t argument.
00146             Rather than pointing to the value, ARG is the value itself.  */
00147 #ifdef MACH_MSG_TYPE_BIT
00148          *t++ = io2mach_type (1, _IOTS (integer_t));
00149          *(integer_t *) t = (integer_t) arg;
00150          t = (void *) t + sizeof (integer_t);
00151 #else
00152          *(integer_t *) p = (integer_t) arg;
00153          p = (void *) p + sizeof (integer_t);
00154 #endif
00155        }
00156 
00157       memset (m, 0, sizeof *m);    /* Clear unused fields.  */
00158       m->msgh_size = (
00159 #ifdef MACH_MSG_TYPE_BIT
00160                     (char *) t
00161 #else
00162                     (char *) p
00163 #endif
00164                     - (char *) &msg);
00165       m->msgh_remote_port = ioport;
00166       m->msgh_local_port = __mig_get_reply_port ();
00167       m->msgh_id = msgid;
00168       m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
00169                                  MACH_MSG_TYPE_MAKE_SEND_ONCE);
00170       err = _hurd_intr_rpc_mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
00171                                  m->msgh_size, sizeof (msg),
00172                                  m->msgh_local_port,
00173                                  MACH_MSG_TIMEOUT_NONE,
00174                                  MACH_PORT_NULL);
00175       switch (err)
00176        {
00177        case MACH_MSG_SUCCESS:
00178          break;
00179        case MACH_SEND_INVALID_REPLY:
00180        case MACH_RCV_INVALID_NAME:
00181          __mig_dealloc_reply_port (m->msgh_local_port);
00182        default:
00183          return err;
00184        }
00185 
00186       if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
00187        {
00188          /* Allow no ports or VM.  */
00189          __mach_msg_destroy (m);
00190          /* Want to return a different error below for a different msgid.  */
00191          if (m->msgh_id == msgid + 100)
00192            return MIG_TYPE_ERROR;
00193        }
00194 
00195       if (m->msgh_id != msgid + 100)
00196        return (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
00197               MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
00198 
00199       if (m->msgh_size != reply_size &&
00200          m->msgh_size != sizeof msg.header)
00201        return MIG_TYPE_ERROR;
00202 
00203 #ifdef MACH_MSG_TYPE_BIT
00204       if (msg.header_typecheck.RetCodeType !=
00205          ((union { mach_msg_type_t t; int i; })
00206           { t: io2mach_type (1, _IOTS (msg.header.RetCode)) }).i)
00207        return MIG_TYPE_ERROR;
00208 #endif
00209       return msg.header.RetCode;
00210     }
00211 
00212   va_list ap;
00213 
00214   va_start (ap, request);
00215   arg = va_arg (ap, void *);
00216   va_end (ap);
00217 
00218   {
00219     /* Check for a registered handler for REQUEST.  */
00220     ioctl_handler_t handler = _hurd_lookup_ioctl_handler (request);
00221     if (handler)
00222       {
00223        /* This handler groks REQUEST.  Se lo puntamonos.  */
00224        int save = errno;
00225        int result = (*handler) (fd, request, arg);
00226        if (result != -1 || errno != ENOTTY)
00227          return result;
00228 
00229        /* The handler doesn't really grok this one.
00230           Try the normal RPC translation.  */
00231        errno = save;
00232       }
00233   }
00234 
00235   /* Compute the Mach message ID for the RPC from the group and command
00236      parts of the ioctl request.  */
00237   msgid = IOC_MSGID (request);
00238 
00239   /* Compute the expected size of the reply.  There is a standard header
00240      consisting of the message header and the reply code.  Then, for out
00241      and in/out ioctls, there come the data with their type headers.  */
00242   reply_size = sizeof msg.header;
00243 
00244   if (_IOC_INOUT (request) & IOC_OUT)
00245     {
00246       inline void figure_reply (unsigned int count, enum __ioctl_datum type)
00247        {
00248          if (count > 0)
00249            {
00250 #ifdef MACH_MSG_TYPE_BIT
00251              /* Add the size of the type and data.  */
00252              reply_size += sizeof (mach_msg_type_t) + typesize (type) * count;
00253              /* Align it to word size.  */
00254              reply_size += sizeof (mach_msg_type_t) - 1;
00255              reply_size &= ~(sizeof (mach_msg_type_t) - 1);
00256 #else
00257              reply_size += typesize (type) * count;
00258 #endif
00259            }
00260        }
00261       figure_reply (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
00262       figure_reply (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
00263       figure_reply (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
00264     }
00265 
00266   /* Marshal the arguments into the request message and make the RPC.
00267      This wrapper function handles EBACKGROUND returns, turning them
00268      into either SIGTTOU or EIO.  */
00269   err = HURD_DPORT_USE (fd, _hurd_ctty_output (port, ctty, send_rpc));
00270 
00271 #ifdef MACH_MSG_TYPE_BIT
00272   t = (mach_msg_type_t *) msg.data;
00273 #else
00274   p = (void *) msg.data;
00275 #endif
00276   switch (err)
00277     {
00278       /* Unpack the message buffer into the argument location.  */
00279       int out (unsigned int count, unsigned int type,
00280               void *store, void **update)
00281        {
00282          if (count > 0)
00283            {
00284              const size_t len = count * typesize (type);
00285 #ifdef MACH_MSG_TYPE_BIT
00286              union { mach_msg_type_t t; int i; } ipctype;
00287              ipctype.t = io2mach_type (count, type);
00288              if (*(int *) t != ipctype.i)
00289               return 1;
00290              ++t;
00291              memcpy (store, t, len);
00292              if (update != NULL)
00293               *update += len;
00294              t = (void *) (((uintptr_t) t + len + sizeof (*t) - 1)
00295                          & ~(sizeof (*t) - 1));
00296 #else
00297              memcpy (store, p, len);
00298              p += len;
00299              if (update != NULL)
00300               *update += len;
00301 #endif
00302            }
00303          return 0;
00304        }
00305 
00306     case 0:
00307       if (m->msgh_size != reply_size ||
00308          ((_IOC_INOUT (request) & IOC_OUT) &&
00309           (out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
00310            out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
00311            out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))))
00312        return __hurd_fail (MIG_TYPE_ERROR);
00313       return 0;
00314 
00315     case MIG_BAD_ID:
00316     case EOPNOTSUPP:
00317       /* The server didn't understand the RPC.  */
00318       err = ENOTTY;
00319     default:
00320       return __hurd_fail (err);
00321     }
00322 }
00323 
00324 weak_alias (__ioctl, ioctl)