/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ int ja_tcp_accept(zbx_sock_t * s) { ZBX_SOCKADDR serv_addr; ZBX_SOCKLEN_T nlen; const char *__function_name = "ja_tcp_accept"; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_tcp_unaccept(s); nlen = sizeof(serv_addr); if (ZBX_SOCK_ERROR == (s->socket = (ZBX_SOCKET) accept(s->sockets[0], (struct sockaddr *) &serv_addr, &nlen))) { if (zbx_sock_last_error() == EINTR) return SUCCEED; zabbix_log(LOG_LEVEL_WARNING, "accept() failed: %s %d", strerror_from_system(zbx_sock_last_error()), zbx_sock_last_error()); return FAIL; } s->socket_orig = ZBX_SOCK_ERROR; s->accepted = 1; return SUCCEED; }
void main_trapper_loop(zbx_sock_t *s) { const char *__function_name = "main_trapper_loop"; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [waiting for connection]", get_process_type_string(process_type)); update_selfmon_counter(ZBX_PROCESS_STATE_IDLE); if (SUCCEED == zbx_tcp_accept(s)) { update_selfmon_counter(ZBX_PROCESS_STATE_BUSY); zbx_setproctitle("%s [processing data]", get_process_type_string(process_type)); process_trapper_child(s); zbx_tcp_unaccept(s); } else zabbix_log(LOG_LEVEL_WARNING, "Trapper failed to accept connection"); } }
void child_trapper_main(zbx_process_t p, zbx_sock_t *s) { struct sigaction phan; zabbix_log( LOG_LEVEL_DEBUG, "In child_trapper_main()"); phan.sa_handler = child_signal_handler; sigemptyset(&phan.sa_mask); phan.sa_flags = 0; sigaction(SIGALRM, &phan, NULL); zbx_process = p; DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("trapper [waiting for connection]"); if (SUCCEED == zbx_tcp_accept(s)) { zbx_setproctitle("trapper [processing data]"); process_trapper_child(s); zbx_tcp_unaccept(s); } else zabbix_log(LOG_LEVEL_WARNING, "Trapper failed to accept connection"); } DBclose(); }
ZBX_THREAD_ENTRY(trapper_thread, args) { double sec = 0.0; zbx_socket_t s; 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, "%s #%d started [%s #%d]", get_program_type_string(program_type), server_num, get_process_type_string(process_type), process_num); memcpy(&s, (zbx_socket_t *)((zbx_thread_args_t *)args)->args, sizeof(zbx_socket_t)); #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) zbx_tls_init_child(); find_psk_in_cache = DCget_psk_by_identity; #endif zbx_setproctitle("%s #%d [connecting to the database]", get_process_type_string(process_type), process_num); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_handle_log(); zbx_setproctitle("%s #%d [processed data in " ZBX_FS_DBL " sec, waiting for connection]", get_process_type_string(process_type), process_num, sec); update_selfmon_counter(ZBX_PROCESS_STATE_IDLE); /* Trapper has to accept all types of connections it can accept with the specified configuration. */ /* Only after receiving data it is known who has sent them and one can decide to accept or discard */ /* the data. */ if (SUCCEED == zbx_tcp_accept(&s, ZBX_TCP_SEC_TLS_CERT | ZBX_TCP_SEC_TLS_PSK | ZBX_TCP_SEC_UNENCRYPTED)) { zbx_timespec_t ts; /* get connection timestamp */ zbx_timespec(&ts); update_selfmon_counter(ZBX_PROCESS_STATE_BUSY); zbx_setproctitle("%s #%d [processing data]", get_process_type_string(process_type), process_num); sec = zbx_time(); process_trapper_child(&s, &ts); sec = zbx_time() - sec; zbx_tcp_unaccept(&s); } else if (EINTR != zbx_socket_last_error()) { zabbix_log(LOG_LEVEL_WARNING, "failed to accept an incoming connection: %s", zbx_socket_strerror()); } } }
int zbx_tcp_accept(zbx_sock_t *s) { struct sockaddr_storage serv_addr; fd_set sock_set; ZBX_SOCKET accepted_socket; socklen_t nlen; int i, n = 0; zbx_tcp_unaccept(s); FD_ZERO(&sock_set); /* For connection orientated protocols, we will handle the packets comprising a connection collectively. For datagram protocols, we have to handle each datagram individually. Check to see if we have any sockets remaining to be served from previous time through this loop. If not, call select() to wait for a connection request or a datagram to arrive. */ for(i = 0; i < s->num_socks; i++) { #if !defined(_WINDOWS) if(s->sockets[i] > n) n = s->sockets[i]; #endif if(FD_ISSET(s->sockets[i], &sock_set)) break; } if(i == s->num_socks) { for (i = 0; i < s->num_socks; i++) FD_SET(s->sockets[i], &sock_set); if(select(n + 1, &sock_set, 0, 0, 0) == ZBX_TCP_ERROR) { zbx_set_tcp_strerror("select() failed with error %d: %s", zbx_sock_last_error(), strerror_from_system(zbx_sock_last_error())); return FAIL; } } for (i = 0; i < s->num_socks; i++) { if (FD_ISSET(s->sockets[i], &sock_set)) { FD_CLR(s->sockets[i], &sock_set); break; } } /* Since this socket was returned by the select(), we know we have a connection waiting and that this accept() won't block.*/ nlen = sizeof(serv_addr); if((accepted_socket = (ZBX_SOCKET)accept(s->sockets[i], (struct sockaddr *)&serv_addr, &nlen)) == ZBX_SOCK_ERROR) { zbx_set_tcp_strerror("accept() failed with error %d: %s", zbx_sock_last_error(), strerror_from_system(zbx_sock_last_error())); return FAIL; } s->socket2 = s->socket; /* remember main socket */ s->socket = accepted_socket; /* replace socket to accepted */ s->accepted = 1; return SUCCEED; }
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, "%s #%d started [%s #%d]", get_daemon_type_string(daemon_type), 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 incoming connection"); local_request_failed = 0; } if (ZBX_IS_RUNNING()) zbx_sleep(1); } #ifdef _WINDOWS ZBX_DO_EXIT(); zbx_thread_exit(EXIT_SUCCESS); #endif }
/****************************************************************************** * * * Function: zbx_tcp_close * * * * Purpose: close open socket * * * * Author: Alexei Vladishev * * * ******************************************************************************/ void zbx_tcp_close(zbx_sock_t *s) { zbx_tcp_unaccept(s); zbx_tcp_free(s); zbx_tcp_timeout_cleanup(s); zbx_sock_close(s->socket); }
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); zabbix_log(LOG_LEVEL_WARNING, "agent #%d started [listener]", ((zbx_thread_args_t *)args)->thread_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 [waiting for connection]"); if (SUCCEED == (ret = zbx_tcp_accept(&s))) { local_request_failed = 0; /* reset consecutive errors counter */ zbx_setproctitle("listener [processing request]"); zabbix_log(LOG_LEVEL_DEBUG, "Processing request."); if (SUCCEED == (ret = zbx_tcp_check_security(&s, CONFIG_HOSTS_ALLOWED, 0))) process_listener(&s); zbx_tcp_unaccept(&s); } if (SUCCEED == ret) continue; zabbix_log(LOG_LEVEL_DEBUG, "Listener error: %s", zbx_tcp_strerror()); if (local_request_failed++ > 1000) { zabbix_log(LOG_LEVEL_WARNING, "Too many consecutive errors on accept() call."); local_request_failed = 0; } if (ZBX_IS_RUNNING()) zbx_sleep(1); } #ifdef _WINDOWS zabbix_log(LOG_LEVEL_INFORMATION, "zabbix_agentd listener stopped"); ZBX_DO_EXIT(); zbx_thread_exit(0); #endif }
ZBX_THREAD_ENTRY(listener_thread, pSock) { int ret, local_request_failed = 0; zbx_sock_t s; assert(pSock); zbx_setproctitle("listener waiting for connection"); zabbix_log( LOG_LEVEL_INFORMATION, "zabbix_agentd listener started"); memcpy(&s, ((zbx_sock_t *)pSock), sizeof(zbx_sock_t)); while(ZBX_IS_RUNNING) { if( SUCCEED == (ret = zbx_tcp_accept(&s)) ) { local_request_failed = 0; /* Reset consecutive errors counter */ zbx_setproctitle("processing request"); zabbix_log(LOG_LEVEL_DEBUG, "Processing request."); if( SUCCEED == (ret = zbx_tcp_check_security(&s, CONFIG_HOSTS_ALLOWED, 0)) ) { process_listener(&s); } zbx_tcp_unaccept(&s); } zbx_setproctitle("listener waiting for connection"); if( SUCCEED == ret ) continue; zabbix_log(LOG_LEVEL_DEBUG, "Listener error: %s", zbx_tcp_strerror()); if (local_request_failed++ > 1000) { zabbix_log( LOG_LEVEL_WARNING, "Too many consecutive errors on accept() call."); local_request_failed = 0; } if(ZBX_IS_RUNNING) zbx_sleep(1); } zabbix_log( LOG_LEVEL_INFORMATION, "zabbix_agentd listener stopped"); ZBX_DO_EXIT(); zbx_tread_exit(0); }
/****************************************************************************** * * * Function: zbx_tcp_close * * * * Purpose: close open socket * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ void zbx_tcp_close(zbx_sock_t *s) { zbx_tcp_unaccept(s); zbx_tcp_free(s); #if !defined(_WINDOWS) if (0 != s->timeout) alarm(0); #endif zbx_sock_close(s->socket); }
/****************************************************************************** * * * Function: zbx_tcp_accept * * * * Purpose: permits an incoming connection attempt on a socket * * * * Return value: SUCCEED - success * * FAIL - an error occurred * * * * Author: Eugene Grigorjev, Aleksandrs Saveljevs * * * ******************************************************************************/ int zbx_tcp_accept(zbx_sock_t *s) { ZBX_SOCKADDR serv_addr; fd_set sock_set; ZBX_SOCKET accepted_socket; ZBX_SOCKLEN_T nlen; int i, n = 0; zbx_tcp_unaccept(s); FD_ZERO(&sock_set); for (i = 0; i < s->num_socks; i++) { FD_SET(s->sockets[i], &sock_set); #if !defined(_WINDOWS) if (s->sockets[i] > n) n = s->sockets[i]; #endif } if (ZBX_TCP_ERROR == select(n + 1, &sock_set, NULL, NULL, NULL)) { zbx_set_tcp_strerror("select() failed: %s", strerror_from_system(zbx_sock_last_error())); return FAIL; } for (i = 0; i < s->num_socks; i++) { if (FD_ISSET(s->sockets[i], &sock_set)) break; } /* Since this socket was returned by select(), we know we have */ /* a connection waiting and that this accept() will not block. */ nlen = sizeof(serv_addr); if (ZBX_SOCK_ERROR == (accepted_socket = (ZBX_SOCKET)accept(s->sockets[i], (struct sockaddr *)&serv_addr, &nlen))) { zbx_set_tcp_strerror("accept() failed: %s", strerror_from_system(zbx_sock_last_error())); return FAIL; } s->socket_orig = s->socket; /* remember main socket */ s->socket = accepted_socket; /* replace socket to accepted */ s->accepted = 1; return SUCCEED; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ void main_jatrapper_loop(zbx_sock_t * s) { const char *__function_name = "main_jatrapper_loop"; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_setproctitle("%s [connecting to the database]", ja_get_process_type_string(process_type)); for (;;) { zbx_setproctitle("%s [waiting for connection]", ja_get_process_type_string(process_type)); ja_update_selfmon_counter(ZBX_PROCESS_STATE_IDLE); if (SUCCEED == zbx_tcp_accept(s)) { ja_update_selfmon_counter(ZBX_PROCESS_STATE_BUSY); zbx_setproctitle("%s [processing data]", get_process_type_string(process_type)); process_jatrapper(s); zbx_tcp_unaccept(s); } else { ja_log("JATRAPPER200039", 0, NULL, 0); } } }
int zbx_tcp_accept(zbx_sock_t *s) { ZBX_SOCKADDR serv_addr; ZBX_SOCKET accepted_socket; socklen_t nlen; nlen = sizeof(serv_addr); zbx_tcp_unaccept(s); if(ZBX_TCP_ERROR == (accepted_socket = (ZBX_SOCKET)accept(s->socket, (struct sockaddr *)&serv_addr, &nlen))) { zbx_set_tcp_strerror("accept() failed [%s]", strerror_from_system(zbx_sock_last_error())); return FAIL; } s->socket2 = s->socket; /* remember main socket */ s->socket = accepted_socket; /* replace socket to accepted */ s->accepted = 1; return SUCCEED; }
ZBX_THREAD_ENTRY(listener_thread, args) { #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) char *msg = NULL; #endif int ret; zbx_socket_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, "%s #%d started [%s #%d]", get_program_type_string(program_type), server_num, get_process_type_string(process_type), process_num); memcpy(&s, (zbx_socket_t *)((zbx_thread_args_t *)args)->args, sizeof(zbx_socket_t)); zbx_free(args); #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) zbx_tls_init_child(); #endif while (ZBX_IS_RUNNING()) { zbx_setproctitle("listener #%d [waiting for connection]", process_num); ret = zbx_tcp_accept(&s, configured_tls_accept_modes); zbx_update_env(zbx_time()); if (SUCCEED == ret) { zbx_setproctitle("listener #%d [processing request]", process_num); if ('\0' != *CONFIG_HOSTS_ALLOWED && SUCCEED == (ret = zbx_tcp_check_allowed_peers(&s, CONFIG_HOSTS_ALLOWED))) { #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) if (ZBX_TCP_SEC_TLS_CERT != s.connection_type || SUCCEED == (ret = zbx_check_server_issuer_subject(&s, &msg))) #endif { process_listener(&s); } } zbx_tcp_unaccept(&s); } if (SUCCEED == ret || EINTR == zbx_socket_last_error()) continue; #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) if (NULL != msg) { zabbix_log(LOG_LEVEL_WARNING, "failed to accept an incoming connection: %s", msg); zbx_free(msg); } else #endif { zabbix_log(LOG_LEVEL_WARNING, "failed to accept an incoming connection: %s", zbx_socket_strerror()); } if (ZBX_IS_RUNNING()) zbx_sleep(1); } #ifdef _WINDOWS ZBX_DO_EXIT(); zbx_thread_exit(EXIT_SUCCESS); #endif }