aboutsummaryrefslogtreecommitdiff
path: root/src/ctl.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2016-02-13 12:48:59 +0200
committerSergey Poznyakoff <gray@gnu.org>2016-02-13 12:48:59 +0200
commit3c774556e22cd30c42304614e14218a767e4d700 (patch)
treecdcc278f470b2ef8863cf10655315f8579f679b2 /src/ctl.c
parent92145d8331e824a54109c53032f0ca4b2df71f5f (diff)
downloadpies-3c774556e22cd30c42304614e14218a767e4d700.tar.gz
pies-3c774556e22cd30c42304614e14218a767e4d700.tar.bz2
Implement on-demaind components; implement control socket telinit interface.
* src/pies.h: Include json.h (sysvinit_report, sysvinit_set_runlevel) (json_object_set_string, json_object_set_number): New protos. * src/ctl.c (ctlio_resource)<predicate>: New member. (restab): New endpoint: runlevel. Available only if run as pid 1. (find_resource): If predicate is not NULL, call it to confirm that the endpoint is available. * src/piesctl.c: New subcommand "telinit". * src/sysvinit.c (sysvinit_set_runlevel): New function. (sysvinit_fifo_handler): Implement on-demaind components.
Diffstat (limited to 'src/ctl.c')
-rw-r--r--src/ctl.c64
1 files changed, 58 insertions, 6 deletions
diff --git a/src/ctl.c b/src/ctl.c
index bc2f78d..6f50246 100644
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -20,3 +20,2 @@
#include "base64.h"
-#include "json.h"
@@ -445,3 +444,3 @@ json_reply_create (void)
-static void
+void
json_object_set_string (struct json_value *obj,
@@ -460,3 +459,3 @@ json_object_set_string (struct json_value *obj,
-static void
+void
json_object_set_number (struct json_value *obj, char const *name, double val)
@@ -906,2 +905,6 @@ static void res_programs (struct ctlio *, enum http_method, char const *,
struct json_value *);
+static void res_runlevel (struct ctlio *, enum http_method, char const *,
+ struct json_value *);
+
+static int pred_sysvinit (void);
@@ -912,2 +915,3 @@ struct ctlio_resource
int states;
+ int (*predicate) (void);
void (*handler) (struct ctlio *, enum http_method,
@@ -918,5 +922,6 @@ 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,
+ { S(/instance), CTL_ADMIN_STATE, NULL, res_instance },
+ { S(/programs), CTL_ADMIN_STATE|CTL_USER_STATE, NULL,
res_programs },
+ { S(/runlevel), CTL_ADMIN_STATE, pred_sysvinit, res_runlevel },
{ NULL }
@@ -941,3 +946,4 @@ find_resource (struct ctlio *io, const char *endpoint)
|| endpoint[p->uri_len] == '/'
- || endpoint[p->uri_len] == '?'))
+ || endpoint[p->uri_len] == '?')
+ && (!p->predicate || p->predicate()))
return p;
@@ -2012 +2018,47 @@ res_programs (struct ctlio *io, enum http_method meth,
}
+
+static int
+pred_sysvinit (void)
+{
+ return init_process;
+}
+
+static void
+res_runlevel (struct ctlio *io, enum http_method meth,
+ char const *uri, struct json_value *json)
+{
+ if (uri)
+ ctlio_reply (io, 404, NULL);
+ else if (meth == METH_GET)
+ {
+ io->output.reply = json_reply_create ();
+ io->code = 200;
+ sysvinit_report (io->output.reply);
+ }
+ else if (meth == METH_PUT)
+ {
+ struct json_value *val;
+ // { "runlevel": "3" }
+ if (json_object_get (json, "runlevel", &val))
+ ctlio_reply (io, 400, "missing runlevel request");
+ else if (val->type != json_string)
+ ctlio_reply (io, 400, "bad runlevel type");
+ else
+ {
+ io->output.reply = json_reply_create ();
+ io->code = 200;
+ if (strlen (val->v.s) == 1
+ && sysvinit_set_runlevel (val->v.s[0]) == 0)
+ json_object_set_string (io->output.reply, "status", "OK");
+ else
+ {
+ json_object_set_string (io->output.reply, "status", "ER");
+ json_object_set_string (io->output.reply, "error_message",
+ "invalid runlevel value");
+ }
+ }
+ }
+ else
+ ctlio_reply (io, 405, NULL);
+}
+

Return to:

Send suggestions and report system problems to the System administrator.