3 @@ -1083,4 +1083,7 @@ int parse_serveractive_element(char *str
5 char *zbx_dyn_escape_shell_single_quote(const char *text);
7 +#define ZBX_RUN_BACKGROUND 0
8 +#define ZBX_RUN_FOREGROUND 1
11 --- a/include/daemon.h
12 +++ b/include/daemon.h
13 @@ -28,7 +28,7 @@ extern char *CONFIG_PID_FILE;
17 -int daemon_start(int allow_root, const char *user);
18 +int daemon_start(int allow_root, const char *user, int run_foreground);
21 int zbx_sigusr_send(int flags);
22 @@ -36,6 +36,6 @@ int zbx_sigusr_send(int flags);
23 #define ZBX_IS_RUNNING() 1
26 -#define START_MAIN_ZABBIX_ENTRY(a, u) daemon_start(a, u)
27 +#define START_MAIN_ZABBIX_ENTRY(a, u, f) daemon_start(a, u, f)
29 #endif /* ZABBIX_DAEMON_H */
30 --- a/src/libs/zbxnix/daemon.c
31 +++ b/src/libs/zbxnix/daemon.c
32 @@ -272,16 +272,17 @@ static void set_daemon_signal_handlers()
34 * Purpose: init process as daemon *
36 - * Parameters: allow_root - allow root permission for application *
37 - * user - user on the system to which to drop the *
39 + * Parameters: allow_root - allow root permission for application *
40 + * user - user on the system to which to drop the *
42 + * run_foreground - should it close its controling tty *
44 * Author: Alexei Vladishev *
46 * Comments: it doesn't allow running under 'root' if allow_root is zero *
48 ******************************************************************************/
49 -int daemon_start(int allow_root, const char *user)
50 +int daemon_start(int allow_root, const char *user, int run_foreground)
54 @@ -336,15 +337,22 @@ int daemon_start(int allow_root, const c
58 - if (0 != (pid = zbx_fork()))
60 + if ( ZBX_RUN_FOREGROUND != run_foreground)
61 + if (0 != (pid = zbx_fork()))
66 signal(SIGHUP, SIG_IGN);
68 - if (0 != (pid = zbx_fork()))
70 + if ( ZBX_RUN_FOREGROUND == run_foreground) {
71 + zabbix_log(LOG_LEVEL_INFORMATION, "Running in foreground...");
73 + if (0 != (pid = zbx_fork()))
79 if (-1 == chdir("/")) /* this is to eliminate warning: ignoring return value of chdir */
81 --- a/src/zabbix_agent/zabbix_agentd.c
82 +++ b/src/zabbix_agent/zabbix_agentd.c
83 @@ -62,6 +62,8 @@ const char *progname = NULL;
84 static char DEFAULT_CONFIG_FILE[] = SYSCONFDIR "/zabbix_agentd.conf";
87 +int CONFIG_FOREGROUND = ZBX_RUN_BACKGROUND;
89 /* application TITLE */
90 const char title_message[] = APPLICATION_NAME
92 @@ -93,6 +95,7 @@ const char usage_message[] =
93 const char *help_message[] = {
95 " -c --config <config-file> Absolute path to the configuration file",
96 + " -f --foreground Run in foreground don't fork",
97 " -p --print Print known items and exit",
98 " -t --test <item key> Test specified item and exit",
99 " -h --help Display help information",
100 @@ -127,6 +130,7 @@ const char *help_message[] = {
101 /* COMMAND LINE OPTIONS */
102 static struct zbx_option longopts[] =
104 + {"foreground", 0, NULL, 'f'},
105 {"config", 1, NULL, 'c'},
106 {"help", 0, NULL, 'h'},
107 {"version", 0, NULL, 'V'},
108 @@ -147,7 +151,7 @@ static struct zbx_option longopts[] =
111 static char shortopts[] =
117 @@ -241,6 +245,9 @@ static void parse_commandline(int argc,
122 + CONFIG_FOREGROUND = ZBX_RUN_FOREGROUND;
125 CONFIG_FILE = strdup(zbx_optarg);
127 @@ -944,7 +951,7 @@ int main(int argc, char **argv)
131 - START_MAIN_ZABBIX_ENTRY(CONFIG_ALLOW_ROOT, CONFIG_USER);
132 + START_MAIN_ZABBIX_ENTRY(CONFIG_ALLOW_ROOT, CONFIG_USER, CONFIG_FOREGROUND);
136 --- a/src/zabbix_proxy/proxy.c
137 +++ b/src/zabbix_proxy/proxy.c
138 @@ -60,6 +60,7 @@ const char usage_message[] = "[-hV] [-c
140 const char *help_message[] = {
142 + " -f --foreground Run in foreground don't fork",
143 " -c --config <file> Absolute path to the configuration file",
144 " -R --runtime-control <option> Perform administrative functions",
146 @@ -84,6 +85,7 @@ const char *help_message[] = {
148 static struct zbx_option longopts[] =
150 + {"foreground", 0, NULL, 'f'},
151 {"config", 1, NULL, 'c'},
152 {"runtime-control", 1, NULL, 'R'},
153 {"help", 0, NULL, 'h'},
154 @@ -92,7 +94,7 @@ static struct zbx_option longopts[] =
158 -static char shortopts[] = "c:n:hVR:";
159 +static char shortopts[] = "c:n:fhVR:";
161 /* end of COMMAND LINE OPTIONS */
163 @@ -202,6 +204,7 @@ char *CONFIG_LOAD_MODULE_PATH = NULL;
164 char **CONFIG_LOAD_MODULE = NULL;
166 char *CONFIG_USER = NULL;
167 +int CONFIG_FOREGROUND = ZBX_RUN_BACKGROUND;
171 @@ -666,6 +669,9 @@ int main(int argc, char **argv)
176 + CONFIG_FOREGROUND = ZBX_RUN_FOREGROUND;
179 CONFIG_FILE = zbx_strdup(CONFIG_FILE, zbx_optarg);
181 @@ -705,7 +711,7 @@ int main(int argc, char **argv)
185 - return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER);
186 + return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER, CONFIG_FOREGROUND);
189 int MAIN_ZABBIX_ENTRY()
190 --- a/src/zabbix_server/server.c
191 +++ b/src/zabbix_server/server.c
192 @@ -64,6 +64,7 @@ const char usage_message[] = "[-hV] [-c
194 const char *help_message[] = {
196 + " -f --foreground Run in foreground don't fork",
197 " -c --config <file> Absolute path to the configuration file",
198 " -R --runtime-control <option> Perform administrative functions",
200 @@ -88,6 +89,7 @@ const char *help_message[] = {
202 static struct zbx_option longopts[] =
204 + {"foreground", 0, NULL, 'f'},
205 {"config", 1, NULL, 'c'},
206 {"runtime-control", 1, NULL, 'R'},
207 {"help", 0, NULL, 'h'},
208 @@ -96,7 +98,7 @@ static struct zbx_option longopts[] =
212 -static char shortopts[] = "c:n:hVR:";
213 +static char shortopts[] = "c:n:fhVR:";
215 /* end of COMMAND LINE OPTIONS */
217 @@ -197,6 +199,7 @@ char *CONFIG_LOAD_MODULE_PATH = NULL;
218 char **CONFIG_LOAD_MODULE = NULL;
220 char *CONFIG_USER = NULL;
221 +int CONFIG_FOREGROUND = ZBX_RUN_BACKGROUND;
225 @@ -631,6 +634,9 @@ int main(int argc, char **argv)
230 + CONFIG_FOREGROUND = ZBX_RUN_FOREGROUND;
233 CONFIG_FILE = zbx_strdup(CONFIG_FILE, zbx_optarg);
235 @@ -670,7 +676,7 @@ int main(int argc, char **argv)
239 - return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER);
240 + return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER, CONFIG_FOREGROUND);
243 int MAIN_ZABBIX_ENTRY()