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