home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / hurd / hurdsock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-20  |  2.5 KB  |  91 lines

  1. /* _hurd_socket_server - Find the server for a socket domain.
  2.  
  3. Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5.  
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10.  
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with the GNU C Library; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21. #include <hurd.h>
  22. #include <sys/socket.h>
  23. #include <gnu-stabs.h>
  24.  
  25. static struct mutex lock;
  26. static void
  27. init_sock (void)
  28. {
  29.   __mutex_init (&lock);
  30. }
  31. text_set_element (__libc_subinit, init_sock);
  32.  
  33. static file_t sockdir = MACH_PORT_NULL;
  34. static file_t *servers;
  35. static int max_domain;
  36.  
  37. /* Return a port to the socket server for DOMAIN.
  38.    Socket servers translate nodes in the directory _SERVERS_SOCKET
  39.    (canonically /servers/socket).  These naming point nodes are named
  40.    by the simplest decimal representation of the socket domain number,
  41.    for example "/servers/socket/3".
  42.  
  43.    Socket servers are assumed not to change very often.
  44.    The library keeps all the server socket ports it has ever looked up,
  45.    and does not look them up in /servers/socket more than once.  */
  46.  
  47. socket_t
  48. _hurd_socket_server (int domain)
  49. {
  50.   error_t err;
  51.  
  52.   __mutex_lock (&lock);
  53.  
  54.   if (sockdir == MACH_PORT_NULL)
  55.     {
  56.       sockdir = __path_lookup (_SERVERS_SOCKET, FS_LOOKUP_EXEC, 0);
  57.       if (sockdir == MACH_PORT_NULL)
  58.     {
  59.       __mutex_unlock (&lock);
  60.       return MACH_PORT_NULL;
  61.     }
  62.     }
  63.  
  64.   if (domain > max_domain)
  65.     {
  66.       file_t *new = realloc (servers, (domain + 1) * sizeof (file_t));
  67.       if (new == NULL)
  68.     {
  69.       __mutex_unlock (&lock);
  70.       return MACH_PORT_NULL;
  71.     }
  72.       while (max_domain < domain)
  73.     new[max_domain++] = MACH_PORT_NULL;
  74.       servers = new;
  75.     }
  76.  
  77.   {
  78.     char name[100];
  79.     sprintf (name, "%d", domain);
  80.     if (err = _HURD_PORT_USE (&_hurd_crdir,
  81.                   __hurd_path_lookup (port, sockdir,
  82.                           name, 0, 0,
  83.                           &servers[domain])))
  84.       errno = err;
  85.   }
  86.  
  87.   __mutex_unlock (&lock);
  88.  
  89.   return servers[domain];
  90. }
  91.