Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / network.c
index e150364..902f270 100644 (file)
@@ -178,12 +178,11 @@ static char         send_buffer[BUFF_SIZE];
 static char        *send_buffer_ptr;
 static int          send_buffer_fill;
 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
-static char         send_buffer_type[DATA_MAX_NAME_LEN];
 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
 
 static c_avl_tree_t      *cache_tree = NULL;
 static pthread_mutex_t  cache_lock = PTHREAD_MUTEX_INITIALIZER;
-static time_t           cache_flush_last;
+static time_t           cache_flush_last = 0;
 static int              cache_flush_interval = 1800;
 
 /*
@@ -246,7 +245,7 @@ static int cache_flush (void)
        return (0);
 } /* int cache_flush */
 
-static int cache_check (const char *type, const value_list_t *vl)
+static int cache_check (const value_list_t *vl)
 {
        char key[1024];
        time_t *value = NULL;
@@ -256,7 +255,7 @@ static int cache_check (const char *type, const value_list_t *vl)
                return (-1);
 
        if (format_name (key, sizeof (key), vl->host, vl->plugin,
-                               vl->plugin_instance, type, vl->type_instance))
+                               vl->plugin_instance, vl->type, vl->type_instance))
                return (-1);
 
        pthread_mutex_lock (&cache_lock);
@@ -499,7 +498,7 @@ static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
 
        exp_size = 3 * sizeof (uint16_t)
                + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
