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