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_handler(struct interface_proto_state
*proto
,
65 enum interface_proto_cmd cmd
, bool force
)
67 struct proto_shell_state
*state
;
68 struct proto_shell_handler
*handler
;
69 struct uloop_process
*proc
;
75 state
= container_of(proto
, struct proto_shell_state
, proto
);
76 handler
= state
->handler
;
78 if (cmd
== PROTO_CMD_SETUP
) {
80 proc
= &state
->setup_task
;
83 proc
= &state
->teardown_task
;
84 if (state
->setup_task
.pending
) {
85 uloop_timeout_set(&state
->setup_timeout
, 1000);
86 kill(state
->setup_task
.pid
, SIGINT
);
87 state
->teardown_pending
= true;
92 config
= blobmsg_format_json(state
->config
, true);
96 argv
[i
++] = handler
->script_name
;
97 argv
[i
++] = handler
->proto
.name
;
99 argv
[i
++] = proto
->iface
->name
;
101 if (proto
->iface
->main_dev
.dev
)
102 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
105 ret
= run_script(argv
, proc
);
112 proto_shell_setup_timeout_cb(struct uloop_timeout
*timeout
)
114 struct proto_shell_state
*state
;
116 state
= container_of(timeout
, struct proto_shell_state
, setup_timeout
);
117 kill(state
->setup_task
.pid
, SIGKILL
);
121 proto_shell_setup_cb(struct uloop_process
*p
, int ret
)
123 struct proto_shell_state
*state
;
125 state
= container_of(p
, struct proto_shell_state
, setup_task
);
126 uloop_timeout_cancel(&state
->setup_timeout
);
127 if (state
->teardown_pending
) {
128 state
->teardown_pending
= false;
129 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
134 proto_shell_teardown_cb(struct uloop_process
*p
, int ret
)
136 struct proto_shell_state
*state
;
138 state
= container_of(p
, struct proto_shell_state
, teardown_task
);
139 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
140 if (state
->l3_dev
.dev
)
141 device_remove_user(&state
->l3_dev
);
145 proto_shell_free(struct interface_proto_state
*proto
)
147 struct proto_shell_state
*state
;
149 state
= container_of(proto
, struct proto_shell_state
, proto
);
160 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
161 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
162 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
166 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
168 struct proto_shell_state
*state
;
169 struct blob_attr
*tb
[__NOTIFY_LAST
];
172 state
= container_of(proto
, struct proto_shell_state
, proto
);
174 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
175 if (!tb
[NOTIFY_LINK_UP
])
176 return UBUS_STATUS_INVALID_ARGUMENT
;
178 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
180 if (!tb
[NOTIFY_IFNAME
])
181 return UBUS_STATUS_INVALID_ARGUMENT
;
183 if (!state
->l3_dev
.dev
) {
184 device_add_user(&state
->l3_dev
,
185 device_get(blobmsg_data(tb
[NOTIFY_IFNAME
]), true));
186 device_claim(&state
->l3_dev
);
187 state
->proto
.iface
->l3_dev
= &state
->l3_dev
;
189 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
191 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
197 struct interface_proto_state
*
198 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
199 struct blob_attr
*attr
)
201 struct proto_shell_state
*state
;
203 state
= calloc(1, sizeof(*state
));
204 state
->config
= malloc(blob_pad_len(attr
));
208 memcpy(state
->config
, attr
, blob_pad_len(attr
));
209 state
->proto
.free
= proto_shell_free
;
210 state
->proto
.notify
= proto_shell_notify
;
211 state
->proto
.cb
= proto_shell_handler
;
212 state
->setup_timeout
.cb
= proto_shell_setup_timeout_cb
;
213 state
->setup_task
.cb
= proto_shell_setup_cb
;
214 state
->teardown_task
.cb
= proto_shell_teardown_cb
;
215 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
217 return &state
->proto
;
225 check_type(json_object
*obj
, json_type type
)
230 if (json_object_get_type(obj
) != type
)
236 static inline json_object
*
237 get_field(json_object
*obj
, const char *name
, json_type type
)
239 return check_type(json_object_object_get(obj
, name
), type
);
243 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
245 struct blobmsg_policy
*attrs
;
246 char *str_buf
, *str_cur
;
250 attrs
= calloc(1, sizeof(*attrs
));
254 config
->n_params
= json_object_array_length(obj
);
255 config
->params
= attrs
;
256 for (i
= 0; i
< config
->n_params
; i
++) {
257 json_object
*cur
, *name
, *type
;
259 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
263 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
267 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
271 attrs
[i
].name
= json_object_get_string(name
);
272 attrs
[i
].type
= json_object_get_int(type
);
273 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
276 str_len
+= strlen(attrs
[i
].name
+ 1);
279 str_buf
= malloc(str_len
);
284 for (i
= 0; i
< config
->n_params
; i
++) {
285 const char *name
= attrs
[i
].name
;
287 attrs
[i
].name
= str_cur
;
288 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
295 config
->n_params
= 0;
300 proto_shell_add_handler(const char *script
, json_object
*obj
)
302 struct proto_shell_handler
*handler
;
303 struct proto_handler
*proto
;
304 json_object
*config
, *tmp
;
308 if (!check_type(obj
, json_type_object
))
311 tmp
= get_field(obj
, "name", json_type_string
);
315 name
= json_object_get_string(tmp
);
317 handler
= calloc(1, sizeof(*handler
) +
323 strcpy(handler
->script_name
, script
);
325 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
328 proto
= &handler
->proto
;
330 proto
->config_params
= &handler
->config
;
331 proto
->attach
= proto_shell_attach
;
333 tmp
= get_field(obj
, "no-device", json_type_boolean
);
334 if (tmp
&& json_object_get_boolean(tmp
))
335 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
337 config
= get_field(obj
, "config", json_type_array
);
339 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
341 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
342 add_proto_handler(proto
);
345 static void proto_shell_add_script(const char *name
)
347 struct json_tokener
*tok
= NULL
;
349 static char buf
[512];
350 char *start
, *end
, *cmd
;
354 #define DUMP_SUFFIX " '' dump"
356 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
357 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
364 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
371 end
= memchr(start
, '\n', len
);
376 tok
= json_tokener_new();
378 obj
= json_tokener_parse_ex(tok
, start
, len
);
379 if (!is_error(obj
)) {
380 proto_shell_add_handler(name
, obj
);
381 json_object_put(obj
);
382 json_tokener_free(tok
);
388 len
= buflen
- (start
- buf
);
391 } while (!feof(f
) && !ferror(f
));
394 json_tokener_free(tok
);
399 void __init
proto_shell_init(void)
405 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
409 if (chdir(main_path
)) {
410 perror("chdir(main path)");
414 if (chdir("./proto"))
417 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
421 glob("./*.sh", 0, NULL
, &g
);
422 for (i
= 0; i
< g
.gl_pathc
; i
++)
423 proto_shell_add_script(g
.gl_pathv
[i
]);