author | viric@llimona |
Mon, 24 Sep 2007 13:59:40 +0200 | |
changeset 38 | f1e581c862d5 |
parent 37 | b0588bddef91 |
child 40 | da3ea7f666e3 |
permissions | -rw-r--r-- |
21 | 1 |
#include <stdlib.h> |
2 |
#include <assert.h> |
|
3 |
#include <unistd.h> |
|
4 |
#include <stdio.h> |
|
5 |
#include <sys/un.h> |
|
6 |
#include <sys/socket.h> |
|
7 |
#include <sys/select.h> |
|
8 |
||
9 |
#include "main.h" |
|
10 |
#include "handlers.h" |
|
11 |
||
12 |
static int served_sockets = 0; |
|
13 |
||
14 |
static int listen_socket = -1; /* not listening */ |
|
15 |
static int connected_sockets = 0; |
|
16 |
static int *conn_sockets = 0; |
|
17 |
||
18 |
static char *socket_path = 0; |
|
19 |
||
22 | 20 |
static char default_path_prefix[] = "/tmp/mt-socket."; |
21 | 21 |
|
22 |
void get_unix_path() |
|
23 |
{ |
|
24 |
char *new; |
|
25 |
||
26 |
new = getenv("SM_SOCKET"); |
|
27 |
if (new == 0) /* Compose the path from the default values */ |
|
28 |
{ |
|
29 |
char num[20]; /* enough for an int */ |
|
30 |
int len; |
|
31 |
||
32 |
sprintf(num, "%i", (int) getuid()); |
|
33 |
||
34 |
len = strlen(default_path_prefix) + strlen(num) + 1; |
|
35 |
||
36 |
command_line.unix_path = malloc(len); |
|
37 |
||
38 |
sprintf(command_line.unix_path, "%s%s", |
|
39 |
default_path_prefix, num); |
|
40 |
} else |
|
41 |
{ |
|
42 |
command_line.unix_path = malloc(strlen(new) + 1); |
|
43 |
strcpy(command_line.unix_path, new); |
|
44 |
} |
|
45 |
} |
|
46 |
||
47 |
||
48 |
static void start_listening(int new) |
|
49 |
{ |
|
50 |
int ls; |
|
51 |
struct sockaddr_un addr; |
|
52 |
int res; |
|
53 |
||
54 |
assert(new > 0); |
|
55 |
assert(command_line.unix_path != 0); |
|
56 |
||
57 |
socket_path = command_line.unix_path; |
|
58 |
||
59 |
ls = socket(AF_UNIX, SOCK_STREAM, 0); |
|
60 |
if (ls == -1) |
|
61 |
error("Cannot create the unix listen socket in the server"); |
|
62 |
||
63 |
addr.sun_family = AF_UNIX; |
|
64 |
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)); |
|
65 |
||
66 |
res = bind(ls, (struct sockaddr *) & addr, sizeof(addr)); |
|
67 |
if (res == -1) |
|
68 |
error("Error binding to %s", socket_path); |
|
69 |
||
70 |
/* NUANCE: 0 backlog. Why should we assure future connections? */ |
|
71 |
res = listen(ls, 0); |
|
72 |
if (res == -1) |
|
73 |
error("Error listening on the binded unix socket"); |
|
74 |
||
75 |
listen_socket = ls; |
|
76 |
} |
|
77 |
||
78 |
void s_unix_update_served(int new) |
|
79 |
{ |
|
80 |
if (new > served_sockets && new > 0 ) |
|
81 |
{ |
|
82 |
conn_sockets = realloc(conn_sockets, sizeof(*conn_sockets) * new); |
|
83 |
if (listen_socket == -1) /* not listening */ |
|
84 |
{ |
|
85 |
start_listening(new); |
|
86 |
} |
|
87 |
served_sockets = new; |
|
88 |
} |
|
89 |
else if (new < served_sockets) |
|
90 |
{ |
|
91 |
not_implemented("new < served_sockets at unix_update_served"); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
void s_unix_shutdown() |
|
96 |
{ |
|
97 |
int i; |
|
98 |
||
99 |
if (listen_socket != -1) |
|
100 |
{ |
|
101 |
close(listen_socket); |
|
102 |
unlink(socket_path); |
|
103 |
} |
|
104 |
||
105 |
for (i=0; i < connected_sockets; ++i) |
|
106 |
{ |
|
107 |
close(conn_sockets[i]); |
|
108 |
} |
|
109 |
connected_sockets = 0; |
|
110 |
served_sockets = 0; |
|
111 |
} |
|
112 |
||
113 |
void s_unix_prepare_read_fdset(fd_set *read_set, int *maxfd) |
|
114 |
{ |
|
115 |
int i; |
|
116 |
||
37
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
117 |
FD_SET(listen_socket, read_set); |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
118 |
*maxfd = max(*maxfd, listen_socket); |
21 | 119 |
|
120 |
for (i=0; i < connected_sockets; ++i) |
|
121 |
{ |
|
122 |
/* We only accept if we don't have any |
|
123 |
* connetion opened. */ |
|
124 |
FD_SET(conn_sockets[i], read_set); |
|
125 |
*maxfd = max(*maxfd, conn_sockets[i]); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
static void remove_conn_socket(int n) |
|
130 |
{ |
|
131 |
int i; |
|
132 |
||
133 |
assert(n >= 0); |
|
134 |
||
135 |
for(i=n+1; i<connected_sockets; ++i) |
|
136 |
{ |
|
137 |
conn_sockets[i-1] = conn_sockets[i]; |
|
138 |
} |
|
139 |
||
140 |
connected_sockets -= 1; |
|
141 |
||
142 |
/* We shrink the memory until we have an array for served_sockets */ |
|
143 |
if (served_sockets <= connected_sockets) |
|
144 |
{ |
|
145 |
conn_sockets = realloc(conn_sockets, sizeof(*conn_sockets) |
|
146 |
* connected_sockets); |
|
147 |
} |
|
148 |
} |
|
149 |
||
150 |
static int accept_connection(int ls) |
|
151 |
{ |
|
152 |
int cs; |
|
153 |
cs = accept(ls, 0, 0); |
|
154 |
if (cs == -1) |
|
155 |
not_implemented("accept unix socket error check"); |
|
156 |
||
157 |
/* TODO: Prepare unblocking sockets if needed */ |
|
158 |
||
159 |
return cs; |
|
160 |
} |
|
161 |
||
162 |
void s_unix_process_read_fdset(fd_set *read_set) |
|
163 |
{ |
|
164 |
int i; |
|
165 |
/* Active streams */ |
|
166 |
for (i=0; i < connected_sockets; ++i) |
|
167 |
{ |
|
168 |
if (FD_ISSET(conn_sockets[i], read_set)) |
|
169 |
{ |
|
170 |
int res; |
|
171 |
res = read(conn_sockets[i], stream_buffer, |
|
172 |
stream_buffer_size); |
|
173 |
if (res == 0) |
|
174 |
{ |
|
175 |
close(conn_sockets[i]); |
|
176 |
remove_conn_socket(i); |
|
177 |
} else |
|
178 |
{ |
|
32
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
22
diff
changeset
|
179 |
app_control_remote_send_to_stdin(stream_buffer, res); |
21 | 180 |
} |
181 |
} |
|
182 |
} |
|
183 |
||
184 |
/* Listen connection */ |
|
37
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
185 |
if (FD_ISSET(listen_socket, read_set)) |
21 | 186 |
{ |
37
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
187 |
if (served_sockets > connected_sockets) |
21 | 188 |
{ |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
189 |
int s; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
190 |
s = accept_connection(listen_socket); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
191 |
conn_sockets[connected_sockets++] = s; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
192 |
welcome_new_client_socket(s); |
37
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
193 |
} else |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
194 |
{ |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
195 |
int s; |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
196 |
s = accept_connection(listen_socket); |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
197 |
close(s); |
21 | 198 |
} |
199 |
} |
|
200 |
} |
|
201 |
||
202 |
void s_unix_send_to_connected(const char *buffer, size_t size) |
|
203 |
{ |
|
204 |
int i; |
|
205 |
||
206 |
for (i=0; i < connected_sockets; ++i) |
|
207 |
{ |
|
208 |
write(conn_sockets[i], buffer, size); |
|
209 |
} |
|
210 |
} |