manage interfaces via vlist
[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 struct uloop_timeout restart_timer = {
22 .cb = netifd_do_restart,
23 };
24
25 void netifd_restart(void)
26 {
27 interface_set_down(NULL);
28 uloop_timeout_set(&restart_timer, 1000);
29 }
30
31 static int usage(const char *progname)
32 {
33 fprintf(stderr, "Usage: %s [options]\n"
34 "Options:\n"
35 " -s <path>: Path to the ubus socket\n"
36 " -p <path>: Path to netifd addons (default: %s)\n"
37 "\n", progname, main_path);
38
39 return 1;
40 }
41
42 int main(int argc, char **argv)
43 {
44 const char *socket = NULL;
45 int ch;
46
47 global_argv = argv;
48
49 while ((ch = getopt(argc, argv, "s:")) != -1) {
50 switch(ch) {
51 case 's':
52 socket = optarg;
53 break;
54 case 'p':
55 main_path = optarg;
56 break;
57 default:
58 return usage(argv[0]);
59 }
60 }
61
62 if (netifd_ubus_init(socket) < 0) {
63 fprintf(stderr, "Failed to connect to ubus\n");
64 return 1;
65 }
66
67 if (system_init()) {
68 fprintf(stderr, "Failed to initialize system control\n");
69 return 1;
70 }
71
72 config_init_interfaces(NULL);
73
74 uloop_run();
75
76 netifd_ubus_done();
77
78 return 0;
79 }