-       if (buffer_len < exp_size)
+       if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
        {
                WARNING ("network plugin: parse_part_values: "
                                "Packet too short: "
@@ -563,7 +562,7 @@ static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
        uint16_t pkg_length;
        uint16_t pkg_type;
 
-       if (buffer_len < exp_size)
+       if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
        {
                WARNING ("network plugin: parse_part_number: "
                                "Packet too short: "
@@ -603,7 +602,7 @@ static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
        uint16_t pkg_length;
        uint16_t pkg_type;
 
-       if (buffer_len < header_size)
+       if ((buffer_len < 0) || ((size_t) buffer_len < header_size))
        {
                WARNING ("network plugin: parse_part_string: "
                                "Packet too short: "
@@ -645,7 +644,8 @@ static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
        /* Check that the package data fits into the output buffer.
         * The previous if-statement ensures that:
         * `pkg_length > header_size' */
-       if ((pkg_length - header_size) > output_len)
+       if ((output_len < 0)
+                       || ((size_t) output_len < ((size_t) pkg_length - header_size)))
        {
                WARNING ("network plugin: parse_part_string: "
                                "Output buffer too small.");
@@ -678,14 +678,12 @@ static int parse_packet (void *buffer, int buffer_len)
        int status;
 
        value_list_t vl = VALUE_LIST_INIT;
-       char type[DATA_MAX_NAME_LEN];
        notification_t n;
 
        DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
                        buffer, buffer_len);
 
        memset (&vl, '\0', sizeof (vl));
-       memset (&type, '\0', sizeof (type));
        memset (&n, '\0', sizeof (n));
        status = 0;
 
@@ -722,10 +720,10 @@ static int parse_packet (void *buffer, int buffer_len)
                        if ((vl.time > 0)
                                        && (strlen (vl.host) > 0)
                                        && (strlen (vl.plugin) > 0)
-                                       && (strlen (type) > 0)
-                                       && (cache_check (type, &vl) == 0))
+                                       && (strlen (vl.type) > 0)
+                                       && (cache_check (&vl) == 0))
                        {
-                               plugin_dispatch_values (type, &vl);
+                               plugin_dispatch_values (&vl);
                        }
                        else
                        {
@@ -782,9 +780,9 @@ static int parse_packet (void *buffer, int buffer_len)
                else if (pkg_type == TYPE_TYPE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
-                                       type, sizeof (type));
+                                       vl.type, sizeof (vl.type));
                        if (status == 0)
-                               sstrncpy (n.type, type, sizeof (n.type));
+                               sstrncpy (n.type, vl.type, sizeof (n.type));
                }
                else if (pkg_type == TYPE_TYPE_INSTANCE)
                {
@@ -872,11 +870,12 @@ static void free_sockent (sockent_t *se)
  */
 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
 {
+       DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
+                       network_config_ttl);
+
        if ((network_config_ttl < 1) || (network_config_ttl > 255))
                return (-1);
 
-       DEBUG ("ttl = %i", network_config_ttl);
-
        if (ai->ai_family == AF_INET)
        {
                struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
@@ -1243,7 +1242,7 @@ static int network_add_sending_socket (const char *node, const char *service)
        return (0);
 } /* int network_get_listen_socket */
 
-static void *dispatch_thread (void *arg)
+static void *dispatch_thread (void __attribute__((unused)) *arg)
 {
   while (42)
   {
@@ -1282,6 +1281,9 @@ static int network_receive (void)
        int i;
        int status;
 
+       receive_list_entry_t *private_list_head;
+       receive_list_entry_t *private_list_tail;
+
        if (listen_sockets_num == 0)
                network_add_listen_socket (NULL, NULL);
 
@@ -1291,6 +1293,9 @@ static int network_receive (void)
                return (-1);
        }
 
+       private_list_head = NULL;
+       private_list_tail = NULL;
+
        while (listen_loop == 0)
        {
                status = poll (listen_sockets, listen_sockets_num, -1);
@@ -1331,7 +1336,8 @@ static int network_receive (void)
                                ERROR ("network plugin: malloc failed.");
                                return (-1);
                        }
-                       memset (ent, '\0', sizeof (receive_list_entry_t));
+                       memset (ent, 0, sizeof (receive_list_entry_t));
+                       ent->next = NULL;
 
                        /* Hopefully this be optimized out by the compiler. It
                         * might help prevent stupid bugs in the future though.
@@ -1341,26 +1347,53 @@ static int network_receive (void)
                        memcpy (ent->data, buffer, buffer_len);
                        ent->data_len = buffer_len;
 
-                       pthread_mutex_lock (&receive_list_lock);
-                       if (receive_list_head == NULL)
-                       {
-                               receive_list_head = ent;
-                               receive_list_tail = ent;
-                       }
+                       if (private_list_head == NULL)
+                               private_list_head = ent;
                        else
+                               private_list_tail->next = ent;
+                       private_list_tail = ent;
+
+                       /* Do not block here. Blocking here has led to
+                        * insufficient performance in the past. */
+                       if (pthread_mutex_trylock (&receive_list_lock) == 0)
                        {
-                               receive_list_tail->next = ent;
-                               receive_list_tail = ent;
+                               if (receive_list_head == NULL)
+                                       receive_list_head = private_list_head;
+                               else
+                                       receive_list_tail->next = private_list_head;
+                               receive_list_tail = private_list_tail;
+
+                               private_list_head = NULL;
+                               private_list_tail = NULL;
+
+                               pthread_cond_signal (&receive_list_cond);
+                               pthread_mutex_unlock (&receive_list_lock);
                        }
-                       pthread_cond_signal (&receive_list_cond);
-                       pthread_mutex_unlock (&receive_list_lock);
                } /* for (listen_sockets) */
        } /* while (listen_loop == 0) */
 
+       /* Make sure everything is dispatched before exiting. */
+       if (private_list_head != NULL)
+       {
+               pthread_mutex_lock (&receive_list_lock);
+
+               if (receive_list_head == NULL)
+                       receive_list_head = private_list_head;
+               else
+                       receive_list_tail->next = private_list_head;
+               receive_list_tail = private_list_tail;
+
+               private_list_head = NULL;
+               private_list_tail = NULL;
+
+               pthread_cond_signal (&receive_list_cond);
+               pthread_mutex_unlock (&receive_list_lock);
+       }
+
        return (0);
 }
 
-static void *receive_thread (void *arg)
+static void *receive_thread (void __attribute__((unused)) *arg)
 {
        return (network_receive () ? (void *) 1 : (void *) 0);
 } /* void *receive_thread */
@@ -1395,7 +1428,7 @@ static void network_send_buffer (const char *buffer, int buffer_len)
 } /* void network_send_buffer */
 
 static int add_to_buffer (char *buffer, int buffer_size,
-               value_list_t *vl_def, char *type_def,
+               value_list_t *vl_def,
                const data_set_t *ds, const value_list_t *vl)
 {
        char *buffer_orig = buffer;
@@ -1405,7 +1438,7 @@ static int add_to_buffer (char *buffer, int buffer_size,
                if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
                                        vl->host, strlen (vl->host)) != 0)
                        return (-1);
-               strcpy (vl_def->host, vl->host);
+               sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
        }
 
        if (vl_def->time != vl->time)
@@ -1429,7 +1462,7 @@ static int add_to_buffer (char *buffer, int buffer_size,
                if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
                                        vl->plugin, strlen (vl->plugin)) != 0)
                        return (-1);
-               strcpy (vl_def->plugin, vl->plugin);
+               sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
        }
 
        if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
