blob: 46f73bbe17e9fab04fab645d62a1ee563050a0fc [file] [log] [blame]
Paul Jakma457eb9a2006-07-27 19:59:58 +00001/* 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 "prefix.h"
29#include "log.h"
30#include "privs.h"
31#include "sigevent.h"
32
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"
38
39/* Zebra instance */
40struct zebra_t zebrad =
41{
42 .rtm_table_default = 0,
43};
44
45/* process id. */
Paul Jakma457eb9a2006-07-27 19:59:58 +000046pid_t pid;
47
48/* zebra_rib's workqueue hold time. Private export for use by test code only */
49extern int rib_process_hold_time;
50
51/* Pacify zclient.o in libzebra, which expects this variable. */
52struct thread_master *master;
53
54/* Command line options. */
55struct option longopts[] =
56{
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 }
67};
68
69zebra_capabilities_t _caps_p [] =
70{
71 ZCAP_NET_ADMIN,
72 ZCAP_SYS_ADMIN,
73 ZCAP_NET_RAW,
74};
75
76/* Default configuration file path. */
77char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
78
79/* Process ID saved for use by init system */
80const char *pid_file = PATH_ZEBRA_PID;
81
82/* Help information display. */
83static void
84usage (char *progname, int status)
85{
86 if (status != 0)
87 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
88 else
89 {
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);
104 }
105
106 exit (status);
107}
108
109static unsigned int test_ifindex = 0;
110
111/* testrib commands */
112DEFUN (test_interface_state,
113 test_interface_state_cmd,
114 "state (up|down)",
115 "configure interface\n"
116 "up\n"
117 "down\n")
118{
119 struct interface *ifp;
120 if (argc < 1)
121 return CMD_WARNING;
122
123 ifp = vty->index;
124 if (ifp->ifindex == IFINDEX_INTERNAL)
125 {
126 ifp->ifindex = ++test_ifindex;
127 ifp->mtu = 1500;
128 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
129 }
130
131 switch (argv[0][0])
132 {
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;
145 }
146 return CMD_SUCCESS;
147}
148
149static void
150test_cmd_init (void)
151{
152 install_element (INTERFACE_NODE, &test_interface_state_cmd);
153}
154
155/* SIGHUP handler. */
156static void
157sighup (void)
158{
159 zlog_info ("SIGHUP received");
160
161 /* Reload of config file. */
162 ;
163}
164
165/* SIGINT handler. */
166static void
167sigint (void)
168{
169 zlog_notice ("Terminating on signal");
170
171 exit (0);
172}
173
174/* SIGUSR1 handler. */
175static void
176sigusr1 (void)
177{
178 zlog_rotate (NULL);
179}
180
181struct quagga_signal_t zebra_signals[] =
182{
183 {
184 .signal = SIGHUP,
185 .handler = &sighup,
186 },
187 {
188 .signal = SIGUSR1,
189 .handler = &sigusr1,
190 },
191 {
192 .signal = SIGINT,
193 .handler = &sigint,
194 },
195 {
196 .signal = SIGTERM,
197 .handler = &sigint,
198 },
199};
200
201/* Main startup routine. */
202int
203main (int argc, char **argv)
204{
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;
213
214 /* Set umask before anything for security */
215 umask (0027);
216
217 /* preserve my name */
218 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
219
220 zlog_default = openzlog (progname, ZLOG_ZEBRA,
221 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
222
223 while (1)
224 {
225 int opt;
226
227 opt = getopt_long (argc, argv, "bdlf:hA:P:r:v", longopts, 0);
228
229 if (opt == EOF)
230 break;
231
232 switch (opt)
233 {
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)
254 {
255 vty_port = 0;
256 break;
257 }
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;
273 }
274 }
275
276 /* port and conf file mandatory */
277 if (!vty_port || !config_file)
278 usage (progname, 1);
279
280 /* Make master thread emulator. */
281 zebrad.master = thread_master_create ();
282
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 ();
292
293 /* Zebra related initialize. */
294 rib_init ();
295 access_list_init ();
296
297 /* Make kernel routing socket. */
298 kernel_init ();
299 route_read ();
300 zebra_vty_init();
301
302 /* Sort VTY commands. */
303 sort_node ();
304
305 /* Configuration file read*/
306 vty_read_config (config_file, config_default);
307
308 /* Clean up rib. */
309 rib_weed_tables ();
310
311 /* Exit when zebra is working in batch mode. */
312 if (batch_mode)
313 exit (0);
314
Paul Jakma457eb9a2006-07-27 19:59:58 +0000315 /* Daemonize. */
316 if (daemon_mode)
317 daemon (0, 0);
318
319 /* Needed for BSD routing socket. */
320 pid = getpid ();
321
322 /* Make vty server socket. */
323 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
324
325 /* Print banner. */
326 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
327
328 while (thread_fetch (zebrad.master, &thread))
329 thread_call (&thread);
330
331 /* Not reached... */
332 return 0;
333}