Hurd: Missing critical region locks.
[platform/upstream/glibc.git] / hurd / hurdsock.c
1 /* _hurd_socket_server - Find the server for a socket domain.
2    Copyright (C) 1991,92,93,94,95,97,99 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <hurd.h>
20 #include <sys/socket.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <hurd/paths.h>
24 #include <stdio.h>
25 #include <_itoa.h>
26 #include <cthreads.h>           /* For `struct mutex'.  */
27 #include "hurdmalloc.h"         /* XXX */
28
29 static struct mutex lock;
30
31 static file_t *servers;
32 static int max_domain = -1;
33
34 /* Return a port to the socket server for DOMAIN.
35    Socket servers translate nodes in the directory _SERVERS_SOCKET
36    (canonically /servers/socket).  These naming point nodes are named
37    by the simplest decimal representation of the socket domain number,
38    for example "/servers/socket/3".
39
40    Socket servers are assumed not to change very often.
41    The library keeps all the server socket ports it has ever looked up,
42    and does not look them up in /servers/socket more than once.  */
43
44 socket_t
45 _hurd_socket_server (int domain, int dead)
46 {
47   socket_t server;
48
49   HURD_CRITICAL_BEGIN;
50   __mutex_lock (&lock);
51
52   if (domain > max_domain)
53     {
54       error_t save = errno;
55       file_t *new = realloc (servers, (domain + 1) * sizeof (file_t));
56       if (new != NULL)
57         {
58           do
59             new[++max_domain] = MACH_PORT_NULL;
60           while (max_domain < domain);
61           servers = new;
62         }
63       else
64         /* No space to cache the port; we will just fetch it anew below.  */
65         errno = save;
66     }
67
68   if (dead && domain <= max_domain)
69     {
70       /* The user says the port we returned earlier (now in SERVERS[DOMAIN])
71          was dead.  Clear the cache and fetch a new one below.  */
72       __mach_port_deallocate (__mach_task_self (), servers[domain]);
73       servers[domain] = MACH_PORT_NULL;
74     }
75
76   if (domain > max_domain || servers[domain] == MACH_PORT_NULL)
77     {
78       char name[sizeof (_SERVERS_SOCKET) + 100];
79       char *np = &name[sizeof (name)];
80       *--np = '\0';
81       np = _itoa (domain, np, 10, 0);
82       *--np = '/';
83       np -= sizeof (_SERVERS_SOCKET) - 1;
84       memcpy (np, _SERVERS_SOCKET, sizeof (_SERVERS_SOCKET) - 1);
85       server = __file_name_lookup (np, 0, 0);
86       if (domain <= max_domain)
87         servers[domain] = server;
88     }
89   else
90     server = servers[domain];
91
92   if (server == MACH_PORT_NULL && errno == ENOENT)
93     /* If the server node is absent, we don't support that protocol.  */
94     errno = EAFNOSUPPORT;
95
96   __mutex_unlock (&lock);
97   HURD_CRITICAL_END;
98
99   return server;
100 }
101 \f
102 static void
103 init (void)
104 {
105   int i;
106
107   __mutex_init (&lock);
108
109   for (i = 0; i < max_domain; ++i)
110     servers[i] = MACH_PORT_NULL;
111
112   (void) &init;                 /* Avoid "defined but not used" warning.  */
113 }
114 text_set_element (_hurd_preinit_hook, init);