Merge branch 'collectd-5.8'
[collectd.git] / src / thermal.c
index d2d31e1..959fec6 100644 (file)
@@ -35,22 +35,18 @@ static const char *config_keys[] = {"Device", "IgnoreSelected",
 static const char *const dirname_sysfs = "/sys/class/thermal";
 static const char *const dirname_procfs = "/proc/acpi/thermal_zone";
 
-static _Bool force_procfs = 0;
+static bool force_procfs;
 static ignorelist_t *device_list;
 
 enum dev_type { TEMP = 0, COOLING_DEV };
 
 static void thermal_submit(const char *plugin_instance, enum dev_type dt,
-                           gauge_t value) {
+                           value_t value) {
   value_list_t vl = VALUE_LIST_INIT;
-  value_t v;
-
-  v.gauge = value;
-  vl.values = &v;
 
+  vl.values = &value;
   vl.values_len = 1;
 
-  sstrncpy(vl.host, hostname_g, sizeof(vl.host));
   sstrncpy(vl.plugin, "thermal", sizeof(vl.plugin));
   if (plugin_instance != NULL)
     sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
@@ -62,55 +58,27 @@ static void thermal_submit(const char *plugin_instance, enum dev_type dt,
 static int thermal_sysfs_device_read(const char __attribute__((unused)) * dir,
                                      const char *name,
                                      void __attribute__((unused)) * user_data) {
-  char filename[256];
-  char data[1024];
-  int len;
-  _Bool success = 0;
+  char filename[PATH_MAX];
+  bool success = false;
+  value_t value;
 
   if (device_list && ignorelist_match(device_list, name))
     return -1;
 
-  len =
-      ssnprintf(filename, sizeof(filename), "%s/%s/temp", dirname_sysfs, name);
-  if ((len < 0) || ((size_t)len >= sizeof(filename)))
-    return -1;
-
-  len = (ssize_t)read_file_contents(filename, data, sizeof(data));
-  if (len > 1 && data[--len] == '\n') {
-    char *endptr = NULL;
-    double temp;
-
-    data[len] = 0;
-    errno = 0;
-    temp = strtod(data, &endptr) / 1000.0;
-
-    if (endptr == data + len && errno == 0) {
-      thermal_submit(name, TEMP, temp);
-      success = 1;
-    }
+  snprintf(filename, sizeof(filename), "%s/%s/temp", dirname_sysfs, name);
+  if (parse_value_file(filename, &value, DS_TYPE_GAUGE) == 0) {
+    value.gauge /= 1000.0;
+    thermal_submit(name, TEMP, value);
+    success = true;
   }
 
-  len = ssnprintf(filename, sizeof(filename), "%s/%s/cur_state", dirname_sysfs,
-                  name);
-  if ((len < 0) || ((size_t)len >= sizeof(filename)))
-    return -1;
-
-  len = (ssize_t)read_file_contents(filename, data, sizeof(data));
-  if (len > 1 && data[--len] == '\n') {
-    char *endptr = NULL;
-    double state;
-
-    data[len] = 0;
-    errno = 0;
-    state = strtod(data, &endptr);
-
-    if (endptr == data + len && errno == 0) {
-      thermal_submit(name, COOLING_DEV, state);
-      success = 1;
-    }
+  snprintf(filename, sizeof(filename), "%s/%s/cur_state", dirname_sysfs, name);
+  if (parse_value_file(filename, &value, DS_TYPE_GAUGE) == 0) {
+    thermal_submit(name, COOLING_DEV, value);
+    success = true;
   }
 
-  return (success ? 0 : -1);
+  return success ? 0 : -1;
 }
 
 static int thermal_procfs_device_read(const char __attribute__((unused)) * dir,
@@ -130,8 +98,8 @@ static int thermal_procfs_device_read(const char __attribute__((unused)) * dir,
    * temperature:             55 C
    */
 
-  len = ssnprintf(filename, sizeof(filename), "%s/%s/temperature",
-                  dirname_procfs, name);
+  len = snprintf(filename, sizeof(filename), "%s/%s/temperature",
+                 dirname_procfs, name);
   if ((len < 0) || ((size_t)len >= sizeof(filename)))
     return -1;
 
@@ -166,7 +134,7 @@ static int thermal_procfs_device_read(const char __attribute__((unused)) * dir,
     temp = (strtod(data + len, &endptr) + add) * factor;
 
     if (endptr != data + len && errno == 0) {
-      thermal_submit(name, TEMP, temp);
+      thermal_submit(name, TEMP, (value_t){.gauge = temp});
       return 0;
     }
   }
@@ -189,9 +157,9 @@ static int thermal_config(const char *key, const char *value) {
     if (IS_TRUE(value))
       ignorelist_set_invert(device_list, 0);
   } else if (strcasecmp(key, "ForceUseProcfs") == 0) {
-    force_procfs = 0;
+    force_procfs = false;
     if (IS_TRUE(value))
-      force_procfs = 1;
+      force_procfs = true;
   } else {
     return -1;
   }
@@ -200,13 +168,11 @@ static int thermal_config(const char *key, const char *value) {
 }
 
 static int thermal_sysfs_read(void) {
-  return walk_directory(dirname_sysfs, thermal_sysfs_device_read,
-                        /* user_data = */ NULL, /* include hidden */ 0);
+  return walk_directory(dirname_sysfs, thermal_sysfs_device_read, NULL, 0);
 }
 
 static int thermal_procfs_read(void) {
-  return walk_directory(dirname_procfs, thermal_procfs_device_read,
-                        /* user_data = */ NULL, /* include hidden */ 0);
+  return walk_directory(dirname_procfs, thermal_procfs_device_read, NULL, 0);
 }
 
 static int thermal_init(void) {