问题
http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch13lev1sec4.html (excerpt from Richard Steven's UNIX Network Programming Vol. 1) includes
Signal(SIGHUP, SIG_IGN);
as part of the daemon_init
function because:
..."We must ignore SIGHUP because when the session leader terminates (the first child), all processes in the session (our second child) receive the SIGHUP signal."
The core of the function is:
int i;
pid_t pid;
if ( (pid = Fork()) < 0)
return (-1);
else if (pid)
_exit(0); /* parent terminates */
/* child 1 continues... */
if (setsid() < 0) /* become session leader */
return (-1);
Signal(SIGHUP, SIG_IGN);
if ( (pid = Fork()) < 0)
return (-1);
else if (pid)
_exit(0); /* child 1 terminates */
/* child 2 continues... */
daemon_proc = 1; /* for err_XXX() functions */
What I don't understand is why should the target child get a SIGHUP?
I tried replicating that code with some info-printing to test that SIGHUP is really received but I can't seem to catch it, not even if I stop (SIGSTOP) the grandchild process, in which case I thought it really should get a SIGHUP because that would cause the grandchild to become a stopped orphaned process group, but no SIGHUP gets received even then (this is obviously related to the SIGSTOP part: why SIGHUP signal not received when becoming an Orphaned Process Group ).
#define _GNU_SOURCE
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int daemon_proc = 0;
static void
pr_ids(char* name){
printf("%s: pid = %ld, ppid = %ld, sid=%ld, pgrp = %ld, tpgrp = %ld\n",
name, (long)getpid(), (long)getppid(), (long)getsid(getpid()), (long)getpgrp(),
(long)tcgetpgrp(STDOUT_FILENO));
fflush(stdout);
}
void hndlr(int s){
static const char msg[] = "Caught SIGHUP\n";
write(2,msg,sizeof(msg));
}
int daemon_init(){
pid_t pid;
pr_ids("gparent");
if ( 0>(pid=fork()))
return -1;
if (pid)
_exit(0);
pr_ids("parent");
if (0>setsid())
return -1;
close(0);
dup(1);
if(0>sysv_signal(SIGHUP, hndlr))
return -1;
if(0>(pid=fork()))
return -1;
if(pid){
sleep(1);
_exit(0);
}
pr_ids("child");
sleep(2);
pr_ids("child");
kill(getpid(), SIGSTOP);
daemon_proc = 1;
chdir("/");
return 0;
}
int main(int argc, char** argv){
daemon_init();
printf("daemon_proc=%d\n", daemon_proc);
return 0;
}
This gives me outputs such as:
gparent: pid = 20904, ppid = 20015, sid=20015, pgrp = 20904, tpgrp = 20904
parent: pid = 20905, ppid = 1, sid=20015, pgrp = 20904, tpgrp = 20015
child: pid = 20906, ppid = 20905, sid=20905, pgrp = 20905, tpgrp = -1
#waiting
child: pid = 20906, ppid = 1, sid=20905, pgrp = 20905, tpgrp = -1
Can you explain why self-termination of the first child (session leader) should cause a SIGHUP in the second child and why it doesn't?
来源:https://stackoverflow.com/questions/39103871/daemonization-and-sighup