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