- 论坛徽章:
- 0
|
在APUE里提到daemonize,我测试时,并没有生成守护进程,用ps -ajx查看,没有我的守护进程
接着我用GDB调试,发现 if ( (pid = fork()) < 0 )
{
err_quit("%s,can't fork",cmd);
}
else if (pid != 0)
{
exit(0);
}
在第一个fork后就终止了,下面的代码完全没有执行,不知那个高手可帮忙调试一下?
我的QQ21429503,欢迎交迎学习。- #include "fun.h"
- void daemonize ( const char *cmd )
- {
- int i,fd0,fd1,fd2;
- pid_t pid;
- struct rlimit rl;
- struct sigaction sa;
- umask(0);
- if ( getrlimit(RLIMIT_NOFILE,&rl) < 0 )
- {
- err_quit("%s: can't get file limit",cmd);
- }
- if ( (pid = fork()) <0 )
- {
- err_quit("%s,can't fork",cmd);
- }
- else if (pid != 0)
- {
- exit(0);
- }
- setsid();
- sa.sa_handler = SIG_IGN;
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = 0;
- if ( sigaction(SIGHUP,&sa,NULL) < 0 )
- {
- err_quit("%s: can't ignore SIGHUP");
- }
- if ( (pid = fork()) < 0 )
- {
- err_quit("%s,can't fork",cmd);
- }
- else if (pid != 0)
- {
- exit(0);
- }
- if ( chdir("/") < 0 )
- {
- err_quit("%s: can't change directory to /");
- }
- if ( rl.rlim_max == RLIM_INFINITY )
- {
- rl.rlim_max = 1024;
- }
- for ( i=0;i<rl.rlim_max ;i++ )
- {
- close(i);
- }
- fd0 = open("/de/null",O_RDWR);
- fd1 = dup(0);
- fd2 = dup(0);
- openlog(cmd,LOG_CONS,LOG_DAEMON);
- if ( fd0 != 0 || fd1 != 0 || fd2 != 0)
- {
- syslog(LOG_ERR,"unexpected file descriptors %d %d %d",fd0,fd1,fd2);
- }
- exit(1);
- return ;
- } /* ----- end of function daemonize ----- */
- int main ( int argc, char *argv[] )
- {
- char *cmd;
- if ( (cmd = strrchr(argv[0],'/')) == NULL )
- {
- cmd = argv[0];
- }
- else
- {
- cmd++;
- }
- daemonize(cmd);
- exit(0);
- } /* ---------- end of function main ---------- */
复制代码 |
|