Blame sysdeps/mach/hurd/socket.c

Packit 6c4009
/* Copyright (C) 1992-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
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the 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; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/socket.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <fcntl-internal.h>
Packit 6c4009
Packit 6c4009
/* Create a new socket of type TYPE in domain DOMAIN, using
Packit 6c4009
   protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
Packit 6c4009
   Returns a file descriptor for the new socket, or -1 for errors.  */
Packit 6c4009
int
Packit 6c4009
__socket (int domain, int type, int protocol)
Packit 6c4009
{
Packit 6c4009
  error_t err;
Packit 6c4009
  socket_t sock, server;
Packit 6c4009
  int flags = sock_to_o_flags (type & ~SOCK_TYPE_MASK);
Packit 6c4009
  type &= SOCK_TYPE_MASK;
Packit 6c4009
Packit 6c4009
  if (flags & ~(O_CLOEXEC | O_NONBLOCK))
Packit 6c4009
    return __hurd_fail (EINVAL);
Packit 6c4009
Packit 6c4009
  /* Find the socket server for DOMAIN.  */
Packit 6c4009
  server = _hurd_socket_server (domain, 0);
Packit 6c4009
  if (server == MACH_PORT_NULL)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  err = __socket_create (server, type, protocol, &sock);
Packit 6c4009
  if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED
Packit 6c4009
      || err == MIG_BAD_ID || err == EOPNOTSUPP)
Packit 6c4009
    {
Packit 6c4009
      /* On the first use of the socket server during the operation,
Packit 6c4009
	 allow for the old server port dying.  */
Packit 6c4009
      server = _hurd_socket_server (domain, 1);
Packit 6c4009
      if (server == MACH_PORT_NULL)
Packit 6c4009
	return -1;
Packit 6c4009
      err = __socket_create (server, type, protocol, &sock);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* These errors all mean that the server node doesn't support the
Packit 6c4009
     socket.defs protocol, which we'll take to mean that the protocol
Packit 6c4009
     isn't supported.  */
Packit 6c4009
  if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED
Packit 6c4009
      || err == MIG_BAD_ID || err == EOPNOTSUPP)
Packit 6c4009
    err = EAFNOSUPPORT;
Packit 6c4009
Packit 6c4009
  if (! err)
Packit 6c4009
    {
Packit 6c4009
      if (flags & O_NONBLOCK)
Packit 6c4009
	err = __io_set_some_openmodes (sock, O_NONBLOCK);
Packit 6c4009
      /* TODO: do we need special ERR massaging after the previous call?  */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (err)
Packit 6c4009
    return __hurd_fail (err);
Packit 6c4009
Packit 6c4009
  return _hurd_intern_fd (sock, O_IGNORE_CTTY | flags, 1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
libc_hidden_def (__socket)
Packit 6c4009
weak_alias (__socket, socket)