#include <glob.h>
#include <fcntl.h>
#include <stdio.h>
-#include <unistd.h>
#include "netifd.h"
#include "system.h"
int prev_fd = open(".", O_RDONLY | O_DIRECTORY);
system_fd_set_cloexec(prev_fd);
if (fd >= 0)
- fchdir(fd);
+ if (fchdir(fd)) {}
return prev_fd;
}
static void
netifd_dir_pop(int prev_fd)
{
- fchdir(prev_fd);
+ if (fchdir(prev_fd)) {}
close(prev_fd);
}
}
static void
-netifd_init_script_handler(const char *name, script_dump_cb cb)
+netifd_init_script_handler(const char *script, json_object *obj, script_dump_cb cb)
+{
+ json_object *tmp;
+ const char *name;
+
+ if (!json_check_type(obj, json_type_object))
+ return;
+
+ tmp = json_get_field(obj, "name", json_type_string);
+ if (!tmp)
+ return;
+
+ name = json_object_get_string(tmp);
+ cb(script, name, obj);
+}
+
+static void
+netifd_parse_script_handler(const char *name, script_dump_cb cb)
{
struct json_tokener *tok = NULL;
json_object *obj;
obj = json_tokener_parse_ex(tok, start, len);
if (!is_error(obj)) {
- cb(name, obj);
+ netifd_init_script_handler(name, obj, cb);
json_object_put(obj);
json_tokener_free(tok);
tok = NULL;
int i, prev_fd;
prev_fd = netifd_dir_push(dir_fd);
- glob("./*.sh", 0, NULL, &g);
+ if (glob("./*.sh", 0, NULL, &g))
+ return;
+
for (i = 0; i < g.gl_pathc; i++)
- netifd_init_script_handler(g.gl_pathv[i], cb);
+ netifd_parse_script_handler(g.gl_pathv[i], cb);
netifd_dir_pop(prev_fd);
+
+ globfree(&g);
}
char *
{
struct blobmsg_policy *attrs;
char *str_buf, *str_cur;
+ char const **validate;
int str_len = 0;
int i;
if (!attrs)
return NULL;
+ validate = calloc(1, sizeof(char*) * config->n_params);
+ if (!validate)
+ goto error;
+
config->params = attrs;
+ config->validate = validate;
for (i = 0; i < config->n_params; i++) {
json_object *cur, *name, *type;
str_cur = str_buf;
for (i = 0; i < config->n_params; i++) {
const char *name = attrs[i].name;
+ char *delim;
attrs[i].name = str_cur;
str_cur += sprintf(str_cur, "%s", name) + 1;
+ delim = strchr(attrs[i].name, ':');
+ if (delim) {
+ *delim = '\0';
+ validate[i] = ++delim;
+ } else {
+ validate[i] = NULL;
+ }
}
return str_buf;
error:
free(attrs);
+ if (validate)
+ free(validate);
config->n_params = 0;
return NULL;
}