Sun Feb 18 12:13:07 1996 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[glibc/history.git] / hurd / hurdsock.c
bloba623270c3cfd0fc2dc1e92931ee19bf0fc96f9e9
1 /* _hurd_socket_server - Find the server for a socket domain.
3 Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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.
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.
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. */
21 #include <hurd.h>
22 #include <sys/socket.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <hurd/paths.h>
26 #include <stdio.h>
27 #include "stdio-common/_itoa.h"
28 #include <cthreads.h> /* For `struct mutex'. */
29 #include "hurdmalloc.h" /* XXX */
31 static struct mutex lock;
33 static file_t *servers;
34 static int max_domain;
36 /* Return a port to the socket server for DOMAIN.
37 Socket servers translate nodes in the directory _SERVERS_SOCKET
38 (canonically /servers/socket). These naming point nodes are named
39 by the simplest decimal representation of the socket domain number,
40 for example "/servers/socket/3".
42 Socket servers are assumed not to change very often.
43 The library keeps all the server socket ports it has ever looked up,
44 and does not look them up in /servers/socket more than once. */
46 socket_t
47 _hurd_socket_server (int domain, int dead)
49 socket_t server;
51 HURD_CRITICAL_BEGIN;
52 __mutex_lock (&lock);
54 if (domain > max_domain)
56 error_t save = errno;
57 file_t *new = realloc (servers, (domain + 1) * sizeof (file_t));
58 if (new != NULL)
60 while (max_domain <= domain)
61 new[max_domain++] = MACH_PORT_NULL;
62 servers = new;
64 else
65 /* No space to cache the port; we will just fetch it anew below. */
66 errno = save;
69 if (dead && domain <= max_domain)
71 /* The user says the port we returned earlier (now in SERVERS[DOMAIN])
72 was dead. Clear the cache and fetch a new one below. */
73 __mach_port_deallocate (__mach_task_self (), servers[domain]);
74 servers[domain] = MACH_PORT_NULL;
77 if (domain > max_domain || servers[domain] == MACH_PORT_NULL)
79 char name[sizeof (_SERVERS_SOCKET) + 100];
80 char *np = &name[sizeof (name)];
81 *--np = '\0';
82 np = _itoa (domain, np, 10, 0);
83 *--np = '/';
84 np -= sizeof (_SERVERS_SOCKET) - 1;
85 memcpy (np, _SERVERS_SOCKET, sizeof (_SERVERS_SOCKET) - 1);
86 server = __file_name_lookup (np, 0, 0);
87 if (domain <= max_domain)
88 servers[domain] = server;
90 else
91 server = servers[domain];
93 if (server == MACH_PORT_NULL && errno == ENOENT)
94 /* If the server node is absent, we don't support that protocol. */
95 errno = EPFNOSUPPORT;
97 __mutex_unlock (&lock);
98 HURD_CRITICAL_END;
100 return server;
103 static void
104 init (void)
106 int i;
108 __mutex_init (&lock);
110 for (i = 0; i < max_domain; ++i)
111 servers[i] = MACH_PORT_NULL;
113 (void) &init; /* Avoid "defined but not used" warning. */
115 text_set_element (_hurd_preinit_hook, init);