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