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 device_user l3_dev
;
34 struct uloop_timeout setup_timeout
;
35 struct uloop_process setup_task
;
36 struct uloop_process teardown_task
;
37 bool teardown_pending
;
41 run_script(const char **argv
, struct uloop_process
*proc
)
45 if ((pid
= fork()) < 0)
50 execvp(argv
[0], (char **) argv
);
58 uloop_process_add(proc
);
64 proto_shell_set_down(struct proto_shell_state
*state
)
66 if (!state
->l3_dev
.dev
)
69 device_remove_user(&state
->l3_dev
);
73 proto_shell_handler(struct interface_proto_state
*proto
,
74 enum interface_proto_cmd cmd
, bool force
)
76 struct proto_shell_state
*state
;
77 struct proto_shell_handler
*handler
;
78 struct uloop_process
*proc
;
84 state
= container_of(proto
, struct proto_shell_state
, proto
);
85 handler
= state
->handler
;
87 if (cmd
== PROTO_CMD_SETUP
) {
89 proc
= &state
->setup_task
;
92 proc
= &state
->teardown_task
;
93 if (state
->setup_task
.pending
) {
94 uloop_timeout_set(&state
->setup_timeout
, 1000);
95 kill(state
->setup_task
.pid
, SIGINT
);
96 state
->teardown_pending
= true;
101 config
= blobmsg_format_json(state
->config
, true);
105 argv
[i
++] = handler
->script_name
;
106 argv
[i
++] = handler
->proto
.name
;
108 argv
[i
++] = proto
->iface
->name
;
110 if (proto
->iface
->main_dev
.dev
)
111 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
114 ret
= run_script(argv
, proc
);
121 proto_shell_setup_timeout_cb(struct uloop_timeout
*timeout
)
123 struct proto_shell_state
*state
;
125 state
= container_of(timeout
, struct proto_shell_state
, setup_timeout
);
126 kill(state
->setup_task
.pid
, SIGKILL
);
130 proto_shell_setup_cb(struct uloop_process
*p
, int ret
)
132 struct proto_shell_state
*state
;
134 state
= container_of(p
, struct proto_shell_state
, setup_task
);
135 uloop_timeout_cancel(&state
->setup_timeout
);
136 if (state
->teardown_pending
) {
137 state
->teardown_pending
= 0;
138 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
143 proto_shell_teardown_cb(struct uloop_process
*p
, int ret
)
145 struct proto_shell_state
*state
;
147 state
= container_of(p
, struct proto_shell_state
, teardown_task
);
148 proto_shell_set_down(state
);
149 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
153 proto_shell_free(struct interface_proto_state
*proto
)
155 struct proto_shell_state
*state
;
157 state
= container_of(proto
, struct proto_shell_state
, proto
);
168 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
169 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
170 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
174 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
176 struct proto_shell_state
*state
;
177 struct blob_attr
*tb
[__NOTIFY_LAST
];
180 state
= container_of(proto
, struct proto_shell_state
, proto
);
182 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
183 if (!tb
[NOTIFY_LINK_UP
])
184 return UBUS_STATUS_INVALID_ARGUMENT
;
186 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
188 if (!tb
[NOTIFY_IFNAME
])
189 return UBUS_STATUS_INVALID_ARGUMENT
;
191 device_add_user(&state
->l3_dev
,
192 device_get(blobmsg_data(tb
[NOTIFY_IFNAME
]), true));
193 device_claim(&state
->l3_dev
);
194 state
->proto
.iface
->l3_dev
= &state
->l3_dev
;
195 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
197 proto_shell_set_down(state
);
203 struct interface_proto_state
*
204 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
205 struct blob_attr
*attr
)
207 struct proto_shell_state
*state
;
209 state
= calloc(1, sizeof(*state
));
210 state
->config
= malloc(blob_pad_len(attr
));
214 memcpy(state
->config
, attr
, blob_pad_len(attr
));
215 state
->proto
.free
= proto_shell_free
;
216 state
->proto
.notify
= proto_shell_notify
;
217 state
->proto
.cb
= proto_shell_handler
;
218 state
->setup_timeout
.cb
= proto_shell_setup_timeout_cb
;
219 state
->setup_task
.cb
= proto_shell_setup_cb
;
220 state
->teardown_task
.cb
= proto_shell_teardown_cb
;
221 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
223 return &state
->proto
;
231 check_type(json_object
*obj
, json_type type
)
236 if (json_object_get_type(obj
) != type
)
242 static inline json_object
*
243 get_field(json_object
*obj
, const char *name
, json_type type
)
245 return check_type(json_object_object_get(obj
, name
), type
);
249 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
251 struct blobmsg_policy
*attrs
;
252 char *str_buf
, *str_cur
;
256 attrs
= calloc(1, sizeof(*attrs
));
260 config
->n_params
= json_object_array_length(obj
);
261 config
->params
= attrs
;
262 for (i
= 0; i
< config
->n_params
; i
++) {
263 json_object
*cur
, *name
, *type
;
265 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
269 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
273 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
277 attrs
[i
].name
= json_object_get_string(name
);
278 attrs
[i
].type
= json_object_get_int(type
);
279 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
282 str_len
+= strlen(attrs
[i
].name
+ 1);
285 str_buf
= malloc(str_len
);
290 for (i
= 0; i
< config
->n_params
; i
++) {
291 const char *name
= attrs
[i
].name
;
293 attrs
[i
].name
= str_cur
;
294 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
301 config
->n_params
= 0;
306 proto_shell_add_handler(const char *script
, json_object
*obj
)
308 struct proto_shell_handler
*handler
;
309 struct proto_handler
*proto
;
310 json_object
*config
, *tmp
;
314 if (!check_type(obj
, json_type_object
))
317 tmp
= get_field(obj
, "name", json_type_string
);
321 name
= json_object_get_string(tmp
);
323 handler
= calloc(1, sizeof(*handler
) +
329 strcpy(handler
->script_name
, script
);
331 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
334 proto
= &handler
->proto
;
336 proto
->config_params
= &handler
->config
;
337 proto
->attach
= proto_shell_attach
;
339 tmp
= get_field(obj
, "no-device", json_type_boolean
);
340 if (tmp
&& json_object_get_boolean(tmp
))
341 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
343 config
= get_field(obj
, "config", json_type_array
);
345 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
347 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
348 add_proto_handler(proto
);
351 static void proto_shell_add_script(const char *name
)
353 struct json_tokener
*tok
= NULL
;
355 static char buf
[512];
356 char *start
, *end
, *cmd
;
360 #define DUMP_SUFFIX " '' dump"
362 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
363 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
370 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
377 end
= memchr(start
, '\n', len
);
382 tok
= json_tokener_new();
384 obj
= json_tokener_parse_ex(tok
, start
, len
);
385 if (!is_error(obj
)) {
386 proto_shell_add_handler(name
, obj
);
387 json_object_put(obj
);
388 json_tokener_free(tok
);
394 len
= buflen
- (start
- buf
);
397 } while (!feof(f
) && !ferror(f
));
400 json_tokener_free(tok
);
405 void __init
proto_shell_init(void)
411 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
415 if (chdir(main_path
)) {
416 perror("chdir(main path)");
420 if (chdir("./proto"))
423 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
427 glob("./*.sh", 0, NULL
, &g
);
428 for (i
= 0; i
< g
.gl_pathc
; i
++)
429 proto_shell_add_script(g
.gl_pathv
[i
]);