fix invoking shell protocol handler scripts
[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 #define DUMP_SUFFIX     " '' dump"
33
34 static int run_script(const char **argv)
35 {
36         int pid, ret;
37
38         if ((pid = fork()) < 0)
39                 return -1;
40
41         if (!pid) {
42                 fchdir(proto_fd);
43                 execvp(argv[0], (char **) argv);
44                 exit(127);
45         }
46
47         if (waitpid(pid, &ret, 0) == -1)
48                 ret = -1;
49
50         if (ret > 0)
51                 return -ret;
52
53         return 0;
54 }
55
56 static int
57 proto_shell_handler(struct interface_proto_state *proto,
58                     enum interface_proto_cmd cmd, bool force)
59 {
60         struct proto_shell_state *state;
61         struct proto_shell_handler *handler;
62         const char *argv[5];
63         char *config;
64         int ret;
65
66         state = container_of(proto, struct proto_shell_state, proto);
67         handler = state->handler;
68
69         config = blobmsg_format_json(state->config, true);
70         if (!config)
71                 return -1;
72
73         argv[0] = handler->script_name;
74         argv[1] = handler->proto.name;
75         argv[2] = "teardown";
76         argv[3] = config;
77         argv[4] = NULL;
78
79         switch(cmd) {
80         case PROTO_CMD_SETUP:
81                 argv[2] = "setup";
82                 /* fall through */
83         case PROTO_CMD_TEARDOWN:
84                 ret = run_script(argv);
85                 break;
86         }
87
88         free(config);
89
90         return ret;
91 }
92
93 static void
94 proto_shell_free(struct interface_proto_state *proto)
95 {
96         struct proto_shell_state *state;
97
98         state = container_of(proto, struct proto_shell_state, proto);
99         free(state->config);
100         free(state);
101 }
102
103 struct interface_proto_state *
104 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
105                    struct blob_attr *attr)
106 {
107         struct proto_shell_state *state;
108
109         state = calloc(1, sizeof(*state));
110         state->config = malloc(blob_pad_len(attr));
111         if (!state->config)
112                 goto error;
113
114         memcpy(state->config, attr, blob_pad_len(attr));
115         state->proto.free = proto_shell_free;
116         state->proto.handler = proto_shell_handler;
117         state->handler = container_of(h, struct proto_shell_handler, proto);
118
119         return &state->proto;
120
121 error:
122         free(state);
123         return NULL;
124 }
125
126 static char *
127 proto_shell_parse_config(struct config_param_list *config, struct json_object *obj)
128 {
129         struct blobmsg_policy *attrs;
130         char *str_buf, *str_cur;
131         int str_len = 0;
132         int i;
133
134         attrs = calloc(1, sizeof(*attrs));
135         if (!attrs)
136                 return NULL;
137
138         config->n_params = json_object_array_length(obj);
139         config->params = attrs;
140         for (i = 0; i < config->n_params; i++) {
141                 struct json_object *cur, *name, *type;
142
143                 cur = json_object_array_get_idx(obj, i);
144                 if (!cur || json_object_get_type(cur) != json_type_array)
145                         goto error;
146
147                 name = json_object_array_get_idx(cur, 0);
148                 if (!name || json_object_get_type(name) != json_type_string)
149                         goto error;
150
151                 type = json_object_array_get_idx(cur, 1);
152                 if (!type || json_object_get_type(type) != json_type_int)
153                         goto error;
154
155                 attrs[i].name = json_object_get_string(name);
156                 attrs[i].type = json_object_get_int(type);
157                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
158                         goto error;
159
160                 str_len += strlen(attrs[i].name + 1);
161         }
162
163         str_buf = malloc(str_len);
164         if (!str_buf)
165                 goto error;
166
167         str_cur = str_buf;
168         for (i = 0; i < config->n_params; i++) {
169                 const char *name = attrs[i].name;
170
171                 attrs[i].name = str_cur;
172                 str_cur += sprintf(str_cur, "%s", name) + 1;
173         }
174
175         return str_buf;
176
177 error:
178         free(attrs);
179         config->n_params = 0;
180         return NULL;
181 }
182
183 static void
184 proto_shell_add_handler(const char *script, struct json_object *obj)
185 {
186         struct proto_shell_handler *handler;
187         struct proto_handler *proto;
188         json_object *config, *tmp;
189         const char *name;
190         char *str;
191
192         if (json_object_get_type(obj) != json_type_object)
193                 return;
194
195         tmp = json_object_object_get(obj, "name");
196         if (!tmp || json_object_get_type(tmp) != json_type_string)
197                 return;
198
199         name = json_object_get_string(tmp);
200
201         handler = calloc(1, sizeof(*handler) +
202                          strlen(script) + 1 +
203                          strlen(name) + 1);
204         if (!handler)
205                 return;
206
207         strcpy(handler->script_name, script);
208
209         str = handler->script_name + strlen(handler->script_name) + 1;
210         strcpy(str, name);
211
212         proto = &handler->proto;
213         proto->name = str;
214         proto->config_params = &handler->config;
215         proto->attach = proto_shell_attach;
216
217         config = json_object_object_get(obj, "config");
218         if (config && json_object_get_type(config) == json_type_array)
219                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
220
221         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
222         add_proto_handler(proto);
223 }
224
225 static void proto_shell_add_script(const char *name)
226 {
227         struct json_tokener *tok = NULL;
228         struct json_object *obj;
229         static char buf[512];
230         char *start, *end, *cmd;
231         FILE *f;
232         int buflen, len;
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 }