summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-06-04 14:54:50 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-06-04 14:54:50 +0200
commit33a2de703db162af0c3d77593d560f3a8e7f1b95 (patch)
tree27faa858db7496fce73e9b3faf820e3a2ae89c13 /src
parent813535cfe40e103a79b1f24f66ee2b1cac58ae05 (diff)
downloadfastd-33a2de703db162af0c3d77593d560f3a8e7f1b95.tar
fastd-33a2de703db162af0c3d77593d560f3a8e7f1b95.zip
Add pidfile support
Diffstat (limited to 'src')
-rw-r--r--src/config.c9
-rw-r--r--src/fastd.c33
-rw-r--r--src/fastd.h2
3 files changed, 42 insertions, 2 deletions
diff --git a/src/config.c b/src/config.c
index a77c258..c28de7f 100644
--- a/src/config.c
+++ b/src/config.c
@@ -92,6 +92,8 @@ static void default_config(fastd_config *conf) {
conf->on_disestablish_dir = NULL;
conf->daemon = false;
+ conf->pid_file = NULL;
+
conf->machine_readable = false;
conf->generate_key = false;
conf->show_key = false;
@@ -374,6 +376,7 @@ static void count_peers(fastd_context *ctx, fastd_config *conf) {
OPTION(usage, "--help" OR "-h", "Shows this help text") \
OPTION(version, "--version" OR "-v", "Shows the fastd version") \
OPTION(option_daemon, "--daemon" OR "-d", "Runs fastd in the background") \
+ OPTION_ARG(option_pid_file, "--pid-file", "<filename>", "Writes fastd's PID to the specified file") \
OPTION_ARG(option_log_level, "--log-level", "error|warn|info|verbose|debug", "Sets the stderr log level; default is info, if no alternative log destination ist configured") \
OPTION_ARG(option_syslog_level, "--syslog-level", "error|warn|info|verbose|debug", "Sets the log level for syslog output; default is not to use syslog") \
OPTION_ARG(option_syslog_ident, "--syslog-ident", "<ident>", "Sets the syslog identification; default is 'fastd'") \
@@ -614,6 +617,12 @@ static void option_on_disestablish(fastd_context *ctx, fastd_config *conf, const
static void option_daemon(fastd_context *ctx, fastd_config *conf) {
conf->daemon = true;
}
+
+static void option_pid_file(fastd_context *ctx, fastd_config *conf, const char *arg) {
+ free(conf->pid_file);
+ conf->pid_file = strdup(arg);
+}
+
static void option_generate_key(fastd_context *ctx, fastd_config *conf) {
conf->generate_key = true;
conf->show_key = false;
diff --git a/src/fastd.c b/src/fastd.c
index 9f92ddb..aa7ed44 100644
--- a/src/fastd.c
+++ b/src/fastd.c
@@ -667,6 +667,23 @@ static void close_fds(fastd_context *ctx) {
}
}
+static void write_pid(fastd_context *ctx, pid_t pid) {
+ if (!ctx->conf->pid_file)
+ return;
+
+ int fd = open(ctx->conf->pid_file, O_WRONLY|O_CREAT, 0666);
+ if (fd < 0) {
+ pr_error_errno(ctx, "can't write PID file: open");
+ return;
+ }
+
+ if (dprintf(fd, "%i", pid) < 0)
+ pr_error_errno(ctx, "can't write PID file: dprintf");
+
+ if (close(fd) < 0)
+ pr_warn_errno(ctx, "close");
+}
+
int main(int argc, char *argv[]) {
fastd_context ctx;
memset(&ctx, 0, sizeof(ctx));
@@ -704,8 +721,20 @@ int main(int argc, char *argv[]) {
init_peers(&ctx);
if (conf.daemon) {
- if (daemon(1, 1) < 0)
- exit_errno(&ctx, "daemon");
+ pid_t pid = fork();
+ if (pid < 0) {
+ exit_errno(&ctx, "fork");
+ }
+ else if (pid > 0) {
+ write_pid(&ctx, pid);
+ exit(0);
+ }
+
+ if (setsid() < 0)
+ pr_error_errno(&ctx, "setsid");
+ }
+ else {
+ write_pid(&ctx, getpid());
}
on_up(&ctx);
diff --git a/src/fastd.h b/src/fastd.h
index fbf86ab..2edfcfe 100644
--- a/src/fastd.h
+++ b/src/fastd.h
@@ -174,6 +174,8 @@ struct _fastd_config {
char *on_disestablish_dir;
bool daemon;
+ char *pid_file;
+
bool machine_readable;
bool generate_key;
bool show_key;