Blame sysdeps/mach/hurd/sendmsg.c

Packit 6c4009
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <sys/un.h>
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <hurd/ifsock.h>
Packit 6c4009
#include <hurd/socket.h>
Packit 6c4009
#include "hurd/hurdsocket.h"
Packit 6c4009
Packit 6c4009
/* Send a message described MESSAGE on socket FD.
Packit 6c4009
   Returns the number of bytes sent, or -1 for errors.  */
Packit 6c4009
ssize_t
Packit 6c4009
__libc_sendmsg (int fd, const struct msghdr *message, int flags)
Packit 6c4009
{
Packit 6c4009
  error_t err = 0;
Packit 6c4009
  struct sockaddr_un *addr = message->msg_name;
Packit 6c4009
  socklen_t addr_len = message->msg_namelen;
Packit 6c4009
  addr_port_t aport = MACH_PORT_NULL;
Packit 6c4009
  union
Packit 6c4009
  {
Packit 6c4009
    char *ptr;
Packit 6c4009
    vm_address_t addr;
Packit 6c4009
  } data = { .ptr = NULL };
Packit 6c4009
  char data_buf[2048];
Packit 6c4009
  mach_msg_type_number_t len;
Packit 6c4009
  mach_msg_type_number_t amount;
Packit 6c4009
  int dealloc = 0;
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  /* Find the total number of bytes to be written.  */
Packit 6c4009
  len = 0;
Packit 6c4009
  for (i = 0; i < message->msg_iovlen; i++)
Packit 6c4009
    {
Packit 6c4009
      if (message->msg_iov[i].iov_len > 0)
Packit 6c4009
	{
Packit 6c4009
	  /* As an optimization, if we only have a single non-empty
Packit 6c4009
             iovec, we set DATA and LEN from it.  */
Packit 6c4009
	  if (len == 0)
Packit 6c4009
	    data.ptr = message->msg_iov[i].iov_base;
Packit 6c4009
	  else
Packit 6c4009
	    data.ptr = NULL;
Packit 6c4009
Packit 6c4009
	  len += message->msg_iov[i].iov_len;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (data.ptr == NULL)
Packit 6c4009
    {
Packit 6c4009
      size_t to_copy;
Packit 6c4009
      char *buf;
Packit 6c4009
Packit 6c4009
      /* Allocate a temporary buffer to hold the data.  For small
Packit 6c4009
         amounts of data, we allocate a buffer on the stack.  Larger
Packit 6c4009
         amounts of data are stored in a page-aligned buffer.  The
Packit 6c4009
         limit of 2048 bytes is inspired by the MiG stubs.  */
Packit 6c4009
      if (len > 2048)
Packit 6c4009
	{
Packit 6c4009
	  err = __vm_allocate (__mach_task_self (), &data.addr, len, 1);
Packit 6c4009
	  if (err)
Packit 6c4009
	    {
Packit 6c4009
	      __set_errno (err);
Packit 6c4009
	      return -1;
Packit 6c4009
	    }
Packit 6c4009
	  dealloc = 1;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	data.ptr = data_buf;
Packit 6c4009
Packit 6c4009
      /* Copy the data into DATA.  */
Packit 6c4009
      to_copy = len;
Packit 6c4009
      buf = data.ptr;
Packit 6c4009
      for (i = 0; i < len; i++)
Packit 6c4009
	{
Packit 6c4009
#define	min(a, b)	((a) > (b) ? (b) : (a))
Packit 6c4009
	  size_t copy = min (message->msg_iov[i].iov_len, to_copy);
Packit 6c4009
Packit 6c4009
	  buf = __mempcpy (buf, message->msg_iov[i].iov_base, copy);
Packit 6c4009
Packit 6c4009
	  to_copy -= copy;
Packit 6c4009
	  if (to_copy == 0)
Packit 6c4009
	    break;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (addr)
Packit 6c4009
    {
Packit 6c4009
      if (addr->sun_family == AF_LOCAL)
Packit 6c4009
	{
Packit 6c4009
	  char *name = _hurd_sun_path_dupa (addr, addr_len);
Packit 6c4009
	  /* For the local domain, we must look up the name as a file
Packit 6c4009
	     and talk to it with the ifsock protocol.  */
Packit 6c4009
	  file_t file = __file_name_lookup (name, 0, 0);
Packit 6c4009
	  if (file == MACH_PORT_NULL)
Packit 6c4009
	    {
Packit 6c4009
	      if (dealloc)
Packit 6c4009
		__vm_deallocate (__mach_task_self (), data.addr, len);
Packit 6c4009
	      return -1;
Packit 6c4009
	    }
Packit 6c4009
	  err = __ifsock_getsockaddr (file, &aport);
Packit 6c4009
	  __mach_port_deallocate (__mach_task_self (), file);
Packit 6c4009
	  if (err == MIG_BAD_ID || err == EOPNOTSUPP)
Packit 6c4009
	    /* The file did not grok the ifsock protocol.  */
Packit 6c4009
	    err = ENOTSOCK;
Packit 6c4009
	  if (err)
Packit 6c4009
	    {
Packit 6c4009
	      if (dealloc)
Packit 6c4009
		__vm_deallocate (__mach_task_self (), data.addr, len);
Packit 6c4009
	      return __hurd_fail (err);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	err = EIEIO;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  err = HURD_DPORT_USE (fd,
Packit 6c4009
			({
Packit 6c4009
			  if (err)
Packit 6c4009
			    err = __socket_create_address (port,
Packit 6c4009
							   addr->sun_family,
Packit 6c4009
							   (char *) addr,
Packit 6c4009
							   addr_len,
Packit 6c4009
							   &aport);
Packit 6c4009
			  if (! err)
Packit 6c4009
			    {
Packit 6c4009
			      /* Send the data.  */
Packit 6c4009
			      err = __socket_send (port, aport,
Packit 6c4009
						   flags, data.ptr, len,
Packit 6c4009
						   NULL,
Packit 6c4009
						   MACH_MSG_TYPE_COPY_SEND, 0,
Packit 6c4009
						   message->msg_control,
Packit 6c4009
						   message->msg_controllen,
Packit 6c4009
						   &amount);
Packit 6c4009
			      __mach_port_deallocate (__mach_task_self (),
Packit 6c4009
						      aport);
Packit 6c4009
			    }
Packit 6c4009
			  err;
Packit 6c4009
			}));
Packit 6c4009
Packit 6c4009
  if (dealloc)
Packit 6c4009
    __vm_deallocate (__mach_task_self (), data.addr, len);
Packit 6c4009
Packit 6c4009
  return err ? __hurd_sockfail (fd, flags, err) : amount;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__libc_sendmsg, sendmsg)
Packit 6c4009
weak_alias (__libc_sendmsg, __sendmsg)