add a ubus call for triggering config reloads
[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 const char *main_path = ".";
14 static char **global_argv;
15
16 static void netifd_do_restart(struct uloop_timeout *timeout)
17 {
18         execvp(global_argv[0], global_argv);
19 }
20
21 static void netifd_do_reload(struct uloop_timeout *timeout)
22 {
23         config_init_interfaces(NULL);
24 }
25
26 static struct uloop_timeout main_timer;
27
28 void netifd_reload(void)
29 {
30         main_timer.cb = netifd_do_reload;
31         uloop_timeout_set(&main_timer, 100);
32 }
33
34 void netifd_restart(void)
35 {
36         main_timer.cb = netifd_do_restart;
37         interface_set_down(NULL);
38         uloop_timeout_set(&main_timer, 1000);
39 }
40
41 static int usage(const char *progname)
42 {
43         fprintf(stderr, "Usage: %s [options]\n"
44                 "Options:\n"
45                 " -s <path>:            Path to the ubus socket\n"
46                 " -p <path>:            Path to netifd addons (default: %s)\n"
47                 "\n", progname, main_path);
48
49         return 1;
50 }
51
52 int main(int argc, char **argv)
53 {
54         const char *socket = NULL;
55         int ch;
56
57         global_argv = argv;
58
59         while ((ch = getopt(argc, argv, "s:")) != -1) {
60                 switch(ch) {
61                 case 's':
62                         socket = optarg;
63                         break;
64                 case 'p':
65                         main_path = optarg;
66                         break;
67                 default:
68                         return usage(argv[0]);
69                 }
70         }
71
72         if (netifd_ubus_init(socket) < 0) {
73                 fprintf(stderr, "Failed to connect to ubus\n");
74                 return 1;
75         }
76
77         if (system_init()) {
78                 fprintf(stderr, "Failed to initialize system control\n");
79                 return 1;
80         }
81
82         config_init_interfaces(NULL);
83
84         uloop_run();
85
86         netifd_ubus_done();
87
88         return 0;
89 }