You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.5 KiB

  1. --- a/include/common.h
  2. +++ b/include/common.h
  3. @@ -1083,4 +1083,7 @@ int parse_serveractive_element(char *str
  4. char *zbx_dyn_escape_shell_single_quote(const char *text);
  5. +#define ZBX_RUN_BACKGROUND 0
  6. +#define ZBX_RUN_FOREGROUND 1
  7. +
  8. #endif
  9. --- a/include/daemon.h
  10. +++ b/include/daemon.h
  11. @@ -28,7 +28,7 @@ extern char *CONFIG_PID_FILE;
  12. #include "threads.h"
  13. -int daemon_start(int allow_root, const char *user);
  14. +int daemon_start(int allow_root, const char *user, int run_foreground);
  15. void daemon_stop();
  16. int zbx_sigusr_send(int flags);
  17. @@ -36,6 +36,6 @@ int zbx_sigusr_send(int flags);
  18. #define ZBX_IS_RUNNING() 1
  19. #define ZBX_DO_EXIT()
  20. -#define START_MAIN_ZABBIX_ENTRY(a, u) daemon_start(a, u)
  21. +#define START_MAIN_ZABBIX_ENTRY(a, u, f) daemon_start(a, u, f)
  22. #endif /* ZABBIX_DAEMON_H */
  23. --- a/src/libs/zbxnix/daemon.c
  24. +++ b/src/libs/zbxnix/daemon.c
  25. @@ -272,16 +272,17 @@ static void set_daemon_signal_handlers()
  26. * *
  27. * Purpose: init process as daemon *
  28. * *
  29. - * Parameters: allow_root - allow root permission for application *
  30. - * user - user on the system to which to drop the *
  31. - * privileges *
  32. + * Parameters: allow_root - allow root permission for application *
  33. + * user - user on the system to which to drop the *
  34. + * privileges *
  35. + * run_foreground - should it close its controling tty *
  36. * *
  37. * Author: Alexei Vladishev *
  38. * *
  39. * Comments: it doesn't allow running under 'root' if allow_root is zero *
  40. * *
  41. ******************************************************************************/
  42. -int daemon_start(int allow_root, const char *user)
  43. +int daemon_start(int allow_root, const char *user, int run_foreground)
  44. {
  45. pid_t pid;
  46. struct passwd *pwd;
  47. @@ -336,15 +337,22 @@ int daemon_start(int allow_root, const c
  48. #endif
  49. }
  50. - if (0 != (pid = zbx_fork()))
  51. - exit(EXIT_SUCCESS);
  52. + if ( ZBX_RUN_FOREGROUND != run_foreground)
  53. + if (0 != (pid = zbx_fork()))
  54. + exit(EXIT_SUCCESS);
  55. setsid();
  56. signal(SIGHUP, SIG_IGN);
  57. - if (0 != (pid = zbx_fork()))
  58. - exit(EXIT_SUCCESS);
  59. + if ( ZBX_RUN_FOREGROUND == run_foreground) {
  60. + zabbix_log(LOG_LEVEL_INFORMATION, "Running in foreground...");
  61. + } else {
  62. + if (0 != (pid = zbx_fork()))
  63. + exit(EXIT_SUCCESS);
  64. + }
  65. +
  66. +
  67. if (-1 == chdir("/")) /* this is to eliminate warning: ignoring return value of chdir */
  68. assert(0);
  69. --- a/src/zabbix_agent/zabbix_agentd.c
  70. +++ b/src/zabbix_agent/zabbix_agentd.c
  71. @@ -62,6 +62,8 @@ const char *progname = NULL;
  72. static char DEFAULT_CONFIG_FILE[] = SYSCONFDIR "/zabbix_agentd.conf";
  73. #endif
  74. +int CONFIG_FOREGROUND = ZBX_RUN_BACKGROUND;
  75. +
  76. /* application TITLE */
  77. const char title_message[] = APPLICATION_NAME
  78. #if defined(_WIN64)
  79. @@ -93,6 +95,7 @@ const char usage_message[] =
  80. const char *help_message[] = {
  81. "Options:",
  82. " -c --config <config-file> Absolute path to the configuration file",
  83. + " -f --foreground Run in foreground don't fork",
  84. " -p --print Print known items and exit",
  85. " -t --test <item key> Test specified item and exit",
  86. " -h --help Display help information",
  87. @@ -127,6 +130,7 @@ const char *help_message[] = {
  88. /* COMMAND LINE OPTIONS */
  89. static struct zbx_option longopts[] =
  90. {
  91. + {"foreground", 0, NULL, 'f'},
  92. {"config", 1, NULL, 'c'},
  93. {"help", 0, NULL, 'h'},
  94. {"version", 0, NULL, 'V'},
  95. @@ -147,7 +151,7 @@ static struct zbx_option longopts[] =
  96. };
  97. static char shortopts[] =
  98. - "c:hVpt:"
  99. + "c:hfVpt:"
  100. #ifndef _WINDOWS
  101. "R:"
  102. #else
  103. @@ -241,6 +245,9 @@ static void parse_commandline(int argc,
  104. {
  105. switch (ch)
  106. {
  107. + case 'f':
  108. + CONFIG_FOREGROUND = ZBX_RUN_FOREGROUND;
  109. + break;
  110. case 'c':
  111. CONFIG_FILE = strdup(zbx_optarg);
  112. break;
  113. @@ -944,7 +951,7 @@ int main(int argc, char **argv)
  114. break;
  115. }
  116. - START_MAIN_ZABBIX_ENTRY(CONFIG_ALLOW_ROOT, CONFIG_USER);
  117. + START_MAIN_ZABBIX_ENTRY(CONFIG_ALLOW_ROOT, CONFIG_USER, CONFIG_FOREGROUND);
  118. exit(EXIT_SUCCESS);
  119. }
  120. --- a/src/zabbix_proxy/proxy.c
  121. +++ b/src/zabbix_proxy/proxy.c
  122. @@ -60,6 +60,7 @@ const char usage_message[] = "[-hV] [-c
  123. const char *help_message[] = {
  124. "Options:",
  125. + " -f --foreground Run in foreground don't fork",
  126. " -c --config <file> Absolute path to the configuration file",
  127. " -R --runtime-control <option> Perform administrative functions",
  128. "",
  129. @@ -84,6 +85,7 @@ const char *help_message[] = {
  130. /* long options */
  131. static struct zbx_option longopts[] =
  132. {
  133. + {"foreground", 0, NULL, 'f'},
  134. {"config", 1, NULL, 'c'},
  135. {"runtime-control", 1, NULL, 'R'},
  136. {"help", 0, NULL, 'h'},
  137. @@ -92,7 +94,7 @@ static struct zbx_option longopts[] =
  138. };
  139. /* short options */
  140. -static char shortopts[] = "c:n:hVR:";
  141. +static char shortopts[] = "c:n:fhVR:";
  142. /* end of COMMAND LINE OPTIONS */
  143. @@ -202,6 +204,7 @@ char *CONFIG_LOAD_MODULE_PATH = NULL;
  144. char **CONFIG_LOAD_MODULE = NULL;
  145. char *CONFIG_USER = NULL;
  146. +int CONFIG_FOREGROUND = ZBX_RUN_BACKGROUND;
  147. /* web monitoring */
  148. #ifdef HAVE_LIBCURL
  149. @@ -666,6 +669,9 @@ int main(int argc, char **argv)
  150. {
  151. switch (ch)
  152. {
  153. + case 'f':
  154. + CONFIG_FOREGROUND = ZBX_RUN_FOREGROUND;
  155. + break;
  156. case 'c':
  157. CONFIG_FILE = zbx_strdup(CONFIG_FILE, zbx_optarg);
  158. break;
  159. @@ -705,7 +711,7 @@ int main(int argc, char **argv)
  160. init_ipmi_handler();
  161. #endif
  162. - return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER);
  163. + return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER, CONFIG_FOREGROUND);
  164. }
  165. int MAIN_ZABBIX_ENTRY()
  166. --- a/src/zabbix_server/server.c
  167. +++ b/src/zabbix_server/server.c
  168. @@ -64,6 +64,7 @@ const char usage_message[] = "[-hV] [-c
  169. const char *help_message[] = {
  170. "Options:",
  171. + " -f --foreground Run in foreground don't fork",
  172. " -c --config <file> Absolute path to the configuration file",
  173. " -R --runtime-control <option> Perform administrative functions",
  174. "",
  175. @@ -88,6 +89,7 @@ const char *help_message[] = {
  176. /* long options */
  177. static struct zbx_option longopts[] =
  178. {
  179. + {"foreground", 0, NULL, 'f'},
  180. {"config", 1, NULL, 'c'},
  181. {"runtime-control", 1, NULL, 'R'},
  182. {"help", 0, NULL, 'h'},
  183. @@ -96,7 +98,7 @@ static struct zbx_option longopts[] =
  184. };
  185. /* short options */
  186. -static char shortopts[] = "c:n:hVR:";
  187. +static char shortopts[] = "c:n:fhVR:";
  188. /* end of COMMAND LINE OPTIONS */
  189. @@ -197,6 +199,7 @@ char *CONFIG_LOAD_MODULE_PATH = NULL;
  190. char **CONFIG_LOAD_MODULE = NULL;
  191. char *CONFIG_USER = NULL;
  192. +int CONFIG_FOREGROUND = ZBX_RUN_BACKGROUND;
  193. /* web monitoring */
  194. #ifdef HAVE_LIBCURL
  195. @@ -631,6 +634,9 @@ int main(int argc, char **argv)
  196. {
  197. switch (ch)
  198. {
  199. + case 'f':
  200. + CONFIG_FOREGROUND = ZBX_RUN_FOREGROUND;
  201. + break;
  202. case 'c':
  203. CONFIG_FILE = zbx_strdup(CONFIG_FILE, zbx_optarg);
  204. break;
  205. @@ -670,7 +676,7 @@ int main(int argc, char **argv)
  206. init_ipmi_handler();
  207. #endif
  208. - return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER);
  209. + return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER, CONFIG_FOREGROUND);
  210. }
  211. int MAIN_ZABBIX_ENTRY()