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