]> git.sur5r.net Git - i3/i3status/commitdiff
make modules more resilient to failure
authorConnor Lane Smith <cls@lubutu.com>
Thu, 25 Aug 2011 21:24:06 +0000 (22:24 +0100)
committerMichael Stapelberg <michael@stapelberg.de>
Thu, 25 Aug 2011 21:27:10 +0000 (23:27 +0200)
src/print_cpu_temperature.c
src/print_cpu_usage.c
src/print_load.c

index 3bc4636f5926d1c90c0c6b5bf209945b8ca4c92a..8c343c26c138c0d77d3716ebd3c1391ddb7dca36 100644 (file)
@@ -41,7 +41,7 @@ void print_cpu_temperature_info(int zone, const char *path, const char *format)
 #if defined(LINUX)
                         long int temp;
                         if (!slurp(path, buf, sizeof(buf)))
-                                die("Could not open \"%s\"\n", path);
+                                goto error;
                         temp = strtol(buf, NULL, 10);
                         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
                                 (void)printf("?");
@@ -50,15 +50,16 @@ void print_cpu_temperature_info(int zone, const char *path, const char *format)
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
                         int sysctl_rslt;
                         size_t sysctl_size = sizeof(sysctl_rslt);
-                        if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0)) {
-                                (void)printf("No thermal zone found");
-                                return;
-                        }
+                        if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
+                                goto error;
 
                         (void)printf("%d.%d", TZ_KELVTOC(sysctl_rslt));
 #endif
                         walk += strlen("degrees");
                 }
         }
+        return;
+error:
 #endif
+        (void)fputs("Cannot read temperature\n", stderr);
 }
index 71fc5b7b97ea02a8da441e600e86f35cae99b7b2..544d6cff11b8f2277bfa3042387bb70736eff558 100644 (file)
@@ -29,11 +29,9 @@ void print_cpu_usage(const char *format) {
 #if defined(LINUX)
         static char statpath[512];
         strcpy(statpath, "/proc/stat");
-        if (!slurp(statpath, buf, sizeof(buf)))
-                die("could not read %s\n", statpath);
-
-        if (sscanf(buf, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle) != 4)
-                die("could not read cpu utilization\n");
+        if (!slurp(statpath, buf, sizeof(buf) ||
+            sscanf(buf, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle) != 4))
+                goto error;
 
         curr_total = curr_user + curr_nice + curr_system + curr_idle;
         diff_idle  = curr_idle - prev_idle;
@@ -41,14 +39,13 @@ void print_cpu_usage(const char *format) {
         diff_usage = (1000 * (diff_total - diff_idle)/diff_total + 5)/10;
         prev_total = curr_total;
         prev_idle  = curr_idle;
-#endif
-#if defined(__FreeBSD__)
+#elif defined(__FreeBSD__)
         size_t size;
         long cp_time[CPUSTATES];
         size = sizeof cp_time;
-        if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
-                return;
-        }
+        if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
+                goto error;
+
         curr_user = cp_time[CP_USER];
         curr_nice = cp_time[CP_NICE];
         curr_system = cp_time[CP_SYS];
@@ -59,7 +56,8 @@ void print_cpu_usage(const char *format) {
         diff_usage = (1000 * (diff_total - diff_idle)/diff_total + 5)/10;
         prev_total = curr_total;
         prev_idle  = curr_idle;
-
+#else
+        goto error;
 #endif
         for (walk = format; *walk != '\0'; walk++) {
                 if (*walk != '%') {
@@ -72,4 +70,7 @@ void print_cpu_usage(const char *format) {
                         walk += strlen("usage");
                 }
         }
+        return;
+error:
+        (void)fputs("Cannot read usage\n", stderr);
 }
index 9fe1964c27d7bd5ad30ad7562021937cd586e7a3..d47eceacdbf3abee23fcb98f6519c924654fa17b 100644 (file)
@@ -1,6 +1,5 @@
 // vim:ts=8:expandtab
 #include "i3status.h"
-#include <err.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -12,7 +11,7 @@ void print_load(const char *format) {
         const char *walk;
 
         if (getloadavg(loadavg, 3) == -1)
-                errx(-1, "getloadavg() failed\n");
+                goto error;
 
         for (walk = format; *walk != '\0'; walk++) {
                 if (*walk != '%') {
@@ -35,5 +34,8 @@ void print_load(const char *format) {
                         walk += strlen("15min");
                 }
         }
+        return;
+error:
 #endif
+        (void)fputs("Cannot read load\n", stderr);
 }