Merge branch 'battery-reopen-files'

Closes #25
This commit is contained in:
Daniel Eklöf 2021-01-14 11:10:17 +01:00
commit ae403c842c
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 74 additions and 40 deletions

View file

@ -21,6 +21,8 @@
* mpd: `elapsed` tag not working (regression, introduced in 1.6.0). * mpd: `elapsed` tag not working (regression, introduced in 1.6.0).
* Wrong background color for (semi-) transparent backgrounds. * Wrong background color for (semi-) transparent backgrounds.
* battery: stats sometimes getting stuck at 0, or impossibly large
values (https://codeberg.org/dnkl/yambar/issues/25).
### Security ### Security

View file

@ -171,21 +171,21 @@ readint_from_fd(int fd)
return ret; return ret;
} }
static int static bool
initialize(struct private *m) initialize(struct private *m)
{ {
int pw_fd = open("/sys/class/power_supply", O_RDONLY); int pw_fd = open("/sys/class/power_supply", O_RDONLY);
if (pw_fd == -1) { if (pw_fd < 0) {
LOG_ERRNO("/sys/class/power_supply"); LOG_ERRNO("/sys/class/power_supply");
return -1; return false;
} }
int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY); int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY);
close(pw_fd); close(pw_fd);
if (base_dir_fd == -1) { if (base_dir_fd < 0) {
LOG_ERRNO("%s", m->battery); LOG_ERRNO("/sys/class/power_supply/%s", m->battery);
return -1; return false;
} }
{ {
@ -268,19 +268,54 @@ initialize(struct private *m)
m->charge_full = m->charge_full_design = -1; m->charge_full = m->charge_full_design = -1;
} }
return base_dir_fd; close(base_dir_fd);
return true;
err: err:
close(base_dir_fd); close(base_dir_fd);
return -1; return false;
} }
static void static bool
update_status(struct module *mod, int capacity_fd, int energy_fd, int power_fd, update_status(struct module *mod)
int charge_fd, int current_fd, int status_fd, int time_to_empty_fd)
{ {
struct private *m = mod->private; struct private *m = mod->private;
int pw_fd = open("/sys/class/power_supply", O_RDONLY);
if (pw_fd < 0) {
LOG_ERRNO("/sys/class/power_supply");
return false;
}
int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY);
close(pw_fd);
if (base_dir_fd < 0) {
LOG_ERRNO("/sys/class/power_supply/%s", m->battery);
return false;
}
int status_fd = openat(base_dir_fd, "status", O_RDONLY);
if (status_fd < 0) {
LOG_ERRNO("/sys/class/power_supply/%s/status", m->battery);
close(base_dir_fd);
return false;
}
int capacity_fd = openat(base_dir_fd, "capacity", O_RDONLY);
if (capacity_fd < 0) {
LOG_ERRNO("/sys/class/power_supply/%s/capacity", m->battery);
close(status_fd);
close(base_dir_fd);
return false;
}
int energy_fd = openat(base_dir_fd, "energy_now", O_RDONLY);
int power_fd = openat(base_dir_fd, "power_now", O_RDONLY);
int charge_fd = openat(base_dir_fd, "charge_now", O_RDONLY);
int current_fd = openat(base_dir_fd, "current_now", O_RDONLY);
int time_to_empty_fd = openat(base_dir_fd, "time_to_empty_now", O_RDONLY);
long capacity = readint_from_fd(capacity_fd); long capacity = readint_from_fd(capacity_fd);
long energy = energy_fd >= 0 ? readint_from_fd(energy_fd) : -1; long energy = energy_fd >= 0 ? readint_from_fd(energy_fd) : -1;
long power = power_fd >= 0 ? readint_from_fd(power_fd) : -1; long power = power_fd >= 0 ? readint_from_fd(power_fd) : -1;
@ -289,6 +324,24 @@ update_status(struct module *mod, int capacity_fd, int energy_fd, int power_fd,
long time_to_empty = time_to_empty_fd >= 0 ? readint_from_fd(time_to_empty_fd) : -1; long time_to_empty = time_to_empty_fd >= 0 ? readint_from_fd(time_to_empty_fd) : -1;
const char *status = readline_from_fd(status_fd); const char *status = readline_from_fd(status_fd);
if (status_fd >= 0)
close(status_fd);
if (capacity_fd >= 0)
close(capacity_fd);
if (energy_fd >= 0)
close(energy_fd);
if (power_fd >= 0)
close(power_fd);
if (charge_fd >= 0)
close(charge_fd);
if (current_fd >= 0)
close(current_fd);
if (time_to_empty_fd >= 0)
close(time_to_empty_fd);
if (base_dir_fd >= 0)
close(base_dir_fd);
enum state state; enum state state;
if (status == NULL) { if (status == NULL) {
@ -322,6 +375,7 @@ update_status(struct module *mod, int capacity_fd, int energy_fd, int power_fd,
m->current = current; m->current = current;
m->time_to_empty = time_to_empty; m->time_to_empty = time_to_empty;
mtx_unlock(&mod->lock); mtx_unlock(&mod->lock);
return true;
} }
static int static int
@ -330,8 +384,7 @@ run(struct module *mod)
const struct bar *bar = mod->bar; const struct bar *bar = mod->bar;
struct private *m = mod->private; struct private *m = mod->private;
int base_dir_fd = initialize(m); if (!initialize(m))
if (base_dir_fd == -1)
return -1; return -1;
LOG_INFO("%s: %s %s (at %.1f%% of original capacity)", LOG_INFO("%s: %s %s (at %.1f%% of original capacity)",
@ -343,26 +396,19 @@ run(struct module *mod)
: 0.0)); : 0.0));
int ret = 1; int ret = 1;
int status_fd = openat(base_dir_fd, "status", O_RDONLY);
int capacity_fd = openat(base_dir_fd, "capacity", O_RDONLY);
int energy_fd = openat(base_dir_fd, "energy_now", O_RDONLY);
int power_fd = openat(base_dir_fd, "power_now", O_RDONLY);
int charge_fd = openat(base_dir_fd, "charge_now", O_RDONLY);
int current_fd = openat(base_dir_fd, "current_now", O_RDONLY);
int time_to_empty_fd = openat(base_dir_fd, "time_to_empty_now", O_RDONLY);
struct udev *udev = udev_new(); struct udev *udev = udev_new();
struct udev_monitor *mon = udev_monitor_new_from_netlink(udev, "udev"); struct udev_monitor *mon = udev_monitor_new_from_netlink(udev, "udev");
if (status_fd < 0 || capacity_fd < 0 || udev == NULL || mon == NULL) if (udev == NULL || mon == NULL)
goto out; goto out;
udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL); udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
udev_monitor_enable_receiving(mon); udev_monitor_enable_receiving(mon);
update_status( if (!update_status(mod))
mod, capacity_fd, energy_fd, power_fd, goto out;
charge_fd, current_fd, status_fd, time_to_empty_fd);
bar->refresh(bar); bar->refresh(bar);
while (true) { while (true) {
@ -388,9 +434,8 @@ run(struct module *mod)
continue; continue;
} }
update_status( if (!update_status(mod))
mod, capacity_fd, energy_fd, power_fd, break;
charge_fd, current_fd, status_fd, time_to_empty_fd);
bar->refresh(bar); bar->refresh(bar);
} }
@ -399,19 +444,6 @@ out:
udev_monitor_unref(mon); udev_monitor_unref(mon);
if (udev != NULL) if (udev != NULL)
udev_unref(udev); udev_unref(udev);
if (power_fd != -1)
close(power_fd);
if (energy_fd != -1)
close(energy_fd);
if (capacity_fd != -1)
close(capacity_fd);
if (status_fd != -1)
close(status_fd);
if (time_to_empty_fd != -1)
close(time_to_empty_fd);
close(base_dir_fd);
return ret; return ret;
} }