trivial-httpd: Daemonize better

I was trying to debug `test-pull-c`, and typing `Ctrl-C` in gdb
ended up sending `SIGINT` to trivial-httpd as well, killing it.

Daemonize a bit more properly to avoid this. I also followed the standard
`/dev/null` guidelines.

Closes: #643
Approved by: jlebon
This commit is contained in:
Colin Walters 2017-01-12 10:02:23 -05:00 committed by Atomic Bot
parent d9f43cd2fb
commit 2a71afc507

View File

@ -30,6 +30,7 @@
#include "otutil.h" #include "otutil.h"
#include <locale.h> #include <locale.h>
#include <err.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <signal.h> #include <signal.h>
@ -597,9 +598,12 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error)
goto out; goto out;
} }
/* Child, continue */ /* Child, continue */
if (setsid () < 0)
err (1, "setsid");
/* Daemonising: close stdout/stderr so $() et al work on us */ /* Daemonising: close stdout/stderr so $() et al work on us */
fclose (stdout); freopen("/dev/null", "r", stdin);
fclose (stdin); freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
} }
else else
{ {