]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* | |
3 | */ | |
4 | ||
5 | #include <zebra.h> | |
6 | ||
7 | #include <lib/version.h> | |
8 | #include "getopt.h" | |
9 | #include "thread.h" | |
10 | #include "vty.h" | |
11 | #include "command.h" | |
12 | #include "memory.h" | |
13 | #include "lib_vty.h" | |
14 | ||
15 | extern void test_init(void); | |
16 | ||
17 | struct thread_master *master; | |
18 | ||
19 | struct option longopts[] = {{"daemon", no_argument, NULL, 'd'}, | |
20 | {"config_file", required_argument, NULL, 'f'}, | |
21 | {"help", no_argument, NULL, 'h'}, | |
22 | {"vty_addr", required_argument, NULL, 'A'}, | |
23 | {"vty_port", required_argument, NULL, 'P'}, | |
24 | {"version", no_argument, NULL, 'v'}, | |
25 | {0}}; | |
26 | ||
27 | DEFUN (daemon_exit, | |
28 | daemon_exit_cmd, | |
29 | "daemon-exit", | |
30 | "Make the daemon exit\n") | |
31 | { | |
32 | exit(0); | |
33 | } | |
34 | ||
35 | static int timer_count; | |
36 | static void test_timer(struct thread *thread) | |
37 | { | |
38 | int *count = THREAD_ARG(thread); | |
39 | ||
40 | printf("run %d of timer\n", (*count)++); | |
41 | thread_add_timer(master, test_timer, count, 5, NULL); | |
42 | } | |
43 | ||
44 | static void test_timer_init(void) | |
45 | { | |
46 | thread_add_timer(master, test_timer, &timer_count, 10, NULL); | |
47 | } | |
48 | ||
49 | static void test_vty_init(void) | |
50 | { | |
51 | install_element(VIEW_NODE, &daemon_exit_cmd); | |
52 | } | |
53 | ||
54 | /* Help information display. */ | |
55 | static void usage(char *progname, int status) | |
56 | { | |
57 | if (status != 0) | |
58 | fprintf(stderr, "Try `%s --help' for more information.\n", | |
59 | progname); | |
60 | else { | |
61 | printf("Usage : %s [OPTION...]\n\ | |
62 | Daemon which does 'slow' things.\n\n\ | |
63 | -d, --daemon Runs in daemon mode\n\ | |
64 | -f, --config_file Set configuration file name\n\ | |
65 | -A, --vty_addr Set vty's bind address\n\ | |
66 | -P, --vty_port Set vty's port number\n\ | |
67 | -v, --version Print program version\n\ | |
68 | -h, --help Display this help and exit\n\ | |
69 | \n\ | |
70 | Report bugs to %s\n", | |
71 | progname, FRR_BUG_ADDRESS); | |
72 | } | |
73 | exit(status); | |
74 | } | |
75 | ||
76 | ||
77 | /* main routine. */ | |
78 | int main(int argc, char **argv) | |
79 | { | |
80 | char *p; | |
81 | char *vty_addr = NULL; | |
82 | int vty_port = 4000; | |
83 | int daemon_mode = 0; | |
84 | char *progname; | |
85 | struct thread thread; | |
86 | char *config_file = NULL; | |
87 | ||
88 | /* Set umask before anything for security */ | |
89 | umask(0027); | |
90 | ||
91 | /* get program name */ | |
92 | progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]); | |
93 | ||
94 | /* master init. */ | |
95 | master = thread_master_create(NULL); | |
96 | ||
97 | while (1) { | |
98 | int opt; | |
99 | ||
100 | opt = getopt_long(argc, argv, "dhf:A:P:v", longopts, 0); | |
101 | ||
102 | if (opt == EOF) | |
103 | break; | |
104 | ||
105 | switch (opt) { | |
106 | case 0: | |
107 | break; | |
108 | case 'f': | |
109 | config_file = optarg; | |
110 | break; | |
111 | case 'd': | |
112 | daemon_mode = 1; | |
113 | break; | |
114 | case 'A': | |
115 | vty_addr = optarg; | |
116 | break; | |
117 | case 'P': | |
118 | /* Deal with atoi() returning 0 on failure */ | |
119 | if (strcmp(optarg, "0") == 0) { | |
120 | vty_port = 0; | |
121 | break; | |
122 | } | |
123 | vty_port = atoi(optarg); | |
124 | vty_port = (vty_port ? vty_port : 4000); | |
125 | break; | |
126 | case 'v': | |
127 | print_version(progname); | |
128 | exit(0); | |
129 | break; | |
130 | case 'h': | |
131 | usage(progname, 0); | |
132 | break; | |
133 | default: | |
134 | usage(progname, 1); | |
135 | break; | |
136 | } | |
137 | } | |
138 | ||
139 | /* Library inits. */ | |
140 | cmd_init(1); | |
141 | vty_init(master, false); | |
142 | lib_cmd_init(); | |
143 | nb_init(master, NULL, 0, false); | |
144 | ||
145 | /* OSPF vty inits. */ | |
146 | test_vty_init(); | |
147 | ||
148 | /* Change to the daemon program. */ | |
149 | if (daemon_mode && daemon(0, 0) < 0) { | |
150 | fprintf(stderr, "daemon failed: %s", strerror(errno)); | |
151 | exit(1); | |
152 | } | |
153 | ||
154 | /* Create VTY socket */ | |
155 | vty_serv_sock(vty_addr, vty_port, "/tmp/.heavy.sock"); | |
156 | ||
157 | /* Configuration file read*/ | |
158 | if (!config_file) | |
159 | usage(progname, 1); | |
160 | vty_read_config(NULL, config_file, NULL); | |
161 | ||
162 | test_timer_init(); | |
163 | ||
164 | test_init(); | |
165 | ||
166 | /* Fetch next active thread. */ | |
167 | while (thread_fetch(master, &thread)) | |
168 | thread_call(&thread); | |
169 | ||
170 | /* Not reached. */ | |
171 | exit(0); | |
172 | } |