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;
17 static struct list_head process_list = LIST_HEAD_INIT(process_list);
20 netifd_process_cb(struct uloop_process *proc, int ret)
22 struct netifd_process *np;
23 np = container_of(proc, struct netifd_process, uloop);
25 return np->cb(np, ret);
29 netifd_start_process(const char **argv, char **env, int dir_fd, struct netifd_process *proc)
33 netifd_kill_process(proc);
35 if ((pid = fork()) < 0)
47 execvp(argv[0], (char **) argv);
54 proc->uloop.cb = netifd_process_cb;
55 proc->uloop.pid = pid;
56 uloop_process_add(&proc->uloop);
57 list_add_tail(&proc->list, &process_list);
63 netifd_kill_process(struct netifd_process *proc)
65 if (!proc->uloop.pending)
68 kill(proc->uloop.pid, SIGTERM);
69 uloop_process_delete(&proc->uloop);
70 list_del(&proc->list);
73 static void netifd_do_restart(struct uloop_timeout *timeout)
75 execvp(global_argv[0], global_argv);
78 static void netifd_do_reload(struct uloop_timeout *timeout)
80 config_init_interfaces(NULL);
83 static struct uloop_timeout main_timer;
85 void netifd_reload(void)
87 main_timer.cb = netifd_do_reload;
88 uloop_timeout_set(&main_timer, 100);
91 void netifd_restart(void)
93 main_timer.cb = netifd_do_restart;
94 interface_set_down(NULL);
95 uloop_timeout_set(&main_timer, 1000);
98 static int usage(const char *progname)
100 fprintf(stderr, "Usage: %s [options]\n"
102 " -d <mask>: Mask for debug messages\n"
103 " -s <path>: Path to the ubus socket\n"
104 " -p <path>: Path to netifd addons (default: %s)\n"
105 " -h <path>: Path to the hotplug script\n"
106 " -r <path>: Path to resolv.conf\n"
107 " (default: "DEFAULT_HOTPLUG_PATH")\n"
108 "\n", progname, main_path);
114 netifd_handle_signal(int signo)
120 netifd_setup_signals(void)
124 memset(&s, 0, sizeof(s));
125 s.sa_handler = netifd_handle_signal;
127 sigaction(SIGINT, &s, NULL);
128 sigaction(SIGTERM, &s, NULL);
129 sigaction(SIGUSR1, &s, NULL);
130 sigaction(SIGUSR2, &s, NULL);
134 netifd_kill_processes(void)
136 struct netifd_process *proc, *tmp;
138 list_for_each_entry_safe(proc, tmp, &process_list, list)
139 netifd_kill_process(proc);
142 int main(int argc, char **argv)
144 const char *socket = NULL;
149 while ((ch = getopt(argc, argv, "d:s:p:h:r:")) != -1) {
152 debug_mask = strtoul(optarg, NULL, 0);
161 hotplug_cmd_path = optarg;
164 resolv_conf = optarg;
167 return usage(argv[0]);
171 netifd_setup_signals();
172 if (netifd_ubus_init(socket) < 0) {
173 fprintf(stderr, "Failed to connect to ubus\n");
178 fprintf(stderr, "Failed to initialize system control\n");
182 config_init_interfaces(NULL);
185 netifd_kill_processes();