Merge branch 'collectd-5.4' into collectd-5.5
authorFlorian Forster <octo@collectd.org>
Tue, 8 Dec 2015 13:39:07 +0000 (14:39 +0100)
committerFlorian Forster <octo@collectd.org>
Tue, 8 Dec 2015 13:39:07 +0000 (14:39 +0100)
src/collectd-tg.c
src/email.c
src/ping.c
src/postgresql.c
src/python.c
src/rrdtool.c
src/tail.c

index 45e788c..08bb748 100644 (file)
@@ -188,6 +188,7 @@ static lcc_value_list_t *create_value_list (void) /* {{{ */
   strncpy (vl->identifier.type,
       (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive",
       sizeof (vl->identifier.type));
+  vl->identifier.type[sizeof (vl->identifier.type) - 1] = 0;
   snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance),
       "ti%li", random ());
 
index 365af27..042c077 100644 (file)
@@ -510,28 +510,42 @@ static void *open_connection (void __attribute__((unused)) *arg)
 
                pthread_mutex_unlock (&available_mutex);
 
-               do {
+               while (42) {
                        errno = 0;
-                       if (-1 == (remote = accept (connector_socket, NULL, NULL))) {
-                               if (EINTR != errno) {
-                                       char errbuf[1024];
-                                       disabled = 1;
-                                       close (connector_socket);
-                                       connector_socket = -1;
-                                       log_err ("accept() failed: %s",
-                                                       sstrerror (errno, errbuf, sizeof (errbuf)));
-                                       pthread_exit ((void *)1);
-                               }
+
+                       remote = accept (connector_socket, NULL, NULL);
+                       if (remote == -1) {
+                               char errbuf[1024];
+
+                               if (errno == EINTR)
+                                       continue;
+
+                               disabled = 1;
+                               close (connector_socket);
+                               connector_socket = -1;
+                               log_err ("accept() failed: %s",
+                                                sstrerror (errno, errbuf, sizeof (errbuf)));
+                               pthread_exit ((void *)1);
                        }
-               } while (EINTR == errno);
 
-               connection = (conn_t *)smalloc (sizeof (conn_t));
+                       /* access() succeeded. */
+                       break;
+               }
+
+               connection = malloc (sizeof (*connection));
+               if (connection != NULL)
+               {
+                       close (remote);
+                       continue;
+               }
+               memset (connection, 0, sizeof (*connection));
 
                connection->socket = fdopen (remote, "r");
                connection->next   = NULL;
 
                if (NULL == connection->socket) {
                        close (remote);
+                       sfree (connection);
                        continue;
                }
 
index d669aa9..c97571b 100644 (file)
@@ -241,7 +241,7 @@ static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
 
 static void *ping_thread (void *arg) /* {{{ */
 {
-  static pingobj_t *pingobj = NULL;
+  pingobj_t *pingobj = NULL;
 
   struct timeval  tv_begin;
   struct timeval  tv_end;
@@ -421,8 +421,10 @@ static int stop_thread (void) /* {{{ */
     status = -1;
   }
 
+  pthread_mutex_lock (&ping_lock);
   memset (&ping_thread_id, 0, sizeof (ping_thread_id));
   ping_thread_error = 0;
+  pthread_mutex_unlock (&ping_lock);
 
   return (status);
 } /* }}} int stop_thread */
index 73d590a..23c4af3 100644 (file)
@@ -1045,17 +1045,19 @@ static int config_query_param_add (udb_query_t *q, oconfig_item_t *ci)
 
        data = udb_query_get_user_data (q);
        if (NULL == data) {
-               data = (c_psql_user_data_t *) malloc (sizeof (*data));
+               data = malloc (sizeof (*data));
                if (NULL == data) {
                        log_err ("Out of memory.");
                        return -1;
                }
                memset (data, 0, sizeof (*data));
                data->params = NULL;
+               data->params_num = 0;
+
+               udb_query_set_user_data (q, data);
        }
 
-       tmp = (c_psql_param_t *) realloc (data->params,
-                       (data->params_num + 1) * sizeof (c_psql_param_t));
+       tmp = realloc (data->params, (data->params_num + 1) * sizeof (*data->params));
        if (NULL == tmp) {
                log_err ("Out of memory.");
                return -1;
@@ -1079,8 +1081,6 @@ static int config_query_param_add (udb_query_t *q, oconfig_item_t *ci)
        }
 
        data->params_num++;
