352519de5d32cc4b2baa4029c983442b3975a3f2
[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 int main(int argc, char **argv)
114 {
115         const char *socket = NULL;
116         int ch;
117
118         global_argv = argv;
119
120         while ((ch = getopt(argc, argv, "d:s:p:h:r:")) != -1) {
121                 switch(ch) {
122                 case 'd':
123                         debug_mask = strtoul(optarg, NULL, 0);
124                         break;
125                 case 's':
126                         socket = optarg;
127                         break;
128                 case 'p':
129                         main_path = optarg;
130                         break;
131                 case 'h':
132                         hotplug_cmd_path = optarg;
133                         break;
134                 case 'r':
135                         resolv_conf = optarg;
136                         break;
137                 default:
138                         return usage(argv[0]);
139                 }
140         }
141
142         if (netifd_ubus_init(socket) < 0) {
143                 fprintf(stderr, "Failed to connect to ubus\n");
144                 return 1;
145         }
146
147         if (system_init()) {
148                 fprintf(stderr, "Failed to initialize system control\n");
149                 return 1;
150         }
151
152         config_init_interfaces(NULL);
153
154         uloop_run();
155
156         netifd_ubus_done();
157
158         return 0;
159 }