aboutsummaryrefslogtreecommitdiff
path: root/src/ctl.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-02-24 16:01:50 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2016-02-24 16:01:50 +0200
commit1ec50721c1aa5959009f0c74afc8b7796f4ffd20 (patch)
tree0dda2c45c87ce03843e876b764d578713a200aa6 /src/ctl.c
parent9912557fa9b4c8596c6e9f69857e9a616b4f4f33 (diff)
downloadpies-1ec50721c1aa5959009f0c74afc8b7796f4ffd20.tar.gz
pies-1ec50721c1aa5959009f0c74afc8b7796f4ffd20.tar.bz2
Improve parsing and handling of stop, start, and restart ctl commands.
* src/ctl.c (pcond_active): New type. (pcond_eval): Handle pcond_active. (pcond_conv): New op: active. (pcond_conv_find): Skip array elements with NULL term value. (object_to_cond): Permit empty argument if no handler function is defined. (fun_stop): Work on all prog types. (fun_start): Work only on components. (fun_restart): Work only on running components. * src/piesctl.c (cmdline_parser_state): New member: command. (pcond_parse_unary): Handle "active" keyword. (parse_condition): Change signature. (parse_condition_to_uri): Likewise. (default_cond): New function, (com_stop, com_start, com_restart): If no arguments supplied, assume default condition "type component". (ctlcom_t): Change signature. All uses changed. (main): Pass a pointer to cmdline_parser_state to the command handler.
Diffstat (limited to 'src/ctl.c')
-rw-r--r--src/ctl.c130
1 files changed, 78 insertions, 52 deletions
diff --git a/src/ctl.c b/src/ctl.c
index a7db410..61ed55e 100644
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -1494,2 +1494,3 @@ enum pcond_type
pcond_mode,
+ pcond_active,
pcond_status,
@@ -1575,2 +1576,5 @@ pcond_eval (struct pcond_node *node, struct prog *p)
return IS_COMPONENT (p) && p->v.p.comp->mode == node->v.mode;
+
+ case pcond_active:
+ return p->active;
@@ -1711,4 +1715,2 @@ struct pcond_conv {
static struct pcond_conv pcond_conv[] = {
- [pcond_true] = { "true", NULL },
- [pcond_false] = { "false", NULL },
[pcond_component] = { "component", pcond_conv_component },
@@ -1716,2 +1718,3 @@ static struct pcond_conv pcond_conv[] = {
[pcond_mode] = { "mode", pcond_conv_mode },
+ [pcond_active] = { "active", NULL },
[pcond_status] = { "status", pcond_conv_status },
@@ -1730,3 +1733,3 @@ pcond_conv_find (char const *name)
{
- if (strcmp (pcond_conv[i].term, name) == 0)
+ if (pcond_conv[i].term && strcmp (pcond_conv[i].term, name) == 0)
return i;
@@ -1749,8 +1752,3 @@ object_to_cond (struct ctlio *io, struct json_value *obj,
}
- if (json_object_get (obj, "arg", &arg))
- {
- ctlio_reply (io, 400, "bad conditional: missing arg");
- return -1;
- }
-
+
if (op->type != json_string)
@@ -1768,2 +1766,8 @@ object_to_cond (struct ctlio *io, struct json_value *obj,
+ if (json_object_get (obj, "arg", &arg) && pcond_conv[type].handler)
+ {
+ ctlio_reply (io, 400, "bad conditional: missing arg");
+ return -1;
+ }
+
node = pcond_node_alloc (type);
@@ -1912,14 +1916,6 @@ fun_stop (struct json_value *result, struct prog *prog)
{
- if (IS_COMPONENT (prog))
- {
- if (!prog->active)
- {
- json_object_set_string (result, "status", "ER");
- json_object_set_string (result, "error_message", "already stopped");
- return 1;
- }
-
- prog->active = 0;
- progman_stop_component (&prog);
- json_object_set_string (result, "status", "OK");
+ if (!prog->active)
+ {
+ json_object_set_string (result, "status", "ER");
+ json_object_set_string (result, "error_message", "already stopped");
}
@@ -1927,3 +1923,4 @@ fun_stop (struct json_value *result, struct prog *prog)
{
- //FIXME progman_stop_component (&prog);
+ prog->active = 0;
+ progman_stop_component (&prog);
json_object_set_string (result, "status", "OK");
@@ -1940,37 +1937,39 @@ fun_start (struct json_value *result, struct prog *prog)
json_object_set_string (result, "error_message", "not a component");
- return 1;
}
- switch (prog->v.p.status)
+ else
{
- case status_stopped:
- prog->v.p.comp->flags &= ~CF_DISABLED;
- json_object_set_string (result, "status", "OK");
- break;
+ switch (prog->v.p.status)
+ {
+ case status_stopped:
+ prog->v.p.comp->flags &= ~CF_DISABLED;
+ json_object_set_string (result, "status", "OK");
+ break;
- case status_sleeping:
- case status_finished:
- prog->v.p.status = status_stopped;
- prog->v.p.failcount = 0;
- prog->v.p.timestamp = 0;
- json_object_set_string (result, "status", "OK");
- break;
+ case status_sleeping:
+ case status_finished:
+ prog->v.p.status = status_stopped;
+ prog->v.p.failcount = 0;
+ prog->v.p.timestamp = 0;
+ json_object_set_string (result, "status", "OK");
+ break;
- case status_listener:
- if (prog_activate_listener (prog) == 0)
- json_object_set_string (result, "status", "OK");
- else
- {
+ case status_listener:
+ if (prog_activate_listener (prog) == 0)
+ json_object_set_string (result, "status", "OK");
+ else
+ {
+ json_object_set_string (result, "status", "ER");
+ /* FIXME: error message */
+ json_object_set_string (result, "error_message",
+ "can't open socket");
+ }
+ break;
+
+ default:
json_object_set_string (result, "status", "ER");
- /* FIXME: error message */
- json_object_set_string (result, "error_message",
- "can't open socket");
+ json_object_set_string (result, "error_message", "already running");
+ return 0;
}
- break;
-
- default:
- json_object_set_string (result, "status", "ER");
- json_object_set_string (result, "error_message", "already running");
- return 1;
+ prog->active = 1;
}
- prog->active = 1;
return 0;
@@ -1981,4 +1980,31 @@ fun_restart (struct json_value *result, struct prog *prog)
{
- progman_stop_component (&prog);
- json_object_set_string (result, "status", "OK");
+ if (!IS_COMPONENT (prog))
+ {
+ json_object_set_string (result, "status", "ER");
+ json_object_set_string (result, "error_message", "not a component");
+ }
+ else if (!prog->active)
+ {
+ json_object_set_string (result, "status", "ER");
+ json_object_set_string (result, "error_message", "not active");
+ }
+ else
+ {
+ switch (prog->v.p.status)
+ {
+ case status_running:
+ progman_stop_component (&prog);
+ json_object_set_string (result, "status", "OK");
+ break;
+
+ case status_listener:
+ json_object_set_string (result, "status", "ER");
+ json_object_set_string (result, "error_message", "not applicable");
+ break;
+
+ default:
+ json_object_set_string (result, "status", "ER");
+ json_object_set_string (result, "error_message", "not running");
+ }
+ }
return 0;

Return to:

Send suggestions and report system problems to the System administrator.