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