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