Blame hurd/hurdsock.c

Packit 6c4009
/* _hurd_socket_server - Find the server for a socket domain.
Packit 6c4009
   Copyright (C) 1991-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 <hurd.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <hurd/paths.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <_itoa.h>
Packit 6c4009
#include <cthreads.h>		/* For `struct mutex'.  */
Packit 6c4009
#include "hurdmalloc.h"		/* XXX */
Packit 6c4009
Packit 6c4009
static struct mutex lock;
Packit 6c4009
Packit 6c4009
static file_t *servers;
Packit 6c4009
static int max_domain = -1;
Packit 6c4009
Packit 6c4009
/* Return a port to the socket server for DOMAIN.
Packit 6c4009
   Socket servers translate nodes in the directory _SERVERS_SOCKET
Packit 6c4009
   (canonically /servers/socket).  These naming point nodes are named
Packit 6c4009
   by the simplest decimal representation of the socket domain number,
Packit 6c4009
   for example "/servers/socket/3".
Packit 6c4009
Packit 6c4009
   Socket servers are assumed not to change very often.
Packit 6c4009
   The library keeps all the server socket ports it has ever looked up,
Packit 6c4009
   and does not look them up in /servers/socket more than once.  */
Packit 6c4009
Packit 6c4009
socket_t
Packit 6c4009
_hurd_socket_server (int domain, int dead)
Packit 6c4009
{
Packit 6c4009
  socket_t server;
Packit 6c4009
Packit 6c4009
  if (domain < 0)
Packit 6c4009
    {
Packit 6c4009
      errno = EAFNOSUPPORT;
Packit 6c4009
      return MACH_PORT_NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  HURD_CRITICAL_BEGIN;
Packit 6c4009
  __mutex_lock (&lock);
Packit 6c4009
Packit 6c4009
  if (domain > max_domain)
Packit 6c4009
    {
Packit 6c4009
      error_t save = errno;
Packit 6c4009
      file_t *new = realloc (servers, (domain + 1) * sizeof (file_t));
Packit 6c4009
      if (new != NULL)
Packit 6c4009
	{
Packit 6c4009
	  do
Packit 6c4009
	    new[++max_domain] = MACH_PORT_NULL;
Packit 6c4009
	  while (max_domain < domain);
Packit 6c4009
	  servers = new;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* No space to cache the port; we will just fetch it anew below.  */
Packit 6c4009
	errno = save;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (dead && domain <= max_domain)
Packit 6c4009
    {
Packit 6c4009
      /* The user says the port we returned earlier (now in SERVERS[DOMAIN])
Packit 6c4009
	 was dead.  Clear the cache and fetch a new one below.  */
Packit 6c4009
      __mach_port_deallocate (__mach_task_self (), servers[domain]);
Packit 6c4009
      servers[domain] = MACH_PORT_NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (domain > max_domain || servers[domain] == MACH_PORT_NULL)
Packit 6c4009
    {
Packit 6c4009
      char name[sizeof (_SERVERS_SOCKET) + 100];
Packit 6c4009
      char *np = &name[sizeof (name)];
Packit 6c4009
      *--np = '\0';
Packit 6c4009
      np = _itoa (domain, np, 10, 0);
Packit 6c4009
      *--np = '/';
Packit 6c4009
      np -= sizeof (_SERVERS_SOCKET) - 1;
Packit 6c4009
      memcpy (np, _SERVERS_SOCKET, sizeof (_SERVERS_SOCKET) - 1);
Packit 6c4009
      server = __file_name_lookup (np, 0, 0);
Packit 6c4009
      if (domain <= max_domain)
Packit 6c4009
	servers[domain] = server;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    server = servers[domain];
Packit 6c4009
Packit 6c4009
  if (server == MACH_PORT_NULL && errno == ENOENT)
Packit 6c4009
    /* If the server node is absent, we don't support that protocol.  */
Packit 6c4009
    errno = EAFNOSUPPORT;
Packit 6c4009
Packit 6c4009
  __mutex_unlock (&lock);
Packit 6c4009
  HURD_CRITICAL_END;
Packit 6c4009
Packit 6c4009
  return server;
Packit 6c4009
}
Packit 6c4009

Packit 6c4009
static void
Packit 6c4009
init (void)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  __mutex_init (&lock);
Packit 6c4009
Packit 6c4009
  for (i = 0; i < max_domain; ++i)
Packit 6c4009
    servers[i] = MACH_PORT_NULL;
Packit 6c4009
Packit 6c4009
  (void) &ini;;			/* Avoid "defined but not used" warning.  */
Packit 6c4009
}
Packit 6c4009
text_set_element (_hurd_preinit_hook, init);