Merge remote-tracking branch 'github/pr/1931'
[collectd.git] / src / hugepages.c
index e7b98da..c3060fe 100644 (file)
@@ -67,14 +67,14 @@ static int huge_config_callback(const char *key, const char *val)
 static void submit_hp(const char *plug_inst, const char *type,
   const char *type_instance, gauge_t free_value, gauge_t used_value)
 {
-  value_t values[2];
   value_list_t vl = VALUE_LIST_INIT;
-
-  values[0].gauge = free_value;
-  values[1].gauge = used_value;
+  value_t values[] = {
+    { .gauge = free_value },
+    { .gauge = used_value },
+  };
 
   vl.values = values;
-  vl.values_len = 2;
+  vl.values_len = STATIC_ARRAY_SIZE (values);
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, g_plugin_name, sizeof (vl.plugin));
   sstrncpy (vl.plugin_instance, plug_inst, sizeof (vl.plugin_instance));
@@ -187,6 +187,7 @@ static int read_syshugepages(const char* path, const char* node)
   while ((result = readdir(dir)) != NULL) {
     if (strncmp(result->d_name, hugepages_dir, sizeof(hugepages_dir)-1)) {
       /* not node dir */
+      errno = 0;
       continue;
     }
 
@@ -196,10 +197,17 @@ static int read_syshugepages(const char* path, const char* node)
     e_info.d_name = result->d_name;
     e_info.node = node;
     walk_directory(path2, read_hugepage_entry, &e_info, 0);
+    errno = 0;
   }
 
-  closedir(dir);
+  /* Check if NULL return from readdir() was an error */
+  if (errno != 0) {
+      ERROR("%s: readdir failed", g_plugin_name);
+      closedir(dir);
+      return -1;
+  }
 
+  closedir(dir);
   return 0;
 }
 
@@ -220,7 +228,7 @@ static int read_nodes(void)
   }
 
   errno = 0;
-  if ((lim = pathconf(path, _PC_NAME_MAX)) == -1) {
+  if ((lim = pathconf(sys_node, _PC_NAME_MAX)) == -1) {
     /* Limit not defined if errno == 0, otherwise error */
     if (errno != 0) {
       ERROR("%s: pathconf failed", g_plugin_name);
@@ -234,15 +242,23 @@ static int read_nodes(void)
   while ((result = readdir(dir)) != NULL) {
     if (strncmp(result->d_name, node_string, sizeof(node_string)-1)) {
       /* not node dir */
+      errno = 0;
       continue;
     }
 
     ssnprintf(path, (size_t) lim, sys_node_hugepages, result->d_name);
     read_syshugepages(path, result->d_name);
+    errno = 0;
   }
 
-  closedir(dir);
+  /* Check if NULL return from readdir() was an error */
+  if (errno != 0) {
+      ERROR("%s: readdir failed", g_plugin_name);
+      closedir(dir);
+      return -1;
+  }
 
+  closedir(dir);
   return 0;
 }