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