]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/hurdsock.c
nptl: Move pthread_attr_setschedparam implementation into libc
[thirdparty/glibc.git] / hurd / hurdsock.c
CommitLineData
28f540f4 1/* _hurd_socket_server - Find the server for a socket domain.
04277e02 2 Copyright (C) 1991-2019 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 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
28f540f4
RM
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>
eb96ffb0 25#include <_itoa.h>
28f540f4
RM
26#include <cthreads.h> /* For `struct mutex'. */
27#include "hurdmalloc.h" /* XXX */
28
29static struct mutex lock;
30
31static file_t *servers;
13f03ba4 32static int max_domain = -1;
28f540f4
RM
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
44socket_t
45_hurd_socket_server (int domain, int dead)
46{
47 socket_t server;
48
63643c85
PT
49 if (domain < 0)
50 {
51 errno = EAFNOSUPPORT;
52 return MACH_PORT_NULL;
53 }
54
28f540f4
RM
55 HURD_CRITICAL_BEGIN;
56 __mutex_lock (&lock);
57
58 if (domain > max_domain)
59 {
60 error_t save = errno;
61 file_t *new = realloc (servers, (domain + 1) * sizeof (file_t));
62 if (new != NULL)
63 {
2d616b0b 64 do
13f03ba4 65 new[++max_domain] = MACH_PORT_NULL;
2d616b0b 66 while (max_domain < domain);
28f540f4
RM
67 servers = new;
68 }
69 else
70 /* No space to cache the port; we will just fetch it anew below. */
71 errno = save;
72 }
73
74 if (dead && domain <= max_domain)
75 {
76 /* The user says the port we returned earlier (now in SERVERS[DOMAIN])
77 was dead. Clear the cache and fetch a new one below. */
78 __mach_port_deallocate (__mach_task_self (), servers[domain]);
79 servers[domain] = MACH_PORT_NULL;
80 }
81
82 if (domain > max_domain || servers[domain] == MACH_PORT_NULL)
83 {
84 char name[sizeof (_SERVERS_SOCKET) + 100];
85 char *np = &name[sizeof (name)];
86 *--np = '\0';
87 np = _itoa (domain, np, 10, 0);
88 *--np = '/';
89 np -= sizeof (_SERVERS_SOCKET) - 1;
90 memcpy (np, _SERVERS_SOCKET, sizeof (_SERVERS_SOCKET) - 1);
91 server = __file_name_lookup (np, 0, 0);
92 if (domain <= max_domain)
93 servers[domain] = server;
94 }
95 else
96 server = servers[domain];
97
98 if (server == MACH_PORT_NULL && errno == ENOENT)
99 /* If the server node is absent, we don't support that protocol. */
173b756d 100 errno = EAFNOSUPPORT;
28f540f4
RM
101
102 __mutex_unlock (&lock);
103 HURD_CRITICAL_END;
104
105 return server;
106}
107\f
108static void
109init (void)
110{
1e9dc039 111 int i;
28f540f4
RM
112
113 __mutex_init (&lock);
114
115 for (i = 0; i < max_domain; ++i)
116 servers[i] = MACH_PORT_NULL;
117
118 (void) &init; /* Avoid "defined but not used" warning. */
119}
120text_set_element (_hurd_preinit_hook, init);