proto-shell: parse shell handler metadata
[project/netifd.git] / proto-shell.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <glob.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 #include <libubox/blobmsg_json.h>
9
10 #include "netifd.h"
11 #include "interface.h"
12 #include "interface-ip.h"
13 #include "proto.h"
14
15 static LIST_HEAD(handlers);
16 static int proto_fd, main_fd;
17
18 struct proto_shell_handler {
19 struct list_head list;
20 struct proto_handler proto;
21 struct config_param_list config;
22 char *config_buf;
23 char script_name[];
24 };
25
26 struct proto_shell_state {
27 struct interface_proto_state proto;
28 struct blob_attr *config;
29 };
30
31 #define DUMP_SUFFIX " dump"
32
33 static int
34 proto_shell_handler(struct interface_proto_state *proto,
35 enum interface_proto_cmd cmd, bool force)
36 {
37 switch(cmd) {
38 case PROTO_CMD_SETUP:
39 case PROTO_CMD_TEARDOWN:
40 break;
41 }
42 return 0;
43 }
44
45 static void
46 proto_shell_free(struct interface_proto_state *proto)
47 {
48 struct proto_shell_state *state;
49
50 state = container_of(proto, struct proto_shell_state, proto);
51 free(state->config);
52 free(state);
53 }
54
55 struct interface_proto_state *
56 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
57 struct blob_attr *attr)
58 {
59 struct proto_shell_state *state;
60
61 state = calloc(1, sizeof(*state));
62 state->config = malloc(blob_pad_len(attr));
63 if (!state->config)
64 goto error;
65
66 memcpy(state->config, attr, blob_pad_len(attr));
67 state->proto.free = proto_shell_free;
68 state->proto.handler = proto_shell_handler;
69
70 return &state->proto;
71
72 error:
73 free(state);
74 return NULL;
75 }
76
77 static char *
78 proto_shell_parse_config(struct config_param_list *config, struct json_object *obj)
79 {
80 struct blobmsg_policy *attrs;
81 char *str_buf, *str_cur;
82 int str_len = 0;
83 int i;
84
85 attrs = calloc(1, sizeof(*attrs));
86 if (!attrs)
87 return NULL;
88
89 config->n_params = json_object_array_length(obj);
90 config->params = attrs;
91 for (i = 0; i < config->n_params; i++) {
92 struct json_object *cur, *name, *type;
93
94 cur = json_object_array_get_idx(obj, i);
95 if (!cur || json_object_get_type(cur) != json_type_array)
96 goto error;
97
98 name = json_object_array_get_idx(cur, 0);
99 if (!name || json_object_get_type(name) != json_type_string)
100 goto error;
101
102 type = json_object_array_get_idx(cur, 1);
103 if (!type || json_object_get_type(type) != json_type_int)
104 goto error;
105
106 attrs[i].name = json_object_get_string(name);
107 attrs[i].type = json_object_get_int(type);
108 if (attrs[i].type > BLOBMSG_TYPE_LAST)
109 goto error;
110
111 str_len += strlen(attrs[i].name + 1);
112 }
113
114 str_buf = malloc(str_len);
115 if (!str_buf)
116 goto error;
117
118 str_cur = str_buf;
119 for (i = 0; i < config->n_params; i++) {
120 const char *name = attrs[i].name;
121
122 attrs[i].name = str_cur;
123 str_cur += sprintf(str_cur, "%s", name) + 1;
124 }
125
126 return str_buf;
127
128 error:
129 free(attrs);
130 config->n_params = 0;
131 return NULL;
132 }
133
134 static void
135 proto_shell_add_handler(const char *script, struct json_object *obj)
136 {
137 struct proto_shell_handler *handler;
138 struct proto_handler *proto;
139 json_object *config, *tmp;
140 const char *name;
141 char *str;
142
143 if (json_object_get_type(obj) != json_type_object)
144 return;
145
146 tmp = json_object_object_get(obj, "name");
147 if (!tmp || json_object_get_type(tmp) != json_type_string)
148 return;
149
150 name = json_object_get_string(tmp);
151
152 handler = calloc(1, sizeof(*handler) +
153 strlen(script) + 1 +
154 strlen(name) + 1);
155 if (!handler)
156 return;
157
158 strcpy(handler->script_name, script);
159
160 str = handler->script_name + strlen(handler->script_name) + 1;
161 strcpy(str, name);
162
163 proto = &handler->proto;
164 proto->name = str;
165 proto->config_params = &handler->config;
166 proto->attach = proto_shell_attach;
167
168 config = json_object_object_get(obj, "config");
169 if (config && json_object_get_type(config) == json_type_array)
170 handler->config_buf = proto_shell_parse_config(&handler->config, config);
171
172 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
173 add_proto_handler(proto);
174 }
175
176 static void proto_shell_add_script(const char *name)
177 {
178 struct json_tokener *tok = NULL;
179 struct json_object *obj;
180 static char buf[512];
181 char *start, *end, *cmd;
182 FILE *f;
183 int buflen, len;
184
185 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
186 sprintf(cmd, "%s" DUMP_SUFFIX, name);
187
188 f = popen(cmd, "r");
189 if (!f)
190 return;
191
192 do {
193 buflen = fread(buf, 1, sizeof(buf) - 1, f);
194 if (buflen <= 0)
195 continue;
196
197 start = buf;
198 len = buflen;
199 do {
200 end = memchr(start, '\n', len);
201 if (end)
202 len = end - start;
203
204 if (!tok)
205 tok = json_tokener_new();
206
207 obj = json_tokener_parse_ex(tok, start, len);
208 if (!is_error(obj)) {
209 proto_shell_add_handler(name, obj);
210 json_object_put(obj);
211 json_tokener_free(tok);
212 tok = NULL;
213 }
214
215 if (end) {
216 start = end + 1;
217 len = buflen - (start - buf);
218 }
219 } while (len > 0);
220 } while (!feof(f) && !ferror(f));
221
222 if (tok)
223 json_tokener_free(tok);
224
225 pclose(f);
226 }
227
228 void __init proto_shell_init(void)
229 {
230 glob_t g;
231 int i;
232
233 main_fd = open(".", O_RDONLY | O_DIRECTORY);
234 if (main_fd < 0)
235 return;
236
237 if (chdir(main_path)) {
238 perror("chdir(main path)");
239 goto close_cur;
240 }
241
242 if (chdir("./proto"))
243 goto close_cur;
244
245 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
246 if (proto_fd < 0)
247 goto close_cur;
248
249 glob("./*.sh", 0, NULL, &g);
250 for (i = 0; i < g.gl_pathc; i++)
251 proto_shell_add_script(g.gl_pathv[i]);
252
253 if (list_empty(&handlers))
254 close(proto_fd);
255
256 close_cur:
257 fchdir(main_fd);
258 if (list_empty(&handlers))
259 close(main_fd);
260 }