move netifd_start_process dir_fd to the data structure
[project/netifd.git] / main.c
diff --git a/main.c b/main.c
index 352519d..04d5648 100644 (file)
--- a/main.c
+++ b/main.c
@@ -26,7 +26,7 @@ netifd_process_cb(struct uloop_process *proc, int ret)
 }
 
 int
-netifd_start_process(const char **argv, char **env, int dir_fd, struct netifd_process *proc)
+netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
 {
        int pid;
 
@@ -42,8 +42,8 @@ netifd_start_process(const char **argv, char **env, int dir_fd, struct netifd_pr
                                env++;
                        }
                }
-               if (dir_fd >= 0)
-                       fchdir(dir_fd);
+               if (proc->dir_fd >= 0)
+                       fchdir(proc->dir_fd);
                execvp(argv[0], (char **) argv);
                exit(127);
        }
@@ -110,6 +110,35 @@ static int usage(const char *progname)
        return 1;
 }
 
+static void
+netifd_handle_signal(int signo)
+{
+       uloop_end();
+}
+
+static void
+netifd_setup_signals(void)
+{
+       struct sigaction s;
+
+       memset(&s, 0, sizeof(s));
+       s.sa_handler = netifd_handle_signal;
+       s.sa_flags = 0;
+       sigaction(SIGINT, &s, NULL);
+       sigaction(SIGTERM, &s, NULL);
+       sigaction(SIGUSR1, &s, NULL);
+       sigaction(SIGUSR2, &s, NULL);
+}
+
+static void
+netifd_kill_processes(void)
+{
+       struct netifd_process *proc, *tmp;
+
+       list_for_each_entry_safe(proc, tmp, &process_list, list)
+               netifd_kill_process(proc);
+}
+
 int main(int argc, char **argv)
 {
        const char *socket = NULL;
@@ -139,6 +168,7 @@ int main(int argc, char **argv)
                }
        }
 
+       netifd_setup_signals();
        if (netifd_ubus_init(socket) < 0) {
                fprintf(stderr, "Failed to connect to ubus\n");
                return 1;
@@ -152,6 +182,7 @@ int main(int argc, char **argv)
        config_init_interfaces(NULL);
 
        uloop_run();
+       netifd_kill_processes();
 
        netifd_ubus_done();