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