]> jfr.im git - solanum.git/blame - ircd/wsproc.c
ircd: integrate ircd side of wsockd support
[solanum.git] / ircd / wsproc.c
CommitLineData
c53ca1e0
AC
1/*
2 * sslproc.c: An interface to ssld
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
37static void ws_read_ctl(rb_fde_t * F, void *data);
38static int wsockd_count;
39
40static char tmpbuf[READBUF_SIZE];
41static char nul = '\0';
42
43#define MAXPASSFD 4
44#define READSIZE 1024
45typedef 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
55struct 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
68static rb_dlink_list wsock_daemons;
69
70static inline uint32_t
71buf_to_uint32(char *buf)
72{
73 uint32_t x;
74 memcpy(&x, buf, sizeof(x));
75 return x;
76}
77
78static inline void
79uint32_to_buf(char *buf, uint32_t x)
80{
81 memcpy(buf, &x, sizeof(x));
82 return;
83}
84
85static ws_ctl_t *
86allocate_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
101static void
102free_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
135static char *wsockd_path;
136
137static int wsockd_spin_count = 0;
138static time_t last_spin;
139static int wsockd_wait = 0;
140
141void
142restart_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
166static void
167ws_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
185static void
186ws_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
203static void
204ws_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
217static void
218restart_ssld_event(void *unused)
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
232int
233start_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 {
256 ilog(L_MAIN, "ssld helper is spinning - will attempt to restart in 1 minute");
257 sendto_realops_snomask(SNO_GENERAL, L_ALL,
258 "ssld helper is spinning - will attempt to restart in 1 minute");
259 rb_event_add("restart_ssld_event", restart_ssld_event, NULL, 60);
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,
278 "Unable to execute ssld%s in %s or %s/bin",
279 suffix, ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
280 return 0;
281 }
282 }
283 wsockd_path = rb_strdup(fullpath);
284 }
285 rb_strlcpy(buf, "-ircd ssld daemon", sizeof(buf));
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 {
320 ilog(L_MAIN, "Unable to create ssld: %s\n", strerror(errno));
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
338static void
339ws_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
365static void
366ws_process_cmd_recv(ws_ctl_t * ctl)
367{
368 static const char *cannot_setup_ssl = "ssld cannot setup ssl, check your certificates and private key";
369 static const char *no_ssl_or_zlib = "ssld has neither SSL/TLS or zlib support killing all sslds";
370 rb_dlink_node *ptr, *next;
371 ws_ctl_buf_t *ctl_buf;
372 unsigned long len;
373
374 if(ctl->dead)
375 return;
376
377 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
378 {
379 ctl_buf = ptr->data;
380 switch (*ctl_buf->buf)
381 {
382 case 'D':
383 ws_process_dead_fd(ctl, ctl_buf);
384 break;
385 default:
386 ilog(L_MAIN, "Received invalid command from ssld: %s", ctl_buf->buf);
387 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from ssld");
388 break;
389 }
390 rb_dlinkDelete(ptr, &ctl->readq);
391 rb_free(ctl_buf->buf);
392 rb_free(ctl_buf);
393 }
394
395}
396
397
398static void
399ws_read_ctl(rb_fde_t * F, void *data)
400{
401 ws_ctl_buf_t *ctl_buf;
402 ws_ctl_t *ctl = data;
403 int retlen;
404
405 if(ctl->dead)
406 return;
407 do
408 {
409 ctl_buf = rb_malloc(sizeof(ws_ctl_buf_t));
410 ctl_buf->buf = rb_malloc(READSIZE);
411 retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4);
412 ctl_buf->buflen = retlen;
413 if(retlen <= 0)
414 {
415 rb_free(ctl_buf->buf);
416 rb_free(ctl_buf);
417 }
418 else
419 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
420 }
421 while(retlen > 0);
422
423 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
424 {
425 ws_dead(ctl);
426 return;
427 }
428 ws_process_cmd_recv(ctl);
429 rb_setselect(ctl->F, RB_SELECT_READ, ws_read_ctl, ctl);
430}
431
432static ws_ctl_t *
433which_wsockd(void)
434{
435 ws_ctl_t *ctl, *lowest = NULL;
436 rb_dlink_node *ptr;
437
438 RB_DLINK_FOREACH(ptr, wsock_daemons.head)
439 {
440 ctl = ptr->data;
441 if(ctl->dead)
442 continue;
443 if(ctl->shutdown)
444 continue;
445 if(lowest == NULL)
446 {
447 lowest = ctl;
448 continue;
449 }
450 if(ctl->cli_count < lowest->cli_count)
451 lowest = ctl;
452 }
453
454 return (lowest);
455}
456
457static void
458ws_write_ctl(rb_fde_t * F, void *data)
459{
460 ws_ctl_t *ctl = data;
461 ws_ctl_buf_t *ctl_buf;
462 rb_dlink_node *ptr, *next;
463 int retlen, x;
464
465 if(ctl->dead)
466 return;
467
468 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
469 {
470 ctl_buf = ptr->data;
471 /* in theory unix sock_dgram shouldn't ever short write this.. */
472 retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen, ctl->pid);
473 if(retlen > 0)
474 {
475 rb_dlinkDelete(ptr, &ctl->writeq);
476 for(x = 0; x < ctl_buf->nfds; x++)
477 rb_close(ctl_buf->F[x]);
478 rb_free(ctl_buf->buf);
479 rb_free(ctl_buf);
480
481 }
482 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
483 {
484 ws_dead(ctl);
485 return;
486 }
487 else
488 {
489 rb_setselect(ctl->F, RB_SELECT_WRITE, ws_write_ctl, ctl);
490 }
491 }
492}
493
494static void
495ws_cmd_write_queue(ws_ctl_t * ctl, rb_fde_t ** F, int count, const void *buf, size_t buflen)
496{
497 ws_ctl_buf_t *ctl_buf;
498 int x;
499
500 /* don't bother */
501 if(ctl->dead)
502 return;
503
504 ctl_buf = rb_malloc(sizeof(ws_ctl_buf_t));
505 ctl_buf->buf = rb_malloc(buflen);
506 memcpy(ctl_buf->buf, buf, buflen);
507 ctl_buf->buflen = buflen;
508
509 for(x = 0; x < count && x < MAXPASSFD; x++)
510 {
511 ctl_buf->F[x] = F[x];
512 }
513 ctl_buf->nfds = count;
514 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
515 ws_write_ctl(ctl->F, ctl);
516}
517
518ws_ctl_t *
519start_wsockd_accept(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id)
520{
521 rb_fde_t *F[2];
522 ws_ctl_t *ctl;
523 char buf[5];
524 F[0] = sslF;
525 F[1] = plainF;
526
527 buf[0] = 'A';
528 uint32_to_buf(&buf[1], id);
529 ctl = which_wsockd();
530 if(!ctl)
531 return NULL;
532 ctl->cli_count++;
533 ws_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
534 return ctl;
535}
536
537void
538wsockd_decrement_clicount(ws_ctl_t * ctl)
539{
540 if(ctl == NULL)
541 return;
542
543 ctl->cli_count--;
544 if(ctl->shutdown && !ctl->cli_count)
545 {
546 ctl->dead = 1;
547 rb_kill(ctl->pid, SIGKILL);
548 }
549 if(ctl->dead && !ctl->cli_count)
550 {
551 free_ws_daemon(ctl);
552 }
553}
554
555static void
556cleanup_dead_ws(void *unused)
557{
558 rb_dlink_node *ptr, *next;
559 ws_ctl_t *ctl;
560 RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head)
561 {
562 ctl = ptr->data;
563 if(ctl->dead && !ctl->cli_count)
564 {
565 free_ws_daemon(ctl);
566 }
567 }
568}
569
570int
571get_wsockd_count(void)
572{
573 return wsockd_count;
574}
575
576void
577wsockd_foreach_info(void (*func)(void *data, pid_t pid, int cli_count, enum wsockd_status status), void *data)
578{
579 rb_dlink_node *ptr, *next;
580 ws_ctl_t *ctl;
581 RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head)
582 {
583 ctl = ptr->data;
584 func(data, ctl->pid, ctl->cli_count,
585 ctl->dead ? WSOCKD_DEAD :
586 (ctl->shutdown ? WSOCKD_SHUTDOWN : WSOCKD_ACTIVE));
587 }
588}
589
590void
591init_wsockd(void)
592{
593 rb_event_addish("cleanup_dead_ws", cleanup_dead_ws, NULL, 60);
594}