From 6b10174bcb67f8a577726850c1742a78b9cfcd19 Mon Sep 17 00:00:00 2001 From: Ming Liu Date: Sat, 26 Feb 2022 13:51:28 +0100 Subject: [PATCH] service.c: create conditions according to runtask exit status In commit f0f358a13: [ service.c: set/clear condition 'done' for run tasks ] a runtask done condition would be set/cleared when entering DONE/HALTED states, but it did not cover all the user cases, for instance, sometimes an end user may want to know if a runtask has finished sucessfully or to decide what to do on its failures. So we now change the conditions to: tsktype/tskname/success and tsktype/tskname/failure. And this change not only applies to run/task types, but also applies to sysv type, in case it fails, a sysv/tskname/failure condition would be set. Signed-off-by: Ming Liu --- src/service.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/service.c b/src/service.c index d44fb246..27e395f7 100644 --- a/src/service.c +++ b/src/service.c @@ -628,10 +628,13 @@ static int service_start(svc_t *svc) switch (svc->type) { case SVC_TYPE_RUN: svc->status = complete(svc->cmd, pid); - if (WIFEXITED(svc->status) && !WEXITSTATUS(svc->status)) + if (WIFEXITED(svc->status) && !WEXITSTATUS(svc->status)) { + svc->started = 1; result = 0; - else + } else { + svc->started = 0; result = 1; + } svc->start_time = svc->pid = 0; svc->once++; svc_set_state(svc, SVC_STOPPING_STATE); @@ -1707,18 +1710,24 @@ static void svc_set_state(svc_t *svc, svc_state_t new) } if (svc_is_runtask(svc)) { - char cond[MAX_COND_LEN]; + char success[MAX_COND_LEN], failure[MAX_COND_LEN]; - snprintf(cond, sizeof(cond), "%s/%s/done", svc_typestr(svc), svc->name); + snprintf(success, sizeof(success), "%s/%s/success", svc_typestr(svc), svc->name); + snprintf(failure, sizeof(failure), "%s/%s/failure", svc_typestr(svc), svc->name); - /* create done condition when entering SVC_DONE_STATE. */ - if (*state == SVC_DONE_STATE) - cond_set_oneshot(cond); - - /* clear done condition when entering SVC_HALTED_STATE. */ - if (*state == SVC_HALTED_STATE) - cond_clear(cond); + /* create success/failure condition when entering SVC_DONE_STATE. */ + if (*state == SVC_DONE_STATE) { + if (svc->started) + cond_set_oneshot(success); + else + cond_set_oneshot(failure); + } + /* clear all conditions when entering SVC_HALTED_STATE. */ + if (*state == SVC_HALTED_STATE) { + cond_clear(success); + cond_clear(failure); + } } }