2 * netifd - network interface daemon
3 * Copyright (C) 2012-2013 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
26 netifd_dir_push(int fd)
28 int prev_fd = open(".", O_RDONLY | O_DIRECTORY);
29 system_fd_set_cloexec(prev_fd);
36 netifd_dir_pop(int prev_fd)
42 int netifd_open_subdir(const char *name)
47 prev_dir = netifd_dir_push(-1);
48 if (chdir(main_path)) {
49 perror("chdir(main path)");
53 ret = open(name, O_RDONLY | O_DIRECTORY);
55 system_fd_set_cloexec(ret);
58 netifd_dir_pop(prev_dir);
63 netifd_init_script_handler(const char *name, script_dump_cb cb)
65 struct json_tokener *tok = NULL;
72 #define DUMP_SUFFIX " '' dump"
74 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
75 sprintf(cmd, "%s" DUMP_SUFFIX, name);
82 start = fgets(buf, sizeof(buf), f);
89 tok = json_tokener_new();
91 obj = json_tokener_parse_ex(tok, start, len);
95 json_tokener_free(tok);
97 } else if (start[len - 1] == '\n') {
98 json_tokener_free(tok);
101 } while (!feof(f) && !ferror(f));
104 json_tokener_free(tok);
109 void netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
114 prev_fd = netifd_dir_push(dir_fd);
115 glob("./*.sh", 0, NULL, &g);
116 for (i = 0; i < g.gl_pathc; i++)
117 netifd_init_script_handler(g.gl_pathv[i], cb);
118 netifd_dir_pop(prev_fd);