Zabbix守護進程例子的分析
作者:dagu
Zabbix是什么?Zabbix是一個基于WEB界面的提供分布式系統監視以及網絡監視功能的企業級的開源解決方案。zabbix能監視各種網絡參數,保證服務器系統的安全運營;本文講述的是Zabbix守護進程例子的分析。
zabbix守護進程例子分析:
很好的一個守護進程例子,貼出來,分析一下!
- /******************************************************************************
- * *
- * 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); //從密碼文件中取得指定帳號的數據
- 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) //設置真實組識別碼
- {
- 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) //設置用戶識別碼
- {
- 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) )
- {//重新設置當前進程的有效用戶,組識別碼
- zbx_error("Cannot setegid or seteuid to zabbix [%s].", strerror(errno));
- exit(FAIL);
- }
- #endif /* HAVE_FUNCTION_SETEUID */
- }
- if( (pid = zbx_fork()) != 0 ) //創建進程
- {
- exit( 0 );
- }
- setsid(); //創建一個新的對話期
- signal( SIGHUP, SIG_IGN ); //設置信號
- if( (pid = zbx_fork()) !=0 )
- {
- exit( 0 );
- }
- /* This is to eliminate warning: ignoring return value of chdir */
- if(-1 == chdir("/")) //設置工作目錄
- {
- assert(0);
- }
- umask(0002); //設置新建文件的權限遮罩
- redirect_std(CONFIG_LOG_FILE);
- #ifdef HAVE_SYS_RESOURCE_SETPRIORITY
- if(setpriority(PRIO_PROCESS,0,5)!=0) //設置程序進程執行優先權
- {
- 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(); //進入開始調用的主函數
- }
Zabbix守護進程例子的分析就到這里了。下一節:Linux下如何安裝Cacti
【編輯推薦】
責任編輯:zhaolei
來源:
chinaunix