From 6b688c4211c5a3ddcd566ef0a800ab0b689c2795 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 11 Mar 2021 15:10:10 +0100 Subject: [PATCH] Refactor, share env helper fns with initctl Signed-off-by: Joachim Wiberg --- src/service.c | 35 ++--------------------------------- src/svc.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/service.c b/src/service.c index 454429bb..a877a452 100644 --- a/src/service.c +++ b/src/service.c @@ -260,37 +260,6 @@ static int redirect(svc_t *svc) return 0; } -/* - * non-zero env, no checking if file exists or not - */ -static char *svc_getenv(svc_t *svc) -{ - int v = 0; - - if (!svc->env[0]) - return NULL; - - if (svc->env[0] == '-') - v = 1; - - return &svc->env[v]; -} - -/* - * Check if svc has env, if env file exists returns true - * if it doesn't exist, but has '-', also true. if svc - * doesn't have env, also true. - */ -static int check_env(svc_t *svc) -{ - char *env = svc_getenv(svc); - - if (!env) - return 1; - - return fexist(env); -} - /* * Source environment file, if it exists * Note: must be called from privsepped child @@ -307,7 +276,7 @@ static void source_env(svc_t *svc) if (!fn) return; - /* Warning in service_start() after check_env() */ + /* Warning in service_start() after svc_checkenv() */ fp = fopen(fn, "r"); if (!fp) return; @@ -411,7 +380,7 @@ static int service_start(svc_t *svc) } /* Unlike systemd we do not allow starting service if env is missing, unless - */ - if (!check_env(svc)) { + if (!svc_checkenv(svc)) { logit(LOG_WARNING, "%s: missing env file %s", svc->cmd, svc->env); svc_missing(svc); return 1; diff --git a/src/svc.h b/src/svc.h index 30ed89a2..02f49b3a 100644 --- a/src/svc.h +++ b/src/svc.h @@ -352,6 +352,37 @@ static inline char *svc_jobid(svc_t *svc, char *buf, size_t len) return buf; } +/* + * non-zero env, no checking if file exists or not + */ +static inline char *svc_getenv(svc_t *svc) +{ + int v = 0; + + if (!svc->env[0]) + return NULL; + + if (svc->env[0] == '-') + v = 1; + + return &svc->env[v]; +} + +/* + * Check if svc has env, if env file exists returns true + * if it doesn't exist, but has '-', also true. if svc + * doesn't have env, also true. + */ +static inline int svc_checkenv(svc_t *svc) +{ + char *env = svc_getenv(svc); + + if (!env) + return 1; + + return fexist(env); +} + #endif /* FINIT_SVC_H_ */ /**