1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
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>
26: #include "stdio-common/_itoa.h"
27: #include <cthreads.h>
28: #include "hurdmalloc.h"
29:
30: static struct mutex lock;
31:
32: static file_t *servers;
33: static int max_domain = -1;
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45: socket_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: {
59: do
60: new[++max_domain] = MACH_PORT_NULL;
61: while (max_domain < domain);
62: servers = new;
63: }
64: else
65:
66: errno = save;
67: }
68:
69: if (dead && domain <= max_domain)
70: {
71:
72:
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:
95: errno = EPFNOSUPPORT;
96:
97: __mutex_unlock (&lock);
98: HURD_CRITICAL_END;
99:
100: return server;
101: }
102: ^L
103: static void
104: init (void)
105: {
106: int i;
107:
108: __mutex_init (&lock);
109:
110: for (i = 0; i < max_domain; ++i)
111: servers[i] = MACH_PORT_NULL;
112:
113: (void) &init;
114: }
115: text_set_element (_hurd_preinit_hook, init);