Zabbix守护进程例子的分析

运维 系统运维
Zabbix是什么?Zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。zabbix能监视各种网络参数,保证服务器系统的安全运营;本文讲述的是Zabbix守护进程例子的分析。

zabbix守护进程例子分析:

  很好的一个守护进程例子,贴出来,分析一下!

  1.   /******************************************************************************  
  2.  
  3.   * *  
  4.  
  5.   * Function: daemon_start *  
  6.  
  7.   * *  
  8.  
  9.   * Purpose: init process as daemon *  
  10.  
  11.   * *  
  12.  
  13.   * Parameters: allow_root - allow root permision for application *  
  14.  
  15.   * *  
  16.  
  17.   * Return value: *  
  18.  
  19.   * *  
  20.  
  21.   * Author: Alexei Vladishev *  
  22.  
  23.   * *  
  24.  
  25.   * Comments: it doesn't allow running under 'root' if allow_root is zero *  
  26.  
  27.   * *  
  28.  

  ******************************************************************************/

  1.   int daemon_start(int allow_root)  
  2.  
  3.   {  
  4.  
  5.   pid_t pid;  
  6.  
  7.   struct passwd *pwd;  
  8.  
  9.   struct sigaction phan;  
  10.  
  11.   char user[7] = "zabbix";  
  12.  
  13.   /* running as root ?*/  
  14.  
  15.   if((0 == allow_root) && (0 == getuid() || 0 == getgid()))  
  16.  
  17.   {  
  18.  
  19.   pwd = getpwnam(user); //从密码文件中取得指定帐号的数据  
  20.  
  21.   if (NULL == pwd)  
  22.  
  23.   {  
  24.  
  25.   zbx_error("User %s does not exist.",  
  26.  
  27.   user);  
  28.  
  29.   zbx_error("Cannot run as root !");  
  30.  
  31.   exit(FAIL);  
  32.  
  33.   }  
  34.  
  35.   if(setgid(pwd->pw_gid) ==-1) //设置真实组识别码  
  36.  
  37.   {  
  38.  
  39.   zbx_error("Cannot setgid to %s [%s].",  
  40.  
  41.   user,  
  42.  
  43.   strerror(errno));  
  44.  
  45.   exit(FAIL);  
  46.  
  47.   }  
  48.  
  49.   #ifdef HAVE_FUNCTION_INITGROUPS  
  50.  
  51.   if(initgroups(user, pwd->pw_gid) == -1) //初始化组清单,/etc/group中  
  52.  
  53.   {  
  54.  
  55.   zbx_error("Cannot initgroups to %s [%s].",  
  56.  
  57.   user,  
  58.  
  59.   strerror(errno));  
  60.  
  61.   exit(FAIL);  
  62.  
  63.   }  
  64.  
  65.   #endif /* HAVE_FUNCTION_INITGROUPS */  
  66.  
  67.   if(setuid(pwd->pw_uid) == -1) //设置用户识别码  
  68.  
  69.   {  
  70.  
  71.   zbx_error("Cannot setuid to %s [%s].",  
  72.  
  73.   user,  
  74.  
  75.   strerror(errno));  
  76.  
  77.   exit(FAIL);  
  78.  
  79.   }  
  80.  
  81.   #ifdef HAVE_FUNCTION_SETEUID  
  82.  
  83.   if( (setegid(pwd->pw_gid) ==-1) || (seteuid(pwd->pw_uid) == -1) )  
  84.  
  85.   {//重新设置当前进程的有效用户,组识别码  
  86.  
  87.   zbx_error("Cannot setegid or seteuid to zabbix [%s].", strerror(errno));  
  88.  
  89.   exit(FAIL);  
  90.  
  91.   }  
  92.  
  93.   #endif /* HAVE_FUNCTION_SETEUID */  
  94.  
  95.   }  
  96.  
  97.   if( (pid = zbx_fork()) != 0 ) //创建进程  
  98.  
  99.   {  
  100.  
  101.   exit( 0 );  
  102.  
  103.   }  
  104.  
  105.   setsid(); //创建一个新的对话期  
  106.  
  107.   signal( SIGHUP, SIG_IGN ); //设置信号  
  108.  
  109.   if( (pid = zbx_fork()) !=0 )  
  110.  
  111.   {  
  112.  
  113.   exit( 0 );  
  114.  
  115.   }  
  116.  
  117.   /* This is to eliminate warning: ignoring return value of chdir */  
  118.  
  119.   if(-1 == chdir("/")) //设置工作目录  
  120.  
  121.   {  
  122.  
  123.   assert(0);  
  124.  
  125.   }  
  126.  
  127.   umask(0002); //设置新建文件的权限遮罩  
  128.  
  129.   redirect_std(CONFIG_LOG_FILE);  
  130.  
  131.   #ifdef HAVE_SYS_RESOURCE_SETPRIORITY  
  132.  
  133.   if(setpriority(PRIO_PROCESS,0,5)!=0) //设置程序进程执行优先权  
  134.  
  135.   {  
  136.  
  137.   zbx_error("Unable to set process priority to 5. Leaving default.");  
  138.  
  139.   }  
  140.  
  141.   #endif /* HAVE_SYS_RESOURCE_SETPRIORITY */  
  142.  
  143.   /*------------------------------------------------*/  
  144.  
  145.   if( FAIL == create_pid_file(APP_PID_FILE)) //pid 文件  
  146.  
  147.   {  
  148.  
  149.   exit(FAIL);  
  150.  
  151.   }  
  152.  
  153.   /* phan.sa_handler = child_signal_handler;*/  
  154.  
  155.   phan.sa_sigaction = child_signal_handler;  
  156.  
  157.   sigemptyset(&phan.sa_mask);  
  158.  
  159.   phan.sa_flags = SA_SIGINFO;  
  160.  
  161.   sigaction(SIGINT, &phan, NULL);  
  162.  
  163.   sigaction(SIGQUIT, &phan, NULL);  
  164.  
  165.   sigaction(SIGTERM, &phan, NULL);  
  166.  
  167.   sigaction(SIGPIPE, &phan, NULL);  
  168.  
  169.   zbx_setproctitle("main process");  
  170.  
  171.   return MAIN_ZABBIX_ENTRY(); //进入开始调用的主函数  
  172.  
  173.   }  

  Zabbix守护进程例子的分析就到这里了。下一节:Linux下如何安装Cacti

【编辑推荐】

监控 Zabbix的应用

Linux安装zabbix网络监控系统

ZABBIX监控的操作步骤

责任编辑:zhaolei 来源: chinaunix
相关推荐

2010-06-28 14:52:30

cron进程

2010-03-02 16:37:53

Linux Quagg

2017-04-11 16:00:40

Linuxsyslog进程

2010-03-16 13:41:09

Python进程

2009-11-24 11:35:59

2010-07-15 15:54:10

Perl守护进程

2013-01-15 15:18:46

Linux守护进程

2012-05-08 11:01:45

linux守护进程

2010-07-14 16:09:52

Telnet命令例子

2010-07-15 15:47:46

Perl守护进程

2011-08-08 10:02:55

iPhone开发 进程 通信

2020-06-02 16:19:09

华为

2021-07-15 12:44:25

Shell编程进程

2009-10-22 17:18:20

CLR触发器

2022-07-19 07:41:09

Centos8操作系统Nginx进程

2011-10-11 09:50:28

UNIXBIRD

2011-03-29 13:25:12

ZabbixNagiosZenoss

2009-04-21 09:12:45

Java多进程运行

2020-12-10 08:04:55

Docker注册表安全

2011-03-29 14:15:29

Zabbix
点赞
收藏

51CTO技术栈公众号