rework debugging code, add debugging levels
[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 "\n", progname, main_path);
50
51 return 1;
52 }
53
54 int main(int argc, char **argv)
55 {
56 const char *socket = NULL;
57 int ch;
58
59 global_argv = argv;
60
61 while ((ch = getopt(argc, argv, "d:s:")) != -1) {
62 switch(ch) {
63 case 'd':
64 debug_mask = strtoul(optarg, NULL, 0);
65 break;
66 case 's':
67 socket = optarg;
68 break;
69 case 'p':
70 main_path = optarg;
71 break;
72 default:
73 return usage(argv[0]);
74 }
75 }
76
77 if (netifd_ubus_init(socket) < 0) {
78 fprintf(stderr, "Failed to connect to ubus\n");
79 return 1;
80 }
81
82 if (system_init()) {
83 fprintf(stderr, "Failed to initialize system control\n");
84 return 1;
85 }
86
87 config_init_interfaces(NULL);
88
89 uloop_run();
90
91 netifd_ubus_done();
92
93 return 0;
94 }