int checkRunningPid(void) { pid_t pid; pid = readPidFile(); if (pid < 2) return 0; if (kill(pid, 0) < 0) return 0; fprintf(stderr, "nginx_master is already running! process id %ld\n", (long int) pid); return 1; }
//SIGPIPE是正常的信号,对端断开后本端协议栈在向对端传送数据时,对端会返回TCP RST,导致本端抛出SIGPIPE信号 void init_daemon(char* cmd) { int pid; char *buf = "This is a Daemon, wcdj\n"; char path[FILEPATH_MAX]; getRealPath(path); strcat(path,"/"); strcat(path,"pushserver.lock"); pid = readPidFile(path); if(pid != -1) { printf("The server is start ...\n"); } if(strcmp(cmd,"stop") == 0 && pid != -1){ closeServer(pid,0); int status; int w; do { w = waitpid(pid, &status, WUNTRACED | WCONTINUED); if (w == -1) { perror("waitpid"); exit(EXIT_FAILURE); } if (WIFEXITED(status)) { printf("exited, status=%d/n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("killed by signal %d/n", WTERMSIG(status)); } else if (WIFSTOPPED(status)) { printf("stopped by signal %d/n", WSTOPSIG(status)); } else if (WIFCONTINUED(status)) { printf("continued/n"); } } while (!WIFEXITED(status) && !WIFSIGNALED(status)); exit(0); } else if(strcmp(cmd,"restart") == 0 && pid != -1){ int status; int w; do { w = waitpid(pid, &status, WUNTRACED | WCONTINUED); if (w == -1) { perror("waitpid"); exit(EXIT_FAILURE); } if (WIFEXITED(status)) { printf("exited, status=%d/n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("killed by signal %d/n", WTERMSIG(status)); } else if (WIFSTOPPED(status)) { printf("stopped by signal %d/n", WSTOPSIG(status)); } else if (WIFCONTINUED(status)) { printf("continued/n"); } } while (!WIFEXITED(status) && !WIFSIGNALED(status)); }else if(pid != -1){ printf("The server is start ...\n"); exit(0); } /* 屏蔽一些有关控制终端操作的信号 * 防止在守护进程没有正常运转起来时,因控制终端受到干扰退出或挂起 */ signal(SIGINT, SIG_IGN);// 终端中断 signal(SIGHUP, SIG_IGN);// 连接挂断 signal(SIGQUIT, SIG_IGN);// 终端退出 signal(SIGPIPE, SIG_IGN);// 向无读进程的管道写数据 signal(SIGTTOU, SIG_IGN);// 后台程序尝试写操作 signal(SIGTTIN, SIG_IGN);// 后台程序尝试读操作 signal(SIGTERM, SIG_IGN);// 终止 struct rlimit rl; if (getrlimit(RLIMIT_NOFILE, &rl) < 0) printf("%s: can't get file limit", "pushserver"); int i; /* pid=fork(); if(pid>0) exit(0);//是父进程,结束父进程 else if(pid< 0) exit(1);//fork失败,退出 //是第一子进程,后台继续执行 setsid();//第一子进程成为新的会话组长和进程组长 //并与控制终端分离 pid=fork(); if(pid>0) exit(0);//是第一子进程,结束第一子进程 else if(pid< 0) exit(1);//fork失败,退出 */ if ((pid = fork()) < 0) printf("%s: can't fork", cmd); else if (pid != 0) /* parent */{ printf("tuichuzhu\n"); exit(0); } printf("jinlaile\n"); setsid(); //是第二子进程,继续 //第二子进程不再是会话组长 main_pid = getpid(); writePidFile(path); for(i=3;i< NOFILE;++i)//关闭打开的文件描述符 MAXFILE 65536 close(i); /* // [3] set current path char szPath[1024]; if(getcwd(szPath, sizeof(szPath)) == NULL) { perror("getcwd"); exit(1); } else { chdir(szPath); printf("set current path succ [%s]\n", szPath); } */ //chdir("/tmp");//改变工作目录到/tmp umask(0);//重设文件创建掩模 // [6] set termianl signal signal(SIGTERM, wait_close); signal(SIGHUP, restart_server); /* * Close all open file descriptors. */ if (rl.rlim_max == RLIM_INFINITY) rl.rlim_max = 1024; for (i = 0; i < rl.rlim_max; i++) close(i); /* * Attach file descriptors 0, 1, and 2 to /dev/null. */ int fd0 = open("/dev/null", O_RDWR); int fd1 = dup(0); int fd2 = dup(0); /* * Initialize the log file. */ openlog("pushserver", LOG_CONS, LOG_DAEMON); if (fd0 != 0 || fd1 != 1 || fd2 != 2) { syslog(LOG_ERR, "unexpected file descriptors %d %d %d",fd0, fd1, fd2); exit(1); } syslog(LOG_DEBUG, "daem ok "); return; }