home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / mach / hurd / __ioctl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-15  |  4.1 KB  |  139 lines

  1. /* Copyright (C) 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  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.
  8.  
  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.
  13.  
  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.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <sys/ioctl.h>
  22. #include <hurd.h>
  23.  
  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.  */
  26. int
  27. DEFUN(__ioctl, (fd, request, arg),
  28.       int fd AND int request AND PTR arg)
  29. {
  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,
  33.       -1 };
  34.  
  35.   /* Message buffer.  */
  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];
  41.   mach_msg_id_t msgid;
  42.  
  43.   union __ioctl r;
  44.   error_t err;
  45.  
  46. #define io2mach_type(count, type) \
  47.   ((mach_msg_type_t)
  48.    { mach_types[type], (type << 1) * 8, count, 1, 0, 0 })
  49.  
  50.   /* Pack an argument into the message buffer.  */
  51.   inline void in (unsigned int count, unsigned int type)
  52.     {
  53.       if (count > 0)
  54.     {
  55.       void *const p = &t[1];
  56.       const size_t len = count * (type << 1);
  57.       *t = io2mach_type (count, type);
  58.       memcpy (p, arg, len);
  59.       arg += len;
  60.       t = p + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof (*t));
  61.     }
  62.     }
  63.  
  64.   /* Unpack the message buffer into the argument location.  */
  65.   inline int out (unsigned int count, unsigned int type,
  66.           void *store, void **update)
  67.     {
  68.       if (count > 0)
  69.     {
  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)
  74.         return 1;
  75.       ++t;
  76.       memcpy (t, store, len);
  77.       if (update != NULL)
  78.         *update += len;
  79.       t = (void *) t + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof *t);
  80.     }
  81.       return 0;
  82.     }
  83.  
  84.   /* Pack the argument data.  */
  85.   r.i = request;
  86.  
  87.   msgid = 100000 + ((r.s.group << 2) * 1000) + r.s.command;
  88.  
  89.   in (r.s.count0, r.s.type0);
  90.   in (r.s.count1, r.s.type1);
  91.   in (r.s.count2, r.s.type2);
  92.  
  93.   err = _HURD_DPORT_USE
  94.     (fd,
  95.      ({
  96.        m->msgh_size = (char *) t - msg;
  97.        m->msgh_request_port = port;
  98.        m->msgh_reply_port = __mig_reply_port ();
  99.        m->msgh_seqno = 0;
  100.        m->msgh_id = msgid;
  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),
  104.                       m->msgh_reply_port,
  105.                       MACH_MSG_TIMEOUT_NONE,
  106.                       MACH_PORT_NULL));
  107.      }));
  108.  
  109.   switch (err)
  110.     {
  111.     case MACH_MSG_SUCCESS:
  112.       break;
  113.     case MACH_SEND_INVALID_REPLY:
  114.     case MACH_RCV_INVALID_NAME:
  115.       __mig_dealloc_reply_port ();
  116.     default:
  117.       return __hurd_fail (err);
  118.     }
  119.  
  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);
  123.  
  124.   if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX) ||
  125.       m->msgh_size != (char *) t - msg)
  126.     return __hurd_fail (MIG_TYPE_ERROR);
  127.  
  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);
  134.  
  135.   if (err)
  136.     return __hurd_fail (err);
  137.   return 0;
  138. }
  139.