diff options
-rw-r--r-- | src/comp.c | 20 | ||||
-rw-r--r-- | src/ctl.c | 7 | ||||
-rw-r--r-- | src/pies.h | 1 | ||||
-rw-r--r-- | src/prog.h | 4 | ||||
-rw-r--r-- | src/progman.c | 39 |
5 files changed, 36 insertions, 35 deletions
@@ -347,28 +347,34 @@ component_config_rollback (void) { struct complist *list = &comp_list[cur]; while (list->head) component_free (list->head); cur = prev_index (); } + +/* Return true if PROG is a leftover from previous configuration */ +static int +prog_is_leftover (struct prog *prog) +{ + return IS_COMPONENT (prog) && !component_is_active (prog->v.p.comp); +} +/* If PROG is a leftover, gracefully stop it and mark as disabled */ static int cb_terminate_prog (struct prog *prog, void *data) { - if (IS_COMPONENT (prog) && !component_is_active (prog->v.p.comp)) - { - progman_stop_component (prog); - prog->v.p.comp->flags |= CF_DISABLED; - } + if (prog_is_leftover (prog)) + progman_stop_component (&prog); return 0; } +/* If PROG is a leftover, slay it with SIGKILL */ static int cb_kill_prog (struct prog *prog, void *data) { - if (!(IS_COMPONENT (prog) && component_is_active (prog->v.p.comp))) + if (prog_is_leftover (prog)) prog_stop (prog, SIGKILL); return 0; } static int list_is_empty (void *p) @@ -506,13 +512,13 @@ component_config_commit (void) if (progman_wait_until (list_is_empty, list)) { progman_foreach (cb_kill_prog, NULL); progman_wait_until (list_is_empty, list); } } - + /* Build dependency map */ component_build_depmap (); /* Register new progs */ for (comp = comp_list[cur].head; comp; comp = comp->next) if (!comp->prog) @@ -1863,14 +1863,15 @@ fun_stop (struct json_value *result, struct prog *prog) json_object_set_string (result, "status", "ER"); json_object_set_string (result, "error_message", "already stopped"); return 1; } else { - progman_stop_component (prog); - prog->v.p.comp->flags |= CF_DISABLED; + progman_stop_component (&prog); + if (prog) + prog->v.p.comp->flags |= CF_DISABLED; json_object_set_string (result, "status", "OK"); } return 0; } static int @@ -1900,13 +1901,13 @@ fun_start (struct json_value *result, struct prog *prog) return 0; } static int fun_restart (struct json_value *result, struct prog *prog) { - progman_stop_component (prog); + progman_stop_component (&prog); json_object_set_string (result, "status", "OK"); return 0; } static void select_and_run (struct ctlio *io, struct pcond_node *cond, @@ -315,13 +315,12 @@ void free_redirector (struct redirector *rp); void pies_schedule_action (int act); void free_action (struct action *act); void register_prog (struct component *comp); int progman_waiting_p (void); -size_t progman_running_count (void); void progman_start (void); void progman_wake_sleeping (int); void progman_stop (void); void progman_cleanup (int expect_term); void progman_filter (int (*filter) (struct component *, void *data), void *data); @@ -81,11 +81,11 @@ struct prog } v; }; #define IS_COMPONENT(p) ((p)->type == TYPE_COMPONENT) struct prog *progman_locate (const char *name); -void progman_foreach (int (*filter) (struct prog *, void *data), void *data); +int progman_foreach (int (*filter) (struct prog *, void *data), void *data); void prog_stop (struct prog *prog, int sig); -void progman_stop_component (struct prog *prog); +void progman_stop_component (struct prog **prog); char const *prog_tag (struct prog const *prog); diff --git a/src/progman.c b/src/progman.c index 020dccd..2c73b2b 100644 --- a/src/progman.c +++ b/src/progman.c @@ -29,19 +29,25 @@ progman_locate (const char *name) for (prog = proghead; prog; prog = prog->next) if (strcmp (prog_tag (prog), name) == 0) break; return prog; } -void +int progman_foreach (int (*filter) (struct prog *, void *data), void *data) { struct prog *prog; - for (prog = proghead; prog; prog = prog->next) - if (filter (prog, data)) - break; + int rc = 0; + for (prog = proghead; prog; ) + { + struct prog *next = prog->next; + if ((rc = filter (prog, data)) != 0) + break; + prog = next; + } + return rc; } /* FIXME: Rewrite this using progman_foreach? */ void progman_iterate_comp (int (*fun) (struct component *, void *), void *data) { @@ -309,24 +315,12 @@ progman_waiting_p () return 1; } } return 0; } -size_t -progman_running_count () -{ - size_t size = 0; - struct prog *prog; - - for (prog = proghead; prog; prog = prog->next) - if (prog->pid > 0) - size++; - return size; -} - RETSIGTYPE redir_exit (int sig) { _exit (0); } @@ -1739,13 +1733,13 @@ progman_wait_until (int (*cond) (void *), void *data) return 1; } static int no_children_left (void *p) { - return progman_running_count () == 0; + return proghead == NULL; } void progman_stop () { prog_stop_all (SIGTERM); @@ -2354,56 +2348,57 @@ progman_cleanup (int expect_term) if (!expect_term) /* This will also recompute alarm settings, if necessary */ progman_wake_sleeping (0); } void -progman_stop_component (struct prog *prog) +progman_stop_component (struct prog **progptr) { + struct prog *prog = *progptr; if (prog && IS_COMPONENT (prog)) { switch (prog->v.p.status) { case status_enabled: case status_listener: logmsg (LOG_INFO, _("stopping component `%s'"), prog_tag (prog)); prog_stop (prog, SIGTERM); break; case status_disabled: if (!component_is_active (prog->v.p.comp)) - destroy_prog (&prog); + destroy_prog (progptr); else if (!(prog->v.p.comp->flags & CF_DISABLED)) { logmsg (LOG_INFO, _("enabling component `%s'"), prog_tag (prog)); prog->v.p.status = status_enabled; } break; case status_sleeping: if (!component_is_active (prog->v.p.comp)) - destroy_prog (&prog); + destroy_prog (progptr); else { logmsg (LOG_INFO, _("waking up component `%s'"), prog_tag (prog)); prog->v.p.failcount = 0; } break; default: if (!component_is_active (prog->v.p.comp)) - destroy_prog (&prog); + destroy_prog (progptr); else logmsg (LOG_INFO, _("stopping component `%s': component not started"), prog_tag (prog)); } } } void progman_stop_tag (const char *name) { struct prog *prog = progman_locate (name); - progman_stop_component (prog); + progman_stop_component (&prog); } /* EOF */ |