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