-       udb_query_set_user_data (q, data);
-
        return (0);
 } /* config_query_param_add */
 
@@ -1164,7 +1164,7 @@ static int c_psql_config_writer (oconfig_item_t *ci)
 
        writers = tmp;
        writer  = writers + writers_num;
-       ++writers_num;
+       memset (writer, 0, sizeof (*writer));
 
        writer->name = sstrdup (ci->values[0].value.string);
        writer->statement = NULL;
@@ -1184,10 +1184,10 @@ static int c_psql_config_writer (oconfig_item_t *ci)
        if (status != 0) {
                sfree (writer->statement);
                sfree (writer->name);
-               sfree (writer);
                return status;
        }
 
+       ++writers_num;
        return 0;
 } /* c_psql_config_writer */
 
index e7795c7..c7a82c4 100644 (file)
@@ -305,21 +305,34 @@ void cpy_log_exception(const char *context) {
        list = PyObject_CallFunction(cpy_format_exception, "NNN", type, value, traceback); /* New reference. Steals references from "type", "value" and "traceback". */
        if (list)
                l = PyObject_Length(list);
+
        for (i = 0; i < l; ++i) {
-               char *s;
                PyObject *line;
-               
+               char const *msg;
+               char *cpy;
+
                line = PyList_GET_ITEM(list, i); /* Borrowed reference. */
                Py_INCREF(line);
-               s = strdup(cpy_unicode_or_bytes_to_string(&line));
+
+               msg = cpy_unicode_or_bytes_to_string(&line);
                Py_DECREF(line);
-               if (s[strlen(s) - 1] == '\n')
-                       s[strlen(s) - 1] = 0;
+               if (msg == NULL)
+                       continue;
+
+               cpy = strdup(msg);
+               if (cpy == NULL)
+                       continue;
+
+               if (cpy[strlen(cpy) - 1] == '\n')
+                       cpy[strlen(cpy) - 1] = 0;
+
                Py_BEGIN_ALLOW_THREADS
-               ERROR("%s", s);
+               ERROR("%s", cpy);
                Py_END_ALLOW_THREADS
-               free(s);
+
+               free(cpy);
        }
+
        Py_XDECREF(list);
        PyErr_Clear();
 }
@@ -547,7 +560,12 @@ static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args
 
        Py_INCREF(callback);
        Py_XINCREF(data);
+
        c = malloc(sizeof(*c));
+       if (c == NULL)
+               return NULL;
+       memset (c, 0, sizeof (*c));
+
        c->name = strdup(buf);
        c->callback = callback;
        c->data = data;
@@ -618,7 +636,7 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec
        char buf[512];
        reg_function_t *register_function = (reg_function_t *) reg;
        cpy_callback_t *c = NULL;
-       user_data_t *user_data = NULL;
+       user_data_t user_data;
        char *name = NULL;
        PyObject *callback = NULL, *data = NULL;
        static char *kwlist[] = {"callback", "data", "name", NULL};
@@ -634,22 +652,29 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec
        
        Py_INCREF(callback);
        Py_XINCREF(data);
+
        c = malloc(sizeof(*c));
+       if (c == NULL)
+               return NULL;
+       memset (c, 0, sizeof (*c));
+
        c->name = strdup(buf);
        c->callback = callback;
        c->data = data;
        c->next = NULL;
