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