add forced shell proto setup kill (timeout: 1 second)
[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 #include <signal.h>
8
9 #include <libubox/blobmsg_json.h>
10
11 #include "netifd.h"
12 #include "interface.h"
13 #include "interface-ip.h"
14 #include "proto.h"
15
16 static LIST_HEAD(handlers);
17 static int proto_fd;
18
19 struct proto_shell_handler {
20         struct list_head list;
21         struct proto_handler proto;
22         struct config_param_list config;
23         char *config_buf;
24         char script_name[];
25 };
26
27 struct proto_shell_state {
28         struct interface_proto_state proto;
29         struct proto_shell_handler *handler;
30         struct blob_attr *config;
31
32         struct uloop_timeout setup_timeout;
33         struct uloop_process setup_task;
34         struct uloop_process teardown_task;
35         bool teardown_pending;
36 };
37
38 static int
39 run_script(const char **argv, struct uloop_process *proc)
40 {
41         int pid;
42
43         if ((pid = fork()) < 0)
44                 return -1;
45
46         if (!pid) {
47                 fchdir(proto_fd);
48                 execvp(argv[0], (char **) argv);
49                 exit(127);
50         }
51
52         if (pid < 0)
53                 return -1;
54
55         proc->pid = pid;
56         uloop_process_add(proc);
57
58         return 0;
59 }
60
61 static int
62 proto_shell_handler(struct interface_proto_state *proto,
63                     enum interface_proto_cmd cmd, bool force)
64 {
65         struct proto_shell_state *state;
66         struct proto_shell_handler *handler;
67         struct uloop_process *proc;
68         const char *argv[6];
69         const char *action;
70         char *config;
71         int ret, i = 0;
72
73         state = container_of(proto, struct proto_shell_state, proto);
74         handler = state->handler;
75
76         if (cmd == PROTO_CMD_SETUP) {
77                 action = "setup";
78                 proc = &state->setup_task;
79         } else {
80                 action = "teardown";
81                 proc = &state->teardown_task;
82                 if (state->setup_task.pending) {
83                         uloop_timeout_set(&state->setup_timeout, 1000);
84                         kill(state->setup_task.pid, SIGINT);
85                         state->teardown_pending = true;
86                         return 0;
87                 }
88         }
89
90         config = blobmsg_format_json(state->config, true);
91         if (!config)
92                 return -1;
93
94         argv[i++] = handler->script_name;
95         argv[i++] = handler->proto.name;
96         argv[i++] = action;
97         argv[i++] = proto->iface->name;
98         argv[i++] = config;
99         if (proto->iface->main_dev.dev)
100                 argv[i++] = proto->iface->main_dev.dev->ifname;
101         argv[i] = NULL;
102
103         ret = run_script(argv, proc);
104         free(config);
105
106         return ret;
107 }
108
109 static void
110 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
111 {
112         struct proto_shell_state *state;
113
114         state = container_of(timeout, struct proto_shell_state, setup_timeout);
115         kill(state->setup_task.pid, SIGKILL);
116 }
117
118 static void
119 proto_shell_setup_cb(struct uloop_process *p, int ret)
120 {
121         struct proto_shell_state *state;
122
123         state = container_of(p, struct proto_shell_state, setup_task);
124         uloop_timeout_cancel(&state->setup_timeout);
125         if (state->teardown_pending) {
126                 state->teardown_pending = 0;
127                 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
128         }
129 }
130
131 static void
132 proto_shell_teardown_cb(struct uloop_process *p, int ret)
133 {
134         struct proto_shell_state *state;
135
136         state = container_of(p, struct proto_shell_state, teardown_task);
137         state->proto.proto_event(&state->proto, IFPEV_DOWN);
138 }
139
140 static void
141 proto_shell_free(struct interface_proto_state *proto)
142 {
143         struct proto_shell_state *state;
144
145         state = container_of(proto, struct proto_shell_state, proto);
146         free(state->config);
147         free(state);
148 }
149
150 struct interface_proto_state *
151 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
152                    struct blob_attr *attr)
153 {
154         struct proto_shell_state *state;
155
156         state = calloc(1, sizeof(*state));
157         state->config = malloc(blob_pad_len(attr));
158         if (!state->config)
159                 goto error;
160
161         memcpy(state->config, attr, blob_pad_len(attr));
162         state->proto.free = proto_shell_free;
163         state->proto.cb = proto_shell_handler;
164         state->setup_timeout.cb = proto_shell_setup_timeout_cb;
165         state->setup_task.cb = proto_shell_setup_cb;
166         state->teardown_task.cb = proto_shell_teardown_cb;
167         state->handler = container_of(h, struct proto_shell_handler, proto);
168
169         return &state->proto;
170
171 error:
172         free(state);
173         return NULL;
174 }
175
176 static json_object *
177 check_type(json_object *obj, json_type type)
178 {
179         if (!obj)
180                 return NULL;
181
182         if (json_object_get_type(obj) != type)
183                 return NULL;
184
185         return obj;
186 }
187
188 static inline json_object *
189 get_field(json_object *obj, const char *name, json_type type)
190 {
191         return check_type(json_object_object_get(obj, name), type);
192 }
193
194 static char *
195 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
196 {
197         struct blobmsg_policy *attrs;
198         char *str_buf, *str_cur;
199         int str_len = 0;
200         int i;
201
202         attrs = calloc(1, sizeof(*attrs));
203         if (!attrs)
204                 return NULL;
205
206         config->n_params = json_object_array_length(obj);
207         config->params = attrs;
208         for (i = 0; i < config->n_params; i++) {
209                 json_object *cur, *name, *type;
210
211                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
212                 if (!cur)
213                         goto error;
214
215                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
216                 if (!name)
217                         goto error;
218
219                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
220                 if (!type)
221                         goto error;
222
223                 attrs[i].name = json_object_get_string(name);
224                 attrs[i].type = json_object_get_int(type);
225                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
226                         goto error;
227
228                 str_len += strlen(attrs[i].name + 1);
229         }
230
231         str_buf = malloc(str_len);
232         if (!str_buf)
233                 goto error;
234
235         str_cur = str_buf;
236         for (i = 0; i < config->n_params; i++) {
237                 const char *name = attrs[i].name;
238
239                 attrs[i].name = str_cur;
240                 str_cur += sprintf(str_cur, "%s", name) + 1;
241         }
242
243         return str_buf;
244
245 error:
246         free(attrs);
247         config->n_params = 0;
248         return NULL;
249 }
250
251 static void
252 proto_shell_add_handler(const char *script, json_object *obj)
253 {
254         struct proto_shell_handler *handler;
255         struct proto_handler *proto;
256         json_object *config, *tmp;
257         const char *name;
258         char *str;
259
260         if (!check_type(obj, json_type_object))
261                 return;
262
263         tmp = get_field(obj, "name", json_type_string);
264         if (!tmp)
265                 return;
266
267         name = json_object_get_string(tmp);
268
269         handler = calloc(1, sizeof(*handler) +
270                          strlen(script) + 1 +
271                          strlen(name) + 1);
272         if (!handler)
273                 return;
274
275         strcpy(handler->script_name, script);
276
277         str = handler->script_name + strlen(handler->script_name) + 1;
278         strcpy(str, name);
279
280         proto = &handler->proto;
281         proto->name = str;
282         proto->config_params = &handler->config;
283         proto->attach = proto_shell_attach;
284
285         tmp = get_field(obj, "no-device", json_type_boolean);
286         if (tmp && json_object_get_boolean(tmp))
287                 handler->proto.flags |= PROTO_FLAG_NODEV;
288
289         config = get_field(obj, "config", json_type_array);
290         if (config)
291                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
292
293         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
294         add_proto_handler(proto);
295 }
296
297 static void proto_shell_add_script(const char *name)
298 {
299         struct json_tokener *tok = NULL;
300         json_object *obj;
301         static char buf[512];
302         char *start, *end, *cmd;
303         FILE *f;
304         int buflen, len;
305
306 #define DUMP_SUFFIX     " '' dump"
307
308         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
309         sprintf(cmd, "%s" DUMP_SUFFIX, name);
310
311         f = popen(cmd, "r");
312         if (!f)
313                 return;
314
315         do {
316                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
317                 if (buflen <= 0)
318                         continue;
319
320                 start = buf;
321                 len = buflen;
322                 do {
323                         end = memchr(start, '\n', len);
324                         if (end)
325                                 len = end - start;
326
327                         if (!tok)
328                                 tok = json_tokener_new();
329
330                         obj = json_tokener_parse_ex(tok, start, len);
331                         if (!is_error(obj)) {
332                                 proto_shell_add_handler(name, obj);
333                                 json_object_put(obj);
334                                 json_tokener_free(tok);
335                                 tok = NULL;
336                         }
337
338                         if (end) {
339                                 start = end + 1;
340                                 len = buflen - (start - buf);
341                         }
342                 } while (len > 0);
343         } while (!feof(f) && !ferror(f));
344
345         if (tok)
346                 json_tokener_free(tok);
347
348         pclose(f);
349 }
350
351 void __init proto_shell_init(void)
352 {
353         glob_t g;
354         int main_fd;
355         int i;
356
357         main_fd = open(".", O_RDONLY | O_DIRECTORY);
358         if (main_fd < 0)
359                 return;
360
361         if (chdir(main_path)) {
362                 perror("chdir(main path)");
363                 goto close_cur;
364         }
365
366         if (chdir("./proto"))
367                 goto close_cur;
368
369         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
370         if (proto_fd < 0)
371                 goto close_cur;
372
373         glob("./*.sh", 0, NULL, &g);
374         for (i = 0; i < g.gl_pathc; i++)
375                 proto_shell_add_script(g.gl_pathv[i]);
376
377 close_cur:
378         fchdir(main_fd);
379         close(main_fd);
380 }