xbattmon

simple battery monitor for X
git clone git://git.2f30.org/xbattmon.git
Log | Files | Refs | README | LICENSE

commit f96aef810a3b434b32882a6f85e252fa291c0488
parent b25283949ee854851d7940ffc08556bbdb7c69a4
Author: sin <sin@2f30.org>
Date:   Wed,  1 Apr 2015 23:23:07 +0100

Retry to open the battery file if we fail

Sometimes upon resuming on Linux, the sysfs path is not available
immediately.

Diffstat:
xbattmon.c | 23++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/xbattmon.c b/xbattmon.c @@ -202,24 +202,37 @@ pollbat(void) state = info.ac_state == APM_AC_ON ? AC_ON : AC_OFF; } #elif __linux__ +#include <unistd.h> void pollbat(void) { FILE *fp; - int acon; + int acon, retry; + retry = 2; +fail1: fp = fopen(PATH_BAT_CAP, "r"); - if (fp == NULL) - err(1, "fopen %s", PATH_BAT_CAP); + if (fp == NULL) { + if (retry-- == 0) + err(1, "fopen %s", PATH_BAT_CAP); + sleep(1); + goto fail1; + } fscanf(fp, "%d", &batcap); fclose(fp); if (batcap > maxcap) batcap = maxcap; + retry = 2; +fail2: fp = fopen(PATH_AC_ONLINE, "r"); - if (fp == NULL) - err(1, "fopen %s", PATH_AC_ONLINE); + if (fp == NULL) { + if (retry-- == 0) + err(1, "fopen %s", PATH_AC_ONLINE); + sleep(1); + goto fail2; + } fscanf(fp, "%d", &acon); fclose(fp);