diff --git a/src/battery.c b/src/battery.c index 3042de6..c1c1b90 100644 --- a/src/battery.c +++ b/src/battery.c @@ -3,6 +3,7 @@ #include #include +#include "log.h" #include "utils.h" #include "config.h" @@ -17,6 +18,10 @@ char /* open up the battery capacity for reading */ d = concat(battery_status_path, "/capacity"); f = fopen(d, "r"); + if (!f) { + log_fatal("battery directory does not exist"); + return NULL; + } free(d); /* create enough space for the battery percentage */ diff --git a/src/config.c b/src/config.c index 9871d6b..487ff6f 100644 --- a/src/config.c +++ b/src/config.c @@ -86,6 +86,7 @@ parse_config_file(char *path) f = fopen(path, "r"); if (!f) { log_fatal("Failed to open config file: %s", path); + return; } config = toml_parse_file(f, errbuf, sizeof(errbuf)); diff --git a/src/led.c b/src/led.c index 4204f0b..76bd4e9 100644 --- a/src/led.c +++ b/src/led.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "led.h" @@ -124,6 +125,13 @@ led_create_thread(char *file) int err; struct sigaction act; static bool running; + struct stat s; + + /* ensure that file exists */ + err = stat(file, &s); + if (err < 0 || !S_ISREG(s.st_mode)) { + return 2; + } /* make sure the led thread cannot be created twice */ if (running) { diff --git a/src/main.c b/src/main.c index 5983869..bda1518 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,13 @@ #include "config.h" #define MAX_BUFLEN 1024 +#define wrap(ok, func) do { \ + if (ok) { \ + func; \ + } \ + } while (0) +bool led_ok, wayland_ok; static struct cag_option options[] = { { .identifier = 'c', @@ -51,19 +57,21 @@ on_battery_event(char *percent, bool plugged) /* convert battery percentage to an int */ res = strtol(percent, &ep, 10); - if (res <= led_blink_trigger_level) { - rate = led_blink_timing_func(res); - if (rate >= 1) { - led_set_rate(rate); + wrap(led_ok, + if (res <= led_blink_trigger_level) { + rate = led_blink_timing_func(res); + if (rate >= 1) { + led_set_rate(rate); + } + } else { + led_set_rate(0); } - } else { - led_set_rate(0); - } + ); - wayland_set_idle_lock(false); + wrap(wayland_ok, wayland_set_idle_lock(false)); } else { - led_set_rate(0); - wayland_set_idle_lock(true); + wrap(led_ok, led_set_rate(0)); + wrap(wayland_ok, wayland_set_idle_lock(true)); } free(percent); @@ -74,9 +82,12 @@ main(int argc, char *argv[]) { cag_option_context context; bool plugged = plugged_in(battery_power_source); - char buffer[MAX_BUFLEN], *out[4], *config_path; + char buffer[MAX_BUFLEN], *out[4], *config_path, *percent; int sock_fd; + /* defaults to working */ + led_ok = wayland_ok = true; + cag_option_init(&context, options, CAG_ARRAY_SIZE(options), argc, argv); while (cag_option_fetch(&context)) { switch (cag_option_get_identifier(&context)) { @@ -104,15 +115,22 @@ main(int argc, char *argv[]) sock_fd = acpi_create_socket(acpi_daemon_socket_path); /* create the led thread */ - led_create_thread(led_brightness_path); + if (led_create_thread(led_brightness_path)) { + led_ok = false; + } /* create the wayland thread */ - wayland_create_thread(); + if (wayland_create_thread()) { + wayland_ok = false; + } /* run events that need to be run on start to ensure that the current state * inside the program reflects that that is outside the program */ - on_battery_event(battery_percent(), plugged); + percent = battery_percent(); + if (percent == NULL) { + on_battery_event(percent, plugged); + } /* zero buffer */ memset(out, 0, sizeof(out)); @@ -145,7 +163,10 @@ main(int argc, char *argv[]) if (strcmp(s, "PNP") == 0) { if (strcmp(out[2], "00000080") == 0) { if (strcmp(out[3], "00000001") == 0) { - on_battery_event(battery_percent(), plugged); + percent = battery_percent(); + if (percent == NULL) { + on_battery_event(percent, plugged); + } } } }