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