From 16553a7216339d447c2a9d17db1f9809e26ffc06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 18 Dec 2018 20:24:30 +0100 Subject: [PATCH] module/battery: acquire lock when reading/writing state --- modules/battery/battery.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/modules/battery/battery.c b/modules/battery/battery.c index 66e8439..24c7dc2 100644 --- a/modules/battery/battery.c +++ b/modules/battery/battery.c @@ -52,6 +52,9 @@ static struct exposable * content(const struct module *mod) { const struct private *m = mod->private; + + mtx_lock(&((struct module *)mod)->lock); + assert(m->state == STATE_FULL || m->state == STATE_CHARGING || m->state == STATE_DISCHARGING); @@ -89,6 +92,8 @@ content(const struct module *mod) .count = 6, }; + mtx_unlock(&((struct module *)mod)->lock); + struct exposable *exposable = m->label->instantiate(m->label, &tags); tag_set_destroy(&tags); @@ -138,6 +143,8 @@ run(struct module_run_context *ctx) int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY); assert(base_dir_fd != -1); + mtx_lock(&ctx->module->lock); + { int fd = openat(base_dir_fd, "manufacturer", O_RDONLY); assert(fd != -1); @@ -175,6 +182,8 @@ run(struct module_run_context *ctx) m->battery, m->manufacturer, m->model, 100.0 * m->energy_full / energy_full_design); + mtx_unlock(&ctx->module->lock); + int status_fd = openat(base_dir_fd, "status", O_RDONLY); assert(status_fd != -1); @@ -242,10 +251,12 @@ run(struct module_run_context *ctx) LOG_DBG("capacity: %ld, energy: %ld, power: %ld", capacity, energy, power); + mtx_lock(&ctx->module->lock); m->state = state; m->capacity = capacity; m->energy = energy; m->power = power; + mtx_unlock(&ctx->module->lock); bar->refresh(bar); }