From 6b237b7b0031a8166a5cd00baef65881a91c9e76 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 12 Sep 2016 19:21:48 +0200 Subject: [PATCH] Refactor abstraction of svc blocking state Signed-off-by: Joachim Nilsson --- api.c | 6 ++++-- inetd.c | 6 +++--- service.c | 22 ++++++++++++---------- svc.h | 8 ++++++++ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/api.c b/api.c index 8ee95a0f..ba1f4b79 100644 --- a/api.c +++ b/api.c @@ -115,15 +115,17 @@ static int call(int (*action)(svc_t *), char *buf, size_t len) static int service_block(svc_t *svc) { - svc->block = SVC_BLOCK_USER; + svc_block(svc); service_step(svc); + return 0; } static int service_unblock(svc_t *svc) { - svc->block = SVC_BLOCK_NONE; + svc_unblock(svc); service_step(svc); + return 0; } diff --git a/inetd.c b/inetd.c index aae008c4..90911842 100755 --- a/inetd.c +++ b/inetd.c @@ -166,7 +166,7 @@ static void socket_cb(uev_t *UNUSED(w), void *arg, int UNUSED(events)) } if (!svc->inetd.forking) { - svc->block = SVC_BLOCK_INETD_BUSY; + svc_busy(svc); service_step(svc); } @@ -311,7 +311,7 @@ void inetd_stop(inetd_t *inetd) /* For dgram inetd services we block the parent SVC * and halt the watcher, so don't close the socket! */ - if (inetd->svc->block != SVC_BLOCK_INETD_BUSY) { + if (!svc_is_busy(inetd->svc)) { _d("Shutting down inet socket %d ...", inetd->watcher.fd); if (inetd->type == SOCK_STREAM) shutdown(inetd->watcher.fd, SHUT_RDWR); @@ -684,7 +684,7 @@ int inetd_new(inetd_t *inetd, char *name, char *service, char *proto, int forkin int inetd_del(inetd_t *inetd) { - inetd->svc->block = 0; + svc_unblock(inetd->svc); inetd_stop(inetd); return inetd_flush(inetd); diff --git a/service.c b/service.c index 5fe6cbe2..0f1f700e 100755 --- a/service.c +++ b/service.c @@ -58,10 +58,13 @@ static svc_t *find_inetd_svc (char *path, char *service, char *proto); */ int service_enabled(svc_t *svc) { - if (!svc || - !svc_in_runlevel(svc, runlevel) || - svc_is_removed(svc) || - svc->block != SVC_BLOCK_NONE) + if (!svc) + return 0; + + if (!svc_in_runlevel(svc, runlevel)) + return 0; + + if (svc_is_removed(svc) || svc_is_blocked(svc)) return 0; return 1; @@ -117,7 +120,7 @@ static int service_start(svc_t *svc) print_result(1); } - svc->block = SVC_BLOCK_MISSING; + svc_missing(svc); return 1; } @@ -309,8 +312,7 @@ static int service_stop(svc_t *svc) #ifndef INETD_DISABLED if (svc_is_inetd(svc)) { - int do_print = runlevel != 1 && !silent && - svc->block != SVC_BLOCK_INETD_BUSY; + int do_print = runlevel != 1 && !silent && !svc_is_busy(svc); if (do_print) print_desc("Stopping ", svc->desc); @@ -733,8 +735,8 @@ restart: case SVC_DONE_STATE: #ifndef INETD_DISABLED if (svc_is_inetd_conn(svc)) { - if (svc->inetd.svc->block == SVC_BLOCK_INETD_BUSY) { - svc->inetd.svc->block = 0; + if (svc_is_busy(svc)) { + svc_unblock(svc->inetd.svc); service_step(svc->inetd.svc); } service_unregister(svc); @@ -772,7 +774,7 @@ restart: } else if (cond_get_agg(svc->cond) == COND_ON) { if (*restart_counter >= RESPAWN_MAX) { _e("%s keeps crashing, not restarting", svc->desc); - svc->block = SVC_BLOCK_CRASHING; + svc_crashing(svc); svc_set_state(svc, SVC_HALTED_STATE); break; } diff --git a/svc.h b/svc.h index 078a9add..5b3a0b12 100755 --- a/svc.h +++ b/svc.h @@ -187,6 +187,14 @@ static inline int svc_is_inetd (svc_t *svc) { return svc && SVC_TYPE_INETD static inline int svc_is_inetd_conn(svc_t *svc) { return svc && SVC_TYPE_INETD_CONN == svc->type; } static inline int svc_is_daemon (svc_t *svc) { return svc && SVC_TYPE_SERVICE == svc->type; } +static inline int svc_is_blocked (svc_t *svc) { return svc->block != SVC_BLOCK_NONE; } +static inline int svc_is_busy (svc_t *svc) { return svc->block == SVC_BLOCK_INETD_BUSY; } +static inline void svc_unblock (svc_t *svc) { svc->block = SVC_BLOCK_NONE; } +static inline void svc_block (svc_t *svc) { svc->block = SVC_BLOCK_USER; } +static inline void svc_busy (svc_t *svc) { svc->block = SVC_BLOCK_INETD_BUSY; } +static inline void svc_missing (svc_t *svc) { svc->block = SVC_BLOCK_MISSING; } +static inline void svc_crashing (svc_t *svc) { svc->block = SVC_BLOCK_CRASHING; } + #endif /* FINIT_SVC_H_ */ /**