dca1bad95260e47bbaa41c6ddb4e1e582d841503
[project/rpcd.git] / include / rpcd / exec.h
1 /*
2 * rpcd - UBUS RPC server
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #ifndef __RPC_EXEC_H
20 #define __RPC_EXEC_H
21
22 #include <libubus.h>
23 #include <libubox/blobmsg.h>
24 #include <libubox/ustream.h>
25
26 #define RPC_EXEC_MAX_SIZE (4096 * 64)
27 #define RPC_EXEC_MAX_RUNTIME (30 * 1000)
28
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))
33
34 #define ustream_declare_read(us, fd, name) \
35 do { \
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); \
42 } while(0)
43
44 #define ustream_declare_write(us, fd, name) \
45 do { \
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); \
51 } while(0)
52
53
54 typedef int (*rpc_exec_write_cb_t)(struct ustream *, void *);
55 typedef int (*rpc_exec_read_cb_t)(struct blob_buf *, char *, int, void *);
56 typedef int (*rpc_exec_done_cb_t)(struct blob_buf *, int, void *);
57
58 struct rpc_exec_context {
59 struct ubus_context *context;
60 struct ubus_request_data request;
61 struct uloop_timeout timeout;
62 struct uloop_process process;
63 struct ustream_fd ipipe;
64 struct ustream_fd opipe;
65 struct ustream_fd epipe;
66 int outlen;
67 char *out;
68 int errlen;
69 char *err;
70 int stat;
71 void *priv;
72 bool blob_array;
73 void *blob_cookie;
74 struct blob_buf blob;
75 rpc_exec_write_cb_t stdin_cb;
76 rpc_exec_read_cb_t stdout_cb;
77 rpc_exec_read_cb_t stderr_cb;
78 rpc_exec_done_cb_t finish_cb;
79 };
80
81 const char *rpc_exec_lookup(const char *cmd);
82
83 int rpc_exec(const char **args, rpc_exec_write_cb_t in,
84 rpc_exec_read_cb_t out, rpc_exec_read_cb_t err,
85 rpc_exec_done_cb_t end, void *priv, struct ubus_context *ctx,
86 struct ubus_request_data *req);
87
88 #endif