* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
- * $Id: helper.c 25038 2008-01-23 16:03:08Z androsyn $
+ * $Id: helper.c 26092 2008-09-19 15:13:52Z androsyn $
*/
#include <libratbox_config.h>
#include <ratbox_lib.h>
/* 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)
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);
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;
*/
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];
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;
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]);
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;
{
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;
}
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;
void
rb_helper_write_flush(rb_helper *helper)
{
- rb_helper_write_sendq(helper->ofd, helper);
+ rb_helper_write_sendq(helper->ofd, helper);
}
int length;
if(helper == NULL)
return;
-
+
while((length = rb_read(helper->ifd, buf, sizeof(buf))) > 0)
{
rb_linebuf_parse(&helper->recvq, buf, length, 0);
rb_helper_restart(helper);
return;
}
-
+
rb_setselect(helper->ifd, RB_SELECT_READ, rb_helper_read_cb, helper);
}
{
if(helper == NULL)
return;
-
+ rb_kill(helper->pid, SIGKILL);
rb_close(helper->ifd);
rb_close(helper->ofd);
- rb_free(helper);
+ rb_free(helper);
}
int
rb_lib_loop(delay);
}
}
-