]> git.proxmox.com Git - mirror_frr.git/blame - zebra/main.c
lib: add new extensible memory-type handling
[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"
b72ede27 35#include "vrf.h"
718e3744 36
37#include "zebra/rib.h"
38#include "zebra/zserv.h"
39#include "zebra/debug.h"
18a6dce6 40#include "zebra/router-id.h"
ca776988 41#include "zebra/irdp.h"
a1ac18c4 42#include "zebra/rtadv.h"
5adc2528 43#include "zebra/zebra_fpm.h"
244c1cdc 44#include "zebra/zebra_ptm.h"
fe18ee2d 45#include "zebra/zebra_ns.h"
e2b1be64 46#include "zebra/redistribute.h"
244c1cdc
DS
47
48#define ZEBRA_PTM_SUPPORT
718e3744 49
b21b19c5 50/* Zebra instance */
51struct zebra_t zebrad =
52{
53 .rtm_table_default = 0,
54};
718e3744 55
56/* process id. */
718e3744 57pid_t pid;
58
87efd646 59/* Pacify zclient.o in libzebra, which expects this variable. */
60struct thread_master *master;
61
718e3744 62/* Route retain mode flag. */
63int retain_mode = 0;
64
6baf7bb8
DS
65/* Allow non-quagga entities to delete quagga routes */
66int allow_delete = 0;
67
718e3744 68/* Don't delete kernel route. */
69int keep_kernel_mode = 0;
70
c34b6b57 71#ifdef HAVE_NETLINK
72/* Receive buffer size for netlink socket */
b6286c70 73u_int32_t nl_rcvbufsize = 4194304;
c34b6b57 74#endif /* HAVE_NETLINK */
75
718e3744 76/* Command line options. */
77struct option longopts[] =
78{
6baf7bb8
DS
79 { "batch", no_argument, NULL, 'b'},
80 { "daemon", no_argument, NULL, 'd'},
81 { "allow_delete", no_argument, NULL, 'a'},
82 { "keep_kernel", no_argument, NULL, 'k'},
83 { "config_file", required_argument, NULL, 'f'},
84 { "pid_file", required_argument, NULL, 'i'},
85 { "socket", required_argument, NULL, 'z'},
86 { "help", no_argument, NULL, 'h'},
87 { "vty_addr", required_argument, NULL, 'A'},
88 { "vty_port", required_argument, NULL, 'P'},
89 { "retain", no_argument, NULL, 'r'},
90 { "dryrun", no_argument, NULL, 'C'},
c34b6b57 91#ifdef HAVE_NETLINK
6baf7bb8 92 { "nl-bufsize", required_argument, NULL, 's'},
c34b6b57 93#endif /* HAVE_NETLINK */
6baf7bb8
DS
94 { "user", required_argument, NULL, 'u'},
95 { "group", required_argument, NULL, 'g'},
96 { "version", no_argument, NULL, 'v'},
718e3744 97 { 0 }
98};
99
edd7c245 100zebra_capabilities_t _caps_p [] =
101{
ceacedba 102 ZCAP_NET_ADMIN,
edd7c245 103 ZCAP_SYS_ADMIN,
ceacedba 104 ZCAP_NET_RAW,
edd7c245 105};
106
107/* zebra privileges to run with */
108struct zebra_privs_t zserv_privs =
109{
d81fadfd 110#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
111 .user = QUAGGA_USER,
112 .group = QUAGGA_GROUP,
edd7c245 113#endif
114#ifdef VTY_GROUP
115 .vty_group = VTY_GROUP,
116#endif
117 .caps_p = _caps_p,
837d16cc 118 .cap_num_p = array_size(_caps_p),
edd7c245 119 .cap_num_i = 0
120};
121
718e3744 122/* Default configuration file path. */
718e3744 123char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
124
125/* Process ID saved for use by init system */
fce954f8 126const char *pid_file = PATH_ZEBRA_PID;
718e3744 127
128/* Help information display. */
129static void
130usage (char *progname, int status)
131{
132 if (status != 0)
133 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
134 else
135 {
c34b6b57 136 printf ("Usage : %s [OPTION...]\n\n"\
137 "Daemon which manages kernel routing table management and "\
138 "redistribution between different routing protocols.\n\n"\
139 "-b, --batch Runs in batch mode\n"\
140 "-d, --daemon Runs in daemon mode\n"\
6baf7bb8 141 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
c34b6b57 142 "-f, --config_file Set configuration file name\n"\
143 "-i, --pid_file Set process identifier file name\n"\
b5114685 144 "-z, --socket Set path of zebra socket\n"\
c34b6b57 145 "-k, --keep_kernel Don't delete old routes which installed by "\
146 "zebra.\n"\
876b8be0 147 "-C, --dryrun Check configuration for validity and exit\n"\
c34b6b57 148 "-A, --vty_addr Set vty's bind address\n"\
149 "-P, --vty_port Set vty's port number\n"\
150 "-r, --retain When program terminates, retain added route "\
151 "by zebra.\n"\
c065230a 152 "-u, --user User to run as\n"\
19380819 153 "-g, --group Group to run as\n", progname);
c34b6b57 154#ifdef HAVE_NETLINK
155 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
156#endif /* HAVE_NETLINK */
157 printf ("-v, --version Print program version\n"\
158 "-h, --help Display this help and exit\n"\
159 "\n"\
160 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
718e3744 161 }
162
163 exit (status);
164}
6b0655a2 165
718e3744 166/* SIGHUP handler. */
a1ac18c4 167static void
2d75d052 168sighup (void)
718e3744 169{
170 zlog_info ("SIGHUP received");
171
172 /* Reload of config file. */
173 ;
174}
175
176/* SIGINT handler. */
a1ac18c4 177static void
2d75d052 178sigint (void)
718e3744 179{
fe18ee2d
DS
180 struct zebra_ns *zns;
181
887c44a4 182 zlog_notice ("Terminating on signal");
718e3744 183
184 if (!retain_mode)
185 rib_close ();
ca776988 186#ifdef HAVE_IRDP
187 irdp_finish();
188#endif
718e3744 189
c8ed14dd 190 zebra_ptm_finish();
fe18ee2d
DS
191
192 zns = zebra_ns_lookup (NS_DEFAULT);
193 zebra_ns_disable (0, (void **)&zns);
718e3744 194 exit (0);
195}
196
197/* SIGUSR1 handler. */
a1ac18c4 198static void
2d75d052 199sigusr1 (void)
718e3744 200{
201 zlog_rotate (NULL);
202}
203
2d75d052 204struct quagga_signal_t zebra_signals[] =
718e3744 205{
2d75d052 206 {
207 .signal = SIGHUP,
208 .handler = &sighup,
209 },
210 {
211 .signal = SIGUSR1,
212 .handler = &sigusr1,
213 },
214 {
215 .signal = SIGINT,
8c903fbb 216 .handler = &sigint,
2d75d052 217 },
f571dab0 218 {
219 .signal = SIGTERM,
220 .handler = &sigint,
221 },
2d75d052 222};
b72ede27 223
718e3744 224/* Main startup routine. */
225int
226main (int argc, char **argv)
227{
228 char *p;
229 char *vty_addr = NULL;
4fc4e7ab 230 int vty_port = ZEBRA_VTY_PORT;
876b8be0 231 int dryrun = 0;
718e3744 232 int batch_mode = 0;
233 int daemon_mode = 0;
234 char *config_file = NULL;
235 char *progname;
236 struct thread thread;
b5114685 237 char *zserv_path = NULL;
718e3744 238
239 /* Set umask before anything for security */
240 umask (0027);
241
242 /* preserve my name */
243 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
244
7c8ff89e 245 zlog_default = openzlog (progname, ZLOG_ZEBRA, 0,
718e3744 246 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
2caa9b39 247 zprivs_init (&zserv_privs);
c05795b1
SK
248#if defined(HAVE_CUMULUS)
249 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
250#endif
718e3744 251
252 while (1)
253 {
254 int opt;
255
c34b6b57 256#ifdef HAVE_NETLINK
6baf7bb8 257 opt = getopt_long (argc, argv, "bdakf:i:z:hA:P:ru:g:vs:C", longopts, 0);
c34b6b57 258#else
6baf7bb8 259 opt = getopt_long (argc, argv, "bdakf:i:z:hA:P:ru:g:vC", longopts, 0);
c34b6b57 260#endif /* HAVE_NETLINK */
718e3744 261
262 if (opt == EOF)
263 break;
264
265 switch (opt)
266 {
267 case 0:
268 break;
269 case 'b':
270 batch_mode = 1;
271 case 'd':
272 daemon_mode = 1;
273 break;
6baf7bb8
DS
274 case 'a':
275 allow_delete = 1;
276 break;
718e3744 277 case 'k':
278 keep_kernel_mode = 1;
279 break;
876b8be0
PJ
280 case 'C':
281 dryrun = 1;
282 break;
718e3744 283 case 'f':
284 config_file = optarg;
285 break;
286 case 'A':
287 vty_addr = optarg;
288 break;
289 case 'i':
290 pid_file = optarg;
291 break;
b5114685
VT
292 case 'z':
293 zserv_path = optarg;
294 break;
718e3744 295 case 'P':
4fc4e7ab 296 /* Deal with atoi() returning 0 on failure, and zebra not
297 listening on zebra port... */
298 if (strcmp(optarg, "0") == 0)
299 {
300 vty_port = 0;
301 break;
302 }
718e3744 303 vty_port = atoi (optarg);
0d6b2ee2
PJ
304 if (vty_port <= 0 || vty_port > 0xffff)
305 vty_port = ZEBRA_VTY_PORT;
718e3744 306 break;
307 case 'r':
308 retain_mode = 1;
309 break;
c34b6b57 310#ifdef HAVE_NETLINK
311 case 's':
312 nl_rcvbufsize = atoi (optarg);
313 break;
314#endif /* HAVE_NETLINK */
c065230a 315 case 'u':
316 zserv_privs.user = optarg;
317 break;
318 case 'g':
319 zserv_privs.group = optarg;
320 break;
718e3744 321 case 'v':
322 print_version (progname);
323 exit (0);
324 break;
325 case 'h':
326 usage (progname, 0);
327 break;
328 default:
329 usage (progname, 1);
330 break;
331 }
332 }
333
334 /* Make master thread emulator. */
b21b19c5 335 zebrad.master = thread_master_create ();
718e3744 336
337 /* Vty related initialize. */
837d16cc 338 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
718e3744 339 cmd_init (1);
b21b19c5 340 vty_init (zebrad.master);
718e3744 341 memory_init ();
342
343 /* Zebra related initialize. */
344 zebra_init ();
345 rib_init ();
346 zebra_if_init ();
347 zebra_debug_init ();
c6ffe645 348 router_id_cmd_init ();
718e3744 349 zebra_vty_init ();
350 access_list_init ();
7514fb77 351 prefix_list_init ();
8da4e946 352#if defined (HAVE_RTADV)
cd80d74f 353 rtadv_cmd_init ();
36735ed9 354#endif
ca776988 355#ifdef HAVE_IRDP
356 irdp_init();
357#endif
244c1cdc
DS
358 /* PTM socket */
359#ifdef ZEBRA_PTM_SUPPORT
360 zebra_ptm_init();
361#endif
718e3744 362
363 /* For debug purpose. */
364 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
365
fe18ee2d
DS
366 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
367 zebra_ns_init ();
718e3744 368
718e3744 369#ifdef HAVE_SNMP
370 zebra_snmp_init ();
371#endif /* HAVE_SNMP */
372
5adc2528
AS
373#ifdef HAVE_FPM
374 zfpm_init (zebrad.master, 1, 0);
375#else
376 zfpm_init (zebrad.master, 0, 0);
377#endif
378
91b7351d
DO
379 /* Process the configuration file. Among other configuration
380 * directives we can meet those installing static routes. Such
381 * requests will not be executed immediately, but queued in
382 * zebra->ribq structure until we enter the main execution loop.
383 * The notifications from kernel will show originating PID equal
384 * to that after daemon() completes (if ever called).
385 */
320ec10a 386 vty_read_config (config_file, config_default);
718e3744 387
876b8be0
PJ
388 /* Don't start execution if we are in dry-run mode */
389 if (dryrun)
390 return(0);
391
718e3744 392 /* Clean up rib. */
7a4bb9c5 393 /* rib_weed_tables (); */
718e3744 394
395 /* Exit when zebra is working in batch mode. */
396 if (batch_mode)
397 exit (0);
398
718e3744 399 /* Daemonize. */
065de903
SH
400 if (daemon_mode && daemon (0, 0) < 0)
401 {
402 zlog_err("Zebra daemon failed: %s", strerror(errno));
403 exit (1);
404 }
718e3744 405
406 /* Output pid of zebra. */
407 pid_output (pid_file);
408
91b7351d
DO
409 /* After we have successfully acquired the pidfile, we can be sure
410 * about being the only copy of zebra process, which is submitting
411 * changes to the FIB.
412 * Clean up zebra-originated routes. The requests will be sent to OS
413 * immediately, so originating PID in notifications from kernel
414 * will be equal to the current getpid(). To know about such routes,
415 * we have to have route_read() called before.
416 */
417 if (! keep_kernel_mode)
418 rib_sweep_route ();
419
718e3744 420 /* Needed for BSD routing socket. */
421 pid = getpid ();
422
97be79f9 423 /* This must be done only after locking pidfile (bug #403). */
b5114685 424 zebra_zserv_socket_init (zserv_path);
97be79f9 425
718e3744 426 /* Make vty server socket. */
4fc4e7ab 427 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
718e3744 428
887c44a4 429 /* Print banner. */
430 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
431
b21b19c5 432 while (thread_fetch (zebrad.master, &thread))
718e3744 433 thread_call (&thread);
434
435 /* Not reached... */
e8e1946e 436 return 0;
718e3744 437}