#include #include #include #include #include #include #include #include #include void doexit(int sig) { printf("exiting parent\n"); exit(0); } int main() { FILE *f; int fd; if (fork()) { printf("in parent\n"); if (signal(SIGTERM, doexit) == SIG_ERR) { printf("problem setting SIGTERM\n"); exit(2); } // syslog does this to give the child enough // time to take over logging sleep(300); // this should not be seen, the child should // kill the parent before the parent can get // past the sleep printf("should not have made it this far\n"); exit(1); } printf("in child\n"); printf("pid %d\n", getpid()); printf("ppid %d\n", getppid()); // make a small delay to make sure all signals are setup sleep(1); // the following file operations are taken from syslogd.c if ( ((fd = open("fork.lock", O_RDWR|O_CREAT, 0644)) == -1) || ((f = fdopen(fd, "r+")) == NULL) ) { fprintf(stderr, "Can't open or create fork.lock.\n"); exit(1); } if (flock(fd, LOCK_EX|LOCK_NB) == -1) { printf("Can't lock fork.lock\n"); exit(1); } // this is how syslog kills off the parent kill(getppid(), SIGTERM); printf("in child after killing parent\n"); exit(0); }