diff --git a/Makefile.am b/Makefile.am index 14c3b03..c3c889a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,18 @@ AUTOMAKE_OPTIONS = foreign +AM_CPPFLAGS = -W -Wall -g -std=c99 + +# bin +bin_PROGRAMS = runner +# source files +runner_SOURCES = runner.c + +#sysconf_DATA = runner.ini +install-data-local: + - cp runner.ini $(prefix)/ + - cp runnerd $(prefix)/bin + - [ -d /etc/systemd/system/ ] && cp antd-tunnel-publisher.service /etc/systemd/system/ + +EXTRA_DIST = runner.ini runnerd tunnel.h antd-tunnel-publisher.service log.h + SUBDIRS = . vterm \ No newline at end of file diff --git a/antd-tunnel-publisher.service b/antd-tunnel-publisher.service new file mode 100644 index 0000000..777c1cd --- /dev/null +++ b/antd-tunnel-publisher.service @@ -0,0 +1,12 @@ +[Unit] +Description=Antd Publisher Service +After=antd.target + +[Service] +Type=simple +User=root +WorkingDirectory=/opt/www +ExecStart=/opt/www/bin/runnerd + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/dist/antd-publishers-0.1.0a.tar.gz b/dist/antd-publishers-0.1.0a.tar.gz new file mode 100644 index 0000000..7d1e6be Binary files /dev/null and b/dist/antd-publishers-0.1.0a.tar.gz differ diff --git a/runner.c b/runner.c new file mode 100644 index 0000000..415b5ea --- /dev/null +++ b/runner.c @@ -0,0 +1,134 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "log.h" + +#define MODULE_NAME "runner" + +static volatile int running = 1; +static list_t pids; +static char socket_file[BUFFLEN]; +static void int_handler(int dummy) { + (void) dummy; + running = 0; +} + +static int ini_handle(void *user_data, const char *section, const char *name, + const char *value) +{ + pid_t pid; + char* argv[3]; + UNUSED(user_data); + if (EQU(section, "RUNNER")) + { + if(EQU(name, "socket")) + { + M_LOG(MODULE_NAME, "Configuration file is %s", value); + (void)strncpy(socket_file,value,BUFFLEN); + } + else if(EQU(name, "service")) + { + M_LOG(MODULE_NAME, "Running service %s...", value); + pid = fork(); + if(pid == -1) + { + M_ERROR(MODULE_NAME, "Unable to fork: %s", strerror(errno)); + return 0; + } + if(pid == 0) + { + // child + argv[0] = (char*)value; + argv[1] = (char*)socket_file; + argv[2] = NULL; + execve(argv[0], &argv[0], NULL); + // Nothing below this line should be executed by child process. If so, + // it means that the execl function wasn't successfull, so lets exit: + _exit(1); + } + // parent + list_put_i(&pids, pid); + } + else + { + M_ERROR(MODULE_NAME, "Ignore unknown configuration %s = %s", name, value); + return 0; + } + } + else + { + return 0; + } + return 1; +} + +int main(int argc, char const *argv[]) +{ + const char* conf_file; + item_t item; + pid_t w_pid; + int pid_count; + signal(SIGPIPE, SIG_IGN); + signal(SIGABRT, SIG_IGN); + signal(SIGINT, int_handler); + if(argc != 2) + { + printf("Usage: %s /path/to/conf.ini", argv[0]); + return -1; + } + conf_file = argv[1]; + + pids = list_init(); + + if (ini_parse(conf_file, ini_handle, NULL) < 0) + { + M_ERROR(MODULE_NAME, "Can't load '%s'", conf_file); + return -1; + } + pid_count = list_size(pids); + // monitoring the process + while(running != 0 && pid_count != 0) + { + pid_count = 0; + list_for_each(item, pids) + { + w_pid = waitpid((pid_t) item->value.i, NULL, WNOHANG); + if(w_pid == -1 || w_pid > 0) + { + // child exits + M_LOG(MODULE_NAME, "Process %d exits\n", item->value.i); + item->value.i = -1; + } + else + { + pid_count++; + } + } + // 500 ms + usleep(500000); + } + // kill all the remaining processes + list_for_each(item, pids) + { + if(item->value.i != -1) + { + if(kill(item->value.i, SIGKILL) == - 1) + { + M_ERROR(MODULE_NAME, "Unable to kill process %d: %s", item->value.i, strerror(errno)); + } + else + { + (void)waitpid(item->value.i, NULL, 0); + } + } + } + list_free(&pids); + return 0; +} diff --git a/runner.ini b/runner.ini new file mode 100644 index 0000000..ebd0da7 --- /dev/null +++ b/runner.ini @@ -0,0 +1,3 @@ +[RUNNER] +socket=/opt/www/tmp/channels/antd_hotline.sock +service=/opt/www/bin/vterm \ No newline at end of file diff --git a/runnerd b/runnerd new file mode 100755 index 0000000..c199e1b --- /dev/null +++ b/runnerd @@ -0,0 +1,2 @@ +#! /bin/bash +/opt/www/bin/runner /opt/www/runner.ini \ No newline at end of file diff --git a/vterm/vterm b/vterm/vterm index a6ca79b..1279b3c 100755 Binary files a/vterm/vterm and b/vterm/vterm differ diff --git a/vterm/vterm.c b/vterm/vterm.c index abc87f9..43f07fe 100644 --- a/vterm/vterm.c +++ b/vterm/vterm.c @@ -14,7 +14,7 @@ #include #include -#include "tunnel.h" +#include "../tunnel.h" #define MODULE_NAME "vterm" @@ -319,6 +319,8 @@ int main(int argc, char** argv) printf("Usage: %s path/to/hotline/socket\n", argv[0]); return -1; } + signal(SIGPIPE, SIG_IGN); + signal(SIGABRT, SIG_IGN); signal(SIGINT, int_handler); M_LOG(MODULE_NAME, "Hotline is: %s", argv[1]); // now try to request new channel from hotline