Skip to content

Commit

Permalink
dhtnode: add service mode
Browse files Browse the repository at this point in the history
Service mode is similar to the daemon mode but:
* doesn't fork
* logs to stdout/stderr by default
  instead of syslog
  • Loading branch information
aberaud committed Jun 29, 2017
1 parent ec6296e commit 673884a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion tools/dhtnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ main(int argc, char **argv)
}
if (params.daemonize) {
daemonize();
} else if (params.service) {
setupSignals();
}

dht::crypto::Identity crt {};
Expand All @@ -383,7 +385,7 @@ main(int argc, char **argv)
dht->bootstrap(params.bootstrap.first.c_str(), params.bootstrap.second.c_str());
}

if (params.daemonize) {
if (params.daemonize or params.service) {
while (true)
std::this_thread::sleep_for(std::chrono::seconds(30));
} else {
Expand Down
30 changes: 20 additions & 10 deletions tools/tools_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ struct dht_params {
dht::NetId network {0};
bool generate_identity {false};
bool daemonize {false};
bool service {false};
std::pair<std::string, std::string> bootstrap {};
};

Expand All @@ -127,6 +128,7 @@ static const constexpr struct option long_options[] = {
{"identity", no_argument , nullptr, 'i'},
{"verbose", no_argument , nullptr, 'v'},
{"daemonize", no_argument , nullptr, 'd'},
{"service", no_argument , nullptr, 's'},
{"logfile", required_argument, nullptr, 'l'},
{"syslog", no_argument , nullptr, 'L'},
{nullptr, 0 , nullptr, 0}
Expand All @@ -136,7 +138,7 @@ dht_params
parseArgs(int argc, char **argv) {
dht_params params;
int opt;
while ((opt = getopt_long(argc, argv, "hidvp:n:b:l:", long_options, nullptr)) != -1) {
while ((opt = getopt_long(argc, argv, "hidsvp:n:b:l:", long_options, nullptr)) != -1) {
switch (opt) {
case 'p': {
int port_arg = atoi(optarg);
Expand All @@ -152,9 +154,7 @@ parseArgs(int argc, char **argv) {
case 'b':
params.bootstrap = splitPort((optarg[0] == '=') ? optarg+1 : optarg);
if (not params.bootstrap.first.empty() and params.bootstrap.second.empty()) {
std::stringstream ss;
ss << DHT_DEFAULT_PORT;
params.bootstrap.second = ss.str();
params.bootstrap.second = std::to_string(DHT_DEFAULT_PORT);
}
break;
case 'h':
Expand All @@ -176,6 +176,9 @@ parseArgs(int argc, char **argv) {
case 'd':
params.daemonize = true;
break;
case 's':
params.service = true;
break;
default:
break;
}
Expand Down Expand Up @@ -212,6 +215,18 @@ void signal_handler(int sig)
}
}

void setupSignals()
{
#ifndef WIN32_NATIVE
signal(SIGCHLD,SIG_IGN); /* ignore child */
signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP,signal_handler); /* catch hangup signal */
signal(SIGTERM,signal_handler); /* catch kill signal */
#endif
}

void daemonize()
{
#ifndef WIN32_NATIVE
Expand All @@ -234,11 +249,6 @@ void daemonize()
close(STDOUT_FILENO);
close(STDERR_FILENO);

signal(SIGCHLD,SIG_IGN); /* ignore child */
signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP,signal_handler); /* catch hangup signal */
signal(SIGTERM,signal_handler); /* catch kill signal */
setupSignals();
#endif
}

0 comments on commit 673884a

Please sign in to comment.