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.c64
1 files changed, 45 insertions, 19 deletions
diff --git a/src/ctl.c b/src/ctl.c
index a7db410..61ed55e 100644
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -1492,6 +1492,7 @@ enum pcond_type
pcond_component,
pcond_type,
pcond_mode,
+ pcond_active,
pcond_status,
pcond_not,
pcond_and,
@@ -1574,6 +1575,9 @@ pcond_eval (struct pcond_node *node, struct prog *p)
case pcond_mode:
return IS_COMPONENT (p) && p->v.p.comp->mode == node->v.mode;
+ case pcond_active:
+ return p->active;
+
case pcond_status:
return IS_COMPONENT (p) && p->v.p.status == node->v.status;
@@ -1709,11 +1713,10 @@ struct pcond_conv {
};
static struct pcond_conv pcond_conv[] = {
- [pcond_true] = { "true", NULL },
- [pcond_false] = { "false", NULL },
[pcond_component] = { "component", pcond_conv_component },
[pcond_type] = { "type", pcond_conv_type },
[pcond_mode] = { "mode", pcond_conv_mode },
+ [pcond_active] = { "active", NULL },
[pcond_status] = { "status", pcond_conv_status },
[pcond_not] = { "not", pcond_conv_not },
[pcond_and] = { "and", pcond_conv_binary },
@@ -1728,7 +1731,7 @@ pcond_conv_find (char const *name)
int i;
for (i = 0; i < max_type; i++)
{
- if (strcmp (pcond_conv[i].term, name) == 0)
+ if (pcond_conv[i].term && strcmp (pcond_conv[i].term, name) == 0)
return i;
}
return -1;
@@ -1747,11 +1750,6 @@ object_to_cond (struct ctlio *io, struct json_value *obj,
ctlio_reply (io, 400, "bad conditional: missing op");
return -1;
}
- if (json_object_get (obj, "arg", &arg))
- {
- ctlio_reply (io, 400, "bad conditional: missing arg");
- return -1;
- }
if (op->type != json_string)
{
@@ -1766,6 +1764,12 @@ object_to_cond (struct ctlio *io, struct json_value *obj,
return -1;
}
+ 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);
if (pcond_conv[type].handler && pcond_conv[type].handler (node, arg, io))
{
@@ -1910,22 +1914,15 @@ fun_list (struct json_value *result, struct prog *prog)
static int
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");
}
else
{
- //FIXME progman_stop_component (&prog);
+ prog->active = 0;
+ progman_stop_component (&prog);
json_object_set_string (result, "status", "OK");
}
return 0;
@@ -1938,8 +1935,9 @@ fun_start (struct json_value *result, struct prog *prog)
{
json_object_set_string (result, "status", "ER");
json_object_set_string (result, "error_message", "not a component");
- return 1;
}
+ else
+ {
switch (prog->v.p.status)
{
case status_stopped:
@@ -1970,17 +1968,45 @@ fun_start (struct json_value *result, struct prog *prog)
default:
json_object_set_string (result, "status", "ER");
json_object_set_string (result, "error_message", "already running");
- return 1;
+ return 0;
}
prog->active = 1;
+ }
return 0;
}
static int
fun_restart (struct json_value *result, struct prog *prog)
{
+ 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.