01981fe34e5d389b1e5591930f7bdb865905ca04
[project/rpcd.git] / include / rpcd / exec.h
1 /*
2 * rpcd - UBUS RPC server
3 *
4 * Copyright (C) 2013-2014 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_DEFAULT_TIMEOUT (120 * 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 extern int exec_timeout;
54
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 *);
58
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;
67 int outlen;
68 char *out;
69 int errlen;
70 char *err;
71 int stat;
72 void *priv;
73 bool blob_array;
74 void *blob_cookie;
75 struct blob_buf blob;
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;
80 };
81
82 const char *rpc_exec_lookup(const char *cmd);
83
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);
88
89 #endif