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