2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 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.
19 #include <rpcd/plugin.h>
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 blobmsg_add_object(blob
, c
->obj
))
115 json_object_put(c
->obj
);
119 rv
= UBUS_STATUS_NO_DATA
;
123 json_tokener_free(c
->tok
);
132 rpc_plugin_call(struct ubus_context
*ctx
, struct ubus_object
*obj
,
133 struct ubus_request_data
*req
, const char *method
,
134 struct blob_attr
*msg
)
136 int rv
= UBUS_STATUS_UNKNOWN_ERROR
;
137 struct call_context
*c
;
140 c
= calloc_a(sizeof(*c
), &mptr
, strlen(method
) + 1);
145 c
->method
= strcpy(mptr
, method
);
146 c
->input
= blobmsg_format_json(msg
, true);
147 c
->tok
= json_tokener_new();
149 if (!c
->input
|| !c
->tok
)
152 plugin
= c
->path
+ sprintf(c
->path
, "%s/", RPC_PLUGIN_DIRECTORY
);
154 if (!rpc_plugin_lookup_plugin(ctx
, obj
, plugin
))
156 rv
= UBUS_STATUS_NOT_FOUND
;
160 c
->argv
[0] = c
->path
;
162 c
->argv
[2] = c
->method
;
164 rv
= rpc_exec(c
->argv
, rpc_plugin_call_stdin_cb
,
165 rpc_plugin_call_stdout_cb
, rpc_plugin_call_stderr_cb
,
166 rpc_plugin_call_finish_cb
, c
, ctx
, req
);
168 if (rv
== UBUS_STATUS_OK
)
178 json_tokener_free(c
->tok
);
187 rpc_plugin_parse_signature(struct blob_attr
*sig
, struct ubus_method
*method
)
190 enum blobmsg_type type
;
191 struct blob_attr
*attr
;
192 struct blobmsg_policy
*policy
= NULL
;
194 if (!sig
|| blobmsg_type(sig
) != BLOBMSG_TYPE_TABLE
)
199 blobmsg_for_each_attr(attr
, sig
, rem
)
204 policy
= calloc(n_attr
, sizeof(*policy
));
211 blobmsg_for_each_attr(attr
, sig
, rem
)
213 type
= blobmsg_type(attr
);
215 if (type
== BLOBMSG_TYPE_INT32
)
217 switch (blobmsg_get_u32(attr
))
220 type
= BLOBMSG_TYPE_INT8
;
224 type
= BLOBMSG_TYPE_INT16
;
228 type
= BLOBMSG_TYPE_INT64
;
232 type
= BLOBMSG_TYPE_INT32
;
237 policy
[n_attr
].name
= strdup(blobmsg_name(attr
));
238 policy
[n_attr
].type
= type
;
244 method
->name
= strdup(blobmsg_name(sig
));
245 method
->handler
= rpc_plugin_call
;
246 method
->policy
= policy
;
247 method
->n_policy
= n_attr
;
252 static struct ubus_object
*
253 rpc_plugin_parse_exec(const char *name
, int fd
)
255 int len
, rem
, n_method
;
256 struct blob_attr
*cur
;
257 struct ubus_method
*methods
;
258 struct ubus_object_type
*obj_type
;
259 struct ubus_object
*obj
;
265 blob_buf_init(&buf
, 0);
267 tok
= json_tokener_new();
272 while ((len
= read(fd
, outbuf
, sizeof(outbuf
))) > 0)
274 jsobj
= json_tokener_parse_ex(tok
, outbuf
, len
);
276 if (json_tokener_get_error(tok
) == json_tokener_continue
)
279 if (json_tokener_get_error(tok
) != json_tokener_success
)
284 if (json_object_get_type(jsobj
) == json_type_object
)
285 blobmsg_add_object(&buf
, jsobj
);
287 json_object_put(jsobj
);
292 json_tokener_free(tok
);
296 blob_for_each_attr(cur
, buf
.head
, rem
)
302 methods
= calloc(n_method
, sizeof(*methods
));
309 blob_for_each_attr(cur
, buf
.head
, rem
)
311 if (!rpc_plugin_parse_signature(cur
, &methods
[n_method
]))
317 obj
= calloc(1, sizeof(*obj
));
322 obj_type
= calloc(1, sizeof(*obj_type
));
329 if (asprintf((char **)&obj_type
->name
, "luci-rpc-plugin-%s", name
) < 0) {
335 obj_type
->methods
= methods
;
336 obj_type
->n_methods
= n_method
;
338 obj
->name
= strdup(name
);
339 obj
->type
= obj_type
;
340 obj
->methods
= methods
;
341 obj
->n_methods
= n_method
;
347 rpc_plugin_register_exec(struct ubus_context
*ctx
, const char *path
)
350 int rv
= UBUS_STATUS_NO_DATA
, fd
, fds
[2];
352 struct ubus_object
*plugin
;
354 name
= strrchr(path
, '/');
357 return UBUS_STATUS_INVALID_ARGUMENT
;
360 return UBUS_STATUS_UNKNOWN_ERROR
;
362 switch ((pid
= fork()))
365 return UBUS_STATUS_UNKNOWN_ERROR
;
368 fd
= open("/dev/null", O_RDWR
);
384 if (execl(path
, path
, "list", NULL
))
385 return UBUS_STATUS_UNKNOWN_ERROR
;
388 plugin
= rpc_plugin_parse_exec(name
+ 1, fds
[0]);
393 rv
= ubus_add_object(ctx
, plugin
);
398 waitpid(pid
, NULL
, 0);
405 static LIST_HEAD(plugins
);
407 static const struct rpc_daemon_ops ops
= {
408 .session_access
= rpc_session_access
,
409 .session_create_cb
= rpc_session_create_cb
,
410 .session_destroy_cb
= rpc_session_destroy_cb
,
412 .exec_timeout
= &rpc_exec_timeout
,
416 rpc_plugin_register_library(struct ubus_context
*ctx
, const char *path
)
418 struct rpc_plugin
*p
;
421 dlh
= dlopen(path
, RTLD_LAZY
| RTLD_LOCAL
);
424 return UBUS_STATUS_UNKNOWN_ERROR
;
426 p
= dlsym(dlh
, "rpc_plugin");
429 return UBUS_STATUS_NOT_FOUND
;
431 list_add(&p
->list
, &plugins
);
433 return p
->init(&ops
, ctx
);
436 int rpc_plugin_api_init(struct ubus_context
*ctx
)
444 if ((d
= opendir(RPC_PLUGIN_DIRECTORY
)) != NULL
)
446 while ((e
= readdir(d
)) != NULL
)
448 snprintf(path
, sizeof(path
) - 1,
449 RPC_PLUGIN_DIRECTORY
"/%s", e
->d_name
);
451 if (stat(path
, &s
) || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
))
454 rv
|= rpc_plugin_register_exec(ctx
, path
);
460 if ((d
= opendir(RPC_LIBRARY_DIRECTORY
)) != NULL
)
462 while ((e
= readdir(d
)) != NULL
)
464 snprintf(path
, sizeof(path
) - 1,
465 RPC_LIBRARY_DIRECTORY
"/%s", e
->d_name
);
467 if (stat(path
, &s
) || !S_ISREG(s
.st_mode
))
470 rv
|= rpc_plugin_register_library(ctx
, path
);