11 #include "interface.h"
13 unsigned int debug_mask = 0;
14 const char *main_path = DEFAULT_MAIN_PATH;
15 const char *resolv_conf = DEFAULT_RESOLV_CONF;
16 static char **global_argv;
18 static struct list_head process_list = LIST_HEAD_INIT(process_list);
19 static struct list_head fds = LIST_HEAD_INIT(fds);
22 netifd_process_cb(struct uloop_process *proc, int ret)
24 struct netifd_process *np;
25 np = container_of(proc, struct netifd_process, uloop);
27 return np->cb(np, ret);
31 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
36 netifd_kill_process(proc);
38 if ((pid = fork()) < 0)
48 if (proc->dir_fd >= 0)
51 /* close all non-essential fds */
52 list_for_each_entry(fd, &fds, list) {
58 execvp(argv[0], (char **) argv);
65 proc->uloop.cb = netifd_process_cb;
66 proc->uloop.pid = pid;
67 uloop_process_add(&proc->uloop);
68 list_add_tail(&proc->list, &process_list);
74 netifd_kill_process(struct netifd_process *proc)
76 if (!proc->uloop.pending)
79 kill(proc->uloop.pid, SIGTERM);
80 uloop_process_delete(&proc->uloop);
81 list_del(&proc->list);
85 netifd_fd_add(struct netifd_fd *fd)
87 list_add_tail(&fd->list, &fds);
91 netifd_fd_delete(struct netifd_fd *fd)
96 static void netifd_do_restart(struct uloop_timeout *timeout)
98 execvp(global_argv[0], global_argv);
101 static void netifd_do_reload(struct uloop_timeout *timeout)
103 config_init_interfaces(NULL);
106 static struct uloop_timeout main_timer;
108 void netifd_reload(void)
110 main_timer.cb = netifd_do_reload;
111 uloop_timeout_set(&main_timer, 100);
114 void netifd_restart(void)
116 main_timer.cb = netifd_do_restart;
117 interface_set_down(NULL);
118 uloop_timeout_set(&main_timer, 1000);
121 static int usage(const char *progname)
123 fprintf(stderr, "Usage: %s [options]\n"
125 " -d <mask>: Mask for debug messages\n"
126 " -s <path>: Path to the ubus socket\n"
127 " -p <path>: Path to netifd addons (default: %s)\n"
128 " -h <path>: Path to the hotplug script\n"
129 " -r <path>: Path to resolv.conf\n"
130 " (default: "DEFAULT_HOTPLUG_PATH")\n"
131 "\n", progname, main_path);
137 netifd_handle_signal(int signo)
143 netifd_setup_signals(void)
147 memset(&s, 0, sizeof(s));
148 s.sa_handler = netifd_handle_signal;
150 sigaction(SIGINT, &s, NULL);
151 sigaction(SIGTERM, &s, NULL);
152 sigaction(SIGUSR1, &s, NULL);
153 sigaction(SIGUSR2, &s, NULL);
157 netifd_kill_processes(void)
159 struct netifd_process *proc, *tmp;
161 list_for_each_entry_safe(proc, tmp, &process_list, list)
162 netifd_kill_process(proc);
165 int main(int argc, char **argv)
167 const char *socket = NULL;
172 while ((ch = getopt(argc, argv, "d:s:p:h:r:")) != -1) {
175 debug_mask = strtoul(optarg, NULL, 0);
184 hotplug_cmd_path = optarg;
187 resolv_conf = optarg;
190 return usage(argv[0]);
194 netifd_setup_signals();
195 if (netifd_ubus_init(socket) < 0) {
196 fprintf(stderr, "Failed to connect to ubus\n");
201 fprintf(stderr, "Failed to initialize system control\n");
205 config_init_interfaces(NULL);
208 netifd_kill_processes();