8 #include <libubox/blobmsg_json.h>
11 #include "interface.h"
12 #include "interface-ip.h"
15 static LIST_HEAD(handlers
);
18 struct proto_shell_handler
{
19 struct list_head list
;
20 struct proto_handler proto
;
21 struct config_param_list config
;
26 struct proto_shell_state
{
27 struct interface_proto_state proto
;
28 struct proto_shell_handler
*handler
;
29 struct blob_attr
*config
;
32 static int run_script(const char **argv
)
36 if ((pid
= fork()) < 0)
41 execvp(argv
[0], (char **) argv
);
45 if (waitpid(pid
, &ret
, 0) == -1)
55 proto_shell_handler(struct interface_proto_state
*proto
,
56 enum interface_proto_cmd cmd
, bool force
)
58 struct proto_shell_state
*state
;
59 struct proto_shell_handler
*handler
;
64 state
= container_of(proto
, struct proto_shell_state
, proto
);
65 handler
= state
->handler
;
67 config
= blobmsg_format_json(state
->config
, true);
71 argv
[0] = handler
->script_name
;
72 argv
[1] = handler
->proto
.name
;
76 if (proto
->iface
->main_dev
.dev
) {
77 argv
[4] = proto
->iface
->main_dev
.dev
->ifname
;
85 case PROTO_CMD_TEARDOWN
:
86 ret
= run_script(argv
);
96 proto_shell_free(struct interface_proto_state
*proto
)
98 struct proto_shell_state
*state
;
100 state
= container_of(proto
, struct proto_shell_state
, proto
);
105 struct interface_proto_state
*
106 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
107 struct blob_attr
*attr
)
109 struct proto_shell_state
*state
;
111 state
= calloc(1, sizeof(*state
));
112 state
->config
= malloc(blob_pad_len(attr
));
116 memcpy(state
->config
, attr
, blob_pad_len(attr
));
117 state
->proto
.free
= proto_shell_free
;
118 state
->proto
.cb
= proto_shell_handler
;
119 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
121 return &state
->proto
;
129 check_type(json_object
*obj
, json_type type
)
134 if (json_object_get_type(obj
) != type
)
140 static inline json_object
*
141 get_field(json_object
*obj
, const char *name
, json_type type
)
143 return check_type(json_object_object_get(obj
, name
), type
);
147 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
149 struct blobmsg_policy
*attrs
;
150 char *str_buf
, *str_cur
;
154 attrs
= calloc(1, sizeof(*attrs
));
158 config
->n_params
= json_object_array_length(obj
);
159 config
->params
= attrs
;
160 for (i
= 0; i
< config
->n_params
; i
++) {
161 json_object
*cur
, *name
, *type
;
163 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
167 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
171 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
175 attrs
[i
].name
= json_object_get_string(name
);
176 attrs
[i
].type
= json_object_get_int(type
);
177 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
180 str_len
+= strlen(attrs
[i
].name
+ 1);
183 str_buf
= malloc(str_len
);
188 for (i
= 0; i
< config
->n_params
; i
++) {
189 const char *name
= attrs
[i
].name
;
191 attrs
[i
].name
= str_cur
;
192 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
199 config
->n_params
= 0;
204 proto_shell_add_handler(const char *script
, json_object
*obj
)
206 struct proto_shell_handler
*handler
;
207 struct proto_handler
*proto
;
208 json_object
*config
, *tmp
;
212 if (!check_type(obj
, json_type_object
))
215 tmp
= get_field(obj
, "name", json_type_string
);
219 name
= json_object_get_string(tmp
);
221 handler
= calloc(1, sizeof(*handler
) +
227 strcpy(handler
->script_name
, script
);
229 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
232 proto
= &handler
->proto
;
234 proto
->config_params
= &handler
->config
;
235 proto
->attach
= proto_shell_attach
;
237 tmp
= get_field(obj
, "no-device", json_type_boolean
);
238 if (tmp
&& json_object_get_boolean(tmp
))
239 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
241 config
= get_field(obj
, "config", json_type_array
);
243 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
245 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
246 add_proto_handler(proto
);
249 static void proto_shell_add_script(const char *name
)
251 struct json_tokener
*tok
= NULL
;
253 static char buf
[512];
254 char *start
, *end
, *cmd
;
258 #define DUMP_SUFFIX " '' dump"
260 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
261 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
268 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
275 end
= memchr(start
, '\n', len
);
280 tok
= json_tokener_new();
282 obj
= json_tokener_parse_ex(tok
, start
, len
);
283 if (!is_error(obj
)) {
284 proto_shell_add_handler(name
, obj
);
285 json_object_put(obj
);
286 json_tokener_free(tok
);
292 len
= buflen
- (start
- buf
);
295 } while (!feof(f
) && !ferror(f
));
298 json_tokener_free(tok
);
303 void __init
proto_shell_init(void)
309 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
313 if (chdir(main_path
)) {
314 perror("chdir(main path)");
318 if (chdir("./proto"))
321 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
325 glob("./*.sh", 0, NULL
, &g
);
326 for (i
= 0; i
< g
.gl_pathc
; i
++)
327 proto_shell_add_script(g
.gl_pathv
[i
]);