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