make modules more resilient to failure

This commit is contained in:
Connor Lane Smith 2011-08-25 22:24:06 +01:00 committed by Michael Stapelberg
parent 67ad80f005
commit cc1457c4f0
3 changed files with 22 additions and 18 deletions

View 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);
}

View 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);
}

View 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);
}