Zabbix守護(hù)進(jìn)程例子的分析
作者:dagu
Zabbix是什么?Zabbix是一個基于WEB界面的提供分布式系統(tǒng)監(jiān)視以及網(wǎng)絡(luò)監(jiān)視功能的企業(yè)級的開源解決方案。zabbix能監(jiān)視各種網(wǎng)絡(luò)參數(shù),保證服務(wù)器系統(tǒng)的安全運(yùn)營;本文講述的是Zabbix守護(hù)進(jìn)程例子的分析。
zabbix守護(hù)進(jìn)程例子分析:
很好的一個守護(hù)進(jìn)程例子,貼出來,分析一下!
- /******************************************************************************
- * *
- * Function: daemon_start *
- * *
- * Purpose: init process as daemon *
- * *
- * Parameters: allow_root - allow root permision for application *
- * *
- * Return value: *
- * *
- * Author: Alexei Vladishev *
- * *
- * Comments: it doesn't allow running under 'root' if allow_root is zero *
- * *
******************************************************************************/
- int daemon_start(int allow_root)
- {
- pid_t pid;
- struct passwd *pwd;
- struct sigaction phan;
- char user[7] = "zabbix";
- /* running as root ?*/
- if((0 == allow_root) && (0 == getuid() || 0 == getgid()))
- {
- pwd = getpwnam(user); //從密碼文件中取得指定帳號的數(shù)據(jù)
- if (NULL == pwd)
- {
- zbx_error("User %s does not exist.",
- user);
- zbx_error("Cannot run as root !");
- exit(FAIL);
- }
- if(setgid(pwd->pw_gid) ==-1) //設(shè)置真實組識別碼
- {
- zbx_error("Cannot setgid to %s [%s].",
- user,
- strerror(errno));
- exit(FAIL);
- }
- #ifdef HAVE_FUNCTION_INITGROUPS
- if(initgroups(user, pwd->pw_gid) == -1) //初始化組清單,/etc/group中
- {
- zbx_error("Cannot initgroups to %s [%s].",
- user,
- strerror(errno));
- exit(FAIL);
- }
- #endif /* HAVE_FUNCTION_INITGROUPS */
- if(setuid(pwd->pw_uid) == -1) //設(shè)置用戶識別碼
- {
- zbx_error("Cannot setuid to %s [%s].",
- user,
- strerror(errno));
- exit(FAIL);
- }
- #ifdef HAVE_FUNCTION_SETEUID
- if( (setegid(pwd->pw_gid) ==-1) || (seteuid(pwd->pw_uid) == -1) )
- {//重新設(shè)置當(dāng)前進(jìn)程的有效用戶,組識別碼
- zbx_error("Cannot setegid or seteuid to zabbix [%s].", strerror(errno));
- exit(FAIL);
- }
- #endif /* HAVE_FUNCTION_SETEUID */
- }
- if( (pid = zbx_fork()) != 0 ) //創(chuàng)建進(jìn)程
- {
- exit( 0 );
- }
- setsid(); //創(chuàng)建一個新的對話期
- signal( SIGHUP, SIG_IGN ); //設(shè)置信號
- if( (pid = zbx_fork()) !=0 )
- {
- exit( 0 );
- }
- /* This is to eliminate warning: ignoring return value of chdir */
- if(-1 == chdir("/")) //設(shè)置工作目錄
- {
- assert(0);
- }
- umask(0002); //設(shè)置新建文件的權(quán)限遮罩
- redirect_std(CONFIG_LOG_FILE);
- #ifdef HAVE_SYS_RESOURCE_SETPRIORITY
- if(setpriority(PRIO_PROCESS,0,5)!=0) //設(shè)置程序進(jìn)程執(zhí)行優(yōu)先權(quán)
- {
- zbx_error("Unable to set process priority to 5. Leaving default.");
- }
- #endif /* HAVE_SYS_RESOURCE_SETPRIORITY */
- /*------------------------------------------------*/
- if( FAIL == create_pid_file(APP_PID_FILE)) //pid 文件
- {
- exit(FAIL);
- }
- /* phan.sa_handler = child_signal_handler;*/
- phan.sa_sigaction = child_signal_handler;
- sigemptyset(&phan.sa_mask);
- phan.sa_flags = SA_SIGINFO;
- sigaction(SIGINT, &phan, NULL);
- sigaction(SIGQUIT, &phan, NULL);
- sigaction(SIGTERM, &phan, NULL);
- sigaction(SIGPIPE, &phan, NULL);
- zbx_setproctitle("main process");
- return MAIN_ZABBIX_ENTRY(); //進(jìn)入開始調(diào)用的主函數(shù)
- }
Zabbix守護(hù)進(jìn)程例子的分析就到這里了。下一節(jié):Linux下如何安裝Cacti
【編輯推薦】
責(zé)任編輯:zhaolei
來源:
chinaunix