-       user_data = malloc(sizeof(*user_data));
-       user_data->free_func = cpy_destroy_user_data;
-       user_data->data = c;
-       register_function(buf, handler, user_data);
+
+       memset (&user_data, 0, sizeof (user_data));
+       user_data.free_func = cpy_destroy_user_data;
+       user_data.data = c;
+
+       register_function(buf, handler, &user_data);
        return cpy_string_to_unicode_or_bytes(buf);
 }
 
 static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwds) {
        char buf[512];
        cpy_callback_t *c = NULL;
-       user_data_t *user_data = NULL;
+       user_data_t user_data;
        double interval = 0;
        char *name = NULL;
        PyObject *callback = NULL, *data = NULL;
@@ -667,18 +692,26 @@ static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwd
        
        Py_INCREF(callback);
        Py_XINCREF(data);
+
        c = malloc(sizeof(*c));
+       if (c == NULL)
+               return NULL;
+       memset (c, 0, sizeof (*c));
+
        c->name = strdup(buf);
        c->callback = callback;
        c->data = data;
        c->next = NULL;
-       user_data = malloc(sizeof(*user_data));
-       user_data->free_func = cpy_destroy_user_data;
-       user_data->data = c;
+
+       memset (&user_data, 0, sizeof (user_data));
+       user_data.free_func = cpy_destroy_user_data;
+       user_data.data = c;
+
        ts.tv_sec = interval;
        ts.tv_nsec = (interval - ts.tv_sec) * 1000000000;
-       plugin_register_complex_read(/* group = */ NULL, buf,
-                       cpy_read_callback, &ts, user_data);
+       plugin_register_complex_read(/* group = */ "python", buf,
+                       cpy_read_callback, &ts, &user_data);
+
        return cpy_string_to_unicode_or_bytes(buf);
 }
 
@@ -778,7 +811,7 @@ static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *ar
        for (tmp = *list_head; tmp; prev = tmp, tmp = tmp->next)
                if (strcmp(name, tmp->name) == 0)
                        break;
-       
+
        Py_DECREF(arg);
        if (tmp == NULL) {
                PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
index 9b5723a..9b04d10 100644 (file)
@@ -1008,23 +1008,32 @@ static int rrd_config (const char *key, const char *value)
        }
        else if (strcasecmp ("DataDir", key) == 0)
        {
-               if (datadir != NULL)
-                       free (datadir);
-               datadir = strdup (value);
-               if (datadir != NULL)
+               char *tmp;
+               size_t len;
+
+               tmp = strdup (value);
+               if (tmp == NULL)
                {
-                       int len = strlen (datadir);
-                       while ((len > 0) && (datadir[len - 1] == '/'))
-                       {
-                               len--;
-                               datadir[len] = '\0';
-                       }
-                       if (len <= 0)
-                       {
-                               free (datadir);
-                               datadir = NULL;
-                       }
+                       ERROR ("rrdtool plugin: strdup failed.");
+                       return (1);
+               }
+
+               len = strlen (datadir);
+               while ((len > 0) && (datadir[len - 1] == '/'))
+               {
+                       len--;
+                       datadir[len] = 0;
                }
+
+               if (len == 0)
+               {
+                       ERROR ("rrdtool plugin: Invalid \"DataDir\" option.");
+                       sfree (tmp);
+                       return (1);
+               }
+
+               sfree (datadir);
+               datadir = tmp;
        }
        else if (strcasecmp ("StepSize", key) == 0)
        {
index 3904f1b..708cd2a 100644 (file)
@@ -236,7 +236,7 @@ static int ctail_config_add_file (oconfig_item_t *ci)
   if (tm == NULL)
   {
     ERROR ("tail plugin: tail_match_create (%s) failed.",
-       ci->values[0].value.string);
+        ci->values[0].value.string);
     return (-1);
   }
 
@@ -253,7 +253,7 @@ static int ctail_config_add_file (oconfig_item_t *ci)
     {
       status = ctail_config_add_match (tm, plugin_instance, option, interval);
       if (status == 0)
-       num_matches++;
+        num_matches++;
       /* Be mild with failed matches.. */
       status = 0;
     }
@@ -266,10 +266,12 @@ static int ctail_config_add_file (oconfig_item_t *ci)
       break;
   } /* for (i = 0; i < ci->children_num; i++) */
 
+  sfree (plugin_instance);
+
   if (num_matches == 0)
   {
     ERROR ("tail plugin: No (valid) matches found for file `%s'.",
-       ci->values[0].value.string);
+        ci->values[0].value.string);
     tail_match_destroy (tm);
     return (-1);
   }
@@ -278,7 +280,7 @@ static int ctail_config_add_file (oconfig_item_t *ci)
     cu_tail_match_t **temp;
 
     temp = (cu_tail_match_t **) realloc (tail_match_list,
-       sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
+        sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
     if (temp == NULL)
     {
       ERROR ("tail plugin: realloc failed.");