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
;
40 static char tmpbuf
[READBUF_SIZE
];
41 static char nul
= '\0';
45 typedef struct _ws_ctl_buf
50 rb_fde_t
*F
[MAXPASSFD
];
68 static rb_dlink_list wsock_daemons
;
70 static inline uint32_t
71 buf_to_uint32(char *buf
)
74 memcpy(&x
, buf
, sizeof(x
));
79 uint32_to_buf(char *buf
, uint32_t x
)
81 memcpy(buf
, &x
, sizeof(x
));
86 allocate_ws_daemon(rb_fde_t
* F
, rb_fde_t
* P
, int pid
)
90 if(F
== NULL
|| pid
< 0)
92 ctl
= rb_malloc(sizeof(ws_ctl_t
));
97 rb_dlinkAdd(ctl
, &ctl
->node
, &wsock_daemons
);
102 free_ws_daemon(ws_ctl_t
* ctl
)
105 ws_ctl_buf_t
*ctl_buf
;
110 RB_DLINK_FOREACH(ptr
, ctl
->readq
.head
)
113 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
114 rb_close(ctl_buf
->F
[x
]);
116 rb_free(ctl_buf
->buf
);
120 RB_DLINK_FOREACH(ptr
, ctl
->writeq
.head
)
123 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
124 rb_close(ctl_buf
->F
[x
]);
126 rb_free(ctl_buf
->buf
);
131 rb_dlinkDelete(&ctl
->node
, &wsock_daemons
);
135 static char *wsockd_path
;
137 static int wsockd_spin_count
= 0;
138 static time_t last_spin
;
139 static int wsockd_wait
= 0;
144 rb_dlink_node
*ptr
, *next
;
147 RB_DLINK_FOREACH_SAFE(ptr
, next
, wsock_daemons
.head
)
158 rb_kill(ctl
->pid
, SIGKILL
);
163 start_wsockd(ServerInfo
.wsockd_count
);
169 rb_dlink_node
*ptr
, *next
;
171 RB_DLINK_FOREACH_SAFE(ptr
, next
, wsock_daemons
.head
)
179 rb_kill(ctl
->pid
, SIGKILL
);
186 ws_dead(ws_ctl_t
* ctl
)
192 rb_kill(ctl
->pid
, SIGKILL
); /* make sure the process is really gone */
197 ilog(L_MAIN
, "wsockd helper died - attempting to restart");
198 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "wsockd helper died - attempting to restart");
204 ws_do_pipe(rb_fde_t
* F
, void *data
)
207 ws_ctl_t
*ctl
= data
;
208 retlen
= rb_write(F
, "0", 1);
209 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
214 rb_setselect(F
, RB_SELECT_READ
, ws_do_pipe
, data
);
218 restart_wsockd_event(void *unused
)
220 wsockd_spin_count
= 0;
223 if(ServerInfo
.wsockd_count
> get_wsockd_count())
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");
233 start_wsockd(int count
)
238 const char *suffix
= ".exe";
240 const char *suffix
= "";
243 char fullpath
[PATH_MAX
+ 1];
254 if(wsockd_spin_count
> 20 && (rb_current_time() - last_spin
< 5))
256 ilog(L_MAIN
, "wsockd helper is spinning - will attempt to restart in 1 minute");
257 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
258 "wsockd helper is spinning - will attempt to restart in 1 minute");
259 rb_event_add("restart_wsockd_event", restart_wsockd_event
, NULL
, 60);
265 last_spin
= rb_current_time();
267 if(wsockd_path
== NULL
)
269 snprintf(fullpath
, sizeof(fullpath
), "%s%cwsockd%s", ircd_paths
[IRCD_PATH_LIBEXEC
], RB_PATH_SEPARATOR
, suffix
);
271 if(access(fullpath
, X_OK
) == -1)
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)
278 "Unable to execute wsockd%s in %s or %s/bin",
279 suffix
, ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
283 wsockd_path
= rb_strdup(fullpath
);
285 rb_strlcpy(buf
, "-ircd wsockd daemon", sizeof(buf
));
289 for(i
= 0; i
< count
; i
++)
292 if(rb_socketpair(AF_UNIX
, SOCK_DGRAM
, 0, &F1
, &F2
, "wsockd handle passing socket") == -1)
294 ilog(L_MAIN
, "Unable to create wsockd - rb_socketpair failed: %s", strerror(errno
));
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)
304 ilog(L_MAIN
, "Unable to create wsockd - rb_pipe failed: %s", strerror(errno
));
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);
313 SetHandleInformation((HANDLE
) rb_get_fd(F2
), HANDLE_FLAG_INHERIT
, 1);
314 SetHandleInformation((HANDLE
) rb_get_fd(P1
), HANDLE_FLAG_INHERIT
, 1);
317 pid
= rb_spawn_process(wsockd_path
, (const char **) parv
);
320 ilog(L_MAIN
, "Unable to create wsockd: %s\n", strerror(errno
));
330 ctl
= allocate_ws_daemon(F1
, P2
, pid
);
331 ws_read_ctl(ctl
->F
, ctl
);
339 ws_process_dead_fd(ws_ctl_t
* ctl
, ws_ctl_buf_t
* ctl_buf
)
341 struct Client
*client_p
;
345 if(ctl_buf
->buflen
< 6)
346 return; /* bogus message..drop it.. XXX should warn here */
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
);
353 if(IsAnyServer(client_p
) || IsRegistered(client_p
))
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
))
361 exit_client(client_p
, client_p
, &me
, reason
);
366 ws_process_cmd_recv(ws_ctl_t
* ctl
)
368 rb_dlink_node
*ptr
, *next
;
369 ws_ctl_buf_t
*ctl_buf
;
375 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->readq
.head
)
378 switch (*ctl_buf
->buf
)
381 ws_process_dead_fd(ctl
, ctl_buf
);
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");
388 rb_dlinkDelete(ptr
, &ctl
->readq
);
389 rb_free(ctl_buf
->buf
);
397 ws_read_ctl(rb_fde_t
* F
, void *data
)
399 ws_ctl_buf_t
*ctl_buf
;
400 ws_ctl_t
*ctl
= data
;
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
;
413 rb_free(ctl_buf
->buf
);
417 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->readq
);
421 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
426 ws_process_cmd_recv(ctl
);
427 rb_setselect(ctl
->F
, RB_SELECT_READ
, ws_read_ctl
, ctl
);
433 ws_ctl_t
*ctl
, *lowest
= NULL
;
436 RB_DLINK_FOREACH(ptr
, wsock_daemons
.head
)
448 if(ctl
->cli_count
< lowest
->cli_count
)
456 ws_write_ctl(rb_fde_t
* F
, void *data
)
458 ws_ctl_t
*ctl
= data
;
459 ws_ctl_buf_t
*ctl_buf
;
460 rb_dlink_node
*ptr
, *next
;
466 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->writeq
.head
)
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
);
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
);
480 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
487 rb_setselect(ctl
->F
, RB_SELECT_WRITE
, ws_write_ctl
, ctl
);
493 ws_cmd_write_queue(ws_ctl_t
* ctl
, rb_fde_t
** F
, int count
, const void *buf
, size_t buflen
)
495 ws_ctl_buf_t
*ctl_buf
;
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
;
507 for(x
= 0; x
< count
&& x
< MAXPASSFD
; x
++)
509 ctl_buf
->F
[x
] = F
[x
];
511 ctl_buf
->nfds
= count
;
512 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->writeq
);
513 ws_write_ctl(ctl
->F
, ctl
);
517 start_wsockd_accept(rb_fde_t
* sslF
, rb_fde_t
* plainF
, uint32_t id
)
526 uint32_to_buf(&buf
[1], id
);
527 ctl
= which_wsockd();
531 ws_cmd_write_queue(ctl
, F
, 2, buf
, sizeof(buf
));
536 wsockd_decrement_clicount(ws_ctl_t
* ctl
)
542 if(ctl
->shutdown
&& !ctl
->cli_count
)
545 rb_kill(ctl
->pid
, SIGKILL
);
547 if(ctl
->dead
&& !ctl
->cli_count
)
554 cleanup_dead_ws(void *unused
)
556 rb_dlink_node
*ptr
, *next
;
558 RB_DLINK_FOREACH_SAFE(ptr
, next
, wsock_daemons
.head
)
561 if(ctl
->dead
&& !ctl
->cli_count
)
569 get_wsockd_count(void)
575 wsockd_foreach_info(void (*func
)(void *data
, pid_t pid
, int cli_count
, enum wsockd_status status
), void *data
)
577 rb_dlink_node
*ptr
, *next
;
579 RB_DLINK_FOREACH_SAFE(ptr
, next
, wsock_daemons
.head
)
582 func(data
, ctl
->pid
, ctl
->cli_count
,
583 ctl
->dead
? WSOCKD_DEAD
:
584 (ctl
->shutdown
? WSOCKD_SHUTDOWN
: WSOCKD_ACTIVE
));
591 rb_event_addish("cleanup_dead_ws", cleanup_dead_ws
, NULL
, 60);