when killed, kill pending child processes
[project/netifd.git] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <getopt.h>
5 #include <unistd.h>
6
7 #include "netifd.h"
8 #include "ubus.h"
9 #include "config.h"
10 #include "system.h"
11 #include "interface.h"
12
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);
18
19 static void
20 netifd_process_cb(struct uloop_process *proc, int ret)
21 {
22         struct netifd_process *np;
23         np = container_of(proc, struct netifd_process, uloop);
24         list_del(&np->list);
25         return np->cb(np, ret);
26 }
27
28 int
29 netifd_start_process(const char **argv, char **env, int dir_fd, struct netifd_process *proc)
30 {
31         int pid;
32
33         netifd_kill_process(proc);
34
35         if ((pid = fork()) < 0)
36                 return -1;
37
38         if (!pid) {
39                 if (env) {
40                         while (*env) {
41                                 putenv(*env);
42                                 env++;
43                         }
44                 }
45                 if (dir_fd >= 0)
46                         fchdir(dir_fd);
47                 execvp(argv[0], (char **) argv);
48                 exit(127);
49         }
50
51         if (pid < 0)
52                 return -1;
53
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);
58
59         return 0;
60 }
61
62 void
63 netifd_kill_process(struct netifd_process *proc)
64 {
65         if (!proc->uloop.pending)
66                 return;
67
68         kill(proc->uloop.pid, SIGTERM);
69         uloop_process_delete(&proc->uloop);
70         list_del(&proc->list);
71 }
72
73 static void netifd_do_restart(struct uloop_timeout *timeout)
74 {
75         execvp(global_argv[0], global_argv);
76 }
77
78 static void netifd_do_reload(struct uloop_timeout *timeout)
79 {
80         config_init_interfaces(NULL);
81 }
82
83 static struct uloop_timeout main_timer;
84
85 void netifd_reload(void)
86 {
87         main_timer.cb = netifd_do_reload;
88         uloop_timeout_set(&main_timer, 100);
89 }
90
91 void netifd_restart(void)
92 {
93         main_timer.cb = netifd_do_restart;
94         interface_set_down(NULL);
95         uloop_timeout_set(&main_timer, 1000);
96 }
97
98 static int usage(const char *progname)
99 {
100         fprintf(stderr, "Usage: %s [options]\n"
101                 "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);
109
110         return 1;
111 }
112
113 static void
114 netifd_handle_signal(int signo)
115 {
116         uloop_end();
117 }
118
119 static void
120 netifd_setup_signals(void)
121 {
122         struct sigaction s;
123
124         memset(&s, 0, sizeof(s));
125         s.sa_handler = netifd_handle_signal;
126         s.sa_flags = 0;
127         sigaction(SIGINT, &s, NULL);
128         sigaction(SIGTERM, &s, NULL);
129         sigaction(SIGUSR1, &s, NULL);
130         sigaction(SIGUSR2, &s, NULL);
131 }
132
133 static void
134 netifd_kill_processes(void)
135 {
136         struct netifd_process *proc, *tmp;
137
138         list_for_each_entry_safe(proc, tmp, &process_list, list)
139                 netifd_kill_process(proc);
140 }
141
142 int main(int argc, char **argv)
143 {
144         const char *socket = NULL;
145         int ch;
146
147         global_argv = argv;
148
149         while ((ch = getopt(argc, argv, "d:s:p:h:r:")) != -1) {
150                 switch(ch) {
151                 case 'd':
152                         debug_mask = strtoul(optarg, NULL, 0);
153                         break;
154                 case 's':
155                         socket = optarg;
156                         break;
157                 case 'p':
158                         main_path = optarg;
159                         break;
160                 case 'h':
161                         hotplug_cmd_path = optarg;
162                         break;
163                 case 'r':
164                         resolv_conf = optarg;
165                         break;
166                 default:
167                         return usage(argv[0]);
168                 }
169         }
170
171         netifd_setup_signals();
172         if (netifd_ubus_init(socket) < 0) {
173                 fprintf(stderr, "Failed to connect to ubus\n");
174                 return 1;
175         }
176
177         if (system_init()) {
178                 fprintf(stderr, "Failed to initialize system control\n");
179                 return 1;
180         }
181
182         config_init_interfaces(NULL);
183
184         uloop_run();
185         netifd_kill_processes();
186
187         netifd_ubus_done();
188
189         return 0;
190 }