c61e5e38a2f3ad02e2a24e5b9cecee0edd1a0c67
[project/rpcd.git] / exec.h
1 /*
2  * luci-rpcd - LuCI 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(us, fd, name)                     \
35         us.stream.string_data   = true;                       \
36         us.stream.r.buffer_len  = 4096;                       \
37         us.stream.r.max_buffers = RPC_EXEC_MAX_SIZE / 4096;   \
38         us.stream.notify_read   = rpc_exec_##name##_read_cb;  \
39         us.stream.notify_state  = rpc_exec_##name##_state_cb; \
40         ustream_fd_init(&us, fd);
41
42 typedef int (*rpc_exec_read_cb_t)(struct blob_buf *, char *, int, void *);
43 typedef void (*rpc_exec_done_cb_t)(struct blob_buf *, int, void *);
44
45 struct rpc_exec_context {
46         struct ubus_context *context;
47         struct ubus_request_data request;
48         struct uloop_timeout timeout;
49         struct uloop_process process;
50         struct ustream_fd opipe;
51         struct ustream_fd epipe;
52         int outlen;
53         char *out;
54         int errlen;
55         char *err;
56         int stat;
57         void *priv;
58         bool blob_array;
59         void *blob_cookie;
60         struct blob_buf blob;
61         rpc_exec_read_cb_t stdout_cb;
62         rpc_exec_read_cb_t stderr_cb;
63         rpc_exec_done_cb_t finish_cb;
64 };
65
66 int rpc_exec(const char **args, rpc_exec_read_cb_t out, rpc_exec_read_cb_t err,
67              rpc_exec_done_cb_t end, void *priv, struct ubus_context *ctx,
68              struct ubus_request_data *req);
69
70 #endif