]> git.proxmox.com Git - mirror_frr.git/blame - zebra/main.c
Fix bugreport URLs here as well.
[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
24#include "version.h"
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"
edd7c245 32#include "privs.h"
2d75d052 33#include "sigevent.h"
718e3744 34
35#include "zebra/rib.h"
36#include "zebra/zserv.h"
37#include "zebra/debug.h"
38#include "zebra/rib.h"
39
b21b19c5 40/* Zebra instance */
41struct zebra_t zebrad =
42{
43 .rtm_table_default = 0,
44};
718e3744 45
46/* process id. */
47pid_t old_pid;
48pid_t pid;
49
50/* Route retain mode flag. */
51int retain_mode = 0;
52
53/* Don't delete kernel route. */
54int keep_kernel_mode = 0;
55
56/* Command line options. */
57struct option longopts[] =
58{
59 { "batch", no_argument, NULL, 'b'},
60 { "daemon", no_argument, NULL, 'd'},
61 { "keep_kernel", no_argument, NULL, 'k'},
62 { "log_mode", no_argument, NULL, 'l'},
63 { "config_file", required_argument, NULL, 'f'},
64 { "pid_file", required_argument, NULL, 'i'},
65 { "help", no_argument, NULL, 'h'},
66 { "vty_addr", required_argument, NULL, 'A'},
67 { "vty_port", required_argument, NULL, 'P'},
68 { "retain", no_argument, NULL, 'r'},
edd7c245 69 { "user", required_argument, NULL, 'u'},
718e3744 70 { "version", no_argument, NULL, 'v'},
71 { 0 }
72};
73
edd7c245 74zebra_capabilities_t _caps_p [] =
75{
76 ZCAP_ADMIN,
77 ZCAP_SYS_ADMIN,
41908818 78 ZCAP_RAW,
edd7c245 79};
80
81/* zebra privileges to run with */
82struct zebra_privs_t zserv_privs =
83{
d81fadfd 84#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
85 .user = QUAGGA_USER,
86 .group = QUAGGA_GROUP,
edd7c245 87#endif
88#ifdef VTY_GROUP
89 .vty_group = VTY_GROUP,
90#endif
91 .caps_p = _caps_p,
92 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
93 .cap_num_i = 0
94};
95
718e3744 96/* Default configuration file path. */
97char config_current[] = DEFAULT_CONFIG_FILE;
98char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
99
100/* Process ID saved for use by init system */
101char *pid_file = PATH_ZEBRA_PID;
102
103/* Help information display. */
104static void
105usage (char *progname, int status)
106{
107 if (status != 0)
108 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
109 else
110 {
111 printf ("Usage : %s [OPTION...]\n\n\
112Daemon which manages kernel routing table management and \
113redistribution between different routing protocols.\n\n\
114-b, --batch Runs in batch mode\n\
115-d, --daemon Runs in daemon mode\n\
116-f, --config_file Set configuration file name\n\
117-i, --pid_file Set process identifier file name\n\
118-k, --keep_kernel Don't delete old routes which installed by zebra.\n\
119-l, --log_mode Set verbose log mode flag\n\
120-A, --vty_addr Set vty's bind address\n\
121-P, --vty_port Set vty's port number\n\
122-r, --retain When program terminates, retain added route by zebra.\n\
edd7c245 123-u, --user User and group to run as\n\
718e3744 124-v, --version Print program version\n\
125-h, --help Display this help and exit\n\
126\n\
127Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
128 }
129
130 exit (status);
131}
132\f
133/* SIGHUP handler. */
134void
2d75d052 135sighup (void)
718e3744 136{
137 zlog_info ("SIGHUP received");
138
139 /* Reload of config file. */
140 ;
141}
142
143/* SIGINT handler. */
144void
2d75d052 145sigint (void)
718e3744 146{
147 /* Decrared in rib.c */
148 void rib_close ();
149
150 zlog_info ("Terminating on signal");
151
152 if (!retain_mode)
153 rib_close ();
154
155 exit (0);
156}
157
158/* SIGUSR1 handler. */
159void
2d75d052 160sigusr1 (void)
718e3744 161{
162 zlog_rotate (NULL);
163}
164
2d75d052 165struct quagga_signal_t zebra_signals[] =
718e3744 166{
2d75d052 167 {
168 .signal = SIGHUP,
169 .handler = &sighup,
170 },
171 {
172 .signal = SIGUSR1,
173 .handler = &sigusr1,
174 },
175 {
176 .signal = SIGINT,
177 .handler = &sigusr1,
178 },
179};
718e3744 180\f
181/* Main startup routine. */
182int
183main (int argc, char **argv)
184{
185 char *p;
186 char *vty_addr = NULL;
4fc4e7ab 187 int vty_port = ZEBRA_VTY_PORT;
718e3744 188 int batch_mode = 0;
189 int daemon_mode = 0;
190 char *config_file = NULL;
191 char *progname;
192 struct thread thread;
193 void rib_weed_tables ();
194 void zebra_vty_init ();
195
196 /* Set umask before anything for security */
197 umask (0027);
198
199 /* preserve my name */
200 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
201
202 zlog_default = openzlog (progname, ZLOG_STDOUT, ZLOG_ZEBRA,
203 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
204
205 while (1)
206 {
207 int opt;
208
96735eea 209 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:v", longopts, 0);
718e3744 210
211 if (opt == EOF)
212 break;
213
214 switch (opt)
215 {
216 case 0:
217 break;
218 case 'b':
219 batch_mode = 1;
220 case 'd':
221 daemon_mode = 1;
222 break;
223 case 'k':
224 keep_kernel_mode = 1;
225 break;
226 case 'l':
227 /* log_mode = 1; */
228 break;
229 case 'f':
230 config_file = optarg;
231 break;
232 case 'A':
233 vty_addr = optarg;
234 break;
235 case 'i':
236 pid_file = optarg;
237 break;
238 case 'P':
4fc4e7ab 239 /* Deal with atoi() returning 0 on failure, and zebra not
240 listening on zebra port... */
241 if (strcmp(optarg, "0") == 0)
242 {
243 vty_port = 0;
244 break;
245 }
718e3744 246 vty_port = atoi (optarg);
4fc4e7ab 247 vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT);
718e3744 248 break;
249 case 'r':
250 retain_mode = 1;
251 break;
edd7c245 252 case 'u':
253 zserv_privs.user = zserv_privs.group = optarg;
254 break;
718e3744 255 case 'v':
256 print_version (progname);
257 exit (0);
258 break;
259 case 'h':
260 usage (progname, 0);
261 break;
262 default:
263 usage (progname, 1);
264 break;
265 }
266 }
267
268 /* Make master thread emulator. */
b21b19c5 269 zebrad.master = thread_master_create ();
718e3744 270
edd7c245 271 /* privs initialise */
272 zprivs_init (&zserv_privs);
273
718e3744 274 /* Vty related initialize. */
2d75d052 275 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
718e3744 276 cmd_init (1);
b21b19c5 277 vty_init (zebrad.master);
718e3744 278 memory_init ();
279
280 /* Zebra related initialize. */
281 zebra_init ();
282 rib_init ();
283 zebra_if_init ();
284 zebra_debug_init ();
285 zebra_vty_init ();
286 access_list_init ();
287 rtadv_init ();
288
289 /* For debug purpose. */
290 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
291
292 /* Make kernel routing socket. */
293 kernel_init ();
294 interface_list ();
295 route_read ();
296
297 /* Sort VTY commands. */
298 sort_node ();
299
300#ifdef HAVE_SNMP
301 zebra_snmp_init ();
302#endif /* HAVE_SNMP */
303
304 /* Clean up self inserted route. */
305 if (! keep_kernel_mode)
306 rib_sweep_route ();
307
308 /* Configuration file read*/
309 vty_read_config (config_file, config_current, config_default);
310
311 /* Clean up rib. */
312 rib_weed_tables ();
313
314 /* Exit when zebra is working in batch mode. */
315 if (batch_mode)
316 exit (0);
317
318 /* Needed for BSD routing socket. */
319 old_pid = getpid ();
320
321 /* Daemonize. */
322 if (daemon_mode)
323 daemon (0, 0);
324
325 /* Output pid of zebra. */
326 pid_output (pid_file);
327
328 /* Needed for BSD routing socket. */
329 pid = getpid ();
330
331 /* Make vty server socket. */
4fc4e7ab 332 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
718e3744 333
b21b19c5 334 while (thread_fetch (zebrad.master, &thread))
718e3744 335 thread_call (&thread);
336
337 /* Not reached... */
338 exit (0);
339}