summaryrefslogtreecommitdiffabout
path: root/src/ctl.c
authorSergey Poznyakoff <gray@gnu.org.ua>2016-01-05 12:56:11 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2016-01-05 12:56:11 (GMT)
commit32a337f3b35c51ceb1d9320c7ff67fa1d5726c2a (patch) (side-by-side diff)
tree098d41b0d07d62144f8398dc3c5f80a295369f98 /src/ctl.c
parent2c03055b8ed023e7d8d65f2dbd51cd128040fcfb (diff)
downloadpies-32a337f3b35c51ceb1d9320c7ff67fa1d5726c2a.tar.gz
pies-32a337f3b35c51ceb1d9320c7ff67fa1d5726c2a.tar.bz2
Select components to operate upon using conditional expression
* grecs: Upgrade. * src/ctl.c (restab): Split /programs in two endpoints. (eval_env): New members allowed_state, fun, total_count, success_count, and noperm_count. (pcond_type): pcond_and and pcond_or can take arbitrary number of arguments. All uses updated. (selector): Rewrite. * src/piesctl.c: All commands that operate on components take conditional expression as argument. (pcond_parse_and) (pcond_parse_or): Optimize consecutive operations.
Diffstat (limited to 'src/ctl.c') (more/less context) (ignore whitespace changes)
-rw-r--r--src/ctl.c318
1 files changed, 229 insertions, 89 deletions
diff --git a/src/ctl.c b/src/ctl.c
index d26bf45..e1c5530 100644
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -888,8 +888,10 @@ method_decode (char const *method)
static void res_instance (struct ctlio *, enum http_method, char const *,
struct json_value *);
-static void res_programs (struct ctlio *, enum http_method, char const *,
- struct json_value *);
+static void res_programs_select (struct ctlio *, enum http_method, char const *,
+ struct json_value *);
+static void res_programs_component (struct ctlio *, enum http_method,
+ char const *, struct json_value *);
struct ctlio_resource
{
@@ -903,7 +905,10 @@ struct ctlio_resource
static struct ctlio_resource restab[] = {
#define S(s) #s, (sizeof (#s)-1)
{ S(/instance), CTL_ADMIN_STATE, res_instance },
- { S(/programs), CTL_ADMIN_STATE|CTL_USER_STATE, res_programs },
+ { S(/programs/select), CTL_ADMIN_STATE|CTL_USER_STATE,
+ res_programs_select },
+ { S(/programs/component), CTL_ADMIN_STATE|CTL_USER_STATE,
+ res_programs_component },
{ NULL }
#undef S
};
@@ -931,7 +936,10 @@ static char *
json_extract (char *uri)
{
char *p = strrchr (uri, '/');
- if (p && strchr ("\"{[", p[1]))
+ if (p && (strchr ("\"{[", p[1])
+ || strcmp ("true", p + 1) == 0
+ || strcmp ("false", p + 1) == 0
+ || strcmp ("null", p + 1) == 0))
{
*p++ = 0;
return p;
@@ -1313,8 +1321,13 @@ res_instance (struct ctlio *io, enum http_method meth,
struct eval_env
{
struct ctlio *io;
+ int allowed_state;
struct pcond_node *cond;
+ int (*fun) (struct json_value *, struct prog *);
struct json_value *result;
+ size_t total_count;
+ size_t success_count;
+ size_t noperm_count;
};
static int
@@ -1390,6 +1403,7 @@ format_idx (struct json_value *obj, const char *name, unsigned i,
enum pcond_type
{
+ pcond_false,
pcond_true,
pcond_component,
pcond_type,
@@ -1409,8 +1423,11 @@ struct pcond_node
enum pies_comp_mode mode;
enum prog_status status;
enum prog_type type;
- struct pcond_node *unary;
- struct pcond_node *binary[2];
+ struct
+ {
+ size_t c;
+ struct pcond_node **v;
+ } arg;
} v;
};
@@ -1432,13 +1449,15 @@ pcond_node_free (struct pcond_node *node)
switch (node->type)
{
case pcond_not:
- pcond_node_free (node->v.unary);
- break;
-
case pcond_and:
case pcond_or:
- pcond_node_free (node->v.binary[0]);
- pcond_node_free (node->v.binary[1]);
+ {
+ size_t i;
+
+ for (i = 0; i < node->v.arg.c; i++)
+ pcond_node_free (node->v.arg.v[i]);
+ }
+ grecs_free (node->v.arg.v);
break;
default:
@@ -1450,12 +1469,17 @@ pcond_node_free (struct pcond_node *node)
static int
pcond_eval (struct pcond_node *node, struct prog *p)
{
+ size_t i;
+
if (!node)
return 0;
switch (node->type)
{
case pcond_true:
return 1;
+
+ case pcond_false:
+ return 0;
case pcond_component:
return strcmp (p->tag, node->v.tag) == 0;
@@ -1470,17 +1494,19 @@ pcond_eval (struct pcond_node *node, struct prog *p)
return IS_COMPONENT (p) && p->v.p.status == node->v.status;
case pcond_not:
- return !pcond_eval (node->v.unary, p);
+ return !pcond_eval (node->v.arg.v[0], p);
case pcond_and:
- if (!pcond_eval (node->v.binary[0], p))
- return 0;
- return pcond_eval (node->v.binary[1], p);
+ for (i = 0; i < node->v.arg.c; i++)
+ if (!pcond_eval (node->v.arg.v[i], p))
+ return 0;
+ return 1;
case pcond_or:
- if (pcond_eval (node->v.binary[0], p))
- return 1;
- return pcond_eval (node->v.binary[1], p);
+ for (i = 0; i < node->v.arg.c; i++)
+ if (pcond_eval (node->v.arg.v[i], p))
+ return 1;
+ return 0;
default:
abort ();
@@ -1560,26 +1586,36 @@ static int
pcond_conv_not (struct pcond_node *node, struct json_value *arg,
struct ctlio *io)
{
- return json_to_pcond (io, arg, &node->v.unary);
+ node->v.arg.v = grecs_calloc (1, sizeof (node->v.arg.v[0]));
+ node->v.arg.c = 1;
+ return json_to_pcond (io, arg, &node->v.arg.v[0]);
}
static int
pcond_conv_binary (struct pcond_node *node, struct json_value *arg,
struct ctlio *io)
{
- struct json_value *v;
+ size_t i, n;
if (arg->type != json_arr)
{
ctlio_reply (io, 400, "arguments to binary opcode must be array");
return -1;
}
- json_array_get (arg, 0, &v);
- if (json_to_pcond (io, v, &node->v.binary[0]))
- return -1;
- json_array_get (arg, 1, &v);
- if (json_to_pcond (io, v, &node->v.binary[1]))
- return -1;
+
+ n = json_array_size (arg);
+ node->v.arg.v = grecs_calloc (n, sizeof (node->v.arg.v[0]));
+ node->v.arg.c = n;
+
+ for (i = 0; i < n; i++)
+ {
+ struct json_value *v;
+ if (json_array_get (arg, i, &v) == 0)
+ {
+ if (json_to_pcond (io, v, &node->v.arg.v[i]))
+ return -1;
+ }
+ }
return 0;
}
@@ -1590,6 +1626,7 @@ 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 },
@@ -1669,10 +1706,7 @@ json_to_pcond (struct ctlio *io, struct json_value *val,
break;
case json_bool:
- if (val->v.b)
- *pnode = pcond_node_alloc (pcond_true);
- else
- *pnode = NULL;
+ *pnode = pcond_node_alloc (val->v.b ? pcond_true : pcond_false);
break;
case json_number:
@@ -1687,26 +1721,34 @@ json_to_pcond (struct ctlio *io, struct json_value *val,
return 0;
}
-static struct json_value *prog_serialize (struct prog *prog);
-
static int
selector (struct prog *prog, void *data)
{
struct eval_env *env = data;
- if (auth_prog (prog, env->io) && pcond_eval (env->cond, prog))
- json_array_append (env->result, prog_serialize (prog));
+ if (pcond_eval (env->cond, prog))
+ {
+ if (auth_prog (prog, env->io) & env->allowed_state)
+ {
+ struct json_value *status = json_new_object ();
+ json_object_set_string (status, "tag", prog->tag);
+ if (env->fun (status, prog) == 0)
+ env->success_count++;
+ json_array_append (env->result, status);
+ env->total_count++;
+ }
+ else
+ env->noperm_count++;
+ }
return 0;
}
static struct json_value *
-prog_serialize (struct prog *prog)
+prog_serialize (struct json_value *ret, struct prog *prog)
{
- struct json_value *ret = json_reply_create ();
struct json_value *v;
size_t i;
- json_object_set_string (ret, "tag", prog->tag);
FORMAT_IDX (ret, "type", pies_type_str, prog->type);
switch (prog->type)
{
@@ -1742,98 +1784,196 @@ prog_serialize (struct prog *prog)
}
return ret;
}
+
+static int
+fun_list (struct json_value *result, struct prog *prog)
+{
+ prog_serialize (result, prog);
+ return 0;
+}
-static void
-component_stop (struct ctlio *io, struct prog *prog)
+static int
+fun_stop (struct json_value *result, struct prog *prog)
{
if (prog->v.p.comp->flags & CF_DISABLED)
- ctlio_reply (io, 409, "Already stopped");
+ {
+ 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;
- ctlio_reply (io, 200, "Component stopped");
+ json_object_set_string (result, "status", "OK");
}
+ return 0;
}
-static void
-component_start (struct ctlio *io, struct prog *prog)
+static int
+fun_start (struct json_value *result, struct prog *prog)
{
if (prog->v.p.comp->flags & CF_DISABLED)
{
prog->v.p.comp->flags &= ~CF_DISABLED;
prog->v.p.status = status_enabled;
- kill (getpid (), SIGALRM);
- ctlio_reply (io, 200, "Component started");
+ json_object_set_string (result, "status", "OK");
}
else
- ctlio_reply (io, 409, "Already running");
+ {
+ json_object_set_string (result, "status", "ER");
+ json_object_set_string (result, "error_message", "already running");
+ return 1;
+ }
+ return 0;
+}
+
+static int
+fun_restart (struct json_value *result, struct prog *prog)
+{
+ progman_stop_component (prog);
+ json_object_set_string (result, "status", "OK");
+ return 0;
}
static void
-res_programs (struct ctlio *io, enum http_method meth,
- char const *uri, struct json_value *json)
+select_and_run (struct ctlio *io, struct pcond_node *cond,
+ enum http_method meth)
{
- if (meth == METH_GET)
+ struct eval_env env;
+ int wakeup = 0;
+
+ memset (&env, 0, sizeof (env));
+ env.io = io;
+ env.cond = cond;
+ env.fun = NULL;
+ env.result = json_new_array ();
+
+ switch (meth)
{
- struct eval_env env;
-
- env.io = io;
- if (json_to_pcond (io, json, &env.cond))
- return;
- env.result = json_new_array ();
- progman_foreach (selector, &env);
- pcond_node_free (env.cond);
- io->output.reply = env.result;
- io->code = 200;
+ case METH_GET:
+ env.allowed_state = CTL_USER_STATE | CTL_ADMIN_STATE;
+ env.fun = fun_list;
+ break;
+
+ case METH_DELETE:
+ env.allowed_state = CTL_ADMIN_STATE;
+ env.fun = fun_stop;
+ break;
+
+ case METH_PUT:
+ env.allowed_state = CTL_ADMIN_STATE;
+ env.fun = fun_start;
+ wakeup = 1;
+ break;
+
+ case METH_POST:
+ env.allowed_state = CTL_ADMIN_STATE;
+ env.fun = fun_restart;
+ wakeup = 1;
+ break;
+
+ default:
+ ctlio_reply (io, 405, NULL);
}
- else if (meth == METH_DELETE || meth == METH_PUT)
+
+ if (env.fun)
{
- if (!uri)
- ctlio_reply (io, 404, NULL);
+ env.result = json_new_array ();
+ progman_foreach (selector, &env);
+
+ if (env.success_count == 0 && env.noperm_count)
+ ctlio_reply (io, 403, NULL);
else
{
- struct prog *prog = progman_locate (uri);
- if (!prog)
- ctlio_reply (io, 404, NULL);
- else if (auth_prog (prog, io) & CTL_ADMIN_STATE)
- {
- if (!IS_COMPONENT (prog))
- ctlio_reply (io, 404, "Not a component");
- else if (meth == METH_DELETE)
- {
- component_stop (io, prog);
- }
- else
- {
- component_start (io, prog);
- }
- }
- else
- ctlio_reply (io, 403, NULL);
+ if (env.success_count && wakeup)
+ progman_wake_sleeping (1);
+ io->output.reply = env.result;
+ io->code = 200;
}
}
- else if (meth == METH_POST)
+}
+
+static void
+res_programs_select (struct ctlio *io, enum http_method meth,
+ char const *uri, struct json_value *json)
+{
+ struct pcond_node *cond;
+
+ if (uri)
+ {
+ ctlio_reply (io, 404, NULL);
+ return;
+ }
+
+ if (json_to_pcond (io, json, &cond))
+ return;
+ select_and_run (io, cond, meth);
+ pcond_node_free (cond);
+}
+
+static void
+res_programs_component (struct ctlio *io, enum http_method meth,
+ char const *uri, struct json_value *json)
+{
+ struct pcond_node *node = pcond_node_alloc (pcond_type);
+ node->v.type = TYPE_COMPONENT;
+
+ if (meth != METH_POST)
{
- if (json->type != json_arr)
+ if (uri)
{
- ctlio_reply (io, 400, NULL);
+ struct pcond_node *np;
+ np = pcond_node_alloc (pcond_and);
+ np->v.arg.c = 2;
+ np->v.arg.v = grecs_calloc (np->v.arg.c, sizeof (np->v.arg.v[0]));
+ np->v.arg.v[0] = node;
+ np->v.arg.v[1] = pcond_node_alloc (pcond_component);
+ np->v.arg.v[1]->v.tag = (char*) uri + 1;
+ node = np;
}
+ }
+ else
+ {
+ if (json->type != json_arr)
+ ctlio_reply (io, 400, NULL);
else
{
size_t i, n;
+ struct pcond_node *or_cond, *np;
n = json_array_size (json);
+ or_cond = pcond_node_alloc (pcond_or);
+ or_cond->v.arg.v = grecs_calloc (n, sizeof (or_cond->v.arg.v[0]));
+ or_cond->v.arg.c = n;
+
for (i = 0; i < n; i++)
{
struct json_value *v;
if (json_array_get (json, i, &v) == 0 && v->type == json_string)
- progman_stop_tag (v->v.s);
+ {
+ or_cond->v.arg.v[i] = pcond_node_alloc (pcond_component);
+ or_cond->v.arg.v[i]->v.tag = v->v.s;
+ }
+ else
+ {
+ ctlio_reply (io, 400, "all elements must be string");
+ pcond_node_free (or_cond);
+ pcond_node_free (node);
+ return;
+ }
}
- kill (getpid (), SIGALRM);
+
+ np = pcond_node_alloc (pcond_and);
+ np->v.arg.c = 2;
+ np->v.arg.v = grecs_calloc (np->v.arg.c, sizeof (np->v.arg.v[0]));
+ np->v.arg.v[0] = node;
+ np->v.arg.v[1] = or_cond;
+
+ node = np;
}
- ctlio_reply (io, 200, "Done");
}
- else
- ctlio_reply (io, 405, NULL);
-}
+
+ select_and_run (io, node, meth);
+ pcond_node_free (node);
+}

Return to:

Send suggestions and report system problems to the System administrator.