@@ -1438,15 +1471,15 @@ static int add_to_buffer (char *buffer, int buffer_size,
                                        vl->plugin_instance,
                                        strlen (vl->plugin_instance)) != 0)
                        return (-1);
-               strcpy (vl_def->plugin_instance, vl->plugin_instance);
+               sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
        }
 
-       if (strcmp (type_def, ds->type) != 0)
+       if (strcmp (vl_def->type, vl->type) != 0)
        {
                if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
-                                       ds->type, strlen (ds->type)) != 0)
+                                       vl->type, strlen (vl->type)) != 0)
                        return (-1);
-               strcpy (type_def, ds->type);
+               sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
        }
 
        if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
@@ -1455,7 +1488,7 @@ static int add_to_buffer (char *buffer, int buffer_size,
                                        vl->type_instance,
                                        strlen (vl->type_instance)) != 0)
                        return (-1);
-               strcpy (vl_def->type_instance, vl->type_instance);
+               sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
        }
        
        if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
@@ -1472,8 +1505,7 @@ static void flush_buffer (void)
        network_send_buffer (send_buffer, send_buffer_fill);
        send_buffer_ptr  = send_buffer;
        send_buffer_fill = 0;
-       memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
-       memset (send_buffer_type, '\0', sizeof (send_buffer_type));
+       memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
 }
 
 static int network_write (const data_set_t *ds, const value_list_t *vl)
@@ -1483,7 +1515,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl)
        /* If the value is already in the cache, we have received it via the
         * network. We write it again if forwarding is activated. It's then in
         * the cache and should we receive it again we will ignore it. */
-       status = cache_check (ds->type, vl);
+       status = cache_check (vl);
        if ((network_config_forward == 0)
                        && (status != 0))
                return (0);
@@ -1492,7 +1524,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl)
 
        status = add_to_buffer (send_buffer_ptr,
                        sizeof (send_buffer) - send_buffer_fill,
-                       &send_buffer_vl, send_buffer_type,
+                       &send_buffer_vl,
                        ds, vl);
        if (status >= 0)
        {
@@ -1506,7 +1538,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl)
 
                status = add_to_buffer (send_buffer_ptr,
                                sizeof (send_buffer) - send_buffer_fill,
-                               &send_buffer_vl, send_buffer_type,
+                               &send_buffer_vl,
                                ds, vl);
 
                if (status >= 0)
@@ -1550,7 +1582,10 @@ static int network_config (const char *key, const char *val)
                fields_num = strsplit (val_cpy, fields, 3);
                if ((fields_num != 1)
                                && (fields_num != 2))
+               {
+                       sfree (val_cpy);
                        return (1);
+               }
                else if (fields_num == 2)
                {
                        if ((service = strchr (fields[1], '.')) != NULL)
@@ -1563,6 +1598,8 @@ static int network_config (const char *key, const char *val)
                        network_add_listen_socket (node, service);
                else
                        network_add_sending_socket (node, service);
+
+               sfree (val_cpy);
        }
        else if (strcasecmp ("TimeToLive", key) == 0)
        {
@@ -1706,17 +1743,24 @@ static int network_shutdown (void)
        plugin_unregister_write ("network");
        plugin_unregister_shutdown ("network");
 
+       /* Let the init function do it's move again ;) */
+       cache_flush_last = 0;
+
        return (0);
 } /* int network_shutdown */
 
 static int network_init (void)
 {
+       /* Check if we were already initialized. If so, just return - there's
+        * nothing more to do (for now, that is). */
+       if (cache_flush_last != 0)
+               return (0);
+
        plugin_register_shutdown ("network", network_shutdown);
 
        send_buffer_ptr  = send_buffer;
        send_buffer_fill = 0;
-       memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
-       memset (send_buffer_type, '\0', sizeof (send_buffer_type));
+       memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
 
        cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
        cache_flush_last = time (NULL);
@@ -1759,7 +1803,15 @@ static int network_init (void)
        return (0);
 } /* int network_init */
 
-static int network_flush (int timeout)
+/* 
+ * The flush option of the network plugin cannot flush individual identifiers.
+ * All the values are added to a buffer and sent when the buffer is full, the
+ * requested value may or may not be in there, it's not worth finding out. We
+ * just send the buffer if `flush'  is called - if the requested value was in
+ * there, good. If not, well, then there is nothing to flush.. -octo
+ */
+static int network_flush (int timeout,
+               const char __attribute__((unused)) *identifier)
 {
        pthread_mutex_lock (&send_buffer_lock);