Replace all occurrences of `strcpy' with `sstrncpy'.
[collectd.git] / src / network.c
index a0e1383..022e46a 100644 (file)
@@ -183,7 +183,7 @@ 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;
 
 /*
@@ -738,7 +738,8 @@ static int parse_packet (void *buffer, int buffer_len)
                else if (pkg_type == TYPE_TIME)
                {
                        uint64_t tmp = 0;
-                       status = parse_part_number (&buffer, &buffer_len, &tmp);
+                       status = parse_part_number (&buffer, &buffer_len,
+                                       &tmp);
                        if (status == 0)
                        {
                                vl.time = (time_t) tmp;
@@ -748,7 +749,8 @@ static int parse_packet (void *buffer, int buffer_len)
                else if (pkg_type == TYPE_INTERVAL)
                {
                        uint64_t tmp = 0;
-                       status = parse_part_number (&buffer, &buffer_len, &tmp);
+                       status = parse_part_number (&buffer, &buffer_len,
+                                       &tmp);
                        if (status == 0)
                                vl.interval = (int) tmp;
                }
@@ -756,47 +758,53 @@ static int parse_packet (void *buffer, int buffer_len)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        vl.host, sizeof (vl.host));
-                       strncpy (n.host, vl.host, sizeof (n.host));
-                       n.host[sizeof (n.host) - 1] = '\0';
+                       if (status == 0)
+                               sstrncpy (n.host, vl.host, sizeof (n.host));
                }
                else if (pkg_type == TYPE_PLUGIN)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        vl.plugin, sizeof (vl.plugin));
-                       strncpy (n.plugin, vl.plugin, sizeof (n.plugin));
-                       n.plugin[sizeof (n.plugin) - 1] = '\0';
+                       if (status == 0)
+                               sstrncpy (n.plugin, vl.plugin,
+                                               sizeof (n.plugin));
                }
                else if (pkg_type == TYPE_PLUGIN_INSTANCE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        vl.plugin_instance,
                                        sizeof (vl.plugin_instance));
-                       strncpy (n.plugin_instance, vl.plugin_instance,
-                                       sizeof (n.plugin_instance));
-                       n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
+                       if (status == 0)
+                               sstrncpy (n.plugin_instance,
+                                               vl.plugin_instance,
+                                               sizeof (n.plugin_instance));
                }
                else if (pkg_type == TYPE_TYPE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        type, sizeof (type));
-                       strncpy (n.type, type, sizeof (n.type));
-                       n.type[sizeof (n.type) - 1] = '\0';
+                       if (status == 0)
+                               sstrncpy (n.type, type, sizeof (n.type));
                }
                else if (pkg_type == TYPE_TYPE_INSTANCE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        vl.type_instance,
                                        sizeof (vl.type_instance));
-                       strncpy (n.type_instance, vl.type_instance,
-                                       sizeof (n.type_instance));
-                       n.type_instance[sizeof (n.type_instance) - 1] = '\0';
+                       if (status == 0)
+                               sstrncpy (n.type_instance, vl.type_instance,
+                                               sizeof (n.type_instance));
                }
                else if (pkg_type == TYPE_MESSAGE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        n.message, sizeof (n.message));
 
-                       if ((n.severity != NOTIF_FAILURE)
+                       if (status != 0)
+                       {
+                               /* do nothing */
+                       }
+                       else if ((n.severity != NOTIF_FAILURE)
                                        && (n.severity != NOTIF_WARNING)
                                        && (n.severity != NOTIF_OKAY))
                        {
@@ -819,17 +827,14 @@ static int parse_packet (void *buffer, int buffer_len)
                        }
                        else
                        {
-                               /*
-                                * TODO: Let this do a separate thread so that
-                                * no packets are lost if this takes too long.
-                                */
                                plugin_dispatch_notification (&n);
                        }
                }
                else if (pkg_type == TYPE_SEVERITY)
                {
                        uint64_t tmp = 0;
-                       status = parse_part_number (&buffer, &buffer_len, &tmp);
+                       status = parse_part_number (&buffer, &buffer_len,
+                                       &tmp);
                        if (status == 0)
                                n.severity = (int) tmp;
                }
@@ -1400,7 +1405,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)
@@ -1424,7 +1429,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)
@@ -1433,7 +1438,7 @@ 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)
@@ -1441,7 +1446,7 @@ static int add_to_buffer (char *buffer, int buffer_size,
                if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
                                        ds->type, strlen (ds->type)) != 0)
                        return (-1);
-               strcpy (type_def, ds->type);
+               sstrncpy (type_def, ds->type, sizeof (type_def));
        }
 
        if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
@@ -1450,7 +1455,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)
@@ -1701,11 +1706,19 @@ 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;