diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2012-06-04 14:54:50 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2012-06-04 14:54:50 +0200 |
commit | 33a2de703db162af0c3d77593d560f3a8e7f1b95 (patch) | |
tree | 27faa858db7496fce73e9b3faf820e3a2ae89c13 /src/fastd.c | |
parent | 813535cfe40e103a79b1f24f66ee2b1cac58ae05 (diff) | |
download | fastd-33a2de703db162af0c3d77593d560f3a8e7f1b95.tar fastd-33a2de703db162af0c3d77593d560f3a8e7f1b95.zip |
Add pidfile support
Diffstat (limited to 'src/fastd.c')
-rw-r--r-- | src/fastd.c | 33 |
1 files changed, 31 insertions, 2 deletions
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); |