diff --git a/src/lib66/service/service_enable_disable.c b/src/lib66/service/service_enable_disable.c index bdc47cbf92928b8044e2df86656947494b8d715f..8f87a54a234cd105115ab8537926282ede1f3db0 100644 --- a/src/lib66/service/service_enable_disable.c +++ b/src/lib66/service/service_enable_disable.c @@ -23,51 +23,33 @@ #include <66/service.h> #include <66/graph.h> #include <66/state.h> +#include <66/utils.h> +#include <66/enum.h> -typedef enum visit_service_e visit_service_t ; -enum visit_service_e -{ - SS_WHITE = 0, - SS_GRAY, - SS_BLACK -} ; - -static void visit_init(visit_service_t *v, size_t len) -{ - log_flow() ; - - size_t pos = 0 ; - for (; pos < len; pos++) - v[pos] = SS_WHITE ; - -} - -static void service_enable_disable_deps(graph_t *g, char const *base, char const *sv, uint8_t action) +static void service_enable_disable_deps(graph_t *g, resolve_service_t *res, resolve_service_t *ares, unsigned int areslen, uint8_t action, visit_t *visit) { log_flow() ; size_t pos = 0, element = 0 ; stralloc sa = STRALLOC_ZERO ; - if (graph_matrix_get_edge_g_sa(&sa, g, sv, action ? 0 : 1, 0) < 0) - log_dieu(LOG_EXIT_SYS, "get ", action ? "dependencies" : "required by" ," of: ", sv) ; - - size_t len = sastr_nelement(&sa) ; - visit_service_t v[len] ; - - visit_init(v, len) ; + if (graph_matrix_get_edge_g_sa(&sa, g, res->sa.s + res->name, action ? 0 : 1, 0) < 0) + log_dieu(LOG_EXIT_SYS, "get ", action ? "dependencies" : "required by" ," of: ", res->sa.s + res->name) ; if (sa.len) { FOREACH_SASTR(&sa, pos) { - if (v[element] == SS_WHITE) { + if (visit[element] == VISIT_WHITE) { char *name = sa.s + pos ; + int aresid = service_resolve_array_search(ares, areslen, name) ; + if (aresid < 0) + log_die(LOG_EXIT_USER, "service: ", name, " not available -- did you parsed it?") ; - service_enable_disable(g, base, name, action) ; + service_enable_disable(g, &ares[aresid], ares, areslen, action, visit) ; - v[element] = SS_GRAY ; + visit[element] = VISIT_GRAY ; } element++ ; } @@ -78,15 +60,64 @@ static void service_enable_disable_deps(graph_t *g, char const *base, char const /** @action -> 0 disable * @action -> 1 enable */ -void service_enable_disable(graph_t *g, char const *base, char const *name, uint8_t action) +void service_enable_disable(graph_t *g, resolve_service_t *res, resolve_service_t *ares, unsigned int areslen, uint8_t action, visit_t *visit) { log_flow() ; - if (!state_messenger(base, name, STATE_FLAGS_ISENABLED, !action ? STATE_FLAGS_FALSE : STATE_FLAGS_TRUE)) - log_dieusys(LOG_EXIT_SYS, "send message to state of: ", name) ; + if (!state_messenger(res, STATE_FLAGS_ISENABLED, !action ? STATE_FLAGS_FALSE : STATE_FLAGS_TRUE)) + log_dieusys(LOG_EXIT_SYS, "send message to state of: ", res->sa.s + res->name) ; + + service_enable_disable_deps(g, res, ares, areslen, action, visit) ; + + /** the logger must be disabled to avoid to start it + * with the 66 tree start <tree> command */ + if (res->logger.want && !action) { + + char *name = res->sa.s + res->logger.name ; + + int aresid = service_resolve_array_search(ares, areslen, name) ; + if (aresid < 0) + log_die(LOG_EXIT_USER, "service: ", name, " not available -- did you parsed it?") ; + + if (!state_messenger(&ares[aresid], STATE_FLAGS_ISENABLED, !action ? STATE_FLAGS_FALSE : STATE_FLAGS_TRUE)) + log_dieusys(LOG_EXIT_SYS, "send message to state of: ", name) ; + + log_info("Disabled successfully service: ", name) ; - service_enable_disable_deps(g, base, name, action) ; + } + + if (res->type == TYPE_MODULE || res->type == TYPE_BUNDLE) { + + if (res->dependencies.ncontents) { + + size_t pos = 0 ; + stralloc sa = STRALLOC_ZERO ; + + visit_t mvisit[SS_MAX_SERVICE] ; + visit_init(mvisit, SS_MAX_SERVICE) ; + + if (!sastr_clean_string(&sa, res->sa.s + res->dependencies.contents)) + log_dieu(LOG_EXIT_SYS, "clean string") ; + + FOREACH_SASTR(&sa, pos) { + + char *name = sa.s + pos ; + int aresid = service_resolve_array_search(ares, areslen, name) ; + if (aresid < 0) + log_die(LOG_EXIT_USER, "service: ", name, " not available -- did you parsed it?") ; + + if (!state_messenger(&ares[aresid], STATE_FLAGS_ISENABLED, !action ? STATE_FLAGS_FALSE : STATE_FLAGS_TRUE)) + log_dieusys(LOG_EXIT_SYS, "send message to state of: ", res->sa.s + res->name) ; + + service_enable_disable_deps(g, &ares[aresid], ares, areslen, action, mvisit) ; + + log_info(!action ? "Disabled" : "Enabled"," successfully service: ", ares[aresid].sa.s + ares[aresid].name) ; + } + + stralloc_free(&sa) ; + } + } - log_info(!action ? "Disabled" : "Enabled"," successfully service: ", name) ; + log_info(!action ? "Disabled" : "Enabled"," successfully service: ", res->sa.s + res->name) ; }