8d99c740f3cd10bc02aa07417238092b34c5eca4
[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;
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 proto_shell_handler *handler;
29         struct blob_attr *config;
30 };
31
32 static int run_script(const char **argv)
33 {
34         int pid, ret;
35
36         if ((pid = fork()) < 0)
37                 return -1;
38
39         if (!pid) {
40                 fchdir(proto_fd);
41                 execvp(argv[0], (char **) argv);
42                 exit(127);
43         }
44
45         if (waitpid(pid, &ret, 0) == -1)
46                 ret = -1;
47
48         if (ret > 0)
49                 return -ret;
50
51         return 0;
52 }
53
54 static int
55 proto_shell_handler(struct interface_proto_state *proto,
56                     enum interface_proto_cmd cmd, bool force)
57 {
58         struct proto_shell_state *state;
59         struct proto_shell_handler *handler;
60         const char *argv[5];
61         char *config;
62         int ret;
63
64         state = container_of(proto, struct proto_shell_state, proto);
65         handler = state->handler;
66
67         config = blobmsg_format_json(state->config, true);
68         if (!config)
69                 return -1;
70
71         argv[0] = handler->script_name;
72         argv[1] = handler->proto.name;
73         argv[2] = "teardown";
74         argv[3] = config;
75         argv[4] = NULL;
76
77         switch(cmd) {
78         case PROTO_CMD_SETUP:
79                 argv[2] = "setup";
80                 /* fall through */
81         case PROTO_CMD_TEARDOWN:
82                 ret = run_script(argv);
83                 break;
84         }
85
86         free(config);
87
88         return ret;
89 }
90
91 static void
92 proto_shell_free(struct interface_proto_state *proto)
93 {
94         struct proto_shell_state *state;
95
96         state = container_of(proto, struct proto_shell_state, proto);
97         free(state->config);
98         free(state);
99 }
100
101 struct interface_proto_state *
102 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
103                    struct blob_attr *attr)
104 {
105         struct proto_shell_state *state;
106
107         state = calloc(1, sizeof(*state));
108         state->config = malloc(blob_pad_len(attr));
109         if (!state->config)
110                 goto error;
111
112         memcpy(state->config, attr, blob_pad_len(attr));
113         state->proto.free = proto_shell_free;
114         state->proto.handler = proto_shell_handler;
115         state->handler = container_of(h, struct proto_shell_handler, proto);
116
117         return &state->proto;
118
119 error:
120         free(state);
121         return NULL;
122 }
123
124 static char *
125 proto_shell_parse_config(struct config_param_list *config, struct json_object *obj)
126 {
127         struct blobmsg_policy *attrs;
128         char *str_buf, *str_cur;
129         int str_len = 0;
130         int i;
131
132         attrs = calloc(1, sizeof(*attrs));
133         if (!attrs)
134                 return NULL;
135
136         config->n_params = json_object_array_length(obj);
137         config->params = attrs;
138         for (i = 0; i < config->n_params; i++) {
139                 struct json_object *cur, *name, *type;
140
141                 cur = json_object_array_get_idx(obj, i);
142                 if (!cur || json_object_get_type(cur) != json_type_array)
143                         goto error;
144
145                 name = json_object_array_get_idx(cur, 0);
146                 if (!name || json_object_get_type(name) != json_type_string)
147                         goto error;
148
149                 type = json_object_array_get_idx(cur, 1);
150                 if (!type || json_object_get_type(type) != json_type_int)
151                         goto error;
152
153                 attrs[i].name = json_object_get_string(name);
154                 attrs[i].type = json_object_get_int(type);
155                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
156                         goto error;
157
158                 str_len += strlen(attrs[i].name + 1);
159         }
160
161         str_buf = malloc(str_len);
162         if (!str_buf)
163                 goto error;
164
165         str_cur = str_buf;
166         for (i = 0; i < config->n_params; i++) {
167                 const char *name = attrs[i].name;
168
169                 attrs[i].name = str_cur;
170                 str_cur += sprintf(str_cur, "%s", name) + 1;
171         }
172
173         return str_buf;
174
175 error:
176         free(attrs);
177         config->n_params = 0;
178         return NULL;
179 }
180
181 static void
182 proto_shell_add_handler(const char *script, struct json_object *obj)
183 {
184         struct proto_shell_handler *handler;
185         struct proto_handler *proto;
186         json_object *config, *tmp;
187         const char *name;
188         char *str;
189
190         if (json_object_get_type(obj) != json_type_object)
191                 return;
192
193         tmp = json_object_object_get(obj, "name");
194         if (!tmp || json_object_get_type(tmp) != json_type_string)
195                 return;
196
197         name = json_object_get_string(tmp);
198
199         handler = calloc(1, sizeof(*handler) +
200                          strlen(script) + 1 +
201                          strlen(name) + 1);
202         if (!handler)
203                 return;
204
205         strcpy(handler->script_name, script);
206
207         str = handler->script_name + strlen(handler->script_name) + 1;
208         strcpy(str, name);
209
210         proto = &handler->proto;
211         proto->name = str;
212         proto->config_params = &handler->config;
213         proto->attach = proto_shell_attach;
214
215         config = json_object_object_get(obj, "config");
216         if (config && json_object_get_type(config) == json_type_array)
217                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
218
219         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
220         add_proto_handler(proto);
221 }
222
223 static void proto_shell_add_script(const char *name)
224 {
225         struct json_tokener *tok = NULL;
226         struct json_object *obj;
227         static char buf[512];
228         char *start, *end, *cmd;
229         FILE *f;
230         int buflen, len;
231
232 #define DUMP_SUFFIX     " '' dump"
233
234         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
235         sprintf(cmd, "%s" DUMP_SUFFIX, name);
236
237         f = popen(cmd, "r");
238         if (!f)
239                 return;
240
241         do {
242                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
243                 if (buflen <= 0)
244                         continue;
245
246                 start = buf;
247                 len = buflen;
248                 do {
249                         end = memchr(start, '\n', len);
250                         if (end)
251                                 len = end - start;
252
253                         if (!tok)
254                                 tok = json_tokener_new();
255
256                         obj = json_tokener_parse_ex(tok, start, len);
257                         if (!is_error(obj)) {
258                                 proto_shell_add_handler(name, obj);
259                                 json_object_put(obj);
260                                 json_tokener_free(tok);
261                                 tok = NULL;
262                         }
263
264                         if (end) {
265                                 start = end + 1;
266                                 len = buflen - (start - buf);
267                         }
268                 } while (len > 0);
269         } while (!feof(f) && !ferror(f));
270
271         if (tok)
272                 json_tokener_free(tok);
273
274         pclose(f);
275 }
276
277 void __init proto_shell_init(void)
278 {
279         glob_t g;
280         int main_fd;
281         int i;
282
283         main_fd = open(".", O_RDONLY | O_DIRECTORY);
284         if (main_fd < 0)
285                 return;
286
287         if (chdir(main_path)) {
288                 perror("chdir(main path)");
289                 goto close_cur;
290         }
291
292         if (chdir("./proto"))
293                 goto close_cur;
294
295         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
296         if (proto_fd < 0)
297                 goto close_cur;
298
299         glob("./*.sh", 0, NULL, &g);
300         for (i = 0; i < g.gl_pathc; i++)
301                 proto_shell_add_script(g.gl_pathv[i]);
302
303 close_cur:
304         fchdir(main_fd);
305         close(main_fd);
306 }