9 #include <libubox/blobmsg_json.h>
12 #include "interface.h"
13 #include "interface-ip.h"
16 static LIST_HEAD(handlers
);
19 struct proto_shell_handler
{
20 struct list_head list
;
21 struct proto_handler proto
;
22 struct config_param_list config
;
27 struct proto_shell_state
{
28 struct interface_proto_state proto
;
29 struct proto_shell_handler
*handler
;
30 struct blob_attr
*config
;
32 struct uloop_timeout setup_timeout
;
33 struct uloop_process setup_task
;
34 struct uloop_process teardown_task
;
35 bool teardown_pending
;
39 run_script(const char **argv
, struct uloop_process
*proc
)
43 if ((pid
= fork()) < 0)
48 execvp(argv
[0], (char **) argv
);
56 uloop_process_add(proc
);
62 proto_shell_handler(struct interface_proto_state
*proto
,
63 enum interface_proto_cmd cmd
, bool force
)
65 struct proto_shell_state
*state
;
66 struct proto_shell_handler
*handler
;
67 struct uloop_process
*proc
;
73 state
= container_of(proto
, struct proto_shell_state
, proto
);
74 handler
= state
->handler
;
76 if (cmd
== PROTO_CMD_SETUP
) {
78 proc
= &state
->setup_task
;
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;
90 config
= blobmsg_format_json(state
->config
, true);
94 argv
[i
++] = handler
->script_name
;
95 argv
[i
++] = handler
->proto
.name
;
97 argv
[i
++] = proto
->iface
->name
;
99 if (proto
->iface
->main_dev
.dev
)
100 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
103 ret
= run_script(argv
, proc
);
110 proto_shell_setup_timeout_cb(struct uloop_timeout
*timeout
)
112 struct proto_shell_state
*state
;
114 state
= container_of(timeout
, struct proto_shell_state
, setup_timeout
);
115 kill(state
->setup_task
.pid
, SIGKILL
);
119 proto_shell_setup_cb(struct uloop_process
*p
, int ret
)
121 struct proto_shell_state
*state
;
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);
132 proto_shell_teardown_cb(struct uloop_process
*p
, int ret
)
134 struct proto_shell_state
*state
;
136 state
= container_of(p
, struct proto_shell_state
, teardown_task
);
137 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
141 proto_shell_free(struct interface_proto_state
*proto
)
143 struct proto_shell_state
*state
;
145 state
= container_of(proto
, struct proto_shell_state
, proto
);
150 struct interface_proto_state
*
151 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
152 struct blob_attr
*attr
)
154 struct proto_shell_state
*state
;
156 state
= calloc(1, sizeof(*state
));
157 state
->config
= malloc(blob_pad_len(attr
));
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
);
169 return &state
->proto
;
177 check_type(json_object
*obj
, json_type type
)
182 if (json_object_get_type(obj
) != type
)
188 static inline json_object
*
189 get_field(json_object
*obj
, const char *name
, json_type type
)
191 return check_type(json_object_object_get(obj
, name
), type
);
195 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
197 struct blobmsg_policy
*attrs
;
198 char *str_buf
, *str_cur
;
202 attrs
= calloc(1, sizeof(*attrs
));
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
;
211 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
215 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
219 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
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
)
228 str_len
+= strlen(attrs
[i
].name
+ 1);
231 str_buf
= malloc(str_len
);
236 for (i
= 0; i
< config
->n_params
; i
++) {
237 const char *name
= attrs
[i
].name
;
239 attrs
[i
].name
= str_cur
;
240 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
247 config
->n_params
= 0;
252 proto_shell_add_handler(const char *script
, json_object
*obj
)
254 struct proto_shell_handler
*handler
;
255 struct proto_handler
*proto
;
256 json_object
*config
, *tmp
;
260 if (!check_type(obj
, json_type_object
))
263 tmp
= get_field(obj
, "name", json_type_string
);
267 name
= json_object_get_string(tmp
);
269 handler
= calloc(1, sizeof(*handler
) +
275 strcpy(handler
->script_name
, script
);
277 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
280 proto
= &handler
->proto
;
282 proto
->config_params
= &handler
->config
;
283 proto
->attach
= proto_shell_attach
;
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
;
289 config
= get_field(obj
, "config", json_type_array
);
291 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
293 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
294 add_proto_handler(proto
);
297 static void proto_shell_add_script(const char *name
)
299 struct json_tokener
*tok
= NULL
;
301 static char buf
[512];
302 char *start
, *end
, *cmd
;
306 #define DUMP_SUFFIX " '' dump"
308 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
309 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
316 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
323 end
= memchr(start
, '\n', len
);
328 tok
= json_tokener_new();
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
);
340 len
= buflen
- (start
- buf
);
343 } while (!feof(f
) && !ferror(f
));
346 json_tokener_free(tok
);
351 void __init
proto_shell_init(void)
357 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
361 if (chdir(main_path
)) {
362 perror("chdir(main path)");
366 if (chdir("./proto"))
369 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
373 glob("./*.sh", 0, NULL
, &g
);
374 for (i
= 0; i
< g
.gl_pathc
; i
++)
375 proto_shell_add_script(g
.gl_pathv
[i
]);