aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-02-12 14:25:26 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2016-02-12 14:25:26 +0200
commit05f16374d677f7c606d5100df3ca3499b9891aca (patch)
treeefa6177f87df2ba1e9f63edec6cc55cb621c9745
parentd9d267052215f223897af18310e4adee310308e7 (diff)
downloadpies-05f16374d677f7c606d5100df3ca3499b9891aca.tar.gz
pies-05f16374d677f7c606d5100df3ca3499b9891aca.tar.bz2
Improve progman API (progman_foreach and progman_stop_component).
* src/progman.c (progman_foreach): Allow filter to remove its prog argument from the list. Return integer (result of the call to filter, that returned non-zero). All uses changed. (progman_running_count): Remove. (progman_stop_component): Take a pointer to pointer. Pass it to destroy_prog when needed. All uses changed. * src/comp.c (prog_is_leftover): New function. (cb_terminate_prog): Use prog_is_leftover. Don't raise CF_DISABLED flag, as prog will have been destroyed by progman_stop_component. (cb_kill_prog): Use prog_is_leftover. * src/ctl.c (fun_stop): Check if prog still exists after the call to progman_stop_component. * src/pies.h (progman_running_count): Removed. * src/prog.h (progman_foreach): Return int. (progman_stop_component): Change signature.
-rw-r--r--src/comp.c20
-rw-r--r--src/ctl.c7
-rw-r--r--src/pies.h1
-rw-r--r--src/prog.h4
-rw-r--r--src/progman.c39
5 files changed, 36 insertions, 35 deletions
diff --git a/src/comp.c b/src/comp.c
index 5613e28..6a0e4ba 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -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)
diff --git a/src/ctl.c b/src/ctl.c
index 489fc7f..bc2f78d 100644
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -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,
diff --git a/src/pies.h b/src/pies.h
index 39977c7..1b77398 100644
--- a/src/pies.h
+++ b/src/pies.h
@@ -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);
diff --git a/src/prog.h b/src/prog.h
index fe42d3a..2dc8591 100644
--- a/src/prog.h
+++ b/src/prog.h
@@ -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 */

Return to:

Send suggestions and report system problems to the System administrator.