From cc1457c4f0f4cccb8bec326dd0bb13082ac806e6 Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Thu, 25 Aug 2011 22:24:06 +0100 Subject: make modules more resilient to failure --- src/print_cpu_temperature.c | 11 ++++++----- src/print_cpu_usage.c | 23 ++++++++++++----------- src/print_load.c | 6 ++++-- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/print_cpu_temperature.c b/src/print_cpu_temperature.c index 3bc4636..8c343c2 100644 --- a/src/print_cpu_temperature.c +++ b/src/print_cpu_temperature.c @@ -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); } diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index 71fc5b7..544d6cf 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -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); } diff --git a/src/print_load.c b/src/print_load.c index 9fe1964..d47ecea 100644 --- a/src/print_load.c +++ b/src/print_load.c @@ -1,6 +1,5 @@ // vim:ts=8:expandtab #include "i3status.h" -#include #include #include #include @@ -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); } -- cgit v1.2.3