1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
24 #include <lib/version.h>
36 #include "zebra/rib.h"
37 #include "zebra/zserv.h"
38 #include "zebra/debug.h"
39 #include "zebra/router-id.h"
40 #include "zebra/irdp.h"
41 #include "zebra/rtadv.h"
44 struct zebra_t zebrad
=
46 .rtm_table_default
= 0,
52 /* Pacify zclient.o in libzebra, which expects this variable. */
53 struct thread_master
*master
;
55 /* Route retain mode flag. */
58 /* Don't delete kernel route. */
59 int keep_kernel_mode
= 0;
62 /* Receive buffer size for netlink socket */
63 u_int32_t nl_rcvbufsize
= 0;
64 #endif /* HAVE_NETLINK */
66 /* Command line options. */
67 struct option longopts
[] =
69 { "batch", no_argument
, NULL
, 'b'},
70 { "daemon", no_argument
, NULL
, 'd'},
71 { "keep_kernel", no_argument
, NULL
, 'k'},
72 { "config_file", required_argument
, NULL
, 'f'},
73 { "pid_file", required_argument
, NULL
, 'i'},
74 { "help", no_argument
, NULL
, 'h'},
75 { "vty_addr", required_argument
, NULL
, 'A'},
76 { "vty_port", required_argument
, NULL
, 'P'},
77 { "retain", no_argument
, NULL
, 'r'},
78 { "dryrun", no_argument
, NULL
, 'C'},
80 { "nl-bufsize", required_argument
, NULL
, 's'},
81 #endif /* HAVE_NETLINK */
82 { "user", required_argument
, NULL
, 'u'},
83 { "group", required_argument
, NULL
, 'g'},
84 { "version", no_argument
, NULL
, 'v'},
88 zebra_capabilities_t _caps_p
[] =
95 /* zebra privileges to run with */
96 struct zebra_privs_t zserv_privs
=
98 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
100 .group
= QUAGGA_GROUP
,
103 .vty_group
= VTY_GROUP
,
106 .cap_num_p
= sizeof(_caps_p
)/sizeof(_caps_p
[0]),
110 /* Default configuration file path. */
111 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
113 /* Process ID saved for use by init system */
114 const char *pid_file
= PATH_ZEBRA_PID
;
116 /* Help information display. */
118 usage (char *progname
, int status
)
121 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
124 printf ("Usage : %s [OPTION...]\n\n"\
125 "Daemon which manages kernel routing table management and "\
126 "redistribution between different routing protocols.\n\n"\
127 "-b, --batch Runs in batch mode\n"\
128 "-d, --daemon Runs in daemon mode\n"\
129 "-f, --config_file Set configuration file name\n"\
130 "-i, --pid_file Set process identifier file name\n"\
131 "-k, --keep_kernel Don't delete old routes which installed by "\
133 "-C, --dryrun Check configuration for validity and exit\n"\
134 "-A, --vty_addr Set vty's bind address\n"\
135 "-P, --vty_port Set vty's port number\n"\
136 "-r, --retain When program terminates, retain added route "\
138 "-u, --user User to run as\n"\
139 "-g, --group Group to run as\n", progname
);
141 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
142 #endif /* HAVE_NETLINK */
143 printf ("-v, --version Print program version\n"\
144 "-h, --help Display this help and exit\n"\
146 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS
);
152 /* SIGHUP handler. */
156 zlog_info ("SIGHUP received");
158 /* Reload of config file. */
162 /* SIGINT handler. */
166 zlog_notice ("Terminating on signal");
177 /* SIGUSR1 handler. */
184 struct quagga_signal_t zebra_signals
[] =
204 /* Main startup routine. */
206 main (int argc
, char **argv
)
209 char *vty_addr
= NULL
;
210 int vty_port
= ZEBRA_VTY_PORT
;
214 char *config_file
= NULL
;
216 struct thread thread
;
218 /* Set umask before anything for security */
221 /* preserve my name */
222 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
224 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
,
225 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
232 opt
= getopt_long (argc
, argv
, "bdkf:i:hA:P:ru:g:vs:C", longopts
, 0);
234 opt
= getopt_long (argc
, argv
, "bdkf:i:hA:P:ru:g:vC", longopts
, 0);
235 #endif /* HAVE_NETLINK */
250 keep_kernel_mode
= 1;
256 config_file
= optarg
;
265 /* Deal with atoi() returning 0 on failure, and zebra not
266 listening on zebra port... */
267 if (strcmp(optarg
, "0") == 0)
272 vty_port
= atoi (optarg
);
273 if (vty_port
<= 0 || vty_port
> 0xffff)
274 vty_port
= ZEBRA_VTY_PORT
;
281 nl_rcvbufsize
= atoi (optarg
);
283 #endif /* HAVE_NETLINK */
285 zserv_privs
.user
= optarg
;
288 zserv_privs
.group
= optarg
;
291 print_version (progname
);
303 /* Make master thread emulator. */
304 zebrad
.master
= thread_master_create ();
306 /* privs initialise */
307 zprivs_init (&zserv_privs
);
309 /* Vty related initialize. */
310 signal_init (zebrad
.master
, Q_SIGC(zebra_signals
), zebra_signals
);
312 vty_init (zebrad
.master
);
315 /* Zebra related initialize. */
329 /* For debug purpose. */
330 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
332 /* Make kernel routing socket. */
337 /* Sort VTY commands. */
342 #endif /* HAVE_SNMP */
344 /* Process the configuration file. Among other configuration
345 * directives we can meet those installing static routes. Such
346 * requests will not be executed immediately, but queued in
347 * zebra->ribq structure until we enter the main execution loop.
348 * The notifications from kernel will show originating PID equal
349 * to that after daemon() completes (if ever called).
351 vty_read_config (config_file
, config_default
);
353 /* Don't start execution if we are in dry-run mode */
360 /* Exit when zebra is working in batch mode. */
365 if (daemon_mode
&& daemon (0, 0) < 0)
367 zlog_err("Zebra daemon failed: %s", strerror(errno
));
371 /* Output pid of zebra. */
372 pid_output (pid_file
);
374 /* After we have successfully acquired the pidfile, we can be sure
375 * about being the only copy of zebra process, which is submitting
376 * changes to the FIB.
377 * Clean up zebra-originated routes. The requests will be sent to OS
378 * immediately, so originating PID in notifications from kernel
379 * will be equal to the current getpid(). To know about such routes,
380 * we have to have route_read() called before.
382 if (! keep_kernel_mode
)
385 /* Needed for BSD routing socket. */
388 /* This must be done only after locking pidfile (bug #403). */
389 zebra_zserv_socket_init ();
391 /* Make vty server socket. */
392 vty_serv_sock (vty_addr
, vty_port
, ZEBRA_VTYSH_PATH
);
395 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION
, vty_port
);
397 while (thread_fetch (zebrad
.master
, &thread
))
398 thread_call (&thread
);