Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suppres SIGHUP on forking #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ccan/daemonize/daemonize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ccan/daemonize/daemonize.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
Expand All @@ -10,14 +11,28 @@
* Environment. */
bool daemonize(void)
{
struct sigaction old_sa, sa;
pid_t pid;
int sa_rc;

/* SIGHUP may be thrown when the parent exits below.
So, set to ignore it and save previous action.
*/
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
sa_rc = sigaction(SIGHUP, &sa, &old_sa);

/* Separate from our parent via fork, so init inherits us. */
if ((pid = fork()) < 0)
return false;
if (pid != 0)
exit(0);

/* Restore previous SIGHUP action. */
if (sa_rc != -1)
sigaction(SIGHUP, &old_sa, NULL);

/* Don't hold files open. */
close(STDIN_FILENO);
close(STDOUT_FILENO);
Expand Down