]>
Commit | Line | Data |
---|---|---|
c53ca1e0 | 1 | /* |
bccb7ded | 2 | * sslproc.c: An interface to wsockd |
c53ca1e0 AC |
3 | * Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org> |
4 | * Copyright (C) 2007 ircd-ratbox development team | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
19 | * USA | |
20 | */ | |
21 | ||
22 | #include <rb_lib.h> | |
23 | #include "stdinc.h" | |
24 | ||
25 | ||
26 | #include "s_conf.h" | |
27 | #include "logger.h" | |
28 | #include "listener.h" | |
29 | #include "wsproc.h" | |
30 | #include "s_serv.h" | |
31 | #include "ircd.h" | |
32 | #include "hash.h" | |
33 | #include "client.h" | |
34 | #include "send.h" | |
35 | #include "packet.h" | |
36 | ||
37 | static void ws_read_ctl(rb_fde_t * F, void *data); | |
38 | static int wsockd_count; | |
39 | ||
40 | static char tmpbuf[READBUF_SIZE]; | |
41 | static char nul = '\0'; | |
42 | ||
43 | #define MAXPASSFD 4 | |
44 | #define READSIZE 1024 | |
45 | typedef struct _ws_ctl_buf | |
46 | { | |
47 | rb_dlink_node node; | |
48 | char *buf; | |
49 | size_t buflen; | |
50 | rb_fde_t *F[MAXPASSFD]; | |
51 | int nfds; | |
52 | } ws_ctl_buf_t; | |
53 | ||
54 | ||
55 | struct ws_ctl | |
56 | { | |
57 | rb_dlink_node node; | |
58 | int cli_count; | |
59 | rb_fde_t *F; | |
60 | rb_fde_t *P; | |
61 | pid_t pid; | |
62 | rb_dlink_list readq; | |
63 | rb_dlink_list writeq; | |
64 | uint8_t shutdown; | |
65 | uint8_t dead; | |
66 | }; | |
67 | ||
68 | static rb_dlink_list wsock_daemons; | |
69 | ||
70 | static inline uint32_t | |
71 | buf_to_uint32(char *buf) | |
72 | { | |
73 | uint32_t x; | |
74 | memcpy(&x, buf, sizeof(x)); | |
75 | return x; | |
76 | } | |
77 | ||
78 | static inline void | |
79 | uint32_to_buf(char *buf, uint32_t x) | |
80 | { | |
81 | memcpy(buf, &x, sizeof(x)); | |
82 | return; | |
83 | } | |
84 | ||
85 | static ws_ctl_t * | |
86 | allocate_ws_daemon(rb_fde_t * F, rb_fde_t * P, int pid) | |
87 | { | |
88 | ws_ctl_t *ctl; | |
89 | ||
90 | if(F == NULL || pid < 0) | |
91 | return NULL; | |
92 | ctl = rb_malloc(sizeof(ws_ctl_t)); | |
93 | ctl->F = F; | |
94 | ctl->P = P; | |
95 | ctl->pid = pid; | |
96 | wsockd_count++; | |
97 | rb_dlinkAdd(ctl, &ctl->node, &wsock_daemons); | |
98 | return ctl; | |
99 | } | |
100 | ||
101 | static void | |
102 | free_ws_daemon(ws_ctl_t * ctl) | |
103 | { | |
104 | rb_dlink_node *ptr; | |
105 | ws_ctl_buf_t *ctl_buf; | |
106 | int x; | |
107 | if(ctl->cli_count) | |
108 | return; | |
109 | ||
110 | RB_DLINK_FOREACH(ptr, ctl->readq.head) | |
111 | { | |
112 | ctl_buf = ptr->data; | |
113 | for(x = 0; x < ctl_buf->nfds; x++) | |
114 | rb_close(ctl_buf->F[x]); | |
115 | ||
116 | rb_free(ctl_buf->buf); | |
117 | rb_free(ctl_buf); | |
118 | } | |
119 | ||
120 | RB_DLINK_FOREACH(ptr, ctl->writeq.head) | |
121 | { | |
122 | ctl_buf = ptr->data; | |
123 | for(x = 0; x < ctl_buf->nfds; x++) | |
124 | rb_close(ctl_buf->F[x]); | |
125 | ||
126 | rb_free(ctl_buf->buf); | |
127 | rb_free(ctl_buf); | |
128 | } | |
129 | rb_close(ctl->F); | |
130 | rb_close(ctl->P); | |
131 | rb_dlinkDelete(&ctl->node, &wsock_daemons); | |
132 | rb_free(ctl); | |
133 | } | |
134 | ||
135 | static char *wsockd_path; | |
136 | ||
137 | static int wsockd_spin_count = 0; | |
138 | static time_t last_spin; | |
139 | static int wsockd_wait = 0; | |
140 | ||
141 | void | |
142 | restart_wsockd(void) | |
143 | { | |
144 | rb_dlink_node *ptr, *next; | |
145 | ws_ctl_t *ctl; | |
146 | ||
147 | RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head) | |
148 | { | |
149 | ctl = ptr->data; | |
150 | if(ctl->dead) | |
151 | continue; | |
152 | if(ctl->shutdown) | |
153 | continue; | |
154 | ctl->shutdown = 1; | |
155 | wsockd_count--; | |
156 | if(!ctl->cli_count) | |
157 | { | |
158 | rb_kill(ctl->pid, SIGKILL); | |
159 | free_ws_daemon(ctl); | |
160 | } | |
161 | } | |
162 | ||
163 | start_wsockd(ServerInfo.wsockd_count); | |
164 | } | |
165 | ||
166 | static void | |
167 | ws_killall(void) | |
168 | { | |
169 | rb_dlink_node *ptr, *next; | |
170 | ws_ctl_t *ctl; | |
171 | RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head) | |
172 | { | |
173 | ctl = ptr->data; | |
174 | if(ctl->dead) | |
175 | continue; | |
176 | ctl->dead = 1; | |
177 | if(!ctl->shutdown) | |
178 | wsockd_count--; | |
179 | rb_kill(ctl->pid, SIGKILL); | |
180 | if(!ctl->cli_count) | |
181 | free_ws_daemon(ctl); | |
182 | } | |
183 | } | |
184 | ||
185 | static void | |
186 | ws_dead(ws_ctl_t * ctl) | |
187 | { | |
188 | if(ctl->dead) | |
189 | return; | |
190 | ||
191 | ctl->dead = 1; | |
192 | rb_kill(ctl->pid, SIGKILL); /* make sure the process is really gone */ | |
193 | ||
194 | if(!ctl->shutdown) | |
195 | { | |
196 | wsockd_count--; | |
197 | ilog(L_MAIN, "wsockd helper died - attempting to restart"); | |
198 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "wsockd helper died - attempting to restart"); | |
199 | start_wsockd(1); | |
200 | } | |
201 | } | |
202 | ||
203 | static void | |
204 | ws_do_pipe(rb_fde_t * F, void *data) | |
205 | { | |
206 | int retlen; | |
207 | ws_ctl_t *ctl = data; | |
208 | retlen = rb_write(F, "0", 1); | |
209 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) | |
210 | { | |
211 | ws_dead(ctl); | |
212 | return; | |
213 | } | |
214 | rb_setselect(F, RB_SELECT_READ, ws_do_pipe, data); | |
215 | } | |
216 | ||
217 | static void | |
bccb7ded | 218 | restart_wsockd_event(void *unused) |
c53ca1e0 AC |
219 | { |
220 | wsockd_spin_count = 0; | |
221 | last_spin = 0; | |
222 | wsockd_wait = 0; | |
223 | if(ServerInfo.wsockd_count > get_wsockd_count()) | |
224 | { | |
225 | int start = ServerInfo.wsockd_count - get_wsockd_count(); | |
226 | ilog(L_MAIN, "Attempting to restart wsockd processes"); | |
227 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Attempting to restart wsockd processes"); | |
228 | start_wsockd(start); | |
229 | } | |
230 | } | |
231 | ||
232 | int | |
233 | start_wsockd(int count) | |
234 | { | |
235 | rb_fde_t *F1, *F2; | |
236 | rb_fde_t *P1, *P2; | |
237 | #ifdef _WIN32 | |
238 | const char *suffix = ".exe"; | |
239 | #else | |
240 | const char *suffix = ""; | |
241 | #endif | |
242 | ||
243 | char fullpath[PATH_MAX + 1]; | |
244 | char fdarg[6]; | |
245 | const char *parv[2]; | |
246 | char buf[128]; | |
247 | char s_pid[10]; | |
248 | pid_t pid; | |
249 | int started = 0, i; | |
250 | ||
251 | if(wsockd_wait) | |
252 | return 0; | |
253 | ||
254 | if(wsockd_spin_count > 20 && (rb_current_time() - last_spin < 5)) | |
255 | { | |
bccb7ded | 256 | ilog(L_MAIN, "wsockd helper is spinning - will attempt to restart in 1 minute"); |
c53ca1e0 | 257 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
bccb7ded AC |
258 | "wsockd helper is spinning - will attempt to restart in 1 minute"); |
259 | rb_event_add("restart_wsockd_event", restart_wsockd_event, NULL, 60); | |
c53ca1e0 AC |
260 | wsockd_wait = 1; |
261 | return 0; | |
262 | } | |
263 | ||
264 | wsockd_spin_count++; | |
265 | last_spin = rb_current_time(); | |
266 | ||
267 | if(wsockd_path == NULL) | |
268 | { | |
269 | snprintf(fullpath, sizeof(fullpath), "%s%cwsockd%s", ircd_paths[IRCD_PATH_LIBEXEC], RB_PATH_SEPARATOR, suffix); | |
270 | ||
271 | if(access(fullpath, X_OK) == -1) | |
272 | { | |
273 | snprintf(fullpath, sizeof(fullpath), "%s%cbin%cwsockd%s", | |
274 | ConfigFileEntry.dpath, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR, suffix); | |
275 | if(access(fullpath, X_OK) == -1) | |
276 | { | |
277 | ilog(L_MAIN, | |
bccb7ded | 278 | "Unable to execute wsockd%s in %s or %s/bin", |
c53ca1e0 AC |
279 | suffix, ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath); |
280 | return 0; | |
281 | } | |
282 | } | |
283 | wsockd_path = rb_strdup(fullpath); | |
284 | } | |
bccb7ded | 285 | rb_strlcpy(buf, "-ircd wsockd daemon", sizeof(buf)); |
c53ca1e0 AC |
286 | parv[0] = buf; |
287 | parv[1] = NULL; | |
288 | ||
289 | for(i = 0; i < count; i++) | |
290 | { | |
291 | ws_ctl_t *ctl; | |
292 | if(rb_socketpair(AF_UNIX, SOCK_DGRAM, 0, &F1, &F2, "wsockd handle passing socket") == -1) | |
293 | { | |
294 | ilog(L_MAIN, "Unable to create wsockd - rb_socketpair failed: %s", strerror(errno)); | |
295 | return started; | |
296 | } | |
297 | ||
298 | rb_set_buffers(F1, READBUF_SIZE); | |
299 | rb_set_buffers(F2, READBUF_SIZE); | |
300 | snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(F2)); | |
301 | rb_setenv("CTL_FD", fdarg, 1); | |
302 | if(rb_pipe(&P1, &P2, "wsockd pipe") == -1) | |
303 | { | |
304 | ilog(L_MAIN, "Unable to create wsockd - rb_pipe failed: %s", strerror(errno)); | |
305 | return started; | |
306 | } | |
307 | snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(P1)); | |
308 | rb_setenv("CTL_PIPE", fdarg, 1); | |
309 | snprintf(s_pid, sizeof(s_pid), "%d", (int)getpid()); | |
310 | rb_setenv("CTL_PPID", s_pid, 1); | |
311 | ||
312 | #ifdef _WIN32 | |
313 | SetHandleInformation((HANDLE) rb_get_fd(F2), HANDLE_FLAG_INHERIT, 1); | |
314 | SetHandleInformation((HANDLE) rb_get_fd(P1), HANDLE_FLAG_INHERIT, 1); | |
315 | #endif | |
316 | ||
317 | pid = rb_spawn_process(wsockd_path, (const char **) parv); | |
318 | if(pid == -1) | |
319 | { | |
bccb7ded | 320 | ilog(L_MAIN, "Unable to create wsockd: %s\n", strerror(errno)); |
c53ca1e0 AC |
321 | rb_close(F1); |
322 | rb_close(F2); | |
323 | rb_close(P1); | |
324 | rb_close(P2); | |
325 | return started; | |
326 | } | |
327 | started++; | |
328 | rb_close(F2); | |
329 | rb_close(P1); | |
330 | ctl = allocate_ws_daemon(F1, P2, pid); | |
331 | ws_read_ctl(ctl->F, ctl); | |
332 | ws_do_pipe(P2, ctl); | |
333 | ||
334 | } | |
335 | return started; | |
336 | } | |
337 | ||
338 | static void | |
339 | ws_process_dead_fd(ws_ctl_t * ctl, ws_ctl_buf_t * ctl_buf) | |
340 | { | |
341 | struct Client *client_p; | |
342 | char reason[256]; | |
343 | uint32_t fd; | |
344 | ||
345 | if(ctl_buf->buflen < 6) | |
346 | return; /* bogus message..drop it.. XXX should warn here */ | |
347 | ||
348 | fd = buf_to_uint32(&ctl_buf->buf[1]); | |
349 | rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason)); | |
350 | client_p = find_cli_connid_hash(fd); | |
351 | if(client_p == NULL) | |
352 | return; | |
353 | if(IsAnyServer(client_p) || IsRegistered(client_p)) | |
354 | { | |
355 | /* read any last moment ERROR, QUIT or the like -- jilles */ | |
356 | if (!strcmp(reason, "Remote host closed the connection")) | |
357 | read_packet(client_p->localClient->F, client_p); | |
358 | if (IsAnyDead(client_p)) | |
359 | return; | |
360 | } | |
361 | exit_client(client_p, client_p, &me, reason); | |
362 | } | |
363 | ||
364 | ||
365 | static void | |
366 | ws_process_cmd_recv(ws_ctl_t * ctl) | |
367 | { | |
c53ca1e0 AC |
368 | rb_dlink_node *ptr, *next; |
369 | ws_ctl_buf_t *ctl_buf; | |
370 | unsigned long len; | |
371 | ||
372 | if(ctl->dead) | |
373 | return; | |
374 | ||
375 | RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head) | |
376 | { | |
377 | ctl_buf = ptr->data; | |
378 | switch (*ctl_buf->buf) | |
379 | { | |
380 | case 'D': | |
381 | ws_process_dead_fd(ctl, ctl_buf); | |
382 | break; | |
383 | default: | |
bccb7ded AC |
384 | ilog(L_MAIN, "Received invalid command from wsockd: %s", ctl_buf->buf); |
385 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from wsockd"); | |
c53ca1e0 AC |
386 | break; |
387 | } | |
388 | rb_dlinkDelete(ptr, &ctl->readq); | |
389 | rb_free(ctl_buf->buf); | |
390 | rb_free(ctl_buf); | |
391 | } | |
392 | ||
393 | } | |
394 | ||
395 | ||
396 | static void | |
397 | ws_read_ctl(rb_fde_t * F, void *data) | |
398 | { | |
399 | ws_ctl_buf_t *ctl_buf; | |
400 | ws_ctl_t *ctl = data; | |
401 | int retlen; | |
402 | ||
403 | if(ctl->dead) | |
404 | return; | |
405 | do | |
406 | { | |
407 | ctl_buf = rb_malloc(sizeof(ws_ctl_buf_t)); | |
408 | ctl_buf->buf = rb_malloc(READSIZE); | |
409 | retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4); | |
410 | ctl_buf->buflen = retlen; | |
411 | if(retlen <= 0) | |
412 | { | |
413 | rb_free(ctl_buf->buf); | |
414 | rb_free(ctl_buf); | |
415 | } | |
416 | else | |
417 | rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq); | |
418 | } | |
419 | while(retlen > 0); | |
420 | ||
421 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) | |
422 | { | |
423 | ws_dead(ctl); | |
424 | return; | |
425 | } | |
426 | ws_process_cmd_recv(ctl); | |
427 | rb_setselect(ctl->F, RB_SELECT_READ, ws_read_ctl, ctl); | |
428 | } | |
429 | ||
430 | static ws_ctl_t * | |
431 | which_wsockd(void) | |
432 | { | |
433 | ws_ctl_t *ctl, *lowest = NULL; | |
434 | rb_dlink_node *ptr; | |
435 | ||
436 | RB_DLINK_FOREACH(ptr, wsock_daemons.head) | |
437 | { | |
438 | ctl = ptr->data; | |
439 | if(ctl->dead) | |
440 | continue; | |
441 | if(ctl->shutdown) | |
442 | continue; | |
443 | if(lowest == NULL) | |
444 | { | |
445 | lowest = ctl; | |
446 | continue; | |
447 | } | |
448 | if(ctl->cli_count < lowest->cli_count) | |
449 | lowest = ctl; | |
450 | } | |
451 | ||
452 | return (lowest); | |
453 | } | |
454 | ||
455 | static void | |
456 | ws_write_ctl(rb_fde_t * F, void *data) | |
457 | { | |
458 | ws_ctl_t *ctl = data; | |
459 | ws_ctl_buf_t *ctl_buf; | |
460 | rb_dlink_node *ptr, *next; | |
461 | int retlen, x; | |
462 | ||
463 | if(ctl->dead) | |
464 | return; | |
465 | ||
466 | RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head) | |
467 | { | |
468 | ctl_buf = ptr->data; | |
469 | /* in theory unix sock_dgram shouldn't ever short write this.. */ | |
470 | retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen, ctl->pid); | |
471 | if(retlen > 0) | |
472 | { | |
473 | rb_dlinkDelete(ptr, &ctl->writeq); | |
474 | for(x = 0; x < ctl_buf->nfds; x++) | |
475 | rb_close(ctl_buf->F[x]); | |
476 | rb_free(ctl_buf->buf); | |
477 | rb_free(ctl_buf); | |
478 | ||
479 | } | |
480 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) | |
481 | { | |
482 | ws_dead(ctl); | |
483 | return; | |
484 | } | |
485 | else | |
486 | { | |
487 | rb_setselect(ctl->F, RB_SELECT_WRITE, ws_write_ctl, ctl); | |
488 | } | |
489 | } | |
490 | } | |
491 | ||
492 | static void | |
493 | ws_cmd_write_queue(ws_ctl_t * ctl, rb_fde_t ** F, int count, const void *buf, size_t buflen) | |
494 | { | |
495 | ws_ctl_buf_t *ctl_buf; | |
496 | int x; | |
497 | ||
498 | /* don't bother */ | |
499 | if(ctl->dead) | |
500 | return; | |
501 | ||
502 | ctl_buf = rb_malloc(sizeof(ws_ctl_buf_t)); | |
503 | ctl_buf->buf = rb_malloc(buflen); | |
504 | memcpy(ctl_buf->buf, buf, buflen); | |
505 | ctl_buf->buflen = buflen; | |
506 | ||
507 | for(x = 0; x < count && x < MAXPASSFD; x++) | |
508 | { | |
509 | ctl_buf->F[x] = F[x]; | |
510 | } | |
511 | ctl_buf->nfds = count; | |
512 | rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq); | |
513 | ws_write_ctl(ctl->F, ctl); | |
514 | } | |
515 | ||
516 | ws_ctl_t * | |
517 | start_wsockd_accept(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id) | |
518 | { | |
519 | rb_fde_t *F[2]; | |
520 | ws_ctl_t *ctl; | |
521 | char buf[5]; | |
522 | F[0] = sslF; | |
523 | F[1] = plainF; | |
524 | ||
525 | buf[0] = 'A'; | |
526 | uint32_to_buf(&buf[1], id); | |
527 | ctl = which_wsockd(); | |
528 | if(!ctl) | |
529 | return NULL; | |
530 | ctl->cli_count++; | |
531 | ws_cmd_write_queue(ctl, F, 2, buf, sizeof(buf)); | |
532 | return ctl; | |
533 | } | |
534 | ||
535 | void | |
536 | wsockd_decrement_clicount(ws_ctl_t * ctl) | |
537 | { | |
538 | if(ctl == NULL) | |
539 | return; | |
540 | ||
541 | ctl->cli_count--; | |
542 | if(ctl->shutdown && !ctl->cli_count) | |
543 | { | |
544 | ctl->dead = 1; | |
545 | rb_kill(ctl->pid, SIGKILL); | |
546 | } | |
547 | if(ctl->dead && !ctl->cli_count) | |
548 | { | |
549 | free_ws_daemon(ctl); | |
550 | } | |
551 | } | |
552 | ||
553 | static void | |
554 | cleanup_dead_ws(void *unused) | |
555 | { | |
556 | rb_dlink_node *ptr, *next; | |
557 | ws_ctl_t *ctl; | |
558 | RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head) | |
559 | { | |
560 | ctl = ptr->data; | |
561 | if(ctl->dead && !ctl->cli_count) | |
562 | { | |
563 | free_ws_daemon(ctl); | |
564 | } | |
565 | } | |
566 | } | |
567 | ||
568 | int | |
569 | get_wsockd_count(void) | |
570 | { | |
571 | return wsockd_count; | |
572 | } | |
573 | ||
574 | void | |
575 | wsockd_foreach_info(void (*func)(void *data, pid_t pid, int cli_count, enum wsockd_status status), void *data) | |
576 | { | |
577 | rb_dlink_node *ptr, *next; | |
578 | ws_ctl_t *ctl; | |
579 | RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head) | |
580 | { | |
581 | ctl = ptr->data; | |
582 | func(data, ctl->pid, ctl->cli_count, | |
583 | ctl->dead ? WSOCKD_DEAD : | |
584 | (ctl->shutdown ? WSOCKD_SHUTDOWN : WSOCKD_ACTIVE)); | |
585 | } | |
586 | } | |
587 | ||
588 | void | |
589 | init_wsockd(void) | |
590 | { | |
591 | rb_event_addish("cleanup_dead_ws", cleanup_dead_ws, NULL, 60); | |
592 | } |