2008-04-01 18:52:26 +02:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: A slightly useful ircd
|
|
|
|
* helper.c: Starts and deals with ircd helpers
|
2014-03-03 05:25:47 +01:00
|
|
|
*
|
2008-04-01 18:52:26 +02:00
|
|
|
* Copyright (C) 2006 Aaron Sethman <androsyn@ratbox.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA
|
|
|
|
*
|
|
|
|
*/
|
2016-03-24 01:10:44 +01:00
|
|
|
|
2016-07-01 05:04:00 +02:00
|
|
|
#include <rb/rb.h>
|
|
|
|
#include <rb/commio_int.h>
|
2008-04-01 18:52:26 +02:00
|
|
|
|
|
|
|
struct _rb_helper
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
buf_head_t sendq;
|
|
|
|
buf_head_t recvq;
|
|
|
|
rb_fde_t *ifd;
|
|
|
|
rb_fde_t *ofd;
|
|
|
|
pid_t pid;
|
|
|
|
int fork_count;
|
|
|
|
rb_helper_cb *read_cb;
|
|
|
|
rb_helper_cb *error_cb;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* setup all the stuff a new child needs */
|
|
|
|
rb_helper *
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_helper_child(rb_helper_cb * read_cb, rb_helper_cb * error_cb, log_cb * ilog,
|
2015-04-20 07:55:20 +02:00
|
|
|
restart_cb * irestart, die_cb * idie, size_t lb_heap_size,
|
2008-12-03 00:49:39 +01:00
|
|
|
size_t dh_size, size_t fd_heap_size)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
|
|
|
rb_helper *helper;
|
|
|
|
int maxfd, x = 0;
|
|
|
|
int ifd, ofd;
|
|
|
|
char *tifd, *tofd, *tmaxfd;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
tifd = getenv("IFD");
|
|
|
|
tofd = getenv("OFD");
|
|
|
|
tmaxfd = getenv("MAXFD");
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
if(tifd == NULL || tofd == NULL || tmaxfd == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
helper = rb_malloc(sizeof(rb_helper));
|
2008-12-03 00:49:39 +01:00
|
|
|
ifd = (int)strtol(tifd, NULL, 10);
|
2008-04-01 18:52:26 +02:00
|
|
|
ofd = (int)strtol(tofd, NULL, 10);
|
|
|
|
maxfd = (int)strtol(tmaxfd, NULL, 10);
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
for(x = 0; x < maxfd; x++)
|
|
|
|
{
|
|
|
|
if(x != ifd && x != ofd)
|
|
|
|
close(x);
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
x = open("/dev/null", O_RDWR);
|
|
|
|
if(ifd != 0 && ofd != 0)
|
|
|
|
dup2(x, 0);
|
|
|
|
if(ifd != 1 && ofd != 1)
|
|
|
|
dup2(x, 1);
|
|
|
|
if(ifd != 2 && ofd != 2)
|
|
|
|
dup2(x, 2);
|
2008-12-03 00:49:39 +01:00
|
|
|
if(x > 2) /* don't undo what we just did */
|
2008-04-01 18:52:26 +02:00
|
|
|
close(x);
|
|
|
|
#else
|
2016-03-20 06:41:01 +01:00
|
|
|
(void) x; /* shut gcc up */
|
2008-04-01 18:52:26 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
rb_lib_init(ilog, irestart, idie, 0, maxfd, dh_size, fd_heap_size);
|
|
|
|
rb_linebuf_init(lb_heap_size);
|
|
|
|
rb_linebuf_newbuf(&helper->sendq);
|
|
|
|
rb_linebuf_newbuf(&helper->recvq);
|
|
|
|
|
|
|
|
helper->ifd = rb_open(ifd, RB_FD_PIPE, "incoming connection");
|
|
|
|
helper->ofd = rb_open(ofd, RB_FD_PIPE, "outgoing connection");
|
|
|
|
rb_set_nb(helper->ifd);
|
|
|
|
rb_set_nb(helper->ofd);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
helper->read_cb = read_cb;
|
|
|
|
helper->error_cb = error_cb;
|
|
|
|
return helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start_fork_helper
|
|
|
|
* starts a new ircd helper
|
|
|
|
* note that this function doesn't start doing reading..thats the job of the caller
|
|
|
|
*/
|
|
|
|
|
|
|
|
rb_helper *
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_helper_start(const char *name, const char *fullpath, rb_helper_cb * read_cb,
|
|
|
|
rb_helper_cb * error_cb)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
|
|
|
rb_helper *helper;
|
|
|
|
const char *parv[2];
|
|
|
|
char buf[128];
|
|
|
|
char fx[16], fy[16];
|
|
|
|
rb_fde_t *in_f[2];
|
|
|
|
rb_fde_t *out_f[2];
|
|
|
|
pid_t pid;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
if(access(fullpath, X_OK) == -1)
|
|
|
|
return NULL;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
helper = rb_malloc(sizeof(rb_helper));
|
|
|
|
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(buf, sizeof(buf), "%s helper - read", name);
|
2008-12-03 00:49:39 +01:00
|
|
|
if(rb_pipe(&in_f[0], &in_f[1], buf) < 0)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
|
|
|
rb_free(helper);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(buf, sizeof(buf), "%s helper - write", name);
|
2008-04-01 18:52:26 +02:00
|
|
|
if(rb_pipe(&out_f[0], &out_f[1], buf) < 0)
|
|
|
|
{
|
|
|
|
rb_free(helper);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(fx, sizeof(fx), "%d", rb_get_fd(in_f[1]));
|
|
|
|
snprintf(fy, sizeof(fy), "%d", rb_get_fd(out_f[0]));
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_set_nb(in_f[0]);
|
|
|
|
rb_set_nb(in_f[1]);
|
|
|
|
rb_set_nb(out_f[0]);
|
|
|
|
rb_set_nb(out_f[1]);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
|
|
|
rb_setenv("IFD", fy, 1);
|
|
|
|
rb_setenv("OFD", fx, 1);
|
|
|
|
rb_setenv("MAXFD", "256", 1);
|
|
|
|
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(buf, sizeof(buf), "-ircd %s daemon", name);
|
2008-04-01 18:52:26 +02:00
|
|
|
parv[0] = buf;
|
|
|
|
parv[1] = NULL;
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
#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);
|
2008-04-01 18:52:26 +02:00
|
|
|
#endif
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
pid = rb_spawn_process(fullpath, (const char **)parv);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
if(pid == -1)
|
|
|
|
{
|
|
|
|
rb_close(in_f[0]);
|
|
|
|
rb_close(in_f[1]);
|
|
|
|
rb_close(out_f[0]);
|
|
|
|
rb_close(out_f[1]);
|
|
|
|
rb_free(helper);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_close(in_f[1]);
|
|
|
|
rb_close(out_f[0]);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_linebuf_newbuf(&helper->sendq);
|
|
|
|
rb_linebuf_newbuf(&helper->recvq);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
helper->ifd = in_f[0];
|
|
|
|
helper->ofd = out_f[1];
|
|
|
|
helper->read_cb = read_cb;
|
2008-12-03 00:49:39 +01:00
|
|
|
helper->error_cb = error_cb;
|
2008-04-01 18:52:26 +02:00
|
|
|
helper->fork_count = 0;
|
|
|
|
helper->pid = pid;
|
|
|
|
|
|
|
|
return helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_helper_restart(rb_helper *helper)
|
|
|
|
{
|
|
|
|
helper->error_cb(helper);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_helper_write_sendq(rb_fde_t *F, void *helper_ptr)
|
|
|
|
{
|
|
|
|
rb_helper *helper = helper_ptr;
|
|
|
|
int retlen;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
if(rb_linebuf_len(&helper->sendq) > 0)
|
|
|
|
{
|
|
|
|
while((retlen = rb_linebuf_flush(F, &helper->sendq)) > 0)
|
2014-02-23 22:23:00 +01:00
|
|
|
;
|
2008-12-03 00:49:39 +01:00
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_helper_restart(helper);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rb_linebuf_len(&helper->sendq) > 0)
|
|
|
|
rb_setselect(helper->ofd, RB_SELECT_WRITE, rb_helper_write_sendq, helper);
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
void
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_helper_write_queue(rb_helper *helper, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
rb_linebuf_putmsg(&helper->sendq, format, &ap, NULL);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_helper_write_flush(rb_helper *helper)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_helper_write_sendq(helper->ofd, helper);
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_helper_write(rb_helper *helper, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
rb_linebuf_putmsg(&helper->sendq, format, &ap, NULL);
|
|
|
|
va_end(ap);
|
|
|
|
rb_helper_write_flush(helper);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_helper_read_cb(rb_fde_t *F, void *data)
|
|
|
|
{
|
|
|
|
rb_helper *helper = (rb_helper *)data;
|
|
|
|
static char buf[32768];
|
|
|
|
int length;
|
|
|
|
if(helper == NULL)
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
while((length = rb_read(helper->ifd, buf, sizeof(buf))) > 0)
|
|
|
|
{
|
|
|
|
rb_linebuf_parse(&helper->recvq, buf, length, 0);
|
|
|
|
helper->read_cb(helper);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
|
|
|
rb_helper_restart(helper);
|
|
|
|
return;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_setselect(helper->ifd, RB_SELECT_READ, rb_helper_read_cb, helper);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_helper_run(rb_helper *helper)
|
|
|
|
{
|
|
|
|
if(helper == NULL)
|
|
|
|
return;
|
|
|
|
rb_helper_read_cb(helper->ifd, helper);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_helper_close(rb_helper *helper)
|
|
|
|
{
|
|
|
|
if(helper == NULL)
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_kill(helper->pid, SIGKILL);
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_close(helper->ifd);
|
|
|
|
rb_close(helper->ofd);
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_free(helper);
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_helper_read(rb_helper *helper, void *buf, size_t bufsize)
|
|
|
|
{
|
|
|
|
return rb_linebuf_get(&helper->recvq, buf, bufsize, LINEBUF_COMPLETE, LINEBUF_PARSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_helper_loop(rb_helper *helper, long delay)
|
|
|
|
{
|
|
|
|
rb_helper_run(helper);
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
rb_lib_loop(delay);
|
|
|
|
}
|
|
|
|
}
|