2 * sslproc.c: An interface to wsockd
3 * Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org>
4 * Copyright (C) 2007 ircd-ratbox development team
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.
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.
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
37 static void ws_read_ctl(rb_fde_t
* F
, void *data
);
38 static int wsockd_count
;
42 typedef struct _ws_ctl_buf
47 rb_fde_t
*F
[MAXPASSFD
];
65 static rb_dlink_list wsock_daemons
;
67 static inline uint32_t
68 buf_to_uint32(char *buf
)
71 memcpy(&x
, buf
, sizeof(x
));
76 uint32_to_buf(char *buf
, uint32_t x
)
78 memcpy(buf
, &x
, sizeof(x
));
83 allocate_ws_daemon(rb_fde_t
* F
, rb_fde_t
* P
, int pid
)
87 if(F
== NULL
|| pid
< 0)
89 ctl
= rb_malloc(sizeof(ws_ctl_t
));
94 rb_dlinkAdd(ctl
, &ctl
->node
, &wsock_daemons
);
99 free_ws_daemon(ws_ctl_t
* ctl
)
102 ws_ctl_buf_t
*ctl_buf
;
107 RB_DLINK_FOREACH(ptr
, ctl
->readq
.head
)
110 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
111 rb_close(ctl_buf
->F
[x
]);
113 rb_free(ctl_buf
->buf
);
117 RB_DLINK_FOREACH(ptr
, ctl
->writeq
.head
)
120 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
121 rb_close(ctl_buf
->F
[x
]);
123 rb_free(ctl_buf
->buf
);
128 rb_dlinkDelete(&ctl
->node
, &wsock_daemons
);
132 static char *wsockd_path
;
134 static int wsockd_spin_count
= 0;
135 static time_t last_spin
;
136 static int wsockd_wait
= 0;
141 rb_dlink_node
*ptr
, *next
;
144 RB_DLINK_FOREACH_SAFE(ptr
, next
, wsock_daemons
.head
)
155 rb_kill(ctl
->pid
, SIGKILL
);
160 start_wsockd(ServerInfo
.wsockd_count
);
167 rb_dlink_node
*ptr
, *next
;
169 RB_DLINK_FOREACH_SAFE(ptr
, next
, wsock_daemons
.head
)
177 rb_kill(ctl
->pid
, SIGKILL
);
185 ws_dead(ws_ctl_t
* ctl
)
191 rb_kill(ctl
->pid
, SIGKILL
); /* make sure the process is really gone */
196 ilog(L_MAIN
, "wsockd helper died - attempting to restart");
197 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "wsockd helper died - attempting to restart");
203 ws_do_pipe(rb_fde_t
* F
, void *data
)
206 ws_ctl_t
*ctl
= data
;
207 retlen
= rb_write(F
, "0", 1);
208 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
213 rb_setselect(F
, RB_SELECT_READ
, ws_do_pipe
, data
);
217 restart_wsockd_event(void *unused
)
219 wsockd_spin_count
= 0;
222 if(ServerInfo
.wsockd_count
> get_wsockd_count())
224 int start
= ServerInfo
.wsockd_count
- get_wsockd_count();
225 ilog(L_MAIN
, "Attempting to restart wsockd processes");
226 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Attempting to restart wsockd processes");
232 start_wsockd(int count
)
236 char fullpath
[PATH_MAX
+ 1];
247 if(wsockd_spin_count
> 20 && (rb_current_time() - last_spin
< 5))
249 ilog(L_MAIN
, "wsockd helper is spinning - will attempt to restart in 1 minute");
250 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
251 "wsockd helper is spinning - will attempt to restart in 1 minute");
252 rb_event_add("restart_wsockd_event", restart_wsockd_event
, NULL
, 60);
258 last_spin
= rb_current_time();
260 if(wsockd_path
== NULL
)
262 snprintf(fullpath
, sizeof(fullpath
), "%s/wsockd", ircd_paths
[IRCD_PATH_LIBEXEC
]);
264 if(access(fullpath
, X_OK
) == -1)
266 snprintf(fullpath
, sizeof(fullpath
), "%s/bin/wsockd", ConfigFileEntry
.dpath
);
267 if(access(fullpath
, X_OK
) == -1)
270 "Unable to execute wsockd in %s or %s/bin",
271 ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
275 wsockd_path
= rb_strdup(fullpath
);
277 rb_strlcpy(buf
, "-ircd wsockd daemon", sizeof(buf
));
281 for(i
= 0; i
< count
; i
++)
284 if(rb_socketpair(AF_UNIX
, SOCK_DGRAM
, 0, &F1
, &F2
, "wsockd handle passing socket") == -1)
286 ilog(L_MAIN
, "Unable to create wsockd - rb_socketpair failed: %s", strerror(errno
));
290 rb_set_buffers(F1
, READBUF_SIZE
);
291 rb_set_buffers(F2
, READBUF_SIZE
);
292 snprintf(fdarg
, sizeof(fdarg
), "%d", rb_get_fd(F2
));
293 rb_setenv("CTL_FD", fdarg
, 1);
294 if(rb_pipe(&P1
, &P2
, "wsockd pipe") == -1)
296 ilog(L_MAIN
, "Unable to create wsockd - rb_pipe failed: %s", strerror(errno
));
299 snprintf(fdarg
, sizeof(fdarg
), "%d", rb_get_fd(P1
));
300 rb_setenv("CTL_PIPE", fdarg
, 1);
301 snprintf(s_pid
, sizeof(s_pid
), "%d", (int)getpid());
302 rb_setenv("CTL_PPID", s_pid
, 1);
304 rb_clear_cloexec(F2
);
305 rb_clear_cloexec(P1
);
307 pid
= rb_spawn_process(wsockd_path
, (const char **) parv
);
310 ilog(L_MAIN
, "Unable to create wsockd: %s\n", strerror(errno
));
320 ctl
= allocate_ws_daemon(F1
, P2
, pid
);
321 ws_read_ctl(ctl
->F
, ctl
);
329 ws_process_dead_fd(ws_ctl_t
* ctl
, ws_ctl_buf_t
* ctl_buf
)
331 struct Client
*client_p
;
335 if(ctl_buf
->buflen
< 6)
336 return; /* bogus message..drop it.. XXX should warn here */
338 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
339 rb_strlcpy(reason
, &ctl_buf
->buf
[5], sizeof(reason
));
340 client_p
= find_cli_connid_hash(fd
);
343 if(IsAnyServer(client_p
) || IsRegistered(client_p
))
345 /* read any last moment ERROR, QUIT or the like -- jilles */
346 if (!strcmp(reason
, "Remote host closed the connection"))
347 read_packet(client_p
->localClient
->F
, client_p
);
348 if (IsAnyDead(client_p
))
351 exit_client(client_p
, client_p
, &me
, reason
);
356 ws_process_cmd_recv(ws_ctl_t
* ctl
)
358 rb_dlink_node
*ptr
, *next
;
359 ws_ctl_buf_t
*ctl_buf
;
364 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->readq
.head
)
367 switch (*ctl_buf
->buf
)
370 ws_process_dead_fd(ctl
, ctl_buf
);
373 ilog(L_MAIN
, "Received invalid command from wsockd: %s", ctl_buf
->buf
);
374 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Received invalid command from wsockd");
377 rb_dlinkDelete(ptr
, &ctl
->readq
);
378 rb_free(ctl_buf
->buf
);
386 ws_read_ctl(rb_fde_t
* F
, void *data
)
388 ws_ctl_buf_t
*ctl_buf
;
389 ws_ctl_t
*ctl
= data
;
396 ctl_buf
= rb_malloc(sizeof(ws_ctl_buf_t
));
397 ctl_buf
->buf
= rb_malloc(READSIZE
);
398 retlen
= rb_recv_fd_buf(ctl
->F
, ctl_buf
->buf
, READSIZE
, ctl_buf
->F
, 4);
399 ctl_buf
->buflen
= retlen
;
402 rb_free(ctl_buf
->buf
);
406 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->readq
);
410 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
415 ws_process_cmd_recv(ctl
);
416 rb_setselect(ctl
->F
, RB_SELECT_READ
, ws_read_ctl
, ctl
);
422 ws_ctl_t
*ctl
, *lowest
= NULL
;
425 RB_DLINK_FOREACH(ptr
, wsock_daemons
.head
)
437 if(ctl
->cli_count
< lowest
->cli_count
)
445 ws_write_ctl(rb_fde_t
* F
, void *data
)
447 ws_ctl_t
*ctl
= data
;
448 ws_ctl_buf_t
*ctl_buf
;
449 rb_dlink_node
*ptr
, *next
;
455 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->writeq
.head
)
458 /* in theory unix sock_dgram shouldn't ever short write this.. */
459 retlen
= rb_send_fd_buf(ctl
->F
, ctl_buf
->F
, ctl_buf
->nfds
, ctl_buf
->buf
, ctl_buf
->buflen
, ctl
->pid
);
462 rb_dlinkDelete(ptr
, &ctl
->writeq
);
463 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
464 rb_close(ctl_buf
->F
[x
]);
465 rb_free(ctl_buf
->buf
);
469 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
476 rb_setselect(ctl
->F
, RB_SELECT_WRITE
, ws_write_ctl
, ctl
);
482 ws_cmd_write_queue(ws_ctl_t
* ctl
, rb_fde_t
** F
, int count
, const void *buf
, size_t buflen
)
484 ws_ctl_buf_t
*ctl_buf
;
491 ctl_buf
= rb_malloc(sizeof(ws_ctl_buf_t
));
492 ctl_buf
->buf
= rb_malloc(buflen
);
493 memcpy(ctl_buf
->buf
, buf
, buflen
);
494 ctl_buf
->buflen
= buflen
;
496 for(x
= 0; x
< count
&& x
< MAXPASSFD
; x
++)
498 ctl_buf
->F
[x
] = F
[x
];
500 ctl_buf
->nfds
= count
;
501 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->writeq
);
502 ws_write_ctl(ctl
->F
, ctl
);
506 start_wsockd_accept(rb_fde_t
* sslF
, rb_fde_t
* plainF
, uint32_t id
)
515 uint32_to_buf(&buf
[1], id
);
516 ctl
= which_wsockd();
520 ws_cmd_write_queue(ctl
, F
, 2, buf
, sizeof(buf
));
525 wsockd_decrement_clicount(ws_ctl_t
* ctl
)
531 if(ctl
->shutdown
&& !ctl
->cli_count
)
534 rb_kill(ctl
->pid
, SIGKILL
);
536 if(ctl
->dead
&& !ctl
->cli_count
)
543 cleanup_dead_ws(void *unused
)
545 rb_dlink_node
*ptr
, *next
;
547 RB_DLINK_FOREACH_SAFE(ptr
, next
, wsock_daemons
.head
)
550 if(ctl
->dead
&& !ctl
->cli_count
)
558 get_wsockd_count(void)
564 wsockd_foreach_info(void (*func
)(void *data
, pid_t pid
, int cli_count
, enum wsockd_status status
), void *data
)
566 rb_dlink_node
*ptr
, *next
;
568 RB_DLINK_FOREACH_SAFE(ptr
, next
, wsock_daemons
.head
)
571 func(data
, ctl
->pid
, ctl
->cli_count
,
572 ctl
->dead
? WSOCKD_DEAD
:
573 (ctl
->shutdown
? WSOCKD_SHUTDOWN
: WSOCKD_ACTIVE
));
580 rb_event_addish("cleanup_dead_ws", cleanup_dead_ws
, NULL
, 60);