]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Load plugin: rewrite Linux implementation using `read_text_file_contents`.
authorFlorian Forster <octo@collectd.org>
Thu, 14 Dec 2023 20:55:20 +0000 (21:55 +0100)
committerFlorian Forster <octo@collectd.org>
Fri, 15 Dec 2023 19:30:53 +0000 (20:30 +0100)
src/load.c

index 69953fef22d3b64231a1fde446ee656213fec8e6..1629095853cb36465b6a3e8a7c41b682508a6db9 100644 (file)
@@ -121,38 +121,23 @@ static int load_read(void) {
     /* #endif HAVE_GETLOADAVG */
 
 #elif defined(KERNEL_LINUX)
-  gauge_t snum, mnum, lnum;
-  FILE *loadavg;
-  char buffer[16];
-
-  char *fields[8];
-  int numfields;
-
-  if ((loadavg = fopen("/proc/loadavg", "r")) == NULL) {
-    WARNING("load: fopen: %s", STRERRNO);
-    return -1;
-  }
+  char buffer[64] = {0};
 
-  if (fgets(buffer, 16, loadavg) == NULL) {
-    WARNING("load: fgets: %s", STRERRNO);
-    fclose(loadavg);
-    return -1;
+  ssize_t status =
+      read_text_file_contents("/proc/loadavg", buffer, sizeof(buffer));
+  if (status < 0) {
+    ERROR("load plugin: Reading \"/proc/loadavg\" failed.");
+    return (int)status;
   }
 
-  if (fclose(loadavg)) {
-    WARNING("load: fclose: %s", STRERRNO);
+  char *fields[4] = {NULL};
+  int numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
+  if (numfields < 3) {
+    ERROR("load plugin: strsplit returned %d field(s), want 3", numfields);
+    return EIO;
   }
 
-  numfields = strsplit(buffer, fields, 8);
-
-  if (numfields < 3)
-    return -1;
-
-  snum = atof(fields[0]);
-  mnum = atof(fields[1]);
-  lnum = atof(fields[2]);
-
-  load_submit(snum, mnum, lnum);
+  load_submit(atof(fields[0]), atof(fields[1]), atof(fields[2]));
   /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBSTATGRAB