void daemonize(const char *cmd) {
int fd0, fd1, fd2;
int i;
pid_t pid;
struct rlimit rl;
struct sigaction sa;
// clear file createtion mask.
umask(0);
// get maximum number of file description.
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
printf("Can not get file limit.\n");
exit(0);
}
// become a session leader to lose controlling TTY
if ((pid = fork()) < 0) {
printf("Can not fork.\n");
exit(0);
}
else if (pid != 0) //parent
exit(0);
setsid();
// ensure future opens won't allocate controlling TTYs.
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGHUP, &sa, NULL) < 0) {
printf("can not ignore SIGUP\n");
exit(0);
}
if((pid = fork()) < 0) {
printf("Can not fork.\n");
exit(0);
}
else if(pid != 0) //parent
exit(0);
if (chdir("/") < 0) {
printf("can not change directory to /.\n");
exit(0);
}
if (rl.rlim_max == RLIM_INFINITY)
rl.rlim_max = 1024;
for (i = 0; i< rl.rlim_max; i++)
close(i); # i = 1为什么就退出了?
}
close(1)就退出了?
为什么啊?