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