Back to index

glibc  2.9
sendmsg.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001,2002,2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public License as
00006    published by the Free Software Foundation; either version 2.1 of the
00007    License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00016    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.  */
00018 
00019 #include <errno.h>
00020 #include <string.h>
00021 #include <sys/socket.h>
00022 #include <sys/un.h>
00023 
00024 #include <hurd.h>
00025 #include <hurd/fd.h>
00026 #include <hurd/ifsock.h>
00027 #include <hurd/socket.h>
00028 
00029 /* Send a message described MESSAGE on socket FD.
00030    Returns the number of bytes sent, or -1 for errors.  */
00031 ssize_t
00032 __libc_sendmsg (int fd, const struct msghdr *message, int flags)
00033 {
00034   error_t err = 0;
00035   struct sockaddr_un *addr = message->msg_name;
00036   socklen_t addr_len = message->msg_namelen;
00037   addr_port_t aport = MACH_PORT_NULL;
00038   union
00039   {
00040     char *ptr;
00041     vm_address_t addr;
00042   } data = { .ptr = NULL };
00043   char data_buf[2048];
00044   mach_msg_type_number_t len;
00045   mach_msg_type_number_t amount;
00046   int dealloc = 0;
00047   int i;
00048 
00049   /* Find the total number of bytes to be written.  */
00050   len = 0;
00051   for (i = 0; i < message->msg_iovlen; i++)
00052     {
00053       if (message->msg_iov[i].iov_len > 0)
00054        {
00055          /* As an optimization, if we only have a single non-empty
00056              iovec, we set DATA and LEN from it.  */
00057          if (len == 0)
00058            data.ptr = message->msg_iov[i].iov_base;
00059          else
00060            data.ptr = NULL;
00061 
00062          len += message->msg_iov[i].iov_len;
00063        }
00064     }
00065 
00066   if (data.ptr == NULL)
00067     {
00068       size_t to_copy;
00069       char *buf;
00070 
00071       /* Allocate a temporary buffer to hold the data.  For small
00072          amounts of data, we allocate a buffer on the stack.  Larger
00073          amounts of data are stored in a page-aligned buffer.  The
00074          limit of 2048 bytes is inspired by the MiG stubs.  */
00075       if (len > 2048)
00076        {
00077          err = __vm_allocate (__mach_task_self (), &data.addr, len, 1);
00078          if (err)
00079            {
00080              __set_errno (err);
00081              return -1;
00082            }
00083          dealloc = 1;
00084        }
00085       else
00086        data.ptr = data_buf;
00087 
00088       /* Copy the data into DATA.  */
00089       to_copy = len;
00090       buf = data.ptr;
00091       for (i = 0; i < len; i++)
00092        {
00093 #define       min(a, b)     ((a) > (b) ? (b) : (a))
00094          size_t copy = min (message->msg_iov[i].iov_len, to_copy);
00095 
00096          buf = __mempcpy (buf, message->msg_iov[i].iov_base, copy);
00097 
00098          to_copy -= copy;
00099          if (to_copy == 0)
00100            break;
00101        }
00102     }
00103 
00104   if (addr)
00105     {
00106       if (addr->sun_family == AF_LOCAL)
00107        {
00108          /* For the local domain, we must look up the name as a file
00109             and talk to it with the ifsock protocol.  */
00110          file_t file = __file_name_lookup (addr->sun_path, 0, 0);
00111          if (file == MACH_PORT_NULL)
00112            return -1;
00113          err = __ifsock_getsockaddr (file, &aport);
00114          __mach_port_deallocate (__mach_task_self (), file);
00115          if (err == MIG_BAD_ID || err == EOPNOTSUPP)
00116            /* The file did not grok the ifsock protocol.  */
00117            err = ENOTSOCK;
00118          if (err)
00119            return __hurd_fail (err);
00120        }
00121       else
00122        err = EIEIO;
00123     }
00124 
00125   err = HURD_DPORT_USE (fd,
00126                      ({
00127                        if (err)
00128                          err = __socket_create_address (port,
00129                                                     addr->sun_family,
00130                                                     (char *) addr,
00131                                                     addr_len,
00132                                                     &aport);
00133                        if (! err)
00134                          {
00135                            /* Send the data.  */
00136                            err = __socket_send (port, aport,
00137                                              flags, data.ptr, len,
00138                                              NULL,
00139                                              MACH_MSG_TYPE_COPY_SEND, 0,
00140                                              message->msg_control,
00141                                              message->msg_controllen,
00142                                              &amount);
00143                            __mach_port_deallocate (__mach_task_self (),
00144                                                 aport);
00145                          }
00146                        err;
00147                      }));
00148 
00149   if (dealloc)
00150     __vm_deallocate (__mach_task_self (), data.addr, len);
00151 
00152   return err ? __hurd_sockfail (fd, flags, err) : amount;
00153 }
00154 
00155 weak_alias (__libc_sendmsg, sendmsg)
00156 weak_alias (__libc_sendmsg, __sendmsg)