Skip to content

Commit

Permalink
when daemonizing setsid() must be used to make sure the daemon is it'…
Browse files Browse the repository at this point in the history
…s own session leader
  • Loading branch information
equinox0815 committed Aug 3, 2024
1 parent b37d4c4 commit 164e7a1
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions god.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,14 @@ int main(int argc, char **argv) {

// Daemonize.
pid_t pid = fork();
if (pid) {
if (pid > 0) {
waitpid(pid, NULL, 0);
} else if (!pid) {
if ((pid = fork())) {
if(setsid() < 0) {
perror("setsid");
exit(1);
}
if ((pid = fork()) > 0) {
exit(0);
} else if (!pid) {
daemon_main(optind, argv);
Expand Down Expand Up @@ -218,7 +222,7 @@ void daemon_main(int optind, char **argv) {
}
signal(SIGHUP, sighup);
pipe(logfd);
if ((childpid = fork())) {
if ((childpid = fork()) > 0) {
close(0);
close(1);
close(2);
Expand Down

0 comments on commit 164e7a1

Please sign in to comment.