]> git.proxmox.com Git - mirror_frr.git/blame - zebra/main.c
+ fixed bug #402: now the second zebra process doesn't destroy routes
[mirror_frr.git] / zebra / main.c
CommitLineData
edd7c245 1/* zebra daemon main routine.
718e3744 2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
5e4fa164 24#include <lib/version.h>
718e3744 25#include "getopt.h"
26#include "command.h"
27#include "thread.h"
28#include "filter.h"
29#include "memory.h"
30#include "prefix.h"
31#include "log.h"
7514fb77 32#include "plist.h"
edd7c245 33#include "privs.h"
2d75d052 34#include "sigevent.h"
718e3744 35
36#include "zebra/rib.h"
37#include "zebra/zserv.h"
38#include "zebra/debug.h"
18a6dce6 39#include "zebra/router-id.h"
ca776988 40#include "zebra/irdp.h"
a1ac18c4 41#include "zebra/rtadv.h"
718e3744 42
b21b19c5 43/* Zebra instance */
44struct zebra_t zebrad =
45{
46 .rtm_table_default = 0,
47};
718e3744 48
49/* process id. */
50pid_t old_pid;
51pid_t pid;
52
87efd646 53/* Pacify zclient.o in libzebra, which expects this variable. */
54struct thread_master *master;
55
718e3744 56/* Route retain mode flag. */
57int retain_mode = 0;
58
59/* Don't delete kernel route. */
60int keep_kernel_mode = 0;
61
c34b6b57 62#ifdef HAVE_NETLINK
63/* Receive buffer size for netlink socket */
64u_int32_t nl_rcvbufsize = 0;
65#endif /* HAVE_NETLINK */
66
718e3744 67/* Command line options. */
68struct option longopts[] =
69{
70 { "batch", no_argument, NULL, 'b'},
71 { "daemon", no_argument, NULL, 'd'},
72 { "keep_kernel", no_argument, NULL, 'k'},
73 { "log_mode", no_argument, NULL, 'l'},
74 { "config_file", required_argument, NULL, 'f'},
75 { "pid_file", required_argument, NULL, 'i'},
76 { "help", no_argument, NULL, 'h'},
77 { "vty_addr", required_argument, NULL, 'A'},
78 { "vty_port", required_argument, NULL, 'P'},
79 { "retain", no_argument, NULL, 'r'},
876b8be0 80 { "dryrun", no_argument, NULL, 'C'},
c34b6b57 81#ifdef HAVE_NETLINK
583d800c 82 { "nl-bufsize", required_argument, NULL, 's'},
c34b6b57 83#endif /* HAVE_NETLINK */
edd7c245 84 { "user", required_argument, NULL, 'u'},
c065230a 85 { "group", required_argument, NULL, 'g'},
718e3744 86 { "version", no_argument, NULL, 'v'},
87 { 0 }
88};
89
edd7c245 90zebra_capabilities_t _caps_p [] =
91{
ceacedba 92 ZCAP_NET_ADMIN,
edd7c245 93 ZCAP_SYS_ADMIN,
ceacedba 94 ZCAP_NET_RAW,
edd7c245 95};
96
97/* zebra privileges to run with */
98struct zebra_privs_t zserv_privs =
99{
d81fadfd 100#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
101 .user = QUAGGA_USER,
102 .group = QUAGGA_GROUP,
edd7c245 103#endif
104#ifdef VTY_GROUP
105 .vty_group = VTY_GROUP,
106#endif
107 .caps_p = _caps_p,
108 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
109 .cap_num_i = 0
110};
111
718e3744 112/* Default configuration file path. */
718e3744 113char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
114
115/* Process ID saved for use by init system */
fce954f8 116const char *pid_file = PATH_ZEBRA_PID;
718e3744 117
118/* Help information display. */
119static void
120usage (char *progname, int status)
121{
122 if (status != 0)
123 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
124 else
125 {
c34b6b57 126 printf ("Usage : %s [OPTION...]\n\n"\
127 "Daemon which manages kernel routing table management and "\
128 "redistribution between different routing protocols.\n\n"\
129 "-b, --batch Runs in batch mode\n"\
130 "-d, --daemon Runs in daemon mode\n"\
131 "-f, --config_file Set configuration file name\n"\
132 "-i, --pid_file Set process identifier file name\n"\
133 "-k, --keep_kernel Don't delete old routes which installed by "\
134 "zebra.\n"\
135 "-l, --log_mode Set verbose log mode flag\n"\
876b8be0 136 "-C, --dryrun Check configuration for validity and exit\n"\
c34b6b57 137 "-A, --vty_addr Set vty's bind address\n"\
138 "-P, --vty_port Set vty's port number\n"\
139 "-r, --retain When program terminates, retain added route "\
140 "by zebra.\n"\
c065230a 141 "-u, --user User to run as\n"\
142 "-g, --group Group to run as\n", progname);
c34b6b57 143#ifdef HAVE_NETLINK
144 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
145#endif /* HAVE_NETLINK */
146 printf ("-v, --version Print program version\n"\
147 "-h, --help Display this help and exit\n"\
148 "\n"\
149 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
718e3744 150 }
151
152 exit (status);
153}
154\f
155/* SIGHUP handler. */
a1ac18c4 156static void
2d75d052 157sighup (void)
718e3744 158{
159 zlog_info ("SIGHUP received");
160
161 /* Reload of config file. */
162 ;
163}
164
165/* SIGINT handler. */
a1ac18c4 166static void
2d75d052 167sigint (void)
718e3744 168{
887c44a4 169 zlog_notice ("Terminating on signal");
718e3744 170
171 if (!retain_mode)
172 rib_close ();
ca776988 173#ifdef HAVE_IRDP
174 irdp_finish();
175#endif
718e3744 176
177 exit (0);
178}
179
180/* SIGUSR1 handler. */
a1ac18c4 181static void
2d75d052 182sigusr1 (void)
718e3744 183{
184 zlog_rotate (NULL);
185}
186
2d75d052 187struct quagga_signal_t zebra_signals[] =
718e3744 188{
2d75d052 189 {
190 .signal = SIGHUP,
191 .handler = &sighup,
192 },
193 {
194 .signal = SIGUSR1,
195 .handler = &sigusr1,
196 },
197 {
198 .signal = SIGINT,
8c903fbb 199 .handler = &sigint,
2d75d052 200 },
f571dab0 201 {
202 .signal = SIGTERM,
203 .handler = &sigint,
204 },
2d75d052 205};
718e3744 206\f
207/* Main startup routine. */
208int
209main (int argc, char **argv)
210{
211 char *p;
212 char *vty_addr = NULL;
4fc4e7ab 213 int vty_port = ZEBRA_VTY_PORT;
876b8be0 214 int dryrun = 0;
718e3744 215 int batch_mode = 0;
216 int daemon_mode = 0;
217 char *config_file = NULL;
218 char *progname;
219 struct thread thread;
718e3744 220
221 /* Set umask before anything for security */
222 umask (0027);
223
224 /* preserve my name */
225 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
226
274a4a44 227 zlog_default = openzlog (progname, ZLOG_ZEBRA,
718e3744 228 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
229
230 while (1)
231 {
232 int opt;
233
c34b6b57 234#ifdef HAVE_NETLINK
876b8be0 235 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:vs:C", longopts, 0);
c34b6b57 236#else
876b8be0 237 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:vC", longopts, 0);
c34b6b57 238#endif /* HAVE_NETLINK */
718e3744 239
240 if (opt == EOF)
241 break;
242
243 switch (opt)
244 {
245 case 0:
246 break;
247 case 'b':
248 batch_mode = 1;
249 case 'd':
250 daemon_mode = 1;
251 break;
252 case 'k':
253 keep_kernel_mode = 1;
254 break;
876b8be0
PJ
255 case 'C':
256 dryrun = 1;
257 break;
718e3744 258 case 'l':
259 /* log_mode = 1; */
260 break;
261 case 'f':
262 config_file = optarg;
263 break;
264 case 'A':
265 vty_addr = optarg;
266 break;
267 case 'i':
268 pid_file = optarg;
269 break;
270 case 'P':
4fc4e7ab 271 /* Deal with atoi() returning 0 on failure, and zebra not
272 listening on zebra port... */
273 if (strcmp(optarg, "0") == 0)
274 {
275 vty_port = 0;
276 break;
277 }
718e3744 278 vty_port = atoi (optarg);
4fc4e7ab 279 vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT);
718e3744 280 break;
281 case 'r':
282 retain_mode = 1;
283 break;
c34b6b57 284#ifdef HAVE_NETLINK
285 case 's':
286 nl_rcvbufsize = atoi (optarg);
287 break;
288#endif /* HAVE_NETLINK */
c065230a 289 case 'u':
290 zserv_privs.user = optarg;
291 break;
292 case 'g':
293 zserv_privs.group = optarg;
294 break;
718e3744 295 case 'v':
296 print_version (progname);
297 exit (0);
298 break;
299 case 'h':
300 usage (progname, 0);
301 break;
302 default:
303 usage (progname, 1);
304 break;
305 }
306 }
307
308 /* Make master thread emulator. */
b21b19c5 309 zebrad.master = thread_master_create ();
718e3744 310
edd7c245 311 /* privs initialise */
312 zprivs_init (&zserv_privs);
313
718e3744 314 /* Vty related initialize. */
2d75d052 315 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
718e3744 316 cmd_init (1);
b21b19c5 317 vty_init (zebrad.master);
718e3744 318 memory_init ();
319
320 /* Zebra related initialize. */
321 zebra_init ();
322 rib_init ();
323 zebra_if_init ();
324 zebra_debug_init ();
18a6dce6 325 router_id_init();
718e3744 326 zebra_vty_init ();
327 access_list_init ();
7514fb77 328 prefix_list_init ();
718e3744 329 rtadv_init ();
ca776988 330#ifdef HAVE_IRDP
331 irdp_init();
332#endif
718e3744 333
334 /* For debug purpose. */
335 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
336
337 /* Make kernel routing socket. */
338 kernel_init ();
339 interface_list ();
340 route_read ();
341
342 /* Sort VTY commands. */
343 sort_node ();
344
345#ifdef HAVE_SNMP
346 zebra_snmp_init ();
347#endif /* HAVE_SNMP */
348
91b7351d
DO
349 /* Process the configuration file. Among other configuration
350 * directives we can meet those installing static routes. Such
351 * requests will not be executed immediately, but queued in
352 * zebra->ribq structure until we enter the main execution loop.
353 * The notifications from kernel will show originating PID equal
354 * to that after daemon() completes (if ever called).
355 */
320ec10a 356 vty_read_config (config_file, config_default);
718e3744 357
876b8be0
PJ
358 /* Don't start execution if we are in dry-run mode */
359 if (dryrun)
360 return(0);
361
718e3744 362 /* Clean up rib. */
363 rib_weed_tables ();
364
365 /* Exit when zebra is working in batch mode. */
366 if (batch_mode)
367 exit (0);
368
369 /* Needed for BSD routing socket. */
370 old_pid = getpid ();
371
372 /* Daemonize. */
373 if (daemon_mode)
374 daemon (0, 0);
375
376 /* Output pid of zebra. */
377 pid_output (pid_file);
378
91b7351d
DO
379 /* After we have successfully acquired the pidfile, we can be sure
380 * about being the only copy of zebra process, which is submitting
381 * changes to the FIB.
382 * Clean up zebra-originated routes. The requests will be sent to OS
383 * immediately, so originating PID in notifications from kernel
384 * will be equal to the current getpid(). To know about such routes,
385 * we have to have route_read() called before.
386 */
387 if (! keep_kernel_mode)
388 rib_sweep_route ();
389
718e3744 390 /* Needed for BSD routing socket. */
391 pid = getpid ();
392
393 /* Make vty server socket. */
4fc4e7ab 394 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
718e3744 395
887c44a4 396 /* Print banner. */
397 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
398
b21b19c5 399 while (thread_fetch (zebrad.master, &thread))
718e3744 400 thread_call (&thread);
401
402 /* Not reached... */
e8e1946e 403 return 0;
718e3744 404}