1. 程式人生 > >APUE:守護進程

APUE:守護進程

iat ant immediate alert umask edi 信號 修改 程序

  1. 重置文件屏蔽字 umask(0)
  2. fork(),父進程 exit(0) 以響應啟動者
  3. setsid(),斷開所有控制終端
  4. 修改工作目錄為 /
  5. 關閉文件描述符
  6. 將 0、1、2 重定位到 /dev/null
  7. 檢測是否已經存在相同的守護進程正在運行,對 /var/run/xxxd.pid 進行 lockfile()
  8. 恢復 SIGHUP 的信號處理程序為默認 SIG_DFL
  9. 主線程調用 pthread_sigmask() 屏蔽所有信號
  10. 開新線程,使用 sigwait() 等待信號,遇到 SIGHUP 重讀配置文件,遇到 SIGTERM 終止
  11. 主線程完成其他工作

寫入日誌:

#define	LOG_EMERG	0	/* system is unusable */
#define	LOG_ALERT	1	/* action must be taken immediately */
#define	LOG_CRIT	2	/* critical conditions */
#define	LOG_ERR		3	/* error conditions */
#define	LOG_WARNING	4	/* warning conditions */
#define	LOG_NOTICE	5	/* normal but significant condition */
#define	LOG_INFO	6	/* informational */
#define	LOG_DEBUG	7	/* debug-level messages */

int syslog (int __pri, const char *__fmt, ...);

APUE:守護進程