1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include "common.h"
21 
22 #ifdef HAVE_SQLITE3
23 #	error SQLite is not supported as a main Zabbix database backend.
24 #endif
25 
26 #include "cfg.h"
27 #include "pid.h"
28 #include "db.h"
29 #include "dbcache.h"
30 #include "zbxdbupgrade.h"
31 #include "log.h"
32 #include "zbxgetopt.h"
33 #include "mutexs.h"
34 
35 #include "sysinfo.h"
36 #include "zbxmodules.h"
37 #include "zbxserver.h"
38 
39 #include "zbxnix.h"
40 #include "daemon.h"
41 #include "zbxself.h"
42 #include "../libs/zbxnix/control.h"
43 
44 #include "alerter/alerter.h"
45 #include "alerter/alert_manager.h"
46 #include "alerter/alert_syncer.h"
47 #include "dbsyncer/dbsyncer.h"
48 #include "dbconfig/dbconfig.h"
49 #include "discoverer/discoverer.h"
50 #include "httppoller/httppoller.h"
51 #include "housekeeper/housekeeper.h"
52 #include "pinger/pinger.h"
53 #include "poller/poller.h"
54 #include "timer/timer.h"
55 #include "trapper/trapper.h"
56 #include "snmptrapper/snmptrapper.h"
57 #include "escalator/escalator.h"
58 #include "proxypoller/proxypoller.h"
59 #include "selfmon/selfmon.h"
60 #include "vmware/vmware.h"
61 #include "taskmanager/taskmanager.h"
62 #include "preprocessor/preproc_manager.h"
63 #include "preprocessor/preproc_worker.h"
64 #include "lld/lld_manager.h"
65 #include "lld/lld_worker.h"
66 #include "events.h"
67 #include "../libs/zbxdbcache/valuecache.h"
68 #include "setproctitle.h"
69 #include "zbxcrypto.h"
70 #include "zbxipcservice.h"
71 #include "zbxhistory.h"
72 #include "postinit.h"
73 #include "export.h"
74 #include "zbxdiag.h"
75 
76 #ifdef HAVE_OPENIPMI
77 #include "ipmi/ipmi_manager.h"
78 #include "ipmi/ipmi_poller.h"
79 #endif
80 
81 const char	*progname = NULL;
82 const char	title_message[] = "zabbix_server";
83 const char	syslog_app_name[] = "zabbix_server";
84 const char	*usage_message[] = {
85 	"[-c config-file]", NULL,
86 	"[-c config-file]", "-R runtime-option", NULL,
87 	"-h", NULL,
88 	"-V", NULL,
89 	NULL	/* end of text */
90 };
91 
92 const char	*help_message[] = {
93 	"The core daemon of Zabbix software.",
94 	"",
95 	"Options:",
96 	"  -c --config config-file        Path to the configuration file",
97 	"                                 (default: \"" DEFAULT_CONFIG_FILE "\")",
98 	"  -f --foreground                Run Zabbix server in foreground",
99 	"  -R --runtime-control runtime-option   Perform administrative functions",
100 	"",
101 	"    Runtime control options:",
102 	"      " ZBX_CONFIG_CACHE_RELOAD "        Reload configuration cache",
103 	"      " ZBX_HOUSEKEEPER_EXECUTE "        Execute the housekeeper",
104 	"      " ZBX_LOG_LEVEL_INCREASE "=target  Increase log level, affects all processes if",
105 	"                                 target is not specified",
106 	"      " ZBX_LOG_LEVEL_DECREASE "=target  Decrease log level, affects all processes if",
107 	"                                 target is not specified",
108 	"      " ZBX_SNMP_CACHE_RELOAD "          Reload SNMP cache",
109 	"      " ZBX_DIAGINFO "=section           Log internal diagnostic information of the",
110 	"                                 section (historycache, preprocessing, alerting,",
111 	"                                 lld, valuecache, locks) or everything if section is",
112 	"                                 not specified",
113 	"",
114 	"      Log level control targets:",
115 	"        process-type             All processes of specified type",
116 	"                                 (alerter, alert manager, configuration syncer,",
117 	"                                 discoverer, escalator, history syncer,",
118 	"                                 housekeeper, http poller, icmp pinger,",
119 	"                                 ipmi manager, ipmi poller, java poller,",
120 	"                                 poller, preprocessing manager,",
121 	"                                 preprocessing worker, proxy poller,",
122 	"                                 self-monitoring, snmp trapper, task manager,",
123 	"                                 timer, trapper, unreachable poller,",
124 	"                                 vmware collector)",
125 	"        process-type,N           Process type and number (e.g., poller,3)",
126 	"        pid                      Process identifier, up to 65535. For larger",
127 	"                                 values specify target as \"process-type,N\"",
128 	"",
129 	"  -h --help                      Display this help message",
130 	"  -V --version                   Display version number",
131 	"",
132 	"Some configuration parameter default locations:",
133 	"  AlertScriptsPath               \"" DEFAULT_ALERT_SCRIPTS_PATH "\"",
134 	"  ExternalScripts                \"" DEFAULT_EXTERNAL_SCRIPTS_PATH "\"",
135 #ifdef HAVE_LIBCURL
136 	"  SSLCertLocation                \"" DEFAULT_SSL_CERT_LOCATION "\"",
137 	"  SSLKeyLocation                 \"" DEFAULT_SSL_KEY_LOCATION "\"",
138 #endif
139 	"  LoadModulePath                 \"" DEFAULT_LOAD_MODULE_PATH "\"",
140 	NULL	/* end of text */
141 };
142 
143 /* COMMAND LINE OPTIONS */
144 
145 /* long options */
146 static struct zbx_option	longopts[] =
147 {
148 	{"config",		1,	NULL,	'c'},
149 	{"foreground",		0,	NULL,	'f'},
150 	{"runtime-control",	1,	NULL,	'R'},
151 	{"help",		0,	NULL,	'h'},
152 	{"version",		0,	NULL,	'V'},
153 	{NULL}
154 };
155 
156 /* short options */
157 static char	shortopts[] = "c:hVR:f";
158 
159 /* end of COMMAND LINE OPTIONS */
160 
161 int		threads_num = 0;
162 pid_t		*threads = NULL;
163 static int	*threads_flags;
164 
165 unsigned char	program_type		= ZBX_PROGRAM_TYPE_SERVER;
166 unsigned char	process_type		= ZBX_PROCESS_TYPE_UNKNOWN;
167 int		process_num		= 0;
168 int		server_num		= 0;
169 
170 int	CONFIG_ALERTER_FORKS		= 3;
171 int	CONFIG_DISCOVERER_FORKS		= 1;
172 int	CONFIG_HOUSEKEEPER_FORKS	= 1;
173 int	CONFIG_PINGER_FORKS		= 1;
174 int	CONFIG_POLLER_FORKS		= 5;
175 int	CONFIG_UNREACHABLE_POLLER_FORKS	= 1;
176 int	CONFIG_HTTPPOLLER_FORKS		= 1;
177 int	CONFIG_IPMIPOLLER_FORKS		= 0;
178 int	CONFIG_TIMER_FORKS		= 1;
179 int	CONFIG_TRAPPER_FORKS		= 5;
180 int	CONFIG_SNMPTRAPPER_FORKS	= 0;
181 int	CONFIG_JAVAPOLLER_FORKS		= 0;
182 int	CONFIG_ESCALATOR_FORKS		= 1;
183 int	CONFIG_SELFMON_FORKS		= 1;
184 int	CONFIG_DATASENDER_FORKS		= 0;
185 int	CONFIG_HEARTBEAT_FORKS		= 0;
186 int	CONFIG_COLLECTOR_FORKS		= 0;
187 int	CONFIG_PASSIVE_FORKS		= 0;
188 int	CONFIG_ACTIVE_FORKS		= 0;
189 int	CONFIG_TASKMANAGER_FORKS	= 1;
190 int	CONFIG_IPMIMANAGER_FORKS	= 0;
191 int	CONFIG_ALERTMANAGER_FORKS	= 1;
192 int	CONFIG_PREPROCMAN_FORKS		= 1;
193 int	CONFIG_PREPROCESSOR_FORKS	= 3;
194 int	CONFIG_LLDMANAGER_FORKS		= 1;
195 int	CONFIG_LLDWORKER_FORKS		= 2;
196 int	CONFIG_ALERTDB_FORKS		= 1;
197 
198 int	CONFIG_LISTEN_PORT		= ZBX_DEFAULT_SERVER_PORT;
199 char	*CONFIG_LISTEN_IP		= NULL;
200 char	*CONFIG_SOURCE_IP		= NULL;
201 int	CONFIG_TRAPPER_TIMEOUT		= 300;
202 char	*CONFIG_SERVER			= NULL;		/* not used in zabbix_server, required for linking */
203 
204 int	CONFIG_HOUSEKEEPING_FREQUENCY	= 1;
205 int	CONFIG_MAX_HOUSEKEEPER_DELETE	= 5000;		/* applies for every separate field value */
206 int	CONFIG_HISTSYNCER_FORKS		= 4;
207 int	CONFIG_HISTSYNCER_FREQUENCY	= 1;
208 int	CONFIG_CONFSYNCER_FORKS		= 1;
209 int	CONFIG_CONFSYNCER_FREQUENCY	= 60;
210 
211 int	CONFIG_VMWARE_FORKS		= 0;
212 int	CONFIG_VMWARE_FREQUENCY		= 60;
213 int	CONFIG_VMWARE_PERF_FREQUENCY	= 60;
214 int	CONFIG_VMWARE_TIMEOUT		= 10;
215 
216 zbx_uint64_t	CONFIG_CONF_CACHE_SIZE		= 8 * ZBX_MEBIBYTE;
217 zbx_uint64_t	CONFIG_HISTORY_CACHE_SIZE	= 16 * ZBX_MEBIBYTE;
218 zbx_uint64_t	CONFIG_HISTORY_INDEX_CACHE_SIZE	= 4 * ZBX_MEBIBYTE;
219 zbx_uint64_t	CONFIG_TRENDS_CACHE_SIZE	= 4 * ZBX_MEBIBYTE;
220 zbx_uint64_t	CONFIG_VALUE_CACHE_SIZE		= 8 * ZBX_MEBIBYTE;
221 zbx_uint64_t	CONFIG_VMWARE_CACHE_SIZE	= 8 * ZBX_MEBIBYTE;
222 zbx_uint64_t	CONFIG_EXPORT_FILE_SIZE		= ZBX_GIBIBYTE;
223 
224 int	CONFIG_UNREACHABLE_PERIOD	= 45;
225 int	CONFIG_UNREACHABLE_DELAY	= 15;
226 int	CONFIG_UNAVAILABLE_DELAY	= 60;
227 int	CONFIG_LOG_LEVEL		= LOG_LEVEL_WARNING;
228 char	*CONFIG_ALERT_SCRIPTS_PATH	= NULL;
229 char	*CONFIG_EXTERNALSCRIPTS		= NULL;
230 char	*CONFIG_TMPDIR			= NULL;
231 char	*CONFIG_FPING_LOCATION		= NULL;
232 char	*CONFIG_FPING6_LOCATION		= NULL;
233 char	*CONFIG_DBHOST			= NULL;
234 char	*CONFIG_DBNAME			= NULL;
235 char	*CONFIG_DBSCHEMA		= NULL;
236 char	*CONFIG_DBUSER			= NULL;
237 char	*CONFIG_DBPASSWORD		= NULL;
238 char	*CONFIG_DBSOCKET		= NULL;
239 char	*CONFIG_DB_TLS_CONNECT		= NULL;
240 char	*CONFIG_DB_TLS_CERT_FILE	= NULL;
241 char	*CONFIG_DB_TLS_KEY_FILE		= NULL;
242 char	*CONFIG_DB_TLS_CA_FILE		= NULL;
243 char	*CONFIG_DB_TLS_CIPHER		= NULL;
244 char	*CONFIG_DB_TLS_CIPHER_13	= NULL;
245 char	*CONFIG_EXPORT_DIR		= NULL;
246 char	*CONFIG_EXPORT_TYPE		= NULL;
247 int	CONFIG_DBPORT			= 0;
248 int	CONFIG_ENABLE_REMOTE_COMMANDS	= 0;
249 int	CONFIG_LOG_REMOTE_COMMANDS	= 0;
250 int	CONFIG_UNSAFE_USER_PARAMETERS	= 0;
251 
252 char	*CONFIG_SNMPTRAP_FILE		= NULL;
253 
254 char	*CONFIG_JAVA_GATEWAY		= NULL;
255 int	CONFIG_JAVA_GATEWAY_PORT	= ZBX_DEFAULT_GATEWAY_PORT;
256 
257 char	*CONFIG_SSH_KEY_LOCATION	= NULL;
258 
259 int	CONFIG_LOG_SLOW_QUERIES		= 0;	/* ms; 0 - disable */
260 
261 int	CONFIG_SERVER_STARTUP_TIME	= 0;	/* zabbix server startup time */
262 
263 int	CONFIG_PROXYPOLLER_FORKS	= 1;	/* parameters for passive proxies */
264 
265 /* how often Zabbix server sends configuration data to proxy, in seconds */
266 int	CONFIG_PROXYCONFIG_FREQUENCY	= SEC_PER_HOUR;
267 int	CONFIG_PROXYDATA_FREQUENCY	= 1;	/* 1s */
268 
269 char	*CONFIG_LOAD_MODULE_PATH	= NULL;
270 char	**CONFIG_LOAD_MODULE		= NULL;
271 
272 char	*CONFIG_USER			= NULL;
273 
274 /* web monitoring */
275 char	*CONFIG_SSL_CA_LOCATION		= NULL;
276 char	*CONFIG_SSL_CERT_LOCATION	= NULL;
277 char	*CONFIG_SSL_KEY_LOCATION	= NULL;
278 
279 /* TLS parameters */
280 unsigned int	configured_tls_connect_mode = ZBX_TCP_SEC_UNENCRYPTED;	/* not used in server, defined for linking */
281 									/* with tls.c */
282 unsigned int	configured_tls_accept_modes = ZBX_TCP_SEC_UNENCRYPTED;	/* not used in server, defined for linking */
283 									/* with tls.c */
284 char	*CONFIG_TLS_CA_FILE		= NULL;
285 char	*CONFIG_TLS_CRL_FILE		= NULL;
286 char	*CONFIG_TLS_CERT_FILE		= NULL;
287 char	*CONFIG_TLS_KEY_FILE		= NULL;
288 char	*CONFIG_TLS_CIPHER_CERT13	= NULL;
289 char	*CONFIG_TLS_CIPHER_CERT		= NULL;
290 char	*CONFIG_TLS_CIPHER_PSK13	= NULL;
291 char	*CONFIG_TLS_CIPHER_PSK		= NULL;
292 char	*CONFIG_TLS_CIPHER_ALL13	= NULL;
293 char	*CONFIG_TLS_CIPHER_ALL		= NULL;
294 char	*CONFIG_TLS_CIPHER_CMD13	= NULL;	/* not used in server, defined for linking with tls.c */
295 char	*CONFIG_TLS_CIPHER_CMD		= NULL;	/* not used in server, defined for linking with tls.c */
296 #if defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
297 /* the following TLS parameters are not used in server, they are defined for linking with tls.c */
298 char	*CONFIG_TLS_CONNECT		= NULL;
299 char	*CONFIG_TLS_ACCEPT		= NULL;
300 char	*CONFIG_TLS_SERVER_CERT_ISSUER	= NULL;
301 char	*CONFIG_TLS_SERVER_CERT_SUBJECT	= NULL;
302 char	*CONFIG_TLS_PSK_IDENTITY	= NULL;
303 char	*CONFIG_TLS_PSK_FILE		= NULL;
304 #endif
305 
306 static char	*CONFIG_SOCKET_PATH	= NULL;
307 
308 char	*CONFIG_HISTORY_STORAGE_URL		= NULL;
309 char	*CONFIG_HISTORY_STORAGE_OPTS		= NULL;
310 int	CONFIG_HISTORY_STORAGE_PIPELINES	= 0;
311 
312 char	*CONFIG_STATS_ALLOWED_IP	= NULL;
313 int	CONFIG_TCP_MAX_BACKLOG_SIZE	= SOMAXCONN;
314 
315 int	CONFIG_DOUBLE_PRECISION		= ZBX_DB_DBL_PRECISION_ENABLED;
316 
317 volatile sig_atomic_t	zbx_diaginfo_scope = ZBX_DIAGINFO_UNDEFINED;
318 
319 int	get_process_info_by_thread(int local_server_num, unsigned char *local_process_type, int *local_process_num);
320 
get_process_info_by_thread(int local_server_num,unsigned char * local_process_type,int * local_process_num)321 int	get_process_info_by_thread(int local_server_num, unsigned char *local_process_type, int *local_process_num)
322 {
323 	int	server_count = 0;
324 
325 	if (0 == local_server_num)
326 	{
327 		/* fail if the main process is queried */
328 		return FAIL;
329 	}
330 	else if (local_server_num <= (server_count += CONFIG_CONFSYNCER_FORKS))
331 	{
332 		/* make initial configuration sync before worker processes are forked */
333 		*local_process_type = ZBX_PROCESS_TYPE_CONFSYNCER;
334 		*local_process_num = local_server_num - server_count + CONFIG_CONFSYNCER_FORKS;
335 	}
336 	else if (local_server_num <= (server_count += CONFIG_ALERTMANAGER_FORKS))
337 	{
338 		/* data collection processes might utilize CPU fully, start manager and worker processes beforehand */
339 		*local_process_type = ZBX_PROCESS_TYPE_ALERTMANAGER;
340 		*local_process_num = local_server_num - server_count + CONFIG_ALERTMANAGER_FORKS;
341 	}
342 	else if (local_server_num <= (server_count += CONFIG_ALERTER_FORKS))
343 	{
344 		*local_process_type = ZBX_PROCESS_TYPE_ALERTER;
345 		*local_process_num = local_server_num - server_count + CONFIG_ALERTER_FORKS;
346 	}
347 	else if (local_server_num <= (server_count += CONFIG_PREPROCMAN_FORKS))
348 	{
349 		*local_process_type = ZBX_PROCESS_TYPE_PREPROCMAN;
350 		*local_process_num = local_server_num - server_count + CONFIG_PREPROCMAN_FORKS;
351 	}
352 	else if (local_server_num <= (server_count += CONFIG_PREPROCESSOR_FORKS))
353 	{
354 		*local_process_type = ZBX_PROCESS_TYPE_PREPROCESSOR;
355 		*local_process_num = local_server_num - server_count + CONFIG_PREPROCESSOR_FORKS;
356 	}
357 	else if (local_server_num <= (server_count += CONFIG_LLDMANAGER_FORKS))
358 	{
359 		*local_process_type = ZBX_PROCESS_TYPE_LLDMANAGER;
360 		*local_process_num = local_server_num - server_count + CONFIG_LLDMANAGER_FORKS;
361 	}
362 	else if (local_server_num <= (server_count += CONFIG_LLDWORKER_FORKS))
363 	{
364 		*local_process_type = ZBX_PROCESS_TYPE_LLDWORKER;
365 		*local_process_num = local_server_num - server_count + CONFIG_LLDWORKER_FORKS;
366 	}
367 	else if (local_server_num <= (server_count += CONFIG_IPMIMANAGER_FORKS))
368 	{
369 		*local_process_type = ZBX_PROCESS_TYPE_IPMIMANAGER;
370 		*local_process_num = local_server_num - server_count + CONFIG_TASKMANAGER_FORKS;
371 	}
372 	else if (local_server_num <= (server_count += CONFIG_HOUSEKEEPER_FORKS))
373 	{
374 		*local_process_type = ZBX_PROCESS_TYPE_HOUSEKEEPER;
375 		*local_process_num = local_server_num - server_count + CONFIG_HOUSEKEEPER_FORKS;
376 	}
377 	else if (local_server_num <= (server_count += CONFIG_TIMER_FORKS))
378 	{
379 		*local_process_type = ZBX_PROCESS_TYPE_TIMER;
380 		*local_process_num = local_server_num - server_count + CONFIG_TIMER_FORKS;
381 	}
382 	else if (local_server_num <= (server_count += CONFIG_HTTPPOLLER_FORKS))
383 	{
384 		*local_process_type = ZBX_PROCESS_TYPE_HTTPPOLLER;
385 		*local_process_num = local_server_num - server_count + CONFIG_HTTPPOLLER_FORKS;
386 	}
387 	else if (local_server_num <= (server_count += CONFIG_DISCOVERER_FORKS))
388 	{
389 		*local_process_type = ZBX_PROCESS_TYPE_DISCOVERER;
390 		*local_process_num = local_server_num - server_count + CONFIG_DISCOVERER_FORKS;
391 	}
392 	else if (local_server_num <= (server_count += CONFIG_HISTSYNCER_FORKS))
393 	{
394 		*local_process_type = ZBX_PROCESS_TYPE_HISTSYNCER;
395 		*local_process_num = local_server_num - server_count + CONFIG_HISTSYNCER_FORKS;
396 	}
397 	else if (local_server_num <= (server_count += CONFIG_ESCALATOR_FORKS))
398 	{
399 		*local_process_type = ZBX_PROCESS_TYPE_ESCALATOR;
400 		*local_process_num = local_server_num - server_count + CONFIG_ESCALATOR_FORKS;
401 	}
402 	else if (local_server_num <= (server_count += CONFIG_IPMIPOLLER_FORKS))
403 	{
404 		*local_process_type = ZBX_PROCESS_TYPE_IPMIPOLLER;
405 		*local_process_num = local_server_num - server_count + CONFIG_IPMIPOLLER_FORKS;
406 	}
407 	else if (local_server_num <= (server_count += CONFIG_JAVAPOLLER_FORKS))
408 	{
409 		*local_process_type = ZBX_PROCESS_TYPE_JAVAPOLLER;
410 		*local_process_num = local_server_num - server_count + CONFIG_JAVAPOLLER_FORKS;
411 	}
412 	else if (local_server_num <= (server_count += CONFIG_SNMPTRAPPER_FORKS))
413 	{
414 		*local_process_type = ZBX_PROCESS_TYPE_SNMPTRAPPER;
415 		*local_process_num = local_server_num - server_count + CONFIG_SNMPTRAPPER_FORKS;
416 	}
417 	else if (local_server_num <= (server_count += CONFIG_PROXYPOLLER_FORKS))
418 	{
419 		*local_process_type = ZBX_PROCESS_TYPE_PROXYPOLLER;
420 		*local_process_num = local_server_num - server_count + CONFIG_PROXYPOLLER_FORKS;
421 	}
422 	else if (local_server_num <= (server_count += CONFIG_SELFMON_FORKS))
423 	{
424 		*local_process_type = ZBX_PROCESS_TYPE_SELFMON;
425 		*local_process_num = local_server_num - server_count + CONFIG_SELFMON_FORKS;
426 	}
427 	else if (local_server_num <= (server_count += CONFIG_VMWARE_FORKS))
428 	{
429 		*local_process_type = ZBX_PROCESS_TYPE_VMWARE;
430 		*local_process_num = local_server_num - server_count + CONFIG_VMWARE_FORKS;
431 	}
432 	else if (local_server_num <= (server_count += CONFIG_TASKMANAGER_FORKS))
433 	{
434 		*local_process_type = ZBX_PROCESS_TYPE_TASKMANAGER;
435 		*local_process_num = local_server_num - server_count + CONFIG_TASKMANAGER_FORKS;
436 	}
437 	else if (local_server_num <= (server_count += CONFIG_POLLER_FORKS))
438 	{
439 		*local_process_type = ZBX_PROCESS_TYPE_POLLER;
440 		*local_process_num = local_server_num - server_count + CONFIG_POLLER_FORKS;
441 	}
442 	else if (local_server_num <= (server_count += CONFIG_UNREACHABLE_POLLER_FORKS))
443 	{
444 		*local_process_type = ZBX_PROCESS_TYPE_UNREACHABLE;
445 		*local_process_num = local_server_num - server_count + CONFIG_UNREACHABLE_POLLER_FORKS;
446 	}
447 	else if (local_server_num <= (server_count += CONFIG_TRAPPER_FORKS))
448 	{
449 		*local_process_type = ZBX_PROCESS_TYPE_TRAPPER;
450 		*local_process_num = local_server_num - server_count + CONFIG_TRAPPER_FORKS;
451 	}
452 	else if (local_server_num <= (server_count += CONFIG_PINGER_FORKS))
453 	{
454 		*local_process_type = ZBX_PROCESS_TYPE_PINGER;
455 		*local_process_num = local_server_num - server_count + CONFIG_PINGER_FORKS;
456 	}
457 	else if (local_server_num <= (server_count += CONFIG_ALERTDB_FORKS))
458 	{
459 		*local_process_type = ZBX_PROCESS_TYPE_ALERTSYNCER;
460 		*local_process_num = local_server_num - server_count + CONFIG_ALERTDB_FORKS;
461 	}
462 	else
463 		return FAIL;
464 
465 	return SUCCEED;
466 }
467 
468 /******************************************************************************
469  *                                                                            *
470  * Function: zbx_set_defaults                                                 *
471  *                                                                            *
472  * Purpose: set configuration defaults                                        *
473  *                                                                            *
474  * Author: Vladimir Levijev                                                   *
475  *                                                                            *
476  ******************************************************************************/
zbx_set_defaults(void)477 static void	zbx_set_defaults(void)
478 {
479 	CONFIG_SERVER_STARTUP_TIME = time(NULL);
480 
481 	if (NULL == CONFIG_DBHOST)
482 		CONFIG_DBHOST = zbx_strdup(CONFIG_DBHOST, "localhost");
483 
484 	if (NULL == CONFIG_SNMPTRAP_FILE)
485 		CONFIG_SNMPTRAP_FILE = zbx_strdup(CONFIG_SNMPTRAP_FILE, "/tmp/zabbix_traps.tmp");
486 
487 	if (NULL == CONFIG_PID_FILE)
488 		CONFIG_PID_FILE = zbx_strdup(CONFIG_PID_FILE, "/var/run/zabbix/zabbix_server.pid");
489 
490 	if (NULL == CONFIG_ALERT_SCRIPTS_PATH)
491 		CONFIG_ALERT_SCRIPTS_PATH = zbx_strdup(CONFIG_ALERT_SCRIPTS_PATH, DEFAULT_ALERT_SCRIPTS_PATH);
492 
493 	if (NULL == CONFIG_LOAD_MODULE_PATH)
494 		CONFIG_LOAD_MODULE_PATH = zbx_strdup(CONFIG_LOAD_MODULE_PATH, DEFAULT_LOAD_MODULE_PATH);
495 
496 	if (NULL == CONFIG_TMPDIR)
497 		CONFIG_TMPDIR = zbx_strdup(CONFIG_TMPDIR, "/tmp");
498 
499 	if (NULL == CONFIG_FPING_LOCATION)
500 		CONFIG_FPING_LOCATION = zbx_strdup(CONFIG_FPING_LOCATION, "/usr/local/sbin/fping");
501 #ifdef HAVE_IPV6
502 	if (NULL == CONFIG_FPING6_LOCATION)
503 		CONFIG_FPING6_LOCATION = zbx_strdup(CONFIG_FPING6_LOCATION, "/usr/local/sbin/fping6");
504 #endif
505 	if (NULL == CONFIG_EXTERNALSCRIPTS)
506 		CONFIG_EXTERNALSCRIPTS = zbx_strdup(CONFIG_EXTERNALSCRIPTS, DEFAULT_EXTERNAL_SCRIPTS_PATH);
507 #ifdef HAVE_LIBCURL
508 	if (NULL == CONFIG_SSL_CERT_LOCATION)
509 		CONFIG_SSL_CERT_LOCATION = zbx_strdup(CONFIG_SSL_CERT_LOCATION, DEFAULT_SSL_CERT_LOCATION);
510 
511 	if (NULL == CONFIG_SSL_KEY_LOCATION)
512 		CONFIG_SSL_KEY_LOCATION = zbx_strdup(CONFIG_SSL_KEY_LOCATION, DEFAULT_SSL_KEY_LOCATION);
513 
514 	if (NULL == CONFIG_HISTORY_STORAGE_OPTS)
515 		CONFIG_HISTORY_STORAGE_OPTS = zbx_strdup(CONFIG_HISTORY_STORAGE_OPTS, "uint,dbl,str,log,text");
516 #endif
517 
518 #ifdef HAVE_SQLITE3
519 	CONFIG_MAX_HOUSEKEEPER_DELETE = 0;
520 #endif
521 
522 	if (NULL == CONFIG_LOG_TYPE_STR)
523 		CONFIG_LOG_TYPE_STR = zbx_strdup(CONFIG_LOG_TYPE_STR, ZBX_OPTION_LOGTYPE_FILE);
524 
525 	if (NULL == CONFIG_SOCKET_PATH)
526 		CONFIG_SOCKET_PATH = zbx_strdup(CONFIG_SOCKET_PATH, "/tmp");
527 
528 	if (0 != CONFIG_IPMIPOLLER_FORKS)
529 		CONFIG_IPMIMANAGER_FORKS = 1;
530 }
531 
532 /******************************************************************************
533  *                                                                            *
534  * Function: zbx_validate_config                                              *
535  *                                                                            *
536  * Purpose: validate configuration parameters                                 *
537  *                                                                            *
538  * Author: Vladimir Levijev                                                   *
539  *                                                                            *
540  ******************************************************************************/
zbx_validate_config(ZBX_TASK_EX * task)541 static void	zbx_validate_config(ZBX_TASK_EX *task)
542 {
543 	char	*ch_error;
544 	int	err = 0;
545 
546 	if (0 == CONFIG_UNREACHABLE_POLLER_FORKS && 0 != CONFIG_POLLER_FORKS + CONFIG_JAVAPOLLER_FORKS)
547 	{
548 		zabbix_log(LOG_LEVEL_CRIT, "\"StartPollersUnreachable\" configuration parameter must not be 0"
549 				" if regular or Java pollers are started");
550 		err = 1;
551 	}
552 
553 	if ((NULL == CONFIG_JAVA_GATEWAY || '\0' == *CONFIG_JAVA_GATEWAY) && 0 < CONFIG_JAVAPOLLER_FORKS)
554 	{
555 		zabbix_log(LOG_LEVEL_CRIT, "\"JavaGateway\" configuration parameter is not specified or empty");
556 		err = 1;
557 	}
558 
559 	if (0 != CONFIG_VALUE_CACHE_SIZE && 128 * ZBX_KIBIBYTE > CONFIG_VALUE_CACHE_SIZE)
560 	{
561 		zabbix_log(LOG_LEVEL_CRIT, "\"ValueCacheSize\" configuration parameter must be either 0"
562 				" or greater than 128KB");
563 		err = 1;
564 	}
565 
566 	if (NULL != CONFIG_SOURCE_IP && SUCCEED != is_supported_ip(CONFIG_SOURCE_IP))
567 	{
568 		zabbix_log(LOG_LEVEL_CRIT, "invalid \"SourceIP\" configuration parameter: '%s'", CONFIG_SOURCE_IP);
569 		err = 1;
570 	}
571 
572 	if (NULL != CONFIG_STATS_ALLOWED_IP && FAIL == zbx_validate_peer_list(CONFIG_STATS_ALLOWED_IP, &ch_error))
573 	{
574 		zabbix_log(LOG_LEVEL_CRIT, "invalid entry in \"StatsAllowedIP\" configuration parameter: %s", ch_error);
575 		zbx_free(ch_error);
576 		err = 1;
577 	}
578 
579 	if (SUCCEED != 	zbx_validate_export_type(CONFIG_EXPORT_TYPE, NULL))
580 	{
581 		zabbix_log(LOG_LEVEL_CRIT, "invalid \"ExportType\" configuration parameter: %s", CONFIG_EXPORT_TYPE);
582 		err = 1;
583 	}
584 #if !defined(HAVE_IPV6)
585 	err |= (FAIL == check_cfg_feature_str("Fping6Location", CONFIG_FPING6_LOCATION, "IPv6 support"));
586 #endif
587 #if !defined(HAVE_LIBCURL)
588 	err |= (FAIL == check_cfg_feature_str("SSLCALocation", CONFIG_SSL_CA_LOCATION, "cURL library"));
589 	err |= (FAIL == check_cfg_feature_str("SSLCertLocation", CONFIG_SSL_CERT_LOCATION, "cURL library"));
590 	err |= (FAIL == check_cfg_feature_str("SSLKeyLocation", CONFIG_SSL_KEY_LOCATION, "cURL library"));
591 	err |= (FAIL == check_cfg_feature_str("HistoryStorageURL", CONFIG_HISTORY_STORAGE_URL, "cURL library"));
592 	err |= (FAIL == check_cfg_feature_str("HistoryStorageTypes", CONFIG_HISTORY_STORAGE_OPTS, "cURL library"));
593 	err |= (FAIL == check_cfg_feature_int("HistoryStorageDateIndex", CONFIG_HISTORY_STORAGE_PIPELINES,
594 			"cURL library"));
595 #endif
596 
597 #if !defined(HAVE_LIBXML2) || !defined(HAVE_LIBCURL)
598 	err |= (FAIL == check_cfg_feature_int("StartVMwareCollectors", CONFIG_VMWARE_FORKS, "VMware support"));
599 
600 	/* parameters VMwareFrequency, VMwarePerfFrequency, VMwareCacheSize, VMwareTimeout are not checked here */
601 	/* because they have non-zero default values */
602 #endif
603 
604 	if (SUCCEED != zbx_validate_log_parameters(task))
605 		err = 1;
606 
607 #if !(defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL))
608 	err |= (FAIL == check_cfg_feature_str("TLSCAFile", CONFIG_TLS_CA_FILE, "TLS support"));
609 	err |= (FAIL == check_cfg_feature_str("TLSCRLFile", CONFIG_TLS_CRL_FILE, "TLS support"));
610 	err |= (FAIL == check_cfg_feature_str("TLSCertFile", CONFIG_TLS_CERT_FILE, "TLS support"));
611 	err |= (FAIL == check_cfg_feature_str("TLSKeyFile", CONFIG_TLS_KEY_FILE, "TLS support"));
612 #endif
613 #if !(defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL))
614 	err |= (FAIL == check_cfg_feature_str("TLSCipherCert", CONFIG_TLS_CIPHER_CERT, "GnuTLS or OpenSSL"));
615 	err |= (FAIL == check_cfg_feature_str("TLSCipherPSK", CONFIG_TLS_CIPHER_PSK, "GnuTLS or OpenSSL"));
616 	err |= (FAIL == check_cfg_feature_str("TLSCipherAll", CONFIG_TLS_CIPHER_ALL, "GnuTLS or OpenSSL"));
617 #endif
618 #if !defined(HAVE_OPENSSL)
619 	err |= (FAIL == check_cfg_feature_str("TLSCipherCert13", CONFIG_TLS_CIPHER_CERT13, "OpenSSL 1.1.1 or newer"));
620 	err |= (FAIL == check_cfg_feature_str("TLSCipherPSK13", CONFIG_TLS_CIPHER_PSK13, "OpenSSL 1.1.1 or newer"));
621 	err |= (FAIL == check_cfg_feature_str("TLSCipherAll13", CONFIG_TLS_CIPHER_ALL13, "OpenSSL 1.1.1 or newer"));
622 #endif
623 
624 #if !defined(HAVE_OPENIPMI)
625 	err |= (FAIL == check_cfg_feature_int("StartIPMIPollers", CONFIG_IPMIPOLLER_FORKS, "IPMI support"));
626 #endif
627 	err |= (FAIL == zbx_db_validate_config_features());
628 
629 	if (0 != err)
630 		exit(EXIT_FAILURE);
631 }
632 
633 /******************************************************************************
634  *                                                                            *
635  * Function: zbx_load_config                                                  *
636  *                                                                            *
637  * Purpose: parse config file and update configuration parameters             *
638  *                                                                            *
639  * Parameters:                                                                *
640  *                                                                            *
641  * Return value:                                                              *
642  *                                                                            *
643  * Author: Alexei Vladishev                                                   *
644  *                                                                            *
645  * Comments: will terminate process if parsing fails                          *
646  *                                                                            *
647  ******************************************************************************/
zbx_load_config(ZBX_TASK_EX * task)648 static void	zbx_load_config(ZBX_TASK_EX *task)
649 {
650 	static struct cfg_line	cfg[] =
651 	{
652 		/* PARAMETER,			VAR,					TYPE,
653 			MANDATORY,	MIN,			MAX */
654 		{"StartDBSyncers",		&CONFIG_HISTSYNCER_FORKS,		TYPE_INT,
655 			PARM_OPT,	1,			100},
656 		{"StartDiscoverers",		&CONFIG_DISCOVERER_FORKS,		TYPE_INT,
657 			PARM_OPT,	0,			250},
658 		{"StartHTTPPollers",		&CONFIG_HTTPPOLLER_FORKS,		TYPE_INT,
659 			PARM_OPT,	0,			1000},
660 		{"StartPingers",		&CONFIG_PINGER_FORKS,			TYPE_INT,
661 			PARM_OPT,	0,			1000},
662 		{"StartPollers",		&CONFIG_POLLER_FORKS,			TYPE_INT,
663 			PARM_OPT,	0,			1000},
664 		{"StartPollersUnreachable",	&CONFIG_UNREACHABLE_POLLER_FORKS,	TYPE_INT,
665 			PARM_OPT,	0,			1000},
666 		{"StartIPMIPollers",		&CONFIG_IPMIPOLLER_FORKS,		TYPE_INT,
667 			PARM_OPT,	0,			1000},
668 		{"StartTimers",			&CONFIG_TIMER_FORKS,			TYPE_INT,
669 			PARM_OPT,	1,			1000},
670 		{"StartTrappers",		&CONFIG_TRAPPER_FORKS,			TYPE_INT,
671 			PARM_OPT,	0,			1000},
672 		{"StartJavaPollers",		&CONFIG_JAVAPOLLER_FORKS,		TYPE_INT,
673 			PARM_OPT,	0,			1000},
674 		{"StartEscalators",		&CONFIG_ESCALATOR_FORKS,		TYPE_INT,
675 			PARM_OPT,	1,			100},
676 		{"JavaGateway",			&CONFIG_JAVA_GATEWAY,			TYPE_STRING,
677 			PARM_OPT,	0,			0},
678 		{"JavaGatewayPort",		&CONFIG_JAVA_GATEWAY_PORT,		TYPE_INT,
679 			PARM_OPT,	1024,			32767},
680 		{"SNMPTrapperFile",		&CONFIG_SNMPTRAP_FILE,			TYPE_STRING,
681 			PARM_OPT,	0,			0},
682 		{"StartSNMPTrapper",		&CONFIG_SNMPTRAPPER_FORKS,		TYPE_INT,
683 			PARM_OPT,	0,			1},
684 		{"CacheSize",			&CONFIG_CONF_CACHE_SIZE,		TYPE_UINT64,
685 			PARM_OPT,	128 * ZBX_KIBIBYTE,	__UINT64_C(64) * ZBX_GIBIBYTE},
686 		{"HistoryCacheSize",		&CONFIG_HISTORY_CACHE_SIZE,		TYPE_UINT64,
687 			PARM_OPT,	128 * ZBX_KIBIBYTE,	__UINT64_C(2) * ZBX_GIBIBYTE},
688 		{"HistoryIndexCacheSize",	&CONFIG_HISTORY_INDEX_CACHE_SIZE,	TYPE_UINT64,
689 			PARM_OPT,	128 * ZBX_KIBIBYTE,	__UINT64_C(2) * ZBX_GIBIBYTE},
690 		{"TrendCacheSize",		&CONFIG_TRENDS_CACHE_SIZE,		TYPE_UINT64,
691 			PARM_OPT,	128 * ZBX_KIBIBYTE,	__UINT64_C(2) * ZBX_GIBIBYTE},
692 		{"ValueCacheSize",		&CONFIG_VALUE_CACHE_SIZE,		TYPE_UINT64,
693 			PARM_OPT,	0,			__UINT64_C(64) * ZBX_GIBIBYTE},
694 		{"CacheUpdateFrequency",	&CONFIG_CONFSYNCER_FREQUENCY,		TYPE_INT,
695 			PARM_OPT,	1,			SEC_PER_HOUR},
696 		{"HousekeepingFrequency",	&CONFIG_HOUSEKEEPING_FREQUENCY,		TYPE_INT,
697 			PARM_OPT,	0,			24},
698 		{"MaxHousekeeperDelete",	&CONFIG_MAX_HOUSEKEEPER_DELETE,		TYPE_INT,
699 			PARM_OPT,	0,			1000000},
700 		{"TmpDir",			&CONFIG_TMPDIR,				TYPE_STRING,
701 			PARM_OPT,	0,			0},
702 		{"FpingLocation",		&CONFIG_FPING_LOCATION,			TYPE_STRING,
703 			PARM_OPT,	0,			0},
704 		{"Fping6Location",		&CONFIG_FPING6_LOCATION,		TYPE_STRING,
705 			PARM_OPT,	0,			0},
706 		{"Timeout",			&CONFIG_TIMEOUT,			TYPE_INT,
707 			PARM_OPT,	1,			30},
708 		{"TrapperTimeout",		&CONFIG_TRAPPER_TIMEOUT,		TYPE_INT,
709 			PARM_OPT,	1,			300},
710 		{"UnreachablePeriod",		&CONFIG_UNREACHABLE_PERIOD,		TYPE_INT,
711 			PARM_OPT,	1,			SEC_PER_HOUR},
712 		{"UnreachableDelay",		&CONFIG_UNREACHABLE_DELAY,		TYPE_INT,
713 			PARM_OPT,	1,			SEC_PER_HOUR},
714 		{"UnavailableDelay",		&CONFIG_UNAVAILABLE_DELAY,		TYPE_INT,
715 			PARM_OPT,	1,			SEC_PER_HOUR},
716 		{"ListenIP",			&CONFIG_LISTEN_IP,			TYPE_STRING_LIST,
717 			PARM_OPT,	0,			0},
718 		{"ListenPort",			&CONFIG_LISTEN_PORT,			TYPE_INT,
719 			PARM_OPT,	1024,			32767},
720 		{"SourceIP",			&CONFIG_SOURCE_IP,			TYPE_STRING,
721 			PARM_OPT,	0,			0},
722 		{"DebugLevel",			&CONFIG_LOG_LEVEL,			TYPE_INT,
723 			PARM_OPT,	0,			5},
724 		{"PidFile",			&CONFIG_PID_FILE,			TYPE_STRING,
725 			PARM_OPT,	0,			0},
726 		{"LogType",			&CONFIG_LOG_TYPE_STR,			TYPE_STRING,
727 			PARM_OPT,	0,			0},
728 		{"LogFile",			&CONFIG_LOG_FILE,			TYPE_STRING,
729 			PARM_OPT,	0,			0},
730 		{"LogFileSize",			&CONFIG_LOG_FILE_SIZE,			TYPE_INT,
731 			PARM_OPT,	0,			1024},
732 		{"AlertScriptsPath",		&CONFIG_ALERT_SCRIPTS_PATH,		TYPE_STRING,
733 			PARM_OPT,	0,			0},
734 		{"ExternalScripts",		&CONFIG_EXTERNALSCRIPTS,		TYPE_STRING,
735 			PARM_OPT,	0,			0},
736 		{"DBHost",			&CONFIG_DBHOST,				TYPE_STRING,
737 			PARM_OPT,	0,			0},
738 		{"DBName",			&CONFIG_DBNAME,				TYPE_STRING,
739 			PARM_MAND,	0,			0},
740 		{"DBSchema",			&CONFIG_DBSCHEMA,			TYPE_STRING,
741 			PARM_OPT,	0,			0},
742 		{"DBUser",			&CONFIG_DBUSER,				TYPE_STRING,
743 			PARM_OPT,	0,			0},
744 		{"DBPassword",			&CONFIG_DBPASSWORD,			TYPE_STRING,
745 			PARM_OPT,	0,			0},
746 		{"DBSocket",			&CONFIG_DBSOCKET,			TYPE_STRING,
747 			PARM_OPT,	0,			0},
748 		{"DBPort",			&CONFIG_DBPORT,				TYPE_INT,
749 			PARM_OPT,	1024,			65535},
750 		{"DBTLSConnect",		&CONFIG_DB_TLS_CONNECT,			TYPE_STRING,
751 			PARM_OPT,	0,			0},
752 		{"DBTLSCertFile",		&CONFIG_DB_TLS_CERT_FILE,		TYPE_STRING,
753 			PARM_OPT,	0,			0},
754 		{"DBTLSKeyFile",		&CONFIG_DB_TLS_KEY_FILE,		TYPE_STRING,
755 			PARM_OPT,	0,			0},
756 		{"DBTLSCAFile",			&CONFIG_DB_TLS_CA_FILE,			TYPE_STRING,
757 			PARM_OPT,	0,			0},
758 		{"DBTLSCipher",			&CONFIG_DB_TLS_CIPHER,			TYPE_STRING,
759 			PARM_OPT,	0,			0},
760 		{"DBTLSCipher13",		&CONFIG_DB_TLS_CIPHER_13,		TYPE_STRING,
761 			PARM_OPT,	0,			0},
762 		{"SSHKeyLocation",		&CONFIG_SSH_KEY_LOCATION,		TYPE_STRING,
763 			PARM_OPT,	0,			0},
764 		{"LogSlowQueries",		&CONFIG_LOG_SLOW_QUERIES,		TYPE_INT,
765 			PARM_OPT,	0,			3600000},
766 		{"StartProxyPollers",		&CONFIG_PROXYPOLLER_FORKS,		TYPE_INT,
767 			PARM_OPT,	0,			250},
768 		{"ProxyConfigFrequency",	&CONFIG_PROXYCONFIG_FREQUENCY,		TYPE_INT,
769 			PARM_OPT,	1,			SEC_PER_WEEK},
770 		{"ProxyDataFrequency",		&CONFIG_PROXYDATA_FREQUENCY,		TYPE_INT,
771 			PARM_OPT,	1,			SEC_PER_HOUR},
772 		{"LoadModulePath",		&CONFIG_LOAD_MODULE_PATH,		TYPE_STRING,
773 			PARM_OPT,	0,			0},
774 		{"LoadModule",			&CONFIG_LOAD_MODULE,			TYPE_MULTISTRING,
775 			PARM_OPT,	0,			0},
776 		{"StartVMwareCollectors",	&CONFIG_VMWARE_FORKS,			TYPE_INT,
777 			PARM_OPT,	0,			250},
778 		{"VMwareFrequency",		&CONFIG_VMWARE_FREQUENCY,		TYPE_INT,
779 			PARM_OPT,	10,			SEC_PER_DAY},
780 		{"VMwarePerfFrequency",		&CONFIG_VMWARE_PERF_FREQUENCY,		TYPE_INT,
781 			PARM_OPT,	10,			SEC_PER_DAY},
782 		{"VMwareCacheSize",		&CONFIG_VMWARE_CACHE_SIZE,		TYPE_UINT64,
783 			PARM_OPT,	256 * ZBX_KIBIBYTE,	__UINT64_C(2) * ZBX_GIBIBYTE},
784 		{"VMwareTimeout",		&CONFIG_VMWARE_TIMEOUT,			TYPE_INT,
785 			PARM_OPT,	1,			300},
786 		{"AllowRoot",			&CONFIG_ALLOW_ROOT,			TYPE_INT,
787 			PARM_OPT,	0,			1},
788 		{"User",			&CONFIG_USER,				TYPE_STRING,
789 			PARM_OPT,	0,			0},
790 		{"SSLCALocation",		&CONFIG_SSL_CA_LOCATION,		TYPE_STRING,
791 			PARM_OPT,	0,			0},
792 		{"SSLCertLocation",		&CONFIG_SSL_CERT_LOCATION,		TYPE_STRING,
793 			PARM_OPT,	0,			0},
794 		{"SSLKeyLocation",		&CONFIG_SSL_KEY_LOCATION,		TYPE_STRING,
795 			PARM_OPT,	0,			0},
796 		{"TLSCAFile",			&CONFIG_TLS_CA_FILE,			TYPE_STRING,
797 			PARM_OPT,	0,			0},
798 		{"TLSCRLFile",			&CONFIG_TLS_CRL_FILE,			TYPE_STRING,
799 			PARM_OPT,	0,			0},
800 		{"TLSCertFile",			&CONFIG_TLS_CERT_FILE,			TYPE_STRING,
801 			PARM_OPT,	0,			0},
802 		{"TLSKeyFile",			&CONFIG_TLS_KEY_FILE,			TYPE_STRING,
803 			PARM_OPT,	0,			0},
804 		{"TLSCipherCert13",		&CONFIG_TLS_CIPHER_CERT13,		TYPE_STRING,
805 			PARM_OPT,	0,			0},
806 		{"TLSCipherCert",		&CONFIG_TLS_CIPHER_CERT,		TYPE_STRING,
807 			PARM_OPT,	0,			0},
808 		{"TLSCipherPSK13",		&CONFIG_TLS_CIPHER_PSK13,		TYPE_STRING,
809 			PARM_OPT,	0,			0},
810 		{"TLSCipherPSK",		&CONFIG_TLS_CIPHER_PSK,			TYPE_STRING,
811 			PARM_OPT,	0,			0},
812 		{"TLSCipherAll13",		&CONFIG_TLS_CIPHER_ALL13,		TYPE_STRING,
813 			PARM_OPT,	0,			0},
814 		{"TLSCipherAll",		&CONFIG_TLS_CIPHER_ALL,			TYPE_STRING,
815 			PARM_OPT,	0,			0},
816 		{"SocketDir",			&CONFIG_SOCKET_PATH,			TYPE_STRING,
817 			PARM_OPT,	0,			0},
818 		{"StartAlerters",		&CONFIG_ALERTER_FORKS,			TYPE_INT,
819 			PARM_OPT,	1,			100},
820 		{"StartPreprocessors",		&CONFIG_PREPROCESSOR_FORKS,		TYPE_INT,
821 			PARM_OPT,	1,			1000},
822 		{"HistoryStorageURL",		&CONFIG_HISTORY_STORAGE_URL,		TYPE_STRING,
823 			PARM_OPT,	0,			0},
824 		{"HistoryStorageTypes",		&CONFIG_HISTORY_STORAGE_OPTS,		TYPE_STRING_LIST,
825 			PARM_OPT,	0,			0},
826 		{"HistoryStorageDateIndex",	&CONFIG_HISTORY_STORAGE_PIPELINES,	TYPE_INT,
827 			PARM_OPT,	0,			1},
828 		{"ExportDir",			&CONFIG_EXPORT_DIR,			TYPE_STRING,
829 			PARM_OPT,	0,			0},
830 		{"ExportType",			&CONFIG_EXPORT_TYPE,			TYPE_STRING_LIST,
831 			PARM_OPT,	0,			0},
832 		{"ExportFileSize",		&CONFIG_EXPORT_FILE_SIZE,		TYPE_UINT64,
833 			PARM_OPT,	ZBX_MEBIBYTE,	ZBX_GIBIBYTE},
834 		{"StartLLDProcessors",		&CONFIG_LLDWORKER_FORKS,		TYPE_INT,
835 			PARM_OPT,	1,			100},
836 		{"StatsAllowedIP",		&CONFIG_STATS_ALLOWED_IP,		TYPE_STRING_LIST,
837 			PARM_OPT,	0,			0},
838 		{"ListenBacklog",		&CONFIG_TCP_MAX_BACKLOG_SIZE,		TYPE_INT,
839 			PARM_OPT,	0,			INT_MAX},
840 		{NULL}
841 	};
842 
843 	/* initialize multistrings */
844 	zbx_strarr_init(&CONFIG_LOAD_MODULE);
845 
846 	parse_cfg_file(CONFIG_FILE, cfg, ZBX_CFG_FILE_REQUIRED, ZBX_CFG_STRICT);
847 
848 	zbx_set_defaults();
849 
850 	CONFIG_LOG_TYPE = zbx_get_log_type(CONFIG_LOG_TYPE_STR);
851 
852 	zbx_validate_config(task);
853 #if defined(HAVE_MYSQL) || defined(HAVE_POSTGRESQL)
854 	zbx_db_validate_config();
855 #endif
856 #if defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
857 	zbx_tls_validate_config();
858 #endif
859 }
860 
861 /******************************************************************************
862  *                                                                            *
863  * Function: zbx_free_config                                                  *
864  *                                                                            *
865  * Purpose: free configuration memory                                         *
866  *                                                                            *
867  ******************************************************************************/
zbx_free_config(void)868 static void	zbx_free_config(void)
869 {
870 	zbx_strarr_free(CONFIG_LOAD_MODULE);
871 }
872 
873 /******************************************************************************
874  *                                                                            *
875  * Function: main                                                             *
876  *                                                                            *
877  * Purpose: executes server processes                                         *
878  *                                                                            *
879  * Author: Eugene Grigorjev                                                   *
880  *                                                                            *
881  ******************************************************************************/
main(int argc,char ** argv)882 int	main(int argc, char **argv)
883 {
884 	ZBX_TASK_EX	t = {ZBX_TASK_START};
885 	char		ch;
886 	int		opt_c = 0, opt_r = 0;
887 
888 #if defined(PS_OVERWRITE_ARGV) || defined(PS_PSTAT_ARGV)
889 	argv = setproctitle_save_env(argc, argv);
890 #endif
891 	progname = get_program_name(argv[0]);
892 
893 	/* parse the command-line */
894 	while ((char)EOF != (ch = (char)zbx_getopt_long(argc, argv, shortopts, longopts, NULL)))
895 	{
896 		switch (ch)
897 		{
898 			case 'c':
899 				opt_c++;
900 				if (NULL == CONFIG_FILE)
901 					CONFIG_FILE = zbx_strdup(CONFIG_FILE, zbx_optarg);
902 				break;
903 			case 'R':
904 				opt_r++;
905 				if (SUCCEED != parse_rtc_options(zbx_optarg, program_type, &t.data))
906 					exit(EXIT_FAILURE);
907 
908 				t.task = ZBX_TASK_RUNTIME_CONTROL;
909 				break;
910 			case 'h':
911 				help();
912 				exit(EXIT_SUCCESS);
913 				break;
914 			case 'V':
915 				version();
916 				exit(EXIT_SUCCESS);
917 				break;
918 			case 'f':
919 				t.flags |= ZBX_TASK_FLAG_FOREGROUND;
920 				break;
921 			default:
922 				usage();
923 				exit(EXIT_FAILURE);
924 				break;
925 		}
926 	}
927 
928 	/* every option may be specified only once */
929 	if (1 < opt_c || 1 < opt_r)
930 	{
931 		if (1 < opt_c)
932 			zbx_error("option \"-c\" or \"--config\" specified multiple times");
933 		if (1 < opt_r)
934 			zbx_error("option \"-R\" or \"--runtime-control\" specified multiple times");
935 
936 		exit(EXIT_FAILURE);
937 	}
938 
939 	/* Parameters which are not option values are invalid. The check relies on zbx_getopt_internal() which */
940 	/* always permutes command line arguments regardless of POSIXLY_CORRECT environment variable. */
941 	if (argc > zbx_optind)
942 	{
943 		int	i;
944 
945 		for (i = zbx_optind; i < argc; i++)
946 			zbx_error("invalid parameter \"%s\"", argv[i]);
947 
948 		exit(EXIT_FAILURE);
949 	}
950 
951 	if (NULL == CONFIG_FILE)
952 		CONFIG_FILE = zbx_strdup(NULL, DEFAULT_CONFIG_FILE);
953 
954 	/* required for simple checks */
955 	init_metrics();
956 
957 	zbx_load_config(&t);
958 
959 	if (ZBX_TASK_RUNTIME_CONTROL == t.task)
960 		exit(SUCCEED == zbx_sigusr_send(t.data) ? EXIT_SUCCESS : EXIT_FAILURE);
961 
962 	zbx_initialize_events();
963 
964 	return daemon_start(CONFIG_ALLOW_ROOT, CONFIG_USER, t.flags);
965 }
966 
zbx_main_sigusr_handler(int flags)967 static void	zbx_main_sigusr_handler(int flags)
968 {
969 	if (ZBX_RTC_DIAGINFO == ZBX_RTC_GET_MSG(flags))
970 	{
971 		int	scope = ZBX_RTC_GET_SCOPE(flags);
972 
973 		if (ZBX_DIAGINFO_ALL == scope)
974 		{
975 			zbx_diaginfo_scope = (1 << ZBX_DIAGINFO_HISTORYCACHE) | (1 << ZBX_DIAGINFO_VALUECACHE) |
976 					(1 << ZBX_DIAGINFO_PREPROCESSING) | (1 << ZBX_DIAGINFO_LLD) |
977 					(1 << ZBX_DIAGINFO_ALERTING) | (1 << ZBX_DIAGINFO_LOCKS);
978 		}
979 		else
980 			zbx_diaginfo_scope = 1 << scope;
981 	}
982 
983 }
984 
MAIN_ZABBIX_ENTRY(int flags)985 int	MAIN_ZABBIX_ENTRY(int flags)
986 {
987 	zbx_socket_t	listen_sock;
988 	char		*error = NULL;
989 	int		i, db_type;
990 
991 	if (0 != (flags & ZBX_TASK_FLAG_FOREGROUND))
992 	{
993 		printf("Starting Zabbix Server. Zabbix %s (revision %s).\nPress Ctrl+C to exit.\n\n",
994 				ZABBIX_VERSION, ZABBIX_REVISION);
995 	}
996 
997 	if (FAIL == zbx_ipc_service_init_env(CONFIG_SOCKET_PATH, &error))
998 	{
999 		zbx_error("Cannot initialize IPC services: %s", error);
1000 		zbx_free(error);
1001 		exit(EXIT_FAILURE);
1002 	}
1003 
1004 	if (SUCCEED != zbx_locks_create(&error))
1005 	{
1006 		zbx_error("cannot create locks: %s", error);
1007 		zbx_free(error);
1008 		exit(EXIT_FAILURE);
1009 	}
1010 
1011 	if (SUCCEED != zabbix_open_log(CONFIG_LOG_TYPE, CONFIG_LOG_LEVEL, CONFIG_LOG_FILE, &error))
1012 	{
1013 		zbx_error("cannot open log: %s", error);
1014 		zbx_free(error);
1015 		exit(EXIT_FAILURE);
1016 	}
1017 
1018 #ifdef HAVE_NETSNMP
1019 #	define SNMP_FEATURE_STATUS	"YES"
1020 #else
1021 #	define SNMP_FEATURE_STATUS	" NO"
1022 #endif
1023 #ifdef HAVE_OPENIPMI
1024 #	define IPMI_FEATURE_STATUS	"YES"
1025 #else
1026 #	define IPMI_FEATURE_STATUS	" NO"
1027 #endif
1028 #ifdef HAVE_LIBCURL
1029 #	define LIBCURL_FEATURE_STATUS	"YES"
1030 #else
1031 #	define LIBCURL_FEATURE_STATUS	" NO"
1032 #endif
1033 #if defined(HAVE_LIBCURL) && defined(HAVE_LIBXML2)
1034 #	define VMWARE_FEATURE_STATUS	"YES"
1035 #else
1036 #	define VMWARE_FEATURE_STATUS	" NO"
1037 #endif
1038 #ifdef HAVE_SMTP_AUTHENTICATION
1039 #	define SMTP_AUTH_FEATURE_STATUS	"YES"
1040 #else
1041 #	define SMTP_AUTH_FEATURE_STATUS	" NO"
1042 #endif
1043 #ifdef HAVE_UNIXODBC
1044 #	define ODBC_FEATURE_STATUS	"YES"
1045 #else
1046 #	define ODBC_FEATURE_STATUS	" NO"
1047 #endif
1048 #if defined(HAVE_SSH2) || defined(HAVE_SSH)
1049 #	define SSH_FEATURE_STATUS	"YES"
1050 #else
1051 #	define SSH_FEATURE_STATUS	" NO"
1052 #endif
1053 #ifdef HAVE_IPV6
1054 #	define IPV6_FEATURE_STATUS	"YES"
1055 #else
1056 #	define IPV6_FEATURE_STATUS	" NO"
1057 #endif
1058 #if defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
1059 #	define TLS_FEATURE_STATUS	"YES"
1060 #else
1061 #	define TLS_FEATURE_STATUS	" NO"
1062 #endif
1063 
1064 	zabbix_log(LOG_LEVEL_INFORMATION, "Starting Zabbix Server. Zabbix %s (revision %s).",
1065 			ZABBIX_VERSION, ZABBIX_REVISION);
1066 
1067 	zabbix_log(LOG_LEVEL_INFORMATION, "****** Enabled features ******");
1068 	zabbix_log(LOG_LEVEL_INFORMATION, "SNMP monitoring:           " SNMP_FEATURE_STATUS);
1069 	zabbix_log(LOG_LEVEL_INFORMATION, "IPMI monitoring:           " IPMI_FEATURE_STATUS);
1070 	zabbix_log(LOG_LEVEL_INFORMATION, "Web monitoring:            " LIBCURL_FEATURE_STATUS);
1071 	zabbix_log(LOG_LEVEL_INFORMATION, "VMware monitoring:         " VMWARE_FEATURE_STATUS);
1072 	zabbix_log(LOG_LEVEL_INFORMATION, "SMTP authentication:       " SMTP_AUTH_FEATURE_STATUS);
1073 	zabbix_log(LOG_LEVEL_INFORMATION, "ODBC:                      " ODBC_FEATURE_STATUS);
1074 	zabbix_log(LOG_LEVEL_INFORMATION, "SSH support:               " SSH_FEATURE_STATUS);
1075 	zabbix_log(LOG_LEVEL_INFORMATION, "IPv6 support:              " IPV6_FEATURE_STATUS);
1076 	zabbix_log(LOG_LEVEL_INFORMATION, "TLS support:               " TLS_FEATURE_STATUS);
1077 	zabbix_log(LOG_LEVEL_INFORMATION, "******************************");
1078 
1079 	zabbix_log(LOG_LEVEL_INFORMATION, "using configuration file: %s", CONFIG_FILE);
1080 
1081 #if defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
1082 	if (SUCCEED != zbx_coredump_disable())
1083 	{
1084 		zabbix_log(LOG_LEVEL_CRIT, "cannot disable core dump, exiting...");
1085 		exit(EXIT_FAILURE);
1086 	}
1087 #endif
1088 	if (FAIL == zbx_load_modules(CONFIG_LOAD_MODULE_PATH, CONFIG_LOAD_MODULE, CONFIG_TIMEOUT, 1))
1089 	{
1090 		zabbix_log(LOG_LEVEL_CRIT, "loading modules failed, exiting...");
1091 		exit(EXIT_FAILURE);
1092 	}
1093 
1094 	zbx_free_config();
1095 
1096 	if (SUCCEED != init_database_cache(&error))
1097 	{
1098 		zabbix_log(LOG_LEVEL_CRIT, "cannot initialize database cache: %s", error);
1099 		zbx_free(error);
1100 		exit(EXIT_FAILURE);
1101 	}
1102 
1103 	if (SUCCEED != init_configuration_cache(&error))
1104 	{
1105 		zabbix_log(LOG_LEVEL_CRIT, "cannot initialize configuration cache: %s", error);
1106 		zbx_free(error);
1107 		exit(EXIT_FAILURE);
1108 	}
1109 
1110 	if (SUCCEED != init_selfmon_collector(&error))
1111 	{
1112 		zabbix_log(LOG_LEVEL_CRIT, "cannot initialize self-monitoring: %s", error);
1113 		zbx_free(error);
1114 		exit(EXIT_FAILURE);
1115 	}
1116 
1117 	if (0 != CONFIG_VMWARE_FORKS && SUCCEED != zbx_vmware_init(&error))
1118 	{
1119 		zabbix_log(LOG_LEVEL_CRIT, "cannot initialize VMware cache: %s", error);
1120 		zbx_free(error);
1121 		exit(EXIT_FAILURE);
1122 	}
1123 
1124 	if (SUCCEED != zbx_vc_init(&error))
1125 	{
1126 		zabbix_log(LOG_LEVEL_CRIT, "cannot initialize history value cache: %s", error);
1127 		zbx_free(error);
1128 		exit(EXIT_FAILURE);
1129 	}
1130 
1131 	if (SUCCEED != zbx_create_itservices_lock(&error))
1132 	{
1133 		zabbix_log(LOG_LEVEL_CRIT, "cannot create IT services lock: %s", error);
1134 		zbx_free(error);
1135 		exit(EXIT_FAILURE);
1136 	}
1137 
1138 	if (SUCCEED != zbx_history_init(&error))
1139 	{
1140 		zabbix_log(LOG_LEVEL_CRIT, "cannot initialize history storage: %s", error);
1141 		zbx_free(error);
1142 		exit(EXIT_FAILURE);
1143 	}
1144 
1145 	if (FAIL == zbx_export_init(&error))
1146 	{
1147 		zabbix_log(LOG_LEVEL_CRIT, "cannot initialize export: %s", error);
1148 		zbx_free(error);
1149 		exit(EXIT_FAILURE);
1150 	}
1151 
1152 	if (ZBX_DB_UNKNOWN == (db_type = zbx_db_get_database_type()))
1153 	{
1154 		zabbix_log(LOG_LEVEL_CRIT, "cannot use database \"%s\": database is not a Zabbix database",
1155 				CONFIG_DBNAME);
1156 		exit(EXIT_FAILURE);
1157 	}
1158 	else if (ZBX_DB_SERVER != db_type)
1159 	{
1160 		zabbix_log(LOG_LEVEL_CRIT, "cannot use database \"%s\": its \"users\" table is empty (is this the"
1161 				" Zabbix proxy database?)", CONFIG_DBNAME);
1162 		exit(EXIT_FAILURE);
1163 	}
1164 
1165 	if (SUCCEED != DBcheck_version())
1166 		exit(EXIT_FAILURE);
1167 	DBcheck_character_set();
1168 
1169 	if (SUCCEED != DBcheck_double_type())
1170 	{
1171 		CONFIG_DOUBLE_PRECISION = ZBX_DB_DBL_PRECISION_DISABLED;
1172 		ZBX_DOUBLE_EPSILON = 0.000001;
1173 		zabbix_log(LOG_LEVEL_WARNING, "database is not upgraded to use double precision values");
1174 	}
1175 
1176 	DBcheck_capabilities();
1177 
1178 	if (SUCCEED != zbx_db_check_instanceid())
1179 		exit(EXIT_FAILURE);
1180 
1181 	threads_num = CONFIG_CONFSYNCER_FORKS + CONFIG_POLLER_FORKS
1182 			+ CONFIG_UNREACHABLE_POLLER_FORKS + CONFIG_TRAPPER_FORKS + CONFIG_PINGER_FORKS
1183 			+ CONFIG_ALERTER_FORKS + CONFIG_HOUSEKEEPER_FORKS + CONFIG_TIMER_FORKS
1184 			+ CONFIG_HTTPPOLLER_FORKS + CONFIG_DISCOVERER_FORKS + CONFIG_HISTSYNCER_FORKS
1185 			+ CONFIG_ESCALATOR_FORKS + CONFIG_IPMIPOLLER_FORKS + CONFIG_JAVAPOLLER_FORKS
1186 			+ CONFIG_SNMPTRAPPER_FORKS + CONFIG_PROXYPOLLER_FORKS + CONFIG_SELFMON_FORKS
1187 			+ CONFIG_VMWARE_FORKS + CONFIG_TASKMANAGER_FORKS + CONFIG_IPMIMANAGER_FORKS
1188 			+ CONFIG_ALERTMANAGER_FORKS + CONFIG_PREPROCMAN_FORKS + CONFIG_PREPROCESSOR_FORKS
1189 			+ CONFIG_LLDMANAGER_FORKS + CONFIG_LLDWORKER_FORKS + CONFIG_ALERTDB_FORKS;
1190 	threads = (pid_t *)zbx_calloc(threads, threads_num, sizeof(pid_t));
1191 	threads_flags = (int *)zbx_calloc(threads_flags, threads_num, sizeof(int));
1192 
1193 	if (0 != CONFIG_TRAPPER_FORKS)
1194 	{
1195 		if (FAIL == zbx_tcp_listen(&listen_sock, CONFIG_LISTEN_IP, (unsigned short)CONFIG_LISTEN_PORT))
1196 		{
1197 			zabbix_log(LOG_LEVEL_CRIT, "listener failed: %s", zbx_socket_strerror());
1198 			exit(EXIT_FAILURE);
1199 		}
1200 	}
1201 
1202 #if defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
1203 	zbx_tls_init_parent();
1204 #endif
1205 	zabbix_log(LOG_LEVEL_INFORMATION, "server #0 started [main process]");
1206 
1207 	for (i = 0; i < threads_num; i++)
1208 	{
1209 		zbx_thread_args_t	thread_args;
1210 		unsigned char		poller_type;
1211 
1212 		if (FAIL == get_process_info_by_thread(i + 1, &thread_args.process_type, &thread_args.process_num))
1213 		{
1214 			THIS_SHOULD_NEVER_HAPPEN;
1215 			exit(EXIT_FAILURE);
1216 		}
1217 
1218 		thread_args.server_num = i + 1;
1219 		thread_args.args = NULL;
1220 
1221 		switch (thread_args.process_type)
1222 		{
1223 			case ZBX_PROCESS_TYPE_CONFSYNCER:
1224 				zbx_thread_start(dbconfig_thread, &thread_args, &threads[i]);
1225 				DCconfig_wait_sync();
1226 
1227 				DBconnect(ZBX_DB_CONNECT_NORMAL);
1228 
1229 				if (SUCCEED != zbx_check_postinit_tasks(&error))
1230 				{
1231 					zabbix_log(LOG_LEVEL_CRIT, "cannot complete post initialization tasks: %s",
1232 							error);
1233 					zbx_free(error);
1234 					exit(EXIT_FAILURE);
1235 				}
1236 
1237 				/* update maintenance states */
1238 				zbx_dc_update_maintenances();
1239 
1240 				DBclose();
1241 
1242 				zbx_vc_enable();
1243 				break;
1244 			case ZBX_PROCESS_TYPE_POLLER:
1245 				poller_type = ZBX_POLLER_TYPE_NORMAL;
1246 				thread_args.args = &poller_type;
1247 				zbx_thread_start(poller_thread, &thread_args, &threads[i]);
1248 				break;
1249 			case ZBX_PROCESS_TYPE_UNREACHABLE:
1250 				poller_type = ZBX_POLLER_TYPE_UNREACHABLE;
1251 				thread_args.args = &poller_type;
1252 				zbx_thread_start(poller_thread, &thread_args, &threads[i]);
1253 				break;
1254 			case ZBX_PROCESS_TYPE_TRAPPER:
1255 				thread_args.args = &listen_sock;
1256 				zbx_thread_start(trapper_thread, &thread_args, &threads[i]);
1257 				break;
1258 			case ZBX_PROCESS_TYPE_PINGER:
1259 				zbx_thread_start(pinger_thread, &thread_args, &threads[i]);
1260 				break;
1261 			case ZBX_PROCESS_TYPE_ALERTER:
1262 				zbx_thread_start(alerter_thread, &thread_args, &threads[i]);
1263 				break;
1264 			case ZBX_PROCESS_TYPE_HOUSEKEEPER:
1265 				zbx_thread_start(housekeeper_thread, &thread_args, &threads[i]);
1266 				break;
1267 			case ZBX_PROCESS_TYPE_TIMER:
1268 				zbx_thread_start(timer_thread, &thread_args, &threads[i]);
1269 				break;
1270 			case ZBX_PROCESS_TYPE_HTTPPOLLER:
1271 				zbx_thread_start(httppoller_thread, &thread_args, &threads[i]);
1272 				break;
1273 			case ZBX_PROCESS_TYPE_DISCOVERER:
1274 				zbx_thread_start(discoverer_thread, &thread_args, &threads[i]);
1275 				break;
1276 			case ZBX_PROCESS_TYPE_HISTSYNCER:
1277 				threads_flags[i] = ZBX_THREAD_WAIT_EXIT;
1278 				zbx_thread_start(dbsyncer_thread, &thread_args, &threads[i]);
1279 				break;
1280 			case ZBX_PROCESS_TYPE_ESCALATOR:
1281 				zbx_thread_start(escalator_thread, &thread_args, &threads[i]);
1282 				break;
1283 			case ZBX_PROCESS_TYPE_JAVAPOLLER:
1284 				poller_type = ZBX_POLLER_TYPE_JAVA;
1285 				thread_args.args = &poller_type;
1286 				zbx_thread_start(poller_thread, &thread_args, &threads[i]);
1287 				break;
1288 			case ZBX_PROCESS_TYPE_SNMPTRAPPER:
1289 				zbx_thread_start(snmptrapper_thread, &thread_args, &threads[i]);
1290 				break;
1291 			case ZBX_PROCESS_TYPE_PROXYPOLLER:
1292 				zbx_thread_start(proxypoller_thread, &thread_args, &threads[i]);
1293 				break;
1294 			case ZBX_PROCESS_TYPE_SELFMON:
1295 				zbx_thread_start(selfmon_thread, &thread_args, &threads[i]);
1296 				break;
1297 			case ZBX_PROCESS_TYPE_VMWARE:
1298 				zbx_thread_start(vmware_thread, &thread_args, &threads[i]);
1299 				break;
1300 			case ZBX_PROCESS_TYPE_TASKMANAGER:
1301 				zbx_thread_start(taskmanager_thread, &thread_args, &threads[i]);
1302 				break;
1303 			case ZBX_PROCESS_TYPE_PREPROCMAN:
1304 				zbx_thread_start(preprocessing_manager_thread, &thread_args, &threads[i]);
1305 				break;
1306 			case ZBX_PROCESS_TYPE_PREPROCESSOR:
1307 				zbx_thread_start(preprocessing_worker_thread, &thread_args, &threads[i]);
1308 				break;
1309 #ifdef HAVE_OPENIPMI
1310 			case ZBX_PROCESS_TYPE_IPMIMANAGER:
1311 				zbx_thread_start(ipmi_manager_thread, &thread_args, &threads[i]);
1312 				break;
1313 			case ZBX_PROCESS_TYPE_IPMIPOLLER:
1314 				zbx_thread_start(ipmi_poller_thread, &thread_args, &threads[i]);
1315 				break;
1316 #endif
1317 			case ZBX_PROCESS_TYPE_ALERTMANAGER:
1318 				zbx_thread_start(alert_manager_thread, &thread_args, &threads[i]);
1319 				break;
1320 			case ZBX_PROCESS_TYPE_LLDMANAGER:
1321 				zbx_thread_start(lld_manager_thread, &thread_args, &threads[i]);
1322 				break;
1323 			case ZBX_PROCESS_TYPE_LLDWORKER:
1324 				zbx_thread_start(lld_worker_thread, &thread_args, &threads[i]);
1325 				break;
1326 			case ZBX_PROCESS_TYPE_ALERTSYNCER:
1327 				zbx_thread_start(alert_syncer_thread, &thread_args, &threads[i]);
1328 				break;
1329 		}
1330 	}
1331 
1332 	if (SUCCEED == zbx_is_export_enabled(ZBX_FLAG_EXPTYPE_EVENTS))
1333 		zbx_problems_export_init("main-process", 0);
1334 
1335 	if (SUCCEED == zbx_is_export_enabled(ZBX_FLAG_EXPTYPE_HISTORY))
1336 		zbx_history_export_init("main-process", 0);
1337 
1338 	if (SUCCEED == zbx_is_export_enabled(ZBX_FLAG_EXPTYPE_TRENDS))
1339 		zbx_trends_export_init("main-process", 0);
1340 
1341 
1342 	zbx_set_sigusr_handler(zbx_main_sigusr_handler);
1343 
1344 	while (-1 == wait(&i))	/* wait for any child to exit */
1345 	{
1346 		if (EINTR != errno)
1347 		{
1348 			zabbix_log(LOG_LEVEL_ERR, "failed to wait on child processes: %s", zbx_strerror(errno));
1349 			break;
1350 		}
1351 
1352 		/* check if the wait was interrupted because of diaginfo remote command */
1353 		if (ZBX_DIAGINFO_UNDEFINED != zbx_diaginfo_scope)
1354 		{
1355 			zbx_diag_log_info(zbx_diaginfo_scope);
1356 			zbx_diaginfo_scope = ZBX_DIAGINFO_UNDEFINED;
1357 		}
1358 	}
1359 
1360 	/* all exiting child processes should be caught by signal handlers */
1361 	THIS_SHOULD_NEVER_HAPPEN;
1362 
1363 	zbx_on_exit(FAIL);
1364 
1365 	return SUCCEED;
1366 }
1367 
zbx_on_exit(int ret)1368 void	zbx_on_exit(int ret)
1369 {
1370 	zabbix_log(LOG_LEVEL_DEBUG, "zbx_on_exit() called");
1371 
1372 	if (SUCCEED == DBtxn_ongoing())
1373 		DBrollback();
1374 
1375 	if (NULL != threads)
1376 	{
1377 		zbx_threads_wait(threads, threads_flags, threads_num, ret);	/* wait for all child processes to exit */
1378 		zbx_free(threads);
1379 		zbx_free(threads_flags);
1380 	}
1381 #ifdef HAVE_PTHREAD_PROCESS_SHARED
1382 	zbx_locks_disable();
1383 #endif
1384 	free_metrics();
1385 	zbx_ipc_service_free_env();
1386 
1387 	DBconnect(ZBX_DB_CONNECT_EXIT);
1388 
1389 	free_database_cache();
1390 
1391 	DBclose();
1392 
1393 	free_configuration_cache();
1394 
1395 	/* free history value cache */
1396 	zbx_vc_destroy();
1397 
1398 	zbx_destroy_itservices_lock();
1399 
1400 	/* free vmware support */
1401 	if (0 != CONFIG_VMWARE_FORKS)
1402 		zbx_vmware_destroy();
1403 
1404 	free_selfmon_collector();
1405 
1406 	zbx_uninitialize_events();
1407 
1408 	zbx_unload_modules();
1409 
1410 	zabbix_log(LOG_LEVEL_INFORMATION, "Zabbix Server stopped. Zabbix %s (revision %s).",
1411 			ZABBIX_VERSION, ZABBIX_REVISION);
1412 
1413 	zabbix_close_log();
1414 
1415 #if defined(PS_OVERWRITE_ARGV)
1416 	setproctitle_free_env();
1417 #endif
1418 
1419 	exit(EXIT_SUCCESS);
1420 }
1421