From cc186334e0170624026ea792edd0794e2138ebd7 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 14 Jan 2018 10:20:33 +0100 Subject: [PATCH] Add support for one-shot conditions, like most hooks Signed-off-by: Joachim Nilsson --- src/cond-w.c | 44 +++++++++++++++++++++++++++++++++----------- src/cond.h | 13 +++++++------ 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/cond-w.c b/src/cond-w.c index 3e87f6c1..1b58511b 100644 --- a/src/cond-w.c +++ b/src/cond-w.c @@ -59,9 +59,27 @@ static void cond_bump_reconf(void) cond_set_gen(COND_RECONF, rgen); } -int cond_set_path(const char *path, enum cond_state new) +static int cond_checkpath(const char *path) { char buf[MAX_ARG_LEN], *dir; + + strlcpy(buf, path, sizeof(buf)); + dir = dirname(buf); + if (!dir) { + _e("Invalid path '%s' for condition", path); + return 1; + } + + if (makepath(dir) && errno != EEXIST) { + _pe("Failed creating dir '%s' for condition '%s'", dir, path); + return 1; + } + + return 0; +} + +int cond_set_path(const char *path, enum cond_state new) +{ enum cond_state old; unsigned int rgen; @@ -77,16 +95,8 @@ int cond_set_path(const char *path, enum cond_state new) switch (new) { case COND_ON: - strlcpy(buf, path, sizeof(buf)); - dir = dirname(buf); - if (!dir) { - _e("Invalid path '%s' for condition", path); - return 0; - } - if (makepath(dir) && errno != EEXIST) { - _pe("Failed creating dir '%s' for condition '%s'", dir, path); - return 0; - } + if (cond_checkpath(path)) + return 0; cond_set_gen(path, rgen); break; @@ -145,6 +155,18 @@ void cond_set(const char *name) cond_update(name); } +void cond_set_oneshot(const char *name) +{ + const char *path = cond_path(name); + + _d("s => %s", name, path); + if (cond_checkpath(path)) + return; + + symlink(COND_RECONF, path); + cond_update(name); +} + void cond_clear(const char *name) { _d("%s", name); diff --git a/src/cond.h b/src/cond.h index c003cc1a..5eb01add 100644 --- a/src/cond.h +++ b/src/cond.h @@ -21,12 +21,13 @@ enum cond_state cond_get (const char *name); enum cond_state cond_get_agg (const char *names); int cond_affects (const char *name, const char *names); -int cond_set_path(const char *path, enum cond_state new); -void cond_set (const char *name); -void cond_clear (const char *name); -void cond_reload (void); -void cond_reassert(const char *pat); -void cond_init (void); +int cond_set_path (const char *path, enum cond_state new); +void cond_set (const char *name); +void cond_set_oneshot (const char *name); +void cond_clear (const char *name); +void cond_reload (void); +void cond_reassert (const char *pat); +void cond_init (void); #endif /* FINIT_COND_H_ */