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 { "log_mode", no_argument
, NULL
, 'l'},
73 { "config_file", required_argument
, NULL
, 'f'},
74 { "pid_file", required_argument
, NULL
, 'i'},
75 { "help", no_argument
, NULL
, 'h'},
76 { "vty_addr", required_argument
, NULL
, 'A'},
77 { "vty_port", required_argument
, NULL
, 'P'},
78 { "retain", no_argument
, NULL
, 'r'},
79 { "dryrun", no_argument
, NULL
, 'C'},
81 { "nl-bufsize", required_argument
, NULL
, 's'},
82 #endif /* HAVE_NETLINK */
83 { "user", required_argument
, NULL
, 'u'},
84 { "group", required_argument
, NULL
, 'g'},
85 { "version", no_argument
, NULL
, 'v'},
89 zebra_capabilities_t _caps_p
[] =
96 /* zebra privileges to run with */
97 struct zebra_privs_t zserv_privs
=
99 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
101 .group
= QUAGGA_GROUP
,
104 .vty_group
= VTY_GROUP
,
107 .cap_num_p
= sizeof(_caps_p
)/sizeof(_caps_p
[0]),
111 /* Default configuration file path. */
112 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
114 /* Process ID saved for use by init system */
115 const char *pid_file
= PATH_ZEBRA_PID
;
117 /* Help information display. */
119 usage (char *progname
, int status
)
122 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
125 printf ("Usage : %s [OPTION...]\n\n"\
126 "Daemon which manages kernel routing table management and "\
127 "redistribution between different routing protocols.\n\n"\
128 "-b, --batch Runs in batch mode\n"\
129 "-d, --daemon Runs in daemon mode\n"\
130 "-f, --config_file Set configuration file name\n"\
131 "-i, --pid_file Set process identifier file name\n"\
132 "-k, --keep_kernel Don't delete old routes which installed by "\
134 "-l, --log_mode Set verbose log mode flag\n"\
135 "-C, --dryrun Check configuration for validity and exit\n"\
136 "-A, --vty_addr Set vty's bind address\n"\
137 "-P, --vty_port Set vty's port number\n"\
138 "-r, --retain When program terminates, retain added route "\
140 "-u, --user User to run as\n"\
141 "-g, --group Group to run as\n", progname
);
143 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
144 #endif /* HAVE_NETLINK */
145 printf ("-v, --version Print program version\n"\
146 "-h, --help Display this help and exit\n"\
148 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS
);
154 /* SIGHUP handler. */
158 zlog_info ("SIGHUP received");
160 /* Reload of config file. */
164 /* SIGINT handler. */
168 zlog_notice ("Terminating on signal");
179 /* SIGUSR1 handler. */
186 struct quagga_signal_t zebra_signals
[] =
206 /* Main startup routine. */
208 main (int argc
, char **argv
)
211 char *vty_addr
= NULL
;
212 int vty_port
= ZEBRA_VTY_PORT
;
216 char *config_file
= NULL
;
218 struct thread thread
;
220 /* Set umask before anything for security */
223 /* preserve my name */
224 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
226 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
,
227 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
234 opt
= getopt_long (argc
, argv
, "bdklf:i:hA:P:ru:g:vs:C", longopts
, 0);
236 opt
= getopt_long (argc
, argv
, "bdklf:i:hA:P:ru:g:vC", longopts
, 0);
237 #endif /* HAVE_NETLINK */
252 keep_kernel_mode
= 1;
261 config_file
= optarg
;
270 /* Deal with atoi() returning 0 on failure, and zebra not
271 listening on zebra port... */
272 if (strcmp(optarg
, "0") == 0)
277 vty_port
= atoi (optarg
);
278 if (vty_port
<= 0 || vty_port
> 0xffff)
279 vty_port
= ZEBRA_VTY_PORT
;
286 nl_rcvbufsize
= atoi (optarg
);
288 #endif /* HAVE_NETLINK */
290 zserv_privs
.user
= optarg
;
293 zserv_privs
.group
= optarg
;
296 print_version (progname
);
308 /* Make master thread emulator. */
309 zebrad
.master
= thread_master_create ();
311 /* privs initialise */
312 zprivs_init (&zserv_privs
);
314 /* Vty related initialize. */
315 signal_init (zebrad
.master
, Q_SIGC(zebra_signals
), zebra_signals
);
317 vty_init (zebrad
.master
);
320 /* Zebra related initialize. */
334 /* For debug purpose. */
335 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
337 /* Make kernel routing socket. */
342 /* Sort VTY commands. */
347 #endif /* HAVE_SNMP */
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).
356 vty_read_config (config_file
, config_default
);
358 /* Don't start execution if we are in dry-run mode */
365 /* Exit when zebra is working in batch mode. */
373 /* Output pid of zebra. */
374 pid_output (pid_file
);
376 /* After we have successfully acquired the pidfile, we can be sure
377 * about being the only copy of zebra process, which is submitting
378 * changes to the FIB.
379 * Clean up zebra-originated routes. The requests will be sent to OS
380 * immediately, so originating PID in notifications from kernel
381 * will be equal to the current getpid(). To know about such routes,
382 * we have to have route_read() called before.
384 if (! keep_kernel_mode
)
387 /* Needed for BSD routing socket. */
390 /* Make vty server socket. */
391 vty_serv_sock (vty_addr
, vty_port
, ZEBRA_VTYSH_PATH
);
394 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION
, vty_port
);
396 while (thread_fetch (zebrad
.master
, &thread
))
397 thread_call (&thread
);