自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Zabbix守護(hù)進(jìn)程例子的分析

運(yùn)維 系統(tǒng)運(yùn)維
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)程例子,貼出來,分析一下!

  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); //從密碼文件中取得指定帳號的數(shù)據(jù)  
  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) //設(shè)置真實組識別碼  
  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) //設(shè)置用戶識別碼  
  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.   {//重新設(shè)置當(dāng)前進(jìn)程的有效用戶,組識別碼  
  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 ) //創(chuàng)建進(jìn)程  
  98.  
  99.   {  
  100.  
  101.   exit( 0 );  
  102.  
  103.   }  
  104.  
  105.   setsid(); //創(chuàng)建一個新的對話期  
  106.  
  107.   signal( SIGHUP, SIG_IGN ); //設(shè)置信號  
  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("/")) //設(shè)置工作目錄  
  120.  
  121.   {  
  122.  
  123.   assert(0);  
  124.  
  125.   }  
  126.  
  127.   umask(0002); //設(shè)置新建文件的權(quán)限遮罩  
  128.  
  129.   redirect_std(CONFIG_LOG_FILE);  
  130.  
  131.   #ifdef HAVE_SYS_RESOURCE_SETPRIORITY  
  132.  
  133.   if(setpriority(PRIO_PROCESS,0,5)!=0) //設(shè)置程序進(jìn)程執(zhí)行優(yōu)先權(quán)  
  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(); //進(jìn)入開始調(diào)用的主函數(shù)  
  172.  
  173.   }  

  Zabbix守護(hù)進(jìn)程例子的分析就到這里了。下一節(jié):Linux下如何安裝Cacti

【編輯推薦】

監(jiān)控 Zabbix的應(yīng)用

Linux安裝zabbix網(wǎng)絡(luò)監(jiān)控系統(tǒng)

ZABBIX監(jiān)控的操作步驟

責(zé)任編輯:zhaolei 來源: chinaunix
相關(guān)推薦

2010-06-28 14:52:30

cron進(jìn)程

2017-04-11 16:00:40

Linuxsyslog進(jìn)程

2010-03-02 16:37:53

Linux Quagg

2010-03-16 13:41:09

Python進(jìn)程

2024-10-07 09:03:15

2009-11-24 11:35:59

2010-07-15 15:54:10

Perl守護(hù)進(jìn)程

2010-07-14 16:09:52

Telnet命令例子

2012-05-08 11:01:45

linux守護(hù)進(jìn)程

2013-01-15 15:18:46

Linux守護(hù)進(jìn)程

2024-08-29 13:23:04

WindowsGo語言

2010-07-15 15:47:46

Perl守護(hù)進(jìn)程

2011-08-08 10:02:55

iPhone開發(fā) 進(jìn)程 通信

2020-06-02 16:19:09

華為

2021-07-15 12:44:25

Shell編程進(jìn)程

2009-10-22 17:18:20

CLR觸發(fā)器

2022-07-19 07:41:09

Centos8操作系統(tǒng)Nginx進(jìn)程

2024-08-12 08:43:09

2011-10-11 09:50:28

UNIXBIRD

2009-04-21 09:12:45

Java多進(jìn)程運(yùn)行
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號