APUE:守護進程
阿新 • • 發佈:2017-05-27
iat ant immediate alert umask edi 信號 修改 程序
- 重置文件屏蔽字 umask(0)
- fork(),父進程 exit(0) 以響應啟動者
- setsid(),斷開所有控制終端
- 修改工作目錄為 /
- 關閉文件描述符
- 將 0、1、2 重定位到 /dev/null
- 檢測是否已經存在相同的守護進程正在運行,對 /var/run/xxxd.pid 進行 lockfile()
- 恢復 SIGHUP 的信號處理程序為默認 SIG_DFL
- 主線程調用 pthread_sigmask() 屏蔽所有信號
- 開新線程,使用 sigwait() 等待信號,遇到 SIGHUP 重讀配置文件,遇到 SIGTERM 終止
- 主線程完成其他工作
寫入日誌:
#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:守護進程