From c4f820e486318dfebade944a77f3c554cf7d3f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Dec 2022 12:06:25 +0100 Subject: [PATCH] =?UTF-8?q?module/script:=20poll-interval:=20convert=20val?= =?UTF-8?q?ue=20from=20=E2=80=98seconds=E2=80=99=20to=20=E2=80=98milliseco?= =?UTF-8?q?nds=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/yambar-modules-script.5.scd | 4 ++-- modules/script.c | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/doc/yambar-modules-script.5.scd b/doc/yambar-modules-script.5.scd index 17ac34e..ec91c7e 100644 --- a/doc/yambar-modules-script.5.scd +++ b/doc/yambar-modules-script.5.scd @@ -78,8 +78,8 @@ User defined. | poll-interval : integer : no -: Number of seconds between each script run. If unset, continuous mode - is used. +: Number of milliseconds between each script run. If unset, or set to + 0, continuous mode is used. # EXAMPLES diff --git a/modules/script.c b/modules/script.c index 1c349a1..2b97590 100644 --- a/modules/script.c +++ b/modules/script.c @@ -22,6 +22,8 @@ #include "../module.h" #include "../plugin.h" +static const long min_poll_interval = 500; + struct private { char *path; size_t argc; @@ -574,7 +576,7 @@ run(struct module *mod) break; if (m->aborted) break; - if (m->poll_interval < 0) + if (m->poll_interval <= 0) break; struct timeval now; @@ -583,7 +585,10 @@ run(struct module *mod) break; } - struct timeval poll_interval = {.tv_sec = m->poll_interval}; + struct timeval poll_interval = { + .tv_sec = m->poll_interval / 1000, + .tv_usec = (m->poll_interval % 1000) * 1000, + }; struct timeval timeout; timeradd(&now, &poll_interval, &timeout); @@ -670,7 +675,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) return script_new( yml_value_as_string(path), argc, argv, - poll_interval != NULL ? yml_value_as_int(poll_interval) : -1, + poll_interval != NULL ? yml_value_as_int(poll_interval) : 0, conf_to_particle(c, inherited)); } @@ -695,13 +700,28 @@ conf_verify_args(keychain_t *chain, const struct yml_node *node) return conf_verify_list(chain, node, &conf_verify_string); } +static bool +conf_verify_poll_interval(keychain_t *chain, const struct yml_node *node) +{ + if (!conf_verify_unsigned(chain, node)) + return false; + + if (yml_value_as_int(node) < min_poll_interval) { + LOG_ERR("%s: interval value cannot be less than %ldms", + conf_err_prefix(chain, node), min_poll_interval); + return false; + } + + return true; +} + static bool verify_conf(keychain_t *chain, const struct yml_node *node) { static const struct attr_info attrs[] = { {"path", true, &conf_verify_path}, {"args", false, &conf_verify_args}, - {"poll-interval", false, &conf_verify_unsigned}, + {"poll-interval", false, &conf_verify_poll_interval}, MODULE_COMMON_ATTRS, };