1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
** Zabbix
** Copyright (C) 2001-2014 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "listener.h"
#include "zbxself.h"
#include "comms.h"
#include "cfg.h"
#include "zbxconf.h"
#include "stats.h"
#include "sysinfo.h"
#include "log.h"
extern unsigned char process_type;
extern int server_num, process_num;
#if defined(ZABBIX_SERVICE)
# include "service.h"
#elif defined(ZABBIX_DAEMON)
# include "daemon.h"
#endif
static void process_listener(zbx_sock_t *s)
{
AGENT_RESULT result;
char *command;
char **value = NULL;
int ret;
if (SUCCEED == (ret = zbx_tcp_recv_to(s, &command, CONFIG_TIMEOUT)))
{
zbx_rtrim(command, "\r\n");
zabbix_log(LOG_LEVEL_DEBUG, "Requested [%s]", command);
init_result(&result);
process(command, 0, &result);
if (NULL == (value = GET_TEXT_RESULT(&result)))
value = GET_MSG_RESULT(&result);
if (NULL != value)
{
zabbix_log(LOG_LEVEL_DEBUG, "Sending back [%s]", *value);
ret = zbx_tcp_send_to(s, *value, CONFIG_TIMEOUT);
}
free_result(&result);
}
if (FAIL == ret)
zabbix_log(LOG_LEVEL_DEBUG, "Process listener error: %s", zbx_tcp_strerror());
}
ZBX_THREAD_ENTRY(listener_thread, args)
{
int ret, local_request_failed = 0;
zbx_sock_t s;
assert(args);
assert(((zbx_thread_args_t *)args)->args);
process_type = ((zbx_thread_args_t *)args)->process_type;
server_num = ((zbx_thread_args_t *)args)->server_num;
process_num = ((zbx_thread_args_t *)args)->process_num;
zabbix_log(LOG_LEVEL_INFORMATION, "agent #%d started [%s #%d]",
server_num, get_process_type_string(process_type), process_num);
memcpy(&s, (zbx_sock_t *)((zbx_thread_args_t *)args)->args, sizeof(zbx_sock_t));
zbx_free(args);
while (ZBX_IS_RUNNING())
{
zbx_setproctitle("listener #%d [waiting for connection]", process_num);
if (SUCCEED == (ret = zbx_tcp_accept(&s)))
{
local_request_failed = 0; /* reset consecutive errors counter */
zbx_setproctitle("listener #%d [processing request]", process_num);
if (SUCCEED == (ret = zbx_tcp_check_security(&s, CONFIG_HOSTS_ALLOWED, 0)))
process_listener(&s);
zbx_tcp_unaccept(&s);
}
if (SUCCEED == ret || EINTR == zbx_sock_last_error())
continue;
zabbix_log(LOG_LEVEL_DEBUG, "failed to accept an incoming connection: %s", zbx_tcp_strerror());
if (local_request_failed++ > 1000)
{
zabbix_log(LOG_LEVEL_WARNING, "too many failures to accept an icoming connection");
local_request_failed = 0;
}
if (ZBX_IS_RUNNING())
zbx_sleep(1);
}
#ifdef _WINDOWS
ZBX_DO_EXIT();
zbx_thread_exit(0);
#endif
}
|