Merge branch 'collectd-5.6' into collectd-5.7
authorFlorian Forster <octo@collectd.org>
Fri, 29 Sep 2017 11:05:26 +0000 (13:05 +0200)
committerFlorian Forster <octo@collectd.org>
Fri, 29 Sep 2017 11:05:26 +0000 (13:05 +0200)
1  2 
src/dbi.c
src/email.c
src/exec.c
src/snmp.c
src/unixsock.c

diff --combined src/dbi.c
+++ b/src/dbi.c
@@@ -172,7 -172,9 +172,9 @@@ static void cdbi_database_free(cdbi_dat
      return;
  
    sfree(db->name);
+   sfree(db->select_db);
    sfree(db->driver);
+   sfree(db->host);
  
    for (size_t i = 0; i < db->driver_options_num; i++) {
      sfree(db->driver_options[i].key);
    if (db->q_prep_areas)
      for (size_t i = 0; i < db->queries_num; ++i)
        udb_query_delete_preparation_area(db->q_prep_areas[i]);
-   free(db->q_prep_areas);
+   sfree(db->q_prep_areas);
+   /* N.B.: db->queries references objects "owned" by the global queries
+    * variable. Free the array here, but not the content. */
+   sfree(db->queries);
  
    sfree(db);
  } /* }}} void cdbi_database_free */
@@@ -352,20 -357,23 +357,20 @@@ static int cdbi_config_add_database(oco
        ERROR("dbi plugin: realloc failed");
        status = -1;
      } else {
 -      char *name = NULL;
 -
        databases = temp;
        databases[databases_num] = db;
        databases_num++;
  
 -      name = ssnprintf_alloc("dbi:%s", db->name);
 -
 -      user_data_t ud = {.data = db};
 -
 +      char *name = ssnprintf_alloc("dbi:%s", db->name);
        plugin_register_complex_read(
            /* group = */ NULL,
            /* name = */ name ? name : db->name,
            /* callback = */ cdbi_read_database,
            /* interval = */ (db->interval > 0) ? db->interval : 0,
 -          /* user_data = */ &ud);
 -      free(name);
 +          &(user_data_t){
 +              .data = db,
 +          });
 +      sfree(name);
      }
    }
  
