diff options
author | John Langford <jl@hunch.net> | 2011-08-12 23:54:06 +0400 |
---|---|---|
committer | John Langford <jl@hunch.net> | 2011-08-12 23:54:06 +0400 |
commit | a5314a63fc3ce77264e58a446cb5d745810b8391 (patch) | |
tree | db920e948a16dbcbd6e3b2f425b496364a7cec5d /parser.cc | |
parent | 0d553ebaa28e28b3363cef99b431da10e03244c0 (diff) |
added Michael Radford's killable daemon patch
Diffstat (limited to 'parser.cc')
-rw-r--r-- | parser.cc | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -7,6 +7,9 @@ embodied in the content of this file are licensed under the BSD #include <sys/types.h> #include <sys/mman.h> #include <sys/wait.h> +#include <signal.h> +#include <unistd.h> +#include <fstream> #include <netdb.h> #include <boost/program_options.hpp> @@ -35,6 +38,13 @@ bool done=false; v_array<size_t> random_nos; v_array<size_t> gram_mask; +bool got_sigterm = false; + +void handle_sigterm (int) +{ + got_sigterm = true; +} + parser* new_parser(const label_parser* lp) { parser* ret = (parser*) calloc(1,sizeof(parser)); @@ -292,6 +302,19 @@ void parse_source_args(po::variables_map& vm, parser* par, bool quiet, size_t pa cerr << "failure to background!" << endl; exit(1); } + // write pid file + if (vm.count("pid_file")) + { + ofstream pid_file; + pid_file.open(vm["pid_file"].as<string>().c_str()); + if (!pid_file.is_open()) + { + cerr << "error writing pid file" << endl; + exit(1); + } + pid_file << getpid() << endl; + pid_file.close(); + } if (global.persistent) { @@ -320,11 +343,27 @@ void parse_source_args(po::variables_map& vm, parser* par, bool quiet, size_t pa goto child; } + // install signal handler so we can kill children when killed + { + struct sigaction sa; + // specifically don't set SA_RESTART in sa.sa_flags, so that + // waitid will be interrupted by SIGTERM with handler installed + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = handle_sigterm; + sigaction(SIGTERM, &sa, NULL); + } + while (true) { // wait for child to change state; if finished, then respawn siginfo_t sig; waitid(P_ALL,0,&sig,WEXITED); + if (got_sigterm) + { + for (size_t i = 0; i < num_children; i++) + kill(children[i], SIGTERM); + exit(0); + } for (size_t i = 0; i < num_children; i++) if (sig.si_pid == children[i]) { |