proto-shell: add a config option for setting PROTO_FLAG_NODEV
[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.cb = 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 json_object *
125 check_type(json_object *obj, json_type type)
126 {
127         if (!obj)
128                 return NULL;
129
130         if (json_object_get_type(obj) != type)
131                 return NULL;
132
133         return obj;
134 }
135
136 static inline json_object *
137 get_field(json_object *obj, const char *name, json_type type)
138 {
139         return check_type(json_object_object_get(obj, name), type);
140 }
141
142 static char *
143 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
144 {
145         struct blobmsg_policy *attrs;
146         char *str_buf, *str_cur;
147         int str_len = 0;
148         int i;
149
150         attrs = calloc(1, sizeof(*attrs));
151         if (!attrs)
152                 return NULL;
153
154         config->n_params = json_object_array_length(obj);
155         config->params = attrs;
156         for (i = 0; i < config->n_params; i++) {
157                 json_object *cur, *name, *type;
158
159                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
160                 if (!cur)
161                         goto error;
162
163                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
164                 if (!name)
165                         goto error;
166
167                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
168                 if (!type)
169                         goto error;
170
171                 attrs[i].name = json_object_get_string(name);
172                 attrs[i].type = json_object_get_int(type);
173                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
174                         goto error;
175
176                 str_len += strlen(attrs[i].name + 1);
177         }
178
179         str_buf = malloc(str_len);
180         if (!str_buf)
181                 goto error;
182
183         str_cur = str_buf;
184         for (i = 0; i < config->n_params; i++) {
185                 const char *name = attrs[i].name;
186
187                 attrs[i].name = str_cur;
188                 str_cur += sprintf(str_cur, "%s", name) + 1;
189         }
190
191         return str_buf;
192
193 error:
194         free(attrs);
195         config->n_params = 0;
196         return NULL;
197 }
198
199 static void
200 proto_shell_add_handler(const char *script, json_object *obj)
201 {
202         struct proto_shell_handler *handler;
203         struct proto_handler *proto;
204         json_object *config, *tmp;
205         const char *name;
206         char *str;
207
208         if (!check_type(obj, json_type_object))
209                 return;
210
211         tmp = get_field(obj, "name", json_type_string);
212         if (!tmp)
213                 return;
214
215         name = json_object_get_string(tmp);
216
217         handler = calloc(1, sizeof(*handler) +
218                          strlen(script) + 1 +
219                          strlen(name) + 1);
220         if (!handler)
221                 return;
222
223         strcpy(handler->script_name, script);
224
225         str = handler->script_name + strlen(handler->script_name) + 1;
226         strcpy(str, name);
227
228         proto = &handler->proto;
229         proto->name = str;
230         proto->config_params = &handler->config;
231         proto->attach = proto_shell_attach;
232
233         tmp = get_field(obj, "no-device", json_type_boolean);
234         if (tmp && json_object_get_boolean(tmp))
235                 handler->proto.flags |= PROTO_FLAG_NODEV;
236
237         config = get_field(obj, "config", json_type_array);
238         if (config)
239                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
240
241         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
242         add_proto_handler(proto);
243 }
244
245 static void proto_shell_add_script(const char *name)
246 {
247         struct json_tokener *tok = NULL;
248         json_object *obj;
249         static char buf[512];
250         char *start, *end, *cmd;
251         FILE *f;
252         int buflen, len;
253
254 #define DUMP_SUFFIX     " '' dump"
255
256         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
257         sprintf(cmd, "%s" DUMP_SUFFIX, name);
258
259         f = popen(cmd, "r");
260         if (!f)
261                 return;
262
263         do {
264                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
265                 if (buflen <= 0)
266                         continue;
267
268                 start = buf;
269                 len = buflen;
270                 do {
271                         end = memchr(start, '\n', len);
272                         if (end)
273                                 len = end - start;
274
275                         if (!tok)
276                                 tok = json_tokener_new();
277
278                         obj = json_tokener_parse_ex(tok, start, len);
279                         if (!is_error(obj)) {
280                                 proto_shell_add_handler(name, obj);
281                                 json_object_put(obj);
282                                 json_tokener_free(tok);
283                                 tok = NULL;
284                         }
285
286                         if (end) {
287                                 start = end + 1;
288                                 len = buflen - (start - buf);
289                         }
290                 } while (len > 0);
291         } while (!feof(f) && !ferror(f));
292
293         if (tok)
294                 json_tokener_free(tok);
295
296         pclose(f);
297 }
298
299 void __init proto_shell_init(void)
300 {
301         glob_t g;
302         int main_fd;
303         int i;
304
305         main_fd = open(".", O_RDONLY | O_DIRECTORY);
306         if (main_fd < 0)
307                 return;
308
309         if (chdir(main_path)) {
310                 perror("chdir(main path)");
311                 goto close_cur;
312         }
313
314         if (chdir("./proto"))
315                 goto close_cur;
316
317         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
318         if (proto_fd < 0)
319                 goto close_cur;
320
321         glob("./*.sh", 0, NULL, &g);
322         for (i = 0; i < g.gl_pathc; i++)
323                 proto_shell_add_script(g.gl_pathv[i]);
324
325 close_cur:
326         fchdir(main_fd);
327         close(main_fd);
328 }