luci2: implement process_list call
[project/rpcd.git] / file.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_FILE_H
20 #define __RPC_FILE_H
21
22 #include <libubus.h>
23 #include <libubox/blobmsg.h>
24 #include <libubox/ustream.h>
25
26 /* limit of sys & proc files */
27 #define RPC_FILE_MIN_SIZE               (128)
28
29 /* limit of regular files and command output data */
30 #define RPC_FILE_MAX_SIZE               (4096 * 64)
31 #define RPC_FILE_MAX_RUNTIME    (3 * 1000)
32
33 #define ustream_for_each_read_buffer(stream, ptr, len) \
34         for (ptr = ustream_get_read_buf(stream, &len);     \
35              ptr != NULL && len > 0;                       \
36              ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
37
38 #define ustream_declare(us, fd, name)                     \
39         us.stream.string_data   = true;                       \
40         us.stream.r.buffer_len  = 4096;                       \
41         us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096;   \
42         us.stream.notify_read   = rpc_file_##name##_read_cb;  \
43         us.stream.notify_state  = rpc_file_##name##_state_cb; \
44         ustream_fd_init(&us, fd);
45
46 struct rpc_file_exec_context {
47         struct ubus_context *context;
48         struct ubus_request_data request;
49         struct uloop_timeout timeout;
50         struct uloop_process process;
51         struct ustream_fd opipe;
52         struct ustream_fd epipe;
53         int outlen;
54         char *out;
55         int errlen;
56         char *err;
57         int stat;
58 };
59
60 int rpc_file_api_init(struct ubus_context *ctx);
61
62 #endif