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.
23 #include <libubox/blobmsg.h>
24 #include <libubox/ustream.h>
26 #define RPC_EXEC_MAX_SIZE (4096 * 64)
27 #define RPC_EXEC_DEFAULT_TIMEOUT (120 * 1000)
29 #define ustream_for_each_read_buffer(stream, ptr, len) \
30 for (ptr = ustream_get_read_buf(stream, &len); \
31 ptr != NULL && len > 0; \
32 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
34 #define ustream_declare_read(us, fd, name) \
36 us.stream.string_data = true; \
37 us.stream.r.buffer_len = 4096; \
38 us.stream.r.max_buffers = RPC_EXEC_MAX_SIZE / 4096; \
39 us.stream.notify_read = rpc_exec_##name##_read_cb; \
40 us.stream.notify_state = rpc_exec_##name##_state_cb; \
41 ustream_fd_init(&us, fd); \
44 #define ustream_declare_write(us, fd, name) \
46 us.stream.string_data = true; \
47 us.stream.w.buffer_len = 4096; \
48 us.stream.w.max_buffers = RPC_EXEC_MAX_SIZE / 4096; \
49 us.stream.notify_write = rpc_exec_##name##_write_cb; \
50 ustream_fd_init(&us, fd); \
53 extern int rpc_exec_timeout
;
55 typedef int (*rpc_exec_write_cb_t
)(struct ustream
*, void *);
56 typedef int (*rpc_exec_read_cb_t
)(struct blob_buf
*, char *, int, void *);
57 typedef int (*rpc_exec_done_cb_t
)(struct blob_buf
*, int, void *);
59 struct rpc_exec_context
{
60 struct ubus_context
*context
;
61 struct ubus_request_data request
;
62 struct uloop_timeout timeout
;
63 struct uloop_process process
;
64 struct ustream_fd ipipe
;
65 struct ustream_fd opipe
;
66 struct ustream_fd epipe
;
76 rpc_exec_write_cb_t stdin_cb
;
77 rpc_exec_read_cb_t stdout_cb
;
78 rpc_exec_read_cb_t stderr_cb
;
79 rpc_exec_done_cb_t finish_cb
;
82 const char *rpc_exec_lookup(const char *cmd
);
84 int rpc_exec(const char **args
, rpc_exec_write_cb_t in
,
85 rpc_exec_read_cb_t out
, rpc_exec_read_cb_t err
,
86 rpc_exec_done_cb_t end
, void *priv
, struct ubus_context
*ctx
,
87 struct ubus_request_data
*req
);