X-Git-Url: https://jfr.im/git/irc/rqf/shadowircd.git/blobdiff_plain/143b6cc1e4a8faa8181301bf43ddafecab3237cc..36fb4e9a7743326d1a2ca7601bc9bf1c5b9a0bcb:/libratbox/src/helper.c diff --git a/libratbox/src/helper.c b/libratbox/src/helper.c index a4a6260..a51fa5d 100644 --- a/libratbox/src/helper.c +++ b/libratbox/src/helper.c @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: helper.c 25245 2008-04-21 18:38:23Z androsyn $ + * $Id: helper.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include #include @@ -41,31 +41,32 @@ struct _rb_helper /* setup all the stuff a new child needs */ rb_helper * -rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, restart_cb *irestart, die_cb *idie, - int maxcon, size_t lb_heap_size, size_t dh_size, size_t fd_heap_size) +rb_helper_child(rb_helper_cb * read_cb, rb_helper_cb * error_cb, log_cb * ilog, + restart_cb * irestart, die_cb * idie, int maxcon, size_t lb_heap_size, + size_t dh_size, size_t fd_heap_size) { rb_helper *helper; int maxfd, x = 0; int ifd, ofd; char *tifd, *tofd, *tmaxfd; - + tifd = getenv("IFD"); tofd = getenv("OFD"); tmaxfd = getenv("MAXFD"); - + if(tifd == NULL || tofd == NULL || tmaxfd == NULL) return NULL; helper = rb_malloc(sizeof(rb_helper)); - ifd = (int)strtol(tifd, NULL, 10); + ifd = (int)strtol(tifd, NULL, 10); ofd = (int)strtol(tofd, NULL, 10); maxfd = (int)strtol(tmaxfd, NULL, 10); -#ifndef WIN32 - for(x = 0; x < maxfd; x++) - { - if(x != ifd && x != ofd) - close(x); +#ifndef _WIN32 + for(x = 0; x < maxfd; x++) + { + if(x != ifd && x != ofd) + close(x); } x = open("/dev/null", O_RDWR); if(ifd != 0 && ofd != 0) @@ -74,10 +75,10 @@ rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, res dup2(x, 1); if(ifd != 2 && ofd != 2) dup2(x, 2); - if(x > 2) /* don't undo what we just did */ + if(x > 2) /* don't undo what we just did */ close(x); #else - x = 0; /* shut gcc up */ + x = 0; /* shut gcc up */ #endif rb_lib_init(ilog, irestart, idie, 0, maxfd, dh_size, fd_heap_size); @@ -89,7 +90,7 @@ rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, res helper->ofd = rb_open(ofd, RB_FD_PIPE, "outgoing connection"); rb_set_nb(helper->ifd); rb_set_nb(helper->ofd); - + helper->read_cb = read_cb; helper->error_cb = error_cb; return helper; @@ -102,7 +103,8 @@ rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, res */ rb_helper * -rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, rb_helper_cb *error_cb) +rb_helper_start(const char *name, const char *fullpath, rb_helper_cb * read_cb, + rb_helper_cb * error_cb) { rb_helper *helper; const char *parv[2]; @@ -111,14 +113,14 @@ rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, r rb_fde_t *in_f[2]; rb_fde_t *out_f[2]; pid_t pid; - + if(access(fullpath, X_OK) == -1) return NULL; - + helper = rb_malloc(sizeof(rb_helper)); rb_snprintf(buf, sizeof(buf), "%s helper - read", name); - if(rb_pipe(&in_f[0], &in_f[1], buf) < 0) + if(rb_pipe(&in_f[0], &in_f[1], buf) < 0) { rb_free(helper); return NULL; @@ -129,30 +131,30 @@ rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, r rb_free(helper); return NULL; } - + rb_snprintf(fx, sizeof(fx), "%d", rb_get_fd(in_f[1])); rb_snprintf(fy, sizeof(fy), "%d", rb_get_fd(out_f[0])); - + rb_set_nb(in_f[0]); rb_set_nb(in_f[1]); rb_set_nb(out_f[0]); rb_set_nb(out_f[1]); - - setenv("IFD", fy, 1); - setenv("OFD", fx, 1); - setenv("MAXFD", "256", 1); - + + rb_setenv("IFD", fy, 1); + rb_setenv("OFD", fx, 1); + rb_setenv("MAXFD", "256", 1); + rb_snprintf(buf, sizeof(buf), "-ircd %s daemon", name); parv[0] = buf; parv[1] = NULL; -#ifdef WIN32 - SetHandleInformation((HANDLE)rb_get_fd(in_f[1]), HANDLE_FLAG_INHERIT, 1); - SetHandleInformation((HANDLE)rb_get_fd(out_f[0]), HANDLE_FLAG_INHERIT, 1); +#ifdef _WIN32 + SetHandleInformation((HANDLE) rb_get_fd(in_f[1]), HANDLE_FLAG_INHERIT, 1); + SetHandleInformation((HANDLE) rb_get_fd(out_f[0]), HANDLE_FLAG_INHERIT, 1); #endif - + pid = rb_spawn_process(fullpath, (const char **)parv); - + if(pid == -1) { rb_close(in_f[0]); @@ -165,14 +167,14 @@ rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, r rb_close(in_f[1]); rb_close(out_f[0]); - + rb_linebuf_newbuf(&helper->sendq); rb_linebuf_newbuf(&helper->recvq); - + helper->ifd = in_f[0]; helper->ofd = out_f[1]; helper->read_cb = read_cb; - helper->error_cb = error_cb; + helper->error_cb = error_cb; helper->fork_count = 0; helper->pid = pid; @@ -192,12 +194,13 @@ rb_helper_write_sendq(rb_fde_t *F, void *helper_ptr) { rb_helper *helper = helper_ptr; int retlen; - + if(rb_linebuf_len(&helper->sendq) > 0) { while((retlen = rb_linebuf_flush(F, &helper->sendq)) > 0) ;; - if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) { + if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) + { rb_helper_restart(helper); return; } @@ -207,7 +210,7 @@ rb_helper_write_sendq(rb_fde_t *F, void *helper_ptr) rb_setselect(helper->ofd, RB_SELECT_WRITE, rb_helper_write_sendq, helper); } -void +void rb_helper_write_queue(rb_helper *helper, const char *format, ...) { va_list ap; @@ -219,7 +222,7 @@ rb_helper_write_queue(rb_helper *helper, const char *format, ...) void rb_helper_write_flush(rb_helper *helper) { - rb_helper_write_sendq(helper->ofd, helper); + rb_helper_write_sendq(helper->ofd, helper); } @@ -241,7 +244,7 @@ rb_helper_read_cb(rb_fde_t *F, void *data) int length; if(helper == NULL) return; - + while((length = rb_read(helper->ifd, buf, sizeof(buf))) > 0) { rb_linebuf_parse(&helper->recvq, buf, length, 0); @@ -253,7 +256,7 @@ rb_helper_read_cb(rb_fde_t *F, void *data) rb_helper_restart(helper); return; } - + rb_setselect(helper->ifd, RB_SELECT_READ, rb_helper_read_cb, helper); } @@ -271,10 +274,10 @@ rb_helper_close(rb_helper *helper) { if(helper == NULL) return; - kill(helper->pid, SIGKILL); + rb_kill(helper->pid, SIGKILL); rb_close(helper->ifd); rb_close(helper->ofd); - rb_free(helper); + rb_free(helper); } int @@ -292,4 +295,3 @@ rb_helper_loop(rb_helper *helper, long delay) rb_lib_loop(delay); } } -