int server( int listen_port, const char* host_ip, int host_port ) { #if !defined(HTTP_PROXY) && !defined(SERVER) assert( NULL != host_ip ); #endif int sockfd = 0; if( (sockfd=socket(AF_INET,SOCK_STREAM,0) ) == -1 ) { SERVER_DEBUG( "server socket error:%s\n", strerror(errno) ); return -1; } SERVER_DEBUG( "server socket successfully\n" ); if( setsockopt_wrapper( sockfd ) < 0 ) { SERVER_DEBUG( "setsockopt_wrapper error:%s\n", strerror(errno) ); close( sockfd ); return -1; } struct sockaddr_in server_addr; memset( &server_addr, 0, sizeof(server_addr) ); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons( listen_port ); if( bind( sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr) ) == - 1 ) { SERVER_DEBUG( "server bind error:%s\n", strerror(errno) ); close( sockfd ); return -1; } SERVER_DEBUG( "server bind successfully\n" ); if( listen( sockfd, BACK_LOG_DEFAULT ) == -1 ) { SERVER_DEBUG( "server listen error:%s\n", strerror(errno) ); close( sockfd ); return -1; } SERVER_DEBUG( "server listen successfully port = %d\n", listen_port ); #if defined(EPOLL) return epoll_server( sockfd ); #endif parent = getpid(); struct pthread_vargs *list = NULL; while(1) { if( exit_flag ) { break; } int sockfd_new = handle_new_connection( sockfd ); if( sockfd_new < 0 ) { SERVER_DEBUG( "handle_new_connection\n" ); break; } #if !defined(MULTI_PTHREAD) pid_t child = fork(); if( child < 0 ) { SERVER_DEBUG( "fork error:%s\n", strerror(errno) ); close( sockfd_new ); break; }else if( child == 0 ){ SERVER_DEBUG( "child process\n" ); close( sockfd ); #if defined(PROXY) if( process_proxy( sockfd_new, host_ip, host_port ) < 0 ) { SERVER_DEBUG( "process_proxy error:%s\n", strerror(errno) ); close( sockfd_new ); break; } #elif defined(HTTP_PROXY) if( process_http_proxy( sockfd_new ) < 0 ) { SERVER_DEBUG( "process_http_proxy error\n" ); close( sockfd_new ); break; } #elif defined(SERVER) if( process_server( sockfd_new ) < 0 ) { SERVER_DEBUG( "process_server error:%s\n", strerror(errno) ); close( sockfd_new ); break; } #endif } else { int status; SERVER_DEBUG( "waitpid\n" ); close( sockfd_new ); waitpid( -1, &status, WNOHANG ); } #elif defined(MULTI_PTHREAD) struct pthread_vargs *pv = (struct pthread_vargs *)malloc(sizeof(struct pthread_vargs)); if( NULL == pv ) { SERVER_DEBUG( "malloc error:%s\n", strerror(errno) ); close( sockfd_new ); break; } memset( pv, 0, sizeof(struct pthread_vargs) ); if( NULL == list ) { list = pv; list->next = NULL; } else { struct pthread_vargs *iter = list; while( NULL != iter->next ) { iter = iter->next; } iter->next = pv; pv->next = NULL; iter = NULL; } pv->sockfd = sockfd_new; if( pthread_create( &pv->pid, NULL, pthread_http_proxy, pv ) < 0 ) { SERVER_DEBUG( "pthread_create error\n" ); close( sockfd_new ); break; } #endif } #if defined(MULTI_PTHREAD) (void)pthread_join_wrapper( list ); pthread_vargs_cleanup( list ); #endif close( sockfd ); SERVER_DEBUG( "server close\n" ); return 0; }
ZBX_THREAD_ENTRY(proxypoller_thread, args) { int nextcheck, sleeptime = -1, processed = 0, old_processed = 0; double sec, total_sec = 0.0, old_total_sec = 0.0; time_t last_stat_time; 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); #define STAT_INTERVAL 5 /* if a process is busy and does not sleep then update status not faster than */ /* once in STAT_INTERVAL seconds */ zbx_setproctitle("%s #%d [connecting to the database]", get_process_type_string(process_type), process_num); last_stat_time = time(NULL); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { if (0 != sleeptime) { zbx_setproctitle("%s #%d [exchanged data with %d proxies in " ZBX_FS_DBL " sec," " exchanging data]", get_process_type_string(process_type), process_num, old_processed, old_total_sec); } sec = zbx_time(); processed += process_proxy(); total_sec += zbx_time() - sec; nextcheck = DCconfig_get_proxypoller_nextcheck(); sleeptime = calculate_sleeptime(nextcheck, POLLER_DELAY); if (0 != sleeptime || STAT_INTERVAL <= time(NULL) - last_stat_time) { if (0 == sleeptime) { zbx_setproctitle("%s #%d [exchanged data with %d proxies in " ZBX_FS_DBL " sec," " exchanging data]", get_process_type_string(process_type), process_num, processed, total_sec); } else { zbx_setproctitle("%s #%d [exchanged data with %d proxies in " ZBX_FS_DBL " sec," " idle %d sec]", get_process_type_string(process_type), process_num, processed, total_sec, sleeptime); old_processed = processed; old_total_sec = total_sec; } processed = 0; total_sec = 0.0; last_stat_time = time(NULL); } zbx_sleep_loop(sleeptime); } #undef STAT_INTERVAL }