blob: 59cec46276275c25844e3ce018953559085e060d [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. */
46pid_t old_pid;
47pid_t pid;
48
49/* zebra_rib's workqueue hold time. Private export for use by test code only */
50extern int rib_process_hold_time;
51
52/* Pacify zclient.o in libzebra, which expects this variable. */
53struct thread_master *master;
54
55/* Command line options. */
56struct option longopts[] =
57{
58 { "batch", no_argument, NULL, 'b'},
59 { "daemon", no_argument, NULL, 'd'},
60 { "log_mode", no_argument, NULL, 'l'},
61 { "config_file", required_argument, NULL, 'f'},
62 { "help", no_argument, NULL, 'h'},
63 { "vty_addr", required_argument, NULL, 'A'},
64 { "vty_port", required_argument, NULL, 'P'},
65 { "version", no_argument, NULL, 'v'},
66 { "rib_hold", required_argument, NULL, 'r'},
67 { 0 }
68};
69
70zebra_capabilities_t _caps_p [] =
71{
72 ZCAP_NET_ADMIN,
73 ZCAP_SYS_ADMIN,
74 ZCAP_NET_RAW,
75};
76
77/* Default configuration file path. */
78char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
79
80/* Process ID saved for use by init system */
81const char *pid_file = PATH_ZEBRA_PID;
82
83/* Help information display. */
84static void
85usage (char *progname, int status)
86{
87 if (status != 0)
88 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
89 else
90 {
91 printf ("Usage : %s [OPTION...]\n\n"\
92 "Daemon which manages kernel routing table management and "\
93 "redistribution between different routing protocols.\n\n"\
94 "-b, --batch Runs in batch mode\n"\
95 "-d, --daemon Runs in daemon mode\n"\
96 "-f, --config_file Set configuration file name\n"\
97 "-l, --log_mode Set verbose log mode flag\n"\
98 "-A, --vty_addr Set vty's bind address\n"\
99 "-P, --vty_port Set vty's port number\n"\
100 "-r, --rib_hold Set rib-queue hold time\n"\
101 "-v, --version Print program version\n"\
102 "-h, --help Display this help and exit\n"\
103 "\n"\
104 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
105 }
106
107 exit (status);
108}
109
110static unsigned int test_ifindex = 0;
111
112/* testrib commands */
113DEFUN (test_interface_state,
114 test_interface_state_cmd,
115 "state (up|down)",
116 "configure interface\n"
117 "up\n"
118 "down\n")
119{
120 struct interface *ifp;
121 if (argc < 1)
122 return CMD_WARNING;
123
124 ifp = vty->index;
125 if (ifp->ifindex == IFINDEX_INTERNAL)
126 {
127 ifp->ifindex = ++test_ifindex;
128 ifp->mtu = 1500;
129 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
130 }
131
132 switch (argv[0][0])
133 {
134 case 'u':
135 SET_FLAG (ifp->flags, IFF_UP);
136 if_add_update (ifp);
137 printf ("up\n");
138 break;
139 case 'd':
140 UNSET_FLAG (ifp->flags, IFF_UP);
141 if_delete_update (ifp);
142 printf ("down\n");
143 break;
144 default:
145 return CMD_WARNING;
146 }
147 return CMD_SUCCESS;
148}
149
150static void
151test_cmd_init (void)
152{
153 install_element (INTERFACE_NODE, &test_interface_state_cmd);
154}
155
156/* SIGHUP handler. */
157static void
158sighup (void)
159{
160 zlog_info ("SIGHUP received");
161
162 /* Reload of config file. */
163 ;
164}
165
166/* SIGINT handler. */
167static void
168sigint (void)
169{
170 zlog_notice ("Terminating on signal");
171
172 exit (0);
173}
174
175/* SIGUSR1 handler. */
176static void
177sigusr1 (void)
178{
179 zlog_rotate (NULL);
180}
181
182struct quagga_signal_t zebra_signals[] =
183{
184 {
185 .signal = SIGHUP,
186 .handler = &sighup,
187 },
188 {
189 .signal = SIGUSR1,
190 .handler = &sigusr1,
191 },
192 {
193 .signal = SIGINT,
194 .handler = &sigint,
195 },
196 {
197 .signal = SIGTERM,
198 .handler = &sigint,
199 },
200};
201
202/* Main startup routine. */
203int
204main (int argc, char **argv)
205{
206 char *p;
207 char *vty_addr = NULL;
208 int vty_port = 0;
209 int batch_mode = 0;
210 int daemon_mode = 0;
211 char *config_file = NULL;
212 char *progname;
213 struct thread thread;
214
215 /* Set umask before anything for security */
216 umask (0027);
217
218 /* preserve my name */
219 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
220
221 zlog_default = openzlog (progname, ZLOG_ZEBRA,
222 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
223
224 while (1)
225 {
226 int opt;
227
228 opt = getopt_long (argc, argv, "bdlf:hA:P:r:v", longopts, 0);
229
230 if (opt == EOF)
231 break;
232
233 switch (opt)
234 {
235 case 0:
236 break;
237 case 'b':
238 batch_mode = 1;
239 case 'd':
240 daemon_mode = 1;
241 break;
242 case 'l':
243 /* log_mode = 1; */
244 break;
245 case 'f':
246 config_file = optarg;
247 break;
248 case 'A':
249 vty_addr = optarg;
250 break;
251 case 'P':
252 /* Deal with atoi() returning 0 on failure, and zebra not
253 listening on zebra port... */
254 if (strcmp(optarg, "0") == 0)
255 {
256 vty_port = 0;
257 break;
258 }
259 vty_port = atoi (optarg);
260 break;
261 case 'r':
262 rib_process_hold_time = atoi(optarg);
263 break;
264 case 'v':
265 print_version (progname);
266 exit (0);
267 break;
268 case 'h':
269 usage (progname, 0);
270 break;
271 default:
272 usage (progname, 1);
273 break;
274 }
275 }
276
277 /* port and conf file mandatory */
278 if (!vty_port || !config_file)
279 usage (progname, 1);
280
281 /* Make master thread emulator. */
282 zebrad.master = thread_master_create ();
283
284 /* Vty related initialize. */
285 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
286 cmd_init (1);
287 vty_init (zebrad.master);
288 memory_init ();
289 if_init();
290 zebra_debug_init ();
291 zebra_if_init ();
292 test_cmd_init ();
293
294 /* Zebra related initialize. */
295 rib_init ();
296 access_list_init ();
297
298 /* Make kernel routing socket. */
299 kernel_init ();
300 route_read ();
301 zebra_vty_init();
302
303 /* Sort VTY commands. */
304 sort_node ();
305
306 /* Configuration file read*/
307 vty_read_config (config_file, config_default);
308
309 /* Clean up rib. */
310 rib_weed_tables ();
311
312 /* Exit when zebra is working in batch mode. */
313 if (batch_mode)
314 exit (0);
315
316 /* Needed for BSD routing socket. */
317 old_pid = getpid ();
318
319 /* Daemonize. */
320 if (daemon_mode)
321 daemon (0, 0);
322
323 /* Needed for BSD routing socket. */
324 pid = getpid ();
325
326 /* Make vty server socket. */
327 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
328
329 /* Print banner. */
330 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
331
332 while (thread_fetch (zebrad.master, &thread))
333 thread_call (&thread);
334
335 /* Not reached... */
336 return 0;
337}