@@@ -406,13 -414,13 +411,13 @@@ static int cdbi_init(void) /* {{{ *
  
    if (queries_num == 0) {
      ERROR("dbi plugin: No <Query> blocks have been found. Without them, "
 -          "this plugin can't do anything useful, so we will returns an error.");
 +          "this plugin can't do anything useful, so we will return an error.");
      return (-1);
    }
  
    if (databases_num == 0) {
      ERROR("dbi plugin: No <Database> blocks have been found. Without them, "
 -          "this plugin can't do anything useful, so we will returns an error.");
 +          "this plugin can't do anything useful, so we will return an error.");
      return (-1);
    }
  
diff --combined src/email.c
@@@ -361,6 -361,8 +361,6 @@@ static void *collect(void *arg) 
  } /* static void *collect (void *) */
  
  static void *open_connection(void __attribute__((unused)) * arg) {
 -  struct sockaddr_un addr;
 -
    const char *path = (NULL == sock_file) ? SOCK_PATH : sock_file;
    const char *group = (NULL == sock_group) ? COLLECTD_GRP_NAME : sock_group;
  
      pthread_exit((void *)1);
    }
  
 -  addr.sun_family = AF_UNIX;
 +  struct sockaddr_un addr = {
 +    .sun_family = AF_UNIX
 +  };
    sstrncpy(addr.sun_path, path, (size_t)(UNIX_PATH_MAX - 1));
  
    errno = 0;
    {
      struct group sg;
      struct group *grp;
-     char grbuf[4096];
      int status;
  
+     long int grbuf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
+     if (grbuf_size <= 0)
+       grbuf_size = sysconf(_SC_PAGESIZE);
+     if (grbuf_size <= 0)
+       grbuf_size = 4096;
+     char grbuf[grbuf_size];
      grp = NULL;
      status = getgrnam_r(group, &sg, grbuf, sizeof(grbuf), &grp);
      if (status != 0) {
        collectors[i]->socket = NULL;
  
        if (plugin_thread_create(&collectors[i]->thread, &ptattr, collect,
 -                               collectors[i]) != 0) {
 +                               collectors[i], "email collector") != 0) {
          char errbuf[1024];
          log_err("plugin_thread_create() failed: %s",
                  sstrerror(errno, errbuf, sizeof(errbuf)));
  } /* static void *open_connection (void *) */
  
  static int email_init(void) {
 -  if (plugin_thread_create(&connector, NULL, open_connection, NULL) != 0) {
 +  if (plugin_thread_create(&connector, NULL, open_connection, NULL,
 +                           "email listener") != 0) {
      char errbuf[1024];
      disabled = 1;
      log_err("plugin_thread_create() failed: %s",
@@@ -614,10 -619,14 +620,10 @@@ static int email_shutdown(void) 
  
  static void email_submit(const char *type, const char *type_instance,
                           gauge_t value) {
 -  value_t values[1];
    value_list_t vl = VALUE_LIST_INIT;
  
 -  values[0].gauge = value;
 -
 -  vl.values = values;
 +  vl.values = &(value_t){.gauge = value};
    vl.values_len = 1;
 -  sstrncpy(vl.host, hostname_g, sizeof(vl.host));
    sstrncpy(vl.plugin, "email", sizeof(vl.plugin));
    sstrncpy(vl.type, type, sizeof(vl.type));
    sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
diff --combined src/exec.c
@@@ -250,7 -250,7 +250,7 @@@ static void set_environment(void) /* {{
              CDTIME_T_TO_DOUBLE(plugin_get_interval()));
    setenv("COLLECTD_INTERVAL", buffer, /* overwrite = */ 1);
  
 -  ssnprintf(buffer, sizeof(buffer), "%s", hostname_g);
 +  sstrncpy(buffer, hostname_g, sizeof(buffer));
    setenv("COLLECTD_HOSTNAME", buffer, /* overwrite = */ 1);
  #else
    ssnprintf(buffer, sizeof(buffer), "COLLECTD_INTERVAL=%.3f",
@@@ -369,11 -369,17 +369,17 @@@ static int fork_child(program_list_t *p
  
    struct passwd *sp_ptr;
    struct passwd sp;
-   char nambuf[4096];
  
    if (pl->pid != 0)
      return (-1);
  
+   long int nambuf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
+   if (nambuf_size <= 0)
+     nambuf_size = sysconf(_SC_PAGESIZE);
+   if (nambuf_size <= 0)
+     nambuf_size = 4096;
+   char nambuf[nambuf_size];
    if ((create_pipe(fd_pipe_in) == -1) || (create_pipe(fd_pipe_out) == -1) ||
        (create_pipe(fd_pipe_err) == -1))
      goto failed;
        struct group *gr_ptr = NULL;
        struct group gr;
  
-       status = getgrnam_r(pl->group, &gr, nambuf, sizeof(nambuf), &gr_ptr);
+       long int grbuf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
+       if (grbuf_size <= 0)
+         grbuf_size = sysconf(_SC_PAGESIZE);
+       if (grbuf_size <= 0)
+         grbuf_size = 4096;
+       char grbuf[grbuf_size];
+       status = getgrnam_r(pl->group, &gr, grbuf, sizeof(grbuf), &gr_ptr);
        if (0 != status) {
          ERROR("exec plugin: Failed to get group information "
                "for group ``%s'': %s",
@@@ -500,7 -513,7 +513,7 @@@ failed
  static int parse_line(char *buffer) /* {{{ */
  {
    if (strncasecmp("PUTVAL", buffer, strlen("PUTVAL")) == 0)
 -    return (handle_putval(stdout, buffer));
 +    return (cmd_handle_putval(stdout, buffer));
    else if (strncasecmp("PUTNOTIF", buffer, strlen("PUTNOTIF")) == 0)
      return (handle_putnotif(stdout, buffer));
    else {
@@@ -787,7 -800,7 +800,7 @@@ static int exec_read(void) /* {{{ *
  
      pthread_attr_init(&attr);
      pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 -    plugin_thread_create(&t, &attr, exec_read_one, (void *)pl);
 +    plugin_thread_create(&t, &attr, exec_read_one, (void *)pl, "exec read");
      pthread_attr_destroy(&attr);
    } /* for (pl) */
  
@@@ -826,8 -839,7 +839,8 @@@ static int exec_notification(const noti
  
      pthread_attr_init(&attr);
      pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 -    plugin_thread_create(&t, &attr, exec_notification_one, (void *)pln);
 +    plugin_thread_create(&t, &attr, exec_notification_one, (void *)pln,
 +                         "exec notify");
      pthread_attr_destroy(&attr);
    } /* for (pl) */
  
diff --combined src/snmp.c
@@@ -706,11 -706,11 +706,11 @@@ static int csnmp_config_add_host(oconfi
  
    ssnprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
  
 -  user_data_t ud = {.data = hd, .free_func = csnmp_host_definition_destroy};
 -
 -  status =
 -      plugin_register_complex_read(/* group = */ NULL, cb_name, csnmp_read_host,
 -                                   hd->interval, /* user_data = */ &ud);
 +  status = plugin_register_complex_read(
 +      /* group = */ NULL, cb_name, csnmp_read_host, hd->interval,
 +      &(user_data_t){
 +          .data = hd, .free_func = csnmp_host_definition_destroy,
 +      });
    if (status != 0) {
      ERROR("snmp plugin: Registering complex read function failed.");
      csnmp_host_definition_destroy(hd);
@@@ -1357,12 -1357,17 +1357,17 @@@ static int csnmp_read_table(host_defini
      if (oid_list_todo_num == 0) {
        /* The request is still empty - so we are finished */
        DEBUG("snmp plugin: all variables have left their subtree");
+       snmp_free_pdu(req);
        status = 0;
        break;
      }
  
      res = NULL;
      status = snmp_sess_synch_response(host->sess_handle, req, &res);
+     /* snmp_sess_synch_response always frees our req PDU */
+     req = NULL;
      if ((status != STAT_SUCCESS) || (res == NULL)) {
        char *errstr = NULL;
  
          snmp_free_pdu(res);
        res = NULL;
  
-       /* snmp_synch_response already freed our PDU */
-       req = NULL;
        sfree(errstr);
        csnmp_host_close_session(host);
  
      snmp_free_pdu(res);
    res = NULL;
  
-   if (req != NULL)
-     snmp_free_pdu(req);
-   req = NULL;
  
    if (status == 0)
      csnmp_dispatch_table(host, data, instance_list_head, value_list_head);
diff --combined src/unixsock.c
@@@ -134,7 -134,13 +134,13 @@@ static int us_open_socket(void) 
      const char *grpname;
      struct group *g;
      struct group sg;
-     char grbuf[4096];
+     long int grbuf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
+     if (grbuf_size <= 0)
+       grbuf_size = sysconf(_SC_PAGESIZE);
+     if (grbuf_size <= 0)
+       grbuf_size = 4096;
+     char grbuf[grbuf_size];
  
      grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
      g = NULL;
@@@ -257,17 -263,17 +263,17 @@@ static void *us_handle_client(void *arg
      }
  
      if (strcasecmp(fields[0], "getval") == 0) {
 -      handle_getval(fhout, buffer);
 +      cmd_handle_getval(fhout, buffer);
      } else if (strcasecmp(fields[0], "getthreshold") == 0) {
        handle_getthreshold(fhout, buffer);
      } else if (strcasecmp(fields[0], "putval") == 0) {
 -      handle_putval(fhout, buffer);
 +      cmd_handle_putval(fhout, buffer);
      } else if (strcasecmp(fields[0], "listval") == 0) {
 -      handle_listval(fhout, buffer);
 +      cmd_handle_listval(fhout, buffer);
      } else if (strcasecmp(fields[0], "putnotif") == 0) {
        handle_putnotif(fhout, buffer);
      } else if (strcasecmp(fields[0], "flush") == 0) {
 -      handle_flush(fhout, buffer);
 +      cmd_handle_flush(fhout, buffer);
      } else {
        if (fprintf(fhout, "-1 Unknown command: %s\n", fields[0]) < 0) {
          char errbuf[1024];
@@@ -328,7 -334,7 +334,7 @@@ static void *us_server_thread(void __at
      DEBUG("Spawning child to handle connection on fd #%i", *remote_fd);
  
      status = plugin_thread_create(&th, &th_attr, us_handle_client,
 -                                  (void *)remote_fd);
 +                                  (void *)remote_fd, "unixsock conn");
      if (status != 0) {
        char errbuf[1024];
        WARNING("unixsock plugin: pthread_create failed: %s",
@@@ -395,8 -401,7 +401,8 @@@ static int us_init(void) 
  
    loop = 1;
  
 -  status = plugin_thread_create(&listen_thread, NULL, us_server_thread, NULL);
 +  status = plugin_thread_create(&listen_thread, NULL, us_server_thread, NULL,
 +                                "unixsock listen");
    if (status != 0) {
      char errbuf[1024];
      ERROR("unixsock plugin: pthread_create failed: %s",