]> sourceware.org Git - glibc.git/blame - hurd/hurdsock.c
Fix typo in recent resolver change which causes segvs
[glibc.git] / hurd / hurdsock.c
CommitLineData
28f540f4 1/* _hurd_socket_server - Find the server for a socket domain.
13f03ba4 2 Copyright (C) 1991,92,93,94,95,97,99 Free Software Foundation, Inc.
c84142e8
UD
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
41bdb6e2
AJ
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.
c84142e8
UD
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
41bdb6e2 13 Lesser General Public License for more details.
c84142e8 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
28f540f4
RM
19
20#include <hurd.h>
21#include <sys/socket.h>
22#include <stdlib.h>
23#include <string.h>
24#include <hurd/paths.h>
25#include <stdio.h>
cabcc273 26#include "stdio-common/_itoa.h"
28f540f4
RM
27#include <cthreads.h> /* For `struct mutex'. */
28#include "hurdmalloc.h" /* XXX */
29
30static struct mutex lock;
31
32static file_t *servers;
13f03ba4 33static int max_domain = -1;
28f540f4
RM
34
35/* Return a port to the socket server for DOMAIN.
36 Socket servers translate nodes in the directory _SERVERS_SOCKET
37 (canonically /servers/socket). These naming point nodes are named
38 by the simplest decimal representation of the socket domain number,
39 for example "/servers/socket/3".
40
41 Socket servers are assumed not to change very often.
42 The library keeps all the server socket ports it has ever looked up,
43 and does not look them up in /servers/socket more than once. */
44
45socket_t
46_hurd_socket_server (int domain, int dead)
47{
48 socket_t server;
49
50 HURD_CRITICAL_BEGIN;
51 __mutex_lock (&lock);
52
53 if (domain > max_domain)
54 {
55 error_t save = errno;
56 file_t *new = realloc (servers, (domain + 1) * sizeof (file_t));
57 if (new != NULL)
58 {
2d616b0b 59 do
13f03ba4 60 new[++max_domain] = MACH_PORT_NULL;
2d616b0b 61 while (max_domain < domain);
28f540f4
RM
62 servers = new;
63 }
64 else
65 /* No space to cache the port; we will just fetch it anew below. */
66 errno = save;
67 }
68
69 if (dead && domain <= max_domain)
70 {
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;
75 }
76
77 if (domain > max_domain || servers[domain] == MACH_PORT_NULL)
78 {
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;
89 }
90 else
91 server = servers[domain];
92
93 if (server == MACH_PORT_NULL && errno == ENOENT)
94 /* If the server node is absent, we don't support that protocol. */
173b756d 95 errno = EAFNOSUPPORT;
28f540f4
RM
96
97 __mutex_unlock (&lock);
98 HURD_CRITICAL_END;
99
100 return server;
101}
102\f
103static void
104init (void)
105{
1e9dc039 106 int i;
28f540f4
RM
107
108 __mutex_init (&lock);
109
110 for (i = 0; i < max_domain; ++i)
111 servers[i] = MACH_PORT_NULL;
112
113 (void) &init; /* Avoid "defined but not used" warning. */
114}
115text_set_element (_hurd_preinit_hook, init);
This page took 0.293386 seconds and 5 git commands to generate.