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