[release] Bump version to 0.99.10
[jleu-quagga.git] / zebra / test_main.c
blob46f73bbe17e9fab04fab645d62a1ee563050a0fc
1 /* main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
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.
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.
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.
20 #include <zebra.h>
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 "prefix.h"
29 #include "log.h"
30 #include "privs.h"
31 #include "sigevent.h"
33 #include "zebra/rib.h"
34 #include "zebra/zserv.h"
35 #include "zebra/debug.h"
36 #include "zebra/router-id.h"
37 #include "zebra/interface.h"
39 /* Zebra instance */
40 struct zebra_t zebrad =
42 .rtm_table_default = 0,
45 /* process id. */
46 pid_t pid;
48 /* zebra_rib's workqueue hold time. Private export for use by test code only */
49 extern int rib_process_hold_time;
51 /* Pacify zclient.o in libzebra, which expects this variable. */
52 struct thread_master *master;
54 /* Command line options. */
55 struct option longopts[] =
57 { "batch", no_argument, NULL, 'b'},
58 { "daemon", no_argument, NULL, 'd'},
59 { "log_mode", no_argument, NULL, 'l'},
60 { "config_file", required_argument, NULL, 'f'},
61 { "help", no_argument, NULL, 'h'},
62 { "vty_addr", required_argument, NULL, 'A'},
63 { "vty_port", required_argument, NULL, 'P'},
64 { "version", no_argument, NULL, 'v'},
65 { "rib_hold", required_argument, NULL, 'r'},
66 { 0 }
69 zebra_capabilities_t _caps_p [] =
71 ZCAP_NET_ADMIN,
72 ZCAP_SYS_ADMIN,
73 ZCAP_NET_RAW,
76 /* Default configuration file path. */
77 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
79 /* Process ID saved for use by init system */
80 const char *pid_file = PATH_ZEBRA_PID;
82 /* Help information display. */
83 static void
84 usage (char *progname, int status)
86 if (status != 0)
87 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
88 else
90 printf ("Usage : %s [OPTION...]\n\n"\
91 "Daemon which manages kernel routing table management and "\
92 "redistribution between different routing protocols.\n\n"\
93 "-b, --batch Runs in batch mode\n"\
94 "-d, --daemon Runs in daemon mode\n"\
95 "-f, --config_file Set configuration file name\n"\
96 "-l, --log_mode Set verbose log mode flag\n"\
97 "-A, --vty_addr Set vty's bind address\n"\
98 "-P, --vty_port Set vty's port number\n"\
99 "-r, --rib_hold Set rib-queue hold time\n"\
100 "-v, --version Print program version\n"\
101 "-h, --help Display this help and exit\n"\
102 "\n"\
103 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
106 exit (status);
109 static unsigned int test_ifindex = 0;
111 /* testrib commands */
112 DEFUN (test_interface_state,
113 test_interface_state_cmd,
114 "state (up|down)",
115 "configure interface\n"
116 "up\n"
117 "down\n")
119 struct interface *ifp;
120 if (argc < 1)
121 return CMD_WARNING;
123 ifp = vty->index;
124 if (ifp->ifindex == IFINDEX_INTERNAL)
126 ifp->ifindex = ++test_ifindex;
127 ifp->mtu = 1500;
128 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
131 switch (argv[0][0])
133 case 'u':
134 SET_FLAG (ifp->flags, IFF_UP);
135 if_add_update (ifp);
136 printf ("up\n");
137 break;
138 case 'd':
139 UNSET_FLAG (ifp->flags, IFF_UP);
140 if_delete_update (ifp);
141 printf ("down\n");
142 break;
143 default:
144 return CMD_WARNING;
146 return CMD_SUCCESS;
149 static void
150 test_cmd_init (void)
152 install_element (INTERFACE_NODE, &test_interface_state_cmd);
155 /* SIGHUP handler. */
156 static void
157 sighup (void)
159 zlog_info ("SIGHUP received");
161 /* Reload of config file. */
165 /* SIGINT handler. */
166 static void
167 sigint (void)
169 zlog_notice ("Terminating on signal");
171 exit (0);
174 /* SIGUSR1 handler. */
175 static void
176 sigusr1 (void)
178 zlog_rotate (NULL);
181 struct quagga_signal_t zebra_signals[] =
184 .signal = SIGHUP,
185 .handler = &sighup,
188 .signal = SIGUSR1,
189 .handler = &sigusr1,
192 .signal = SIGINT,
193 .handler = &sigint,
196 .signal = SIGTERM,
197 .handler = &sigint,
201 /* Main startup routine. */
203 main (int argc, char **argv)
205 char *p;
206 char *vty_addr = NULL;
207 int vty_port = 0;
208 int batch_mode = 0;
209 int daemon_mode = 0;
210 char *config_file = NULL;
211 char *progname;
212 struct thread thread;
214 /* Set umask before anything for security */
215 umask (0027);
217 /* preserve my name */
218 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
220 zlog_default = openzlog (progname, ZLOG_ZEBRA,
221 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
223 while (1)
225 int opt;
227 opt = getopt_long (argc, argv, "bdlf:hA:P:r:v", longopts, 0);
229 if (opt == EOF)
230 break;
232 switch (opt)
234 case 0:
235 break;
236 case 'b':
237 batch_mode = 1;
238 case 'd':
239 daemon_mode = 1;
240 break;
241 case 'l':
242 /* log_mode = 1; */
243 break;
244 case 'f':
245 config_file = optarg;
246 break;
247 case 'A':
248 vty_addr = optarg;
249 break;
250 case 'P':
251 /* Deal with atoi() returning 0 on failure, and zebra not
252 listening on zebra port... */
253 if (strcmp(optarg, "0") == 0)
255 vty_port = 0;
256 break;
258 vty_port = atoi (optarg);
259 break;
260 case 'r':
261 rib_process_hold_time = atoi(optarg);
262 break;
263 case 'v':
264 print_version (progname);
265 exit (0);
266 break;
267 case 'h':
268 usage (progname, 0);
269 break;
270 default:
271 usage (progname, 1);
272 break;
276 /* port and conf file mandatory */
277 if (!vty_port || !config_file)
278 usage (progname, 1);
280 /* Make master thread emulator. */
281 zebrad.master = thread_master_create ();
283 /* Vty related initialize. */
284 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
285 cmd_init (1);
286 vty_init (zebrad.master);
287 memory_init ();
288 if_init();
289 zebra_debug_init ();
290 zebra_if_init ();
291 test_cmd_init ();
293 /* Zebra related initialize. */
294 rib_init ();
295 access_list_init ();
297 /* Make kernel routing socket. */
298 kernel_init ();
299 route_read ();
300 zebra_vty_init();
302 /* Sort VTY commands. */
303 sort_node ();
305 /* Configuration file read*/
306 vty_read_config (config_file, config_default);
308 /* Clean up rib. */
309 rib_weed_tables ();
311 /* Exit when zebra is working in batch mode. */
312 if (batch_mode)
313 exit (0);
315 /* Daemonize. */
316 if (daemon_mode)
317 daemon (0, 0);
319 /* Needed for BSD routing socket. */
320 pid = getpid ();
322 /* Make vty server socket. */
323 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
325 /* Print banner. */
326 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
328 while (thread_fetch (zebrad.master, &thread))
329 thread_call (&thread);
331 /* Not reached... */
332 return 0;