summaryrefslogtreecommitdiffabout
path: root/src/ctl.c
authorSergey Poznyakoff <gray@gnu.org.ua>2016-02-25 14:17:36 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2016-02-25 14:17:36 (GMT)
commitc9abb69acae95a0136ee1b03dec8b08d9639005e (patch) (side-by-side diff)
treefa5849647e6d9430289a71035630475a82f4cf54 /src/ctl.c
parent1ec50721c1aa5959009f0c74afc8b7796f4ffd20 (diff)
downloadpies-c9abb69acae95a0136ee1b03dec8b08d9639005e.tar.gz
pies-c9abb69acae95a0136ee1b03dec8b08d9639005e.tar.bz2
Implement "telinit environ" ctl command
* src/ctl.c: New endpoint "environ" * src/pies.h (sysvinit_envlocate) (sysvinit_envdelete) (sysvinit_envupdate): New protos. * src/piesctl.c: New subcommand "telinit environ". * src/sysvinit.c (sysvinit_envlocate) (sysvinit_envdelete) (sysvinit_envupdate): New functions. (sysvinit_begin): Create allocated copies of instance and pies_master_argv to avoid them being rewritten by calls to mf_proctitle_format
Diffstat (limited to 'src/ctl.c') (more/less context) (ignore whitespace changes)
-rw-r--r--src/ctl.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/ctl.c b/src/ctl.c
index 61ed55e..731b31b 100644
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -485,6 +485,7 @@ json_error_reply_create (const char *msg)
struct json_value *val;
val = json_reply_create ();
+ json_object_set_string (val, "status", "ER");
json_object_set_string (val, "error_message", "%s", msg);
return val;
}
@@ -920,6 +921,8 @@ 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 void res_environ (struct ctlio *, enum http_method, char const *,
+ struct json_value *);
static void res_conf (struct ctlio *, enum http_method, char const *,
struct json_value *);
@@ -942,6 +945,7 @@ static struct ctlio_resource restab[] = {
{ S(/programs), CTL_ADMIN_STATE|CTL_USER_STATE, NULL,
res_programs },
{ S(/runlevel), CTL_ADMIN_STATE, pred_sysvinit, res_runlevel },
+ { S(/environ), CTL_ADMIN_STATE, pred_sysvinit, res_environ },
{ NULL }
#undef S
};
@@ -2403,3 +2407,85 @@ res_conf (struct ctlio *io, enum http_method meth,
else
ctlio_reply (io, 404, NULL);
}
+
+/* GET /environ - List entire environment
+ * ["RUNLEVEL=3", "CONSOLE=/dev/tty", ...]
+ * GET /environ/NAME - Get value of variable NAME
+ * { "status":"OK", "value":"..." }
+ * { "status":"ER", "error_message":"..." }
+ * DELETE /environ/NAME - Unset variable
+ * { "status":"OK" }
+ * { "status":"ER", "error_message":"..." }
+ * PUT /environ/NAME=VALUE - Set variable
+ * { "status":"OK" }
+ * { "status":"ER", "error_message":"..." }
+ */
+static void
+env_reply (struct ctlio *io, int ok, int rc)
+{
+ switch (rc)
+ {
+ case 0:
+ io->code = ok;
+ io->output.reply = json_reply_create ();
+ json_object_set_string (io->output.reply, "status", "OK");
+ break;
+
+ case 1:
+ ctlio_reply (io, 403, NULL);
+ break;
+
+ case -1:
+ ctlio_reply (io, 404, NULL);
+ }
+}
+
+static void
+res_environ (struct ctlio *io, enum http_method meth,
+ char const *uri, struct json_value *json)
+{
+ if (meth == METH_GET)
+ {
+ if (uri && uri[1])
+ {
+ char *value;
+
+ if (sysvinit_envlocate (uri + 1, &value) == -1)
+ ctlio_reply (io, 404, NULL);
+ else
+ {
+ env_reply (io, 200, 0);
+ json_object_set_string (io->output.reply, "value", "%s", value);
+ }
+ }
+ else
+ {
+ size_t i;
+
+ io->output.reply = json_new_array ();
+ io->code = 200;
+ for (i = 0; sysvinit_environ_hint[i]; i++)
+ {
+ json_array_append (io->output.reply,
+ json_new_string (sysvinit_environ_hint[i]));
+ }
+ }
+ }
+ else if (meth == METH_DELETE)
+ {
+ if (!(uri && uri[0]))
+ ctlio_reply (io, 400, NULL);
+ else
+ env_reply (io, 200, sysvinit_envupdate (uri + 1));
+ }
+ else if (meth == METH_PUT)
+ {
+ if (!(uri && uri[0]))
+ ctlio_reply (io, 400, NULL);
+ else
+ env_reply (io, 201, sysvinit_envupdate (uri + 1));
+ }
+ else
+ ctlio_reply (io, 405, NULL);
+
+}

Return to:

Send suggestions and report system problems to the System administrator.