X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fnetwork.c;h=a2221e5b4661f8f5410cf2e701896ecbbd0b24a3;hb=49ac868d2346c77335272b8a643bca295db9feaa;hp=bf0b2bc647e63f7de410e561dd2d9b16817fbd7c;hpb=5c78be034ebf8edbf2ae98e9163a38b2d61fbb68;p=collectd.git diff --git a/src/network.c b/src/network.c index bf0b2bc6..7c15e8f9 100644 --- a/src/network.c +++ b/src/network.c @@ -22,6 +22,7 @@ * Aman Gupta **/ +#define _DEFAULT_SOURCE #define _BSD_SOURCE /* For struct ip_mreq */ #include "collectd.h" @@ -76,7 +77,9 @@ /* Re enable deprecation warnings */ # pragma GCC diagnostic warning "-Wdeprecated-declarations" # endif +# if GCRYPT_VERSION_NUMBER < 0x010600 GCRY_THREAD_OPTION_PTHREAD_IMPL; +# endif #endif #ifndef IPV6_ADD_MEMBERSHIP @@ -497,6 +500,8 @@ static int network_dispatch_notification (notification_t *n) /* {{{ */ #if HAVE_LIBGCRYPT static void network_init_gcrypt (void) /* {{{ */ { + gcry_error_t err; + /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html * Because you can't know in a library whether another library has * already initialized the library */ @@ -510,9 +515,24 @@ static void network_init_gcrypt (void) /* {{{ */ * above doesn't count, as it doesn't implicitly initalize Libgcrypt. * * tl;dr: keep all these gry_* statements in this exact order please. */ - gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); +# if GCRYPT_VERSION_NUMBER < 0x010600 + err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + if (err) + { + ERROR ("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s", gcry_strerror (err)); + abort (); + } +# endif + gcry_check_version (NULL); - gcry_control (GCRYCTL_INIT_SECMEM, 32768); + + err = gcry_control (GCRYCTL_INIT_SECMEM, 32768); + if (err) + { + ERROR ("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s", gcry_strerror (err)); + abort (); + } + gcry_control (GCRYCTL_INITIALIZATION_FINISHED); } /* }}} void network_init_gcrypt */ @@ -1439,6 +1459,7 @@ static int parse_packet (sockent_t *se, /* {{{ */ printed_ignore_warning = 1; } buffer = ((char *) buffer) + pkg_length; + buffer_size -= (size_t) pkg_length; continue; } #endif /* HAVE_LIBGCRYPT */ @@ -1466,6 +1487,7 @@ static int parse_packet (sockent_t *se, /* {{{ */ printed_ignore_warning = 1; } buffer = ((char *) buffer) + pkg_length; + buffer_size -= (size_t) pkg_length; continue; } #endif /* HAVE_LIBGCRYPT */ @@ -1607,6 +1629,7 @@ static int parse_packet (sockent_t *se, /* {{{ */ DEBUG ("network plugin: parse_packet: Unknown part" " type: 0x%04hx", pkg_type); buffer = ((char *) buffer) + pkg_length; + buffer_size -= (size_t) pkg_length; } } /* while (buffer_size > sizeof (part_header_t)) */ @@ -2022,6 +2045,7 @@ static sockent_t *sockent_create (int type) /* {{{ */ if (type == SOCKENT_TYPE_SERVER) { se->data.server.fd = NULL; + se->data.server.fd_num = 0; #if HAVE_LIBGCRYPT se->data.server.security_level = SECURITY_LEVEL_NONE; se->data.server.auth_file = NULL; @@ -2235,6 +2259,9 @@ static int sockent_server_listen (sockent_t *se) /* {{{ */ if (se == NULL) return (-1); + assert (se->data.server.fd == NULL); + assert (se->data.server.fd_num == 0); + node = se->node; service = se->service; @@ -2434,13 +2461,13 @@ static int network_receive (void) /* {{{ */ int buffer_len; int i; - int status; + int status = 0; receive_list_entry_t *private_list_head; receive_list_entry_t *private_list_tail; uint64_t private_list_length; - assert (listen_sockets_num > 0); + assert (listen_sockets_num > 0); private_list_head = NULL; private_list_tail = NULL; @@ -2449,15 +2476,14 @@ static int network_receive (void) /* {{{ */ while (listen_loop == 0) { status = poll (listen_sockets_pollfd, listen_sockets_num, -1); - if (status <= 0) { char errbuf[1024]; if (errno == EINTR) continue; - ERROR ("poll failed: %s", + ERROR ("network plugin: poll(2) failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); + break; } for (i = 0; (i < listen_sockets_num) && (status > 0); i++) @@ -2475,10 +2501,10 @@ static int network_receive (void) /* {{{ */ if (buffer_len < 0) { char errbuf[1024]; - ERROR ("recv failed: %s", - sstrerror (errno, errbuf, - sizeof (errbuf))); - return (-1); + status = (errno != 0) ? errno : -1; + ERROR ("network plugin: recv(2) failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + break; } stats_octets_rx += ((uint64_t) buffer_len); @@ -2492,7 +2518,8 @@ static int network_receive (void) /* {{{ */ if (ent == NULL) { ERROR ("network plugin: malloc failed."); - return (-1); + status = ENOMEM; + break; } memset (ent, 0, sizeof (receive_list_entry_t)); ent->data = malloc (network_config_packet_size); @@ -2500,7 +2527,8 @@ static int network_receive (void) /* {{{ */ { sfree (ent); ERROR ("network plugin: malloc failed."); - return (-1); + status = ENOMEM; + break; } ent->fd = listen_sockets_pollfd[i].fd; ent->next = NULL; @@ -2536,7 +2564,12 @@ static int network_receive (void) /* {{{ */ private_list_tail = NULL; private_list_length = 0; } + + status = 0; } /* for (listen_sockets_pollfd) */ + + if (status != 0) + break; } /* while (listen_loop == 0) */ /* Make sure everything is dispatched before exiting. */ @@ -2551,15 +2584,11 @@ static int network_receive (void) /* {{{ */ receive_list_tail = private_list_tail; receive_list_length += private_list_length; - private_list_head = NULL; - private_list_tail = NULL; - private_list_length = 0; - pthread_cond_signal (&receive_list_cond); pthread_mutex_unlock (&receive_list_lock); } - return (0); + return (status); } /* }}} int network_receive */ static void *receive_thread (void __attribute__((unused)) *arg) @@ -3002,7 +3031,7 @@ static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */ network_config_ttl = tmp; else { WARNING ("network plugin: The `TimeToLive' must be between 1 and 255."); - return (-1); + return (-1); } return (0);