|
|
1.1 ! root 1: /* ! 2: * Disassociate from controling terminal and process ! 3: * group. Close all open files. ! 4: */ ! 5: #include <stdio.h> ! 6: #include <signal.h> ! 7: #include <kernel/param.h> ! 8: ! 9: bedaemon() ! 10: { ! 11: int child_pid; ! 12: int fd; ! 13: ! 14: /* Daemon does its own sigsetup. Do it to be standard module for all ! 15: * daemon processes do this extra signal here. ! 16: */ ! 17: signal(SIGHUP, SIG_IGN); /* immune from the group leader death */ ! 18: ! 19: /* When we did not start from background fork and let parent die. */ ! 20: if ((child_pid = fork()) < 0) { ! 21: fprintf(stderr, "cron: cannot fork\n"); ! 22: exit(1); ! 23: } else ! 24: if (child_pid > 0) /* parent */ ! 25: exit(0); ! 26: ! 27: /* Disassociate from controlling terminal & process group */ ! 28: setpgrp(); ! 29: ! 30: /* This fork should garantee that process can't reacquire a new ! 31: * controlling terminal. ! 32: */ ! 33: if ((child_pid = fork()) < 0) { ! 34: fprintf(stderr, "cron: cannot fork\n"); ! 35: exit(1); ! 36: } else ! 37: if (child_pid > 0) /* parent */ ! 38: exit(0); ! 39: ! 40: /* Close all open files, chdir "/", and umask(0). */ ! 41: for (fd = 0; fd < NOFILE; fd++) ! 42: close(fd); ! 43: ! 44: chdir("/"); ! 45: umask(0); /* We do not want any surprise with file creation */ ! 46: } ! 47: ! 48: #ifdef TEST ! 49: main() ! 50: { ! 51: bedaemon(); ! 52: sleep(100); ! 53: } ! 54: #endif ! 55: ! 56: ! 57: ! 58: ! 59:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.