]> jfr.im git - solanum.git/blame - ircd/wsproc.c
free server_p->certfp, allocated in newconf.c
[solanum.git] / ircd / wsproc.c
CommitLineData
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
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
9a9bc518 166#if 0
c53ca1e0
AC
167static void
168ws_killall(void)
169{
170 rb_dlink_node *ptr, *next;
171 ws_ctl_t *ctl;
172 RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head)
173 {
174 ctl = ptr->data;
175 if(ctl->dead)
176 continue;
177 ctl->dead = 1;
178 if(!ctl->shutdown)
179 wsockd_count--;
180 rb_kill(ctl->pid, SIGKILL);
181 if(!ctl->cli_count)
182 free_ws_daemon(ctl);
183 }
184}
9a9bc518 185#endif
c53ca1e0
AC
186
187static void
188ws_dead(ws_ctl_t * ctl)
189{
190 if(ctl->dead)
191 return;
192
193 ctl->dead = 1;
194 rb_kill(ctl->pid, SIGKILL); /* make sure the process is really gone */
195
196 if(!ctl->shutdown)
197 {
198 wsockd_count--;
199 ilog(L_MAIN, "wsockd helper died - attempting to restart");
200 sendto_realops_snomask(SNO_GENERAL, L_ALL, "wsockd helper died - attempting to restart");
201 start_wsockd(1);
202 }
203}
204
205static void
206ws_do_pipe(rb_fde_t * F, void *data)
207{
208 int retlen;
209 ws_ctl_t *ctl = data;
210 retlen = rb_write(F, "0", 1);
211 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
212 {
213 ws_dead(ctl);
214 return;
215 }
216 rb_setselect(F, RB_SELECT_READ, ws_do_pipe, data);
217}
218
219static void
bccb7ded 220restart_wsockd_event(void *unused)
c53ca1e0
AC
221{
222 wsockd_spin_count = 0;
223 last_spin = 0;
224 wsockd_wait = 0;
225 if(ServerInfo.wsockd_count > get_wsockd_count())
226 {
227 int start = ServerInfo.wsockd_count - get_wsockd_count();
228 ilog(L_MAIN, "Attempting to restart wsockd processes");
229 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Attempting to restart wsockd processes");
230 start_wsockd(start);
231 }
232}
233
234int
235start_wsockd(int count)
236{
237 rb_fde_t *F1, *F2;
238 rb_fde_t *P1, *P2;
239#ifdef _WIN32
240 const char *suffix = ".exe";
241#else
242 const char *suffix = "";
243#endif
244
245 char fullpath[PATH_MAX + 1];
246 char fdarg[6];
247 const char *parv[2];
248 char buf[128];
249 char s_pid[10];
250 pid_t pid;
251 int started = 0, i;
252
253 if(wsockd_wait)
254 return 0;
255
256 if(wsockd_spin_count > 20 && (rb_current_time() - last_spin < 5))
257 {
bccb7ded 258 ilog(L_MAIN, "wsockd helper is spinning - will attempt to restart in 1 minute");
c53ca1e0 259 sendto_realops_snomask(SNO_GENERAL, L_ALL,
bccb7ded
AC
260 "wsockd helper is spinning - will attempt to restart in 1 minute");
261 rb_event_add("restart_wsockd_event", restart_wsockd_event, NULL, 60);
c53ca1e0
AC
262 wsockd_wait = 1;
263 return 0;
264 }
265
266 wsockd_spin_count++;
267 last_spin = rb_current_time();
268
269 if(wsockd_path == NULL)
270 {
271 snprintf(fullpath, sizeof(fullpath), "%s%cwsockd%s", ircd_paths[IRCD_PATH_LIBEXEC], RB_PATH_SEPARATOR, suffix);
272
273 if(access(fullpath, X_OK) == -1)
274 {
275 snprintf(fullpath, sizeof(fullpath), "%s%cbin%cwsockd%s",
276 ConfigFileEntry.dpath, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR, suffix);
277 if(access(fullpath, X_OK) == -1)
278 {
279 ilog(L_MAIN,
bccb7ded 280 "Unable to execute wsockd%s in %s or %s/bin",
c53ca1e0
AC
281 suffix, ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
282 return 0;
283 }
284 }
285 wsockd_path = rb_strdup(fullpath);
286 }
bccb7ded 287 rb_strlcpy(buf, "-ircd wsockd daemon", sizeof(buf));
c53ca1e0
AC
288 parv[0] = buf;
289 parv[1] = NULL;
290
291 for(i = 0; i < count; i++)
292 {
293 ws_ctl_t *ctl;
294 if(rb_socketpair(AF_UNIX, SOCK_DGRAM, 0, &F1, &F2, "wsockd handle passing socket") == -1)
295 {
296 ilog(L_MAIN, "Unable to create wsockd - rb_socketpair failed: %s", strerror(errno));
297 return started;
298 }
299
300 rb_set_buffers(F1, READBUF_SIZE);
301 rb_set_buffers(F2, READBUF_SIZE);
302 snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(F2));
303 rb_setenv("CTL_FD", fdarg, 1);
304 if(rb_pipe(&P1, &P2, "wsockd pipe") == -1)
305 {
306 ilog(L_MAIN, "Unable to create wsockd - rb_pipe failed: %s", strerror(errno));
307 return started;
308 }
309 snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(P1));
310 rb_setenv("CTL_PIPE", fdarg, 1);
311 snprintf(s_pid, sizeof(s_pid), "%d", (int)getpid());
312 rb_setenv("CTL_PPID", s_pid, 1);
313
314#ifdef _WIN32
315 SetHandleInformation((HANDLE) rb_get_fd(F2), HANDLE_FLAG_INHERIT, 1);
316 SetHandleInformation((HANDLE) rb_get_fd(P1), HANDLE_FLAG_INHERIT, 1);
317#endif
318
319 pid = rb_spawn_process(wsockd_path, (const char **) parv);
320 if(pid == -1)
321 {
bccb7ded 322 ilog(L_MAIN, "Unable to create wsockd: %s\n", strerror(errno));
c53ca1e0
AC
323 rb_close(F1);
324 rb_close(F2);
325 rb_close(P1);
326 rb_close(P2);
327 return started;
328 }
329 started++;
330 rb_close(F2);
331 rb_close(P1);
332 ctl = allocate_ws_daemon(F1, P2, pid);
333 ws_read_ctl(ctl->F, ctl);
334 ws_do_pipe(P2, ctl);
335
336 }
337 return started;
338}
339
340static void
341ws_process_dead_fd(ws_ctl_t * ctl, ws_ctl_buf_t * ctl_buf)
342{
343 struct Client *client_p;
344 char reason[256];
345 uint32_t fd;
346
347 if(ctl_buf->buflen < 6)
348 return; /* bogus message..drop it.. XXX should warn here */
349
350 fd = buf_to_uint32(&ctl_buf->buf[1]);
351 rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason));
352 client_p = find_cli_connid_hash(fd);
353 if(client_p == NULL)
354 return;
355 if(IsAnyServer(client_p) || IsRegistered(client_p))
356 {
357 /* read any last moment ERROR, QUIT or the like -- jilles */
358 if (!strcmp(reason, "Remote host closed the connection"))
359 read_packet(client_p->localClient->F, client_p);
360 if (IsAnyDead(client_p))
361 return;
362 }
363 exit_client(client_p, client_p, &me, reason);
364}
365
366
367static void
368ws_process_cmd_recv(ws_ctl_t * ctl)
369{
c53ca1e0
AC
370 rb_dlink_node *ptr, *next;
371 ws_ctl_buf_t *ctl_buf;
c53ca1e0
AC
372
373 if(ctl->dead)
374 return;
375
376 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
377 {
378 ctl_buf = ptr->data;
379 switch (*ctl_buf->buf)
380 {
381 case 'D':
382 ws_process_dead_fd(ctl, ctl_buf);
383 break;
384 default:
bccb7ded
AC
385 ilog(L_MAIN, "Received invalid command from wsockd: %s", ctl_buf->buf);
386 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from wsockd");
c53ca1e0
AC
387 break;
388 }
389 rb_dlinkDelete(ptr, &ctl->readq);
390 rb_free(ctl_buf->buf);
391 rb_free(ctl_buf);
392 }
393
394}
395
396
397static void
398ws_read_ctl(rb_fde_t * F, void *data)
399{
400 ws_ctl_buf_t *ctl_buf;
401 ws_ctl_t *ctl = data;
402 int retlen;
403
404 if(ctl->dead)
405 return;
406 do
407 {
408 ctl_buf = rb_malloc(sizeof(ws_ctl_buf_t));
409 ctl_buf->buf = rb_malloc(READSIZE);
410 retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4);
411 ctl_buf->buflen = retlen;
412 if(retlen <= 0)
413 {
414 rb_free(ctl_buf->buf);
415 rb_free(ctl_buf);
416 }
417 else
418 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
419 }
420 while(retlen > 0);
421
422 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
423 {
424 ws_dead(ctl);
425 return;
426 }
427 ws_process_cmd_recv(ctl);
428 rb_setselect(ctl->F, RB_SELECT_READ, ws_read_ctl, ctl);
429}
430
431static ws_ctl_t *
432which_wsockd(void)
433{
434 ws_ctl_t *ctl, *lowest = NULL;
435 rb_dlink_node *ptr;
436
437 RB_DLINK_FOREACH(ptr, wsock_daemons.head)
438 {
439 ctl = ptr->data;
440 if(ctl->dead)
441 continue;
442 if(ctl->shutdown)
443 continue;
444 if(lowest == NULL)
445 {
446 lowest = ctl;
447 continue;
448 }
449 if(ctl->cli_count < lowest->cli_count)
450 lowest = ctl;
451 }
452
453 return (lowest);
454}
455
456static void
457ws_write_ctl(rb_fde_t * F, void *data)
458{
459 ws_ctl_t *ctl = data;
460 ws_ctl_buf_t *ctl_buf;
461 rb_dlink_node *ptr, *next;
462 int retlen, x;
463
464 if(ctl->dead)
465 return;
466
467 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
468 {
469 ctl_buf = ptr->data;
470 /* in theory unix sock_dgram shouldn't ever short write this.. */
471 retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen, ctl->pid);
472 if(retlen > 0)
473 {
474 rb_dlinkDelete(ptr, &ctl->writeq);
475 for(x = 0; x < ctl_buf->nfds; x++)
476 rb_close(ctl_buf->F[x]);
477 rb_free(ctl_buf->buf);
478 rb_free(ctl_buf);
479
480 }
481 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
482 {
483 ws_dead(ctl);
484 return;
485 }
486 else
487 {
488 rb_setselect(ctl->F, RB_SELECT_WRITE, ws_write_ctl, ctl);
489 }
490 }
491}
492
493static void
494ws_cmd_write_queue(ws_ctl_t * ctl, rb_fde_t ** F, int count, const void *buf, size_t buflen)
495{
496 ws_ctl_buf_t *ctl_buf;
497 int x;
498
499 /* don't bother */
500 if(ctl->dead)
501 return;
502
503 ctl_buf = rb_malloc(sizeof(ws_ctl_buf_t));
504 ctl_buf->buf = rb_malloc(buflen);
505 memcpy(ctl_buf->buf, buf, buflen);
506 ctl_buf->buflen = buflen;
507
508 for(x = 0; x < count && x < MAXPASSFD; x++)
509 {
510 ctl_buf->F[x] = F[x];
511 }
512 ctl_buf->nfds = count;
513 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
514 ws_write_ctl(ctl->F, ctl);
515}
516
517ws_ctl_t *
518start_wsockd_accept(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id)
519{
520 rb_fde_t *F[2];
521 ws_ctl_t *ctl;
522 char buf[5];
523 F[0] = sslF;
524 F[1] = plainF;
525
526 buf[0] = 'A';
527 uint32_to_buf(&buf[1], id);
528 ctl = which_wsockd();
529 if(!ctl)
530 return NULL;
531 ctl->cli_count++;
532 ws_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
533 return ctl;
534}
535
536void
537wsockd_decrement_clicount(ws_ctl_t * ctl)
538{
539 if(ctl == NULL)
540 return;
541
542 ctl->cli_count--;
543 if(ctl->shutdown && !ctl->cli_count)
544 {
545 ctl->dead = 1;
546 rb_kill(ctl->pid, SIGKILL);
547 }
548 if(ctl->dead && !ctl->cli_count)
549 {
550 free_ws_daemon(ctl);
551 }
552}
553
554static void
555cleanup_dead_ws(void *unused)
556{
557 rb_dlink_node *ptr, *next;
558 ws_ctl_t *ctl;
559 RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head)
560 {
561 ctl = ptr->data;
562 if(ctl->dead && !ctl->cli_count)
563 {
564 free_ws_daemon(ctl);
565 }
566 }
567}
568
569int
570get_wsockd_count(void)
571{
572 return wsockd_count;
573}
574
575void
576wsockd_foreach_info(void (*func)(void *data, pid_t pid, int cli_count, enum wsockd_status status), void *data)
577{
578 rb_dlink_node *ptr, *next;
579 ws_ctl_t *ctl;
580 RB_DLINK_FOREACH_SAFE(ptr, next, wsock_daemons.head)
581 {
582 ctl = ptr->data;
583 func(data, ctl->pid, ctl->cli_count,
584 ctl->dead ? WSOCKD_DEAD :
585 (ctl->shutdown ? WSOCKD_SHUTDOWN : WSOCKD_ACTIVE));
586 }
587}
588
589void
590init_wsockd(void)
591{
592 rb_event_addish("cleanup_dead_ws", cleanup_dead_ws, NULL, 60);
593}