2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 static struct blob_buf buf;
23 struct rpc_plugin_lookup_context {
30 rpc_plugin_lookup_plugin_cb(struct ubus_context *ctx,
31 struct ubus_object_data *obj, void *priv)
33 struct rpc_plugin_lookup_context *c = priv;
38 sprintf(c->name, "%s", obj->path);
43 rpc_plugin_lookup_plugin(struct ubus_context *ctx, struct ubus_object *obj,
46 struct rpc_plugin_lookup_context c = { .id = obj->id, .name = strptr };
48 if (ubus_lookup(ctx, NULL, rpc_plugin_lookup_plugin_cb, &c))
66 rpc_plugin_call_stdin_cb(struct ustream *s, void *priv)
68 struct call_context *c = priv;
72 ustream_write(s, c->input, strlen(c->input), false);
80 rpc_plugin_call_stdout_cb(struct blob_buf *blob, char *buf, int len, void *priv)
82 struct call_context *c = priv;
86 c->obj = json_tokener_parse_ex(c->tok, buf, len);
88 if (json_tokener_get_error(c->tok) != json_tokener_continue)
89 c->output_done = true;
96 rpc_plugin_call_stderr_cb(struct blob_buf *blob, char *buf, int len, void *priv)
102 rpc_plugin_call_finish_cb(struct blob_buf *blob, int stat, void *priv)
104 struct call_context *c = priv;
105 int rv = UBUS_STATUS_INVALID_ARGUMENT;
107 if (json_tokener_get_error(c->tok) == json_tokener_success)
111 if (json_object_get_type(c->obj) == json_type_object ||
112 json_object_get_type(c->obj) == json_type_array)
114 blobmsg_add_json_element(blob, NULL, c->obj);
118 json_object_put(c->obj);
122 rv = UBUS_STATUS_NO_DATA;
126 json_tokener_free(c->tok);
135 rpc_plugin_call(struct ubus_context *ctx, struct ubus_object *obj,
136 struct ubus_request_data *req, const char *method,
137 struct blob_attr *msg)
139 int rv = UBUS_STATUS_UNKNOWN_ERROR;
140 struct call_context *c;
143 c = calloc(1, sizeof(*c));
148 c->method = strdup(method);
149 c->input = blobmsg_format_json(msg, true);
150 c->tok = json_tokener_new();
152 if (!c->method || !c->input || !c->tok)
155 plugin = c->path + sprintf(c->path, "%s/", RPC_PLUGIN_DIRECTORY);
157 if (!rpc_plugin_lookup_plugin(ctx, obj, plugin))
159 rv = UBUS_STATUS_NOT_FOUND;
163 c->argv[0] = c->path;
165 c->argv[2] = c->method;
167 return rpc_exec(c->argv, rpc_plugin_call_stdin_cb,
168 rpc_plugin_call_stdout_cb, rpc_plugin_call_stderr_cb,
169 rpc_plugin_call_finish_cb, c, ctx, req);
181 json_tokener_free(c->tok);
190 rpc_plugin_parse_signature(struct blob_attr *sig, struct ubus_method *method)
193 enum blobmsg_type type;
194 struct blob_attr *attr;
195 struct blobmsg_policy *policy = NULL;
197 if (!sig || blob_id(sig) != BLOBMSG_TYPE_TABLE)
202 blobmsg_for_each_attr(attr, sig, rem)
207 policy = calloc(n_attr, sizeof(*policy));
214 blobmsg_for_each_attr(attr, sig, rem)
216 type = blob_id(attr);
218 if (type == BLOBMSG_TYPE_INT32)
220 switch (blobmsg_get_u32(attr))
223 type = BLOBMSG_TYPE_INT8;
227 type = BLOBMSG_TYPE_INT16;
231 type = BLOBMSG_TYPE_INT64;
235 type = BLOBMSG_TYPE_INT32;
240 policy[n_attr].name = strdup(blobmsg_name(attr));
241 policy[n_attr].type = type;
247 method->name = strdup(blobmsg_name(sig));
248 method->handler = rpc_plugin_call;
249 method->policy = policy;
250 method->n_policy = n_attr;
255 static struct ubus_object *
256 rpc_plugin_parse_exec(const char *name, int fd)
258 int len, rem, n_method;
259 struct blob_attr *cur;
260 struct ubus_method *methods;
261 struct ubus_object_type *obj_type;
262 struct ubus_object *obj;
268 blob_buf_init(&buf, 0);
270 tok = json_tokener_new();
275 while ((len = read(fd, outbuf, sizeof(outbuf))) > 0)
277 jsobj = json_tokener_parse_ex(tok, outbuf, len);
279 if (json_tokener_get_error(tok) == json_tokener_continue)
282 if (json_tokener_get_error(tok) != json_tokener_success)
287 if (json_object_get_type(jsobj) == json_type_object)
288 blobmsg_add_object(&buf, jsobj);
290 json_object_put(jsobj);
295 json_tokener_free(tok);
299 blob_for_each_attr(cur, buf.head, rem)
305 methods = calloc(n_method, sizeof(*methods));
312 blob_for_each_attr(cur, buf.head, rem)
314 if (!rpc_plugin_parse_signature(cur, &methods[n_method]))
320 obj = calloc(1, sizeof(*obj));
325 obj_type = calloc(1, sizeof(*obj_type));
330 asprintf((char **)&obj_type->name, "luci-rpc-plugin-%s", name);
331 obj_type->methods = methods;
332 obj_type->n_methods = n_method;
334 obj->name = strdup(name);
335 obj->type = obj_type;
336 obj->methods = methods;
337 obj->n_methods = n_method;
343 rpc_plugin_register_exec(struct ubus_context *ctx, const char *path)
346 int rv = UBUS_STATUS_NO_DATA, fd, fds[2];
348 struct ubus_object *plugin;
350 name = strrchr(path, '/');
353 return UBUS_STATUS_INVALID_ARGUMENT;
356 return UBUS_STATUS_UNKNOWN_ERROR;
358 switch ((pid = fork()))
361 return UBUS_STATUS_UNKNOWN_ERROR;
364 fd = open("/dev/null", O_RDWR);
380 if (execl(path, path, "list", NULL))
381 return UBUS_STATUS_UNKNOWN_ERROR;
384 plugin = rpc_plugin_parse_exec(name + 1, fds[0]);
389 rv = ubus_add_object(ctx, plugin);
394 waitpid(pid, NULL, 0);
401 static LIST_HEAD(plugins);
403 static const struct rpc_daemon_ops ops = {
404 .access = rpc_session_access,
409 rpc_plugin_register_library(struct ubus_context *ctx, const char *path)
411 struct rpc_plugin *p;
414 dlh = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
417 return UBUS_STATUS_UNKNOWN_ERROR;
419 p = dlsym(dlh, "rpc_plugin");
422 return UBUS_STATUS_NOT_FOUND;
424 list_add(&p->list, &plugins);
426 return p->init(&ops, ctx);
429 int rpc_plugin_api_init(struct ubus_context *ctx)
437 if ((d = opendir(RPC_PLUGIN_DIRECTORY)) != NULL)
439 while ((e = readdir(d)) != NULL)
441 snprintf(path, sizeof(path) - 1,
442 RPC_PLUGIN_DIRECTORY "/%s", e->d_name);
444 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
447 rv |= rpc_plugin_register_exec(ctx, path);
453 if ((d = opendir(RPC_LIBRARY_DIRECTORY)) != NULL)
455 while ((e = readdir(d)) != NULL)
457 snprintf(path, sizeof(path) - 1,
458 RPC_LIBRARY_DIRECTORY "/%s", e->d_name);
460 if (stat(path, &s) || !S_ISREG(s.st_mode))
463 rv |= rpc_plugin_register_library(ctx, path);