]> git.proxmox.com Git - mirror_frr.git/blob - zebra/test_main.c
build: rename (1 of ?): configure.ac + preproc
[mirror_frr.git] / zebra / test_main.c
1 /* main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
4 * GNU Zebra is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2, or (at your option) any
7 * later version.
8 *
9 * GNU Zebra is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with GNU Zebra; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 */
19
20 #include <zebra.h>
21
22 #include <lib/version.h>
23 #include "getopt.h"
24 #include "command.h"
25 #include "thread.h"
26 #include "filter.h"
27 #include "memory.h"
28 #include "zebra_memory.h"
29 #include "memory_vty.h"
30 #include "prefix.h"
31 #include "log.h"
32 #include "privs.h"
33 #include "sigevent.h"
34 #include "vrf.h"
35
36 #include "zebra/rib.h"
37 #include "zebra/zebra_ns.h"
38 #include "zebra/zserv.h"
39 #include "zebra/zebra_vrf.h"
40 #include "zebra/debug.h"
41 #include "zebra/router-id.h"
42 #include "zebra/interface.h"
43
44 /* Zebra instance */
45 struct zebra_t zebrad =
46 {
47 .rtm_table_default = 0,
48 };
49
50 /* process id. */
51 pid_t pid;
52
53 /* Allow non-quagga entities to delete quagga routes */
54 int allow_delete = 0;
55
56 /* zebra_rib's workqueue hold time. Private export for use by test code only */
57 extern int rib_process_hold_time;
58
59 /* Pacify zclient.o in libzebra, which expects this variable. */
60 struct thread_master *master;
61
62 /* Command line options. */
63 struct option longopts[] =
64 {
65 { "batch", no_argument, NULL, 'b'},
66 { "daemon", no_argument, NULL, 'd'},
67 { "allow_delete", no_argument, NULL, 'a'},
68 { "config_file", required_argument, NULL, 'f'},
69 { "help", no_argument, NULL, 'h'},
70 { "vty_addr", required_argument, NULL, 'A'},
71 { "vty_port", required_argument, NULL, 'P'},
72 { "version", no_argument, NULL, 'v'},
73 { "rib_hold", required_argument, NULL, 'r'},
74 { 0 }
75 };
76
77 zebra_capabilities_t _caps_p [] =
78 {
79 ZCAP_NET_ADMIN,
80 ZCAP_SYS_ADMIN,
81 ZCAP_NET_RAW,
82 };
83
84 /* Default configuration file path. */
85 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
86
87 /* Process ID saved for use by init system */
88 const char *pid_file = PATH_ZEBRA_PID;
89
90 /* Help information display. */
91 static void
92 usage (char *progname, int status)
93 {
94 if (status != 0)
95 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
96 else
97 {
98 printf ("Usage : %s [OPTION...]\n\n"\
99 "Daemon which manages kernel routing table management and "\
100 "redistribution between different routing protocols.\n\n"\
101 "-b, --batch Runs in batch mode\n"\
102 "-d, --daemon Runs in daemon mode\n"\
103 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
104 "-f, --config_file Set configuration file name\n"\
105 "-A, --vty_addr Set vty's bind address\n"\
106 "-P, --vty_port Set vty's port number\n"\
107 "-r, --rib_hold Set rib-queue hold time\n"\
108 "-v, --version Print program version\n"\
109 "-h, --help Display this help and exit\n"\
110 "\n"\
111 "Report bugs to %s\n", progname, FRR_BUG_ADDRESS);
112 }
113
114 exit (status);
115 }
116
117 static ifindex_t test_ifindex = 0;
118
119 /* testrib commands */
120 DEFUN (test_interface_state,
121 test_interface_state_cmd,
122 "state (up|down)",
123 "configure interface\n"
124 "up\n"
125 "down\n")
126 {
127 VTY_DECLVAR_CONTEXT (interface, ifp);
128 if (argc < 1)
129 return CMD_WARNING;
130
131 if (ifp->ifindex == IFINDEX_INTERNAL)
132 {
133 ifp->ifindex = ++test_ifindex;
134 ifp->mtu = 1500;
135 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
136 }
137
138 switch (argv[0][0])
139 {
140 case 'u':
141 SET_FLAG (ifp->flags, IFF_UP);
142 if_add_update (ifp);
143 printf ("up\n");
144 break;
145 case 'd':
146 UNSET_FLAG (ifp->flags, IFF_UP);
147 if_delete_update (ifp);
148 printf ("down\n");
149 break;
150 default:
151 return CMD_WARNING;
152 }
153 return CMD_SUCCESS;
154 }
155
156 static void
157 test_cmd_init (void)
158 {
159 install_element (INTERFACE_NODE, &test_interface_state_cmd);
160 }
161
162 /* SIGHUP handler. */
163 static void
164 sighup (void)
165 {
166 zlog_info ("SIGHUP received");
167
168 /* Reload of config file. */
169 ;
170 }
171
172 /* SIGINT handler. */
173 static void
174 sigint (void)
175 {
176 zlog_notice ("Terminating on signal");
177
178 exit (0);
179 }
180
181 /* SIGUSR1 handler. */
182 static void
183 sigusr1 (void)
184 {
185 zlog_rotate (NULL);
186 }
187
188 struct quagga_signal_t zebra_signals[] =
189 {
190 {
191 .signal = SIGHUP,
192 .handler = &sighup,
193 },
194 {
195 .signal = SIGUSR1,
196 .handler = &sigusr1,
197 },
198 {
199 .signal = SIGINT,
200 .handler = &sigint,
201 },
202 {
203 .signal = SIGTERM,
204 .handler = &sigint,
205 },
206 };
207 /* Main startup routine. */
208 int
209 main (int argc, char **argv)
210 {
211 char *p;
212 char *vty_addr = NULL;
213 int vty_port = 0;
214 int batch_mode = 0;
215 int daemon_mode = 0;
216 char *config_file = NULL;
217 char *progname;
218 struct thread thread;
219
220 /* Set umask before anything for security */
221 umask (0027);
222
223 /* preserve my name */
224 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
225
226 zlog_default = openzlog (progname, ZLOG_ZEBRA, 0,
227 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
228
229 while (1)
230 {
231 int opt;
232
233 opt = getopt_long (argc, argv, "bdaf:hA:P:r:v", longopts, 0);
234
235 if (opt == EOF)
236 break;
237
238 switch (opt)
239 {
240 case 0:
241 break;
242 case 'b':
243 batch_mode = 1;
244 case 'd':
245 daemon_mode = 1;
246 break;
247 case 'a':
248 allow_delete =1;
249 break;
250 case 'f':
251 config_file = optarg;
252 break;
253 case 'A':
254 vty_addr = optarg;
255 break;
256 case 'P':
257 /* Deal with atoi() returning 0 on failure, and zebra not
258 listening on zebra port... */
259 if (strcmp(optarg, "0") == 0)
260 {
261 vty_port = 0;
262 break;
263 }
264 vty_port = atoi (optarg);
265 break;
266 case 'r':
267 rib_process_hold_time = atoi(optarg);
268 break;
269 case 'v':
270 print_version (progname);
271 exit (0);
272 break;
273 case 'h':
274 usage (progname, 0);
275 break;
276 default:
277 usage (progname, 1);
278 break;
279 }
280 }
281
282 /* port and conf file mandatory */
283 if (!vty_port || !config_file)
284 {
285 fprintf (stderr, "Error: --vty_port and --config_file arguments"
286 " are both required\n");
287 usage (progname, 1);
288 }
289
290 /* Make master thread emulator. */
291 zebrad.master = thread_master_create ();
292
293 /* Vty related initialize. */
294 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
295 cmd_init (1);
296 vty_config_lockless ();
297 vty_init (zebrad.master);
298 memory_init ();
299 zebra_debug_init ();
300 zebra_if_init ();
301 test_cmd_init ();
302
303 /* Zebra related initialize. */
304 rib_init ();
305 access_list_init ();
306
307 /* Make kernel routing socket. */
308 zebra_vrf_init ();
309 zebra_vty_init();
310
311 /* Configuration file read*/
312 vty_read_config (config_file, config_default);
313
314 /* Clean up rib. */
315 rib_weed_tables ();
316
317 /* Exit when zebra is working in batch mode. */
318 if (batch_mode)
319 exit (0);
320
321 /* Daemonize. */
322 if (daemon_mode && daemon (0, 0) < 0)
323 {
324 perror("daemon start failed");
325 exit (1);
326 }
327
328 /* Needed for BSD routing socket. */
329 pid = getpid ();
330
331 /* Make vty server socket. */
332 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
333
334 /* Print banner. */
335 zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION, vty_port);
336
337 while (thread_fetch (zebrad.master, &thread))
338 thread_call (&thread);
339
340 /* Not reached... */
341 return 0;
342 }