[daemons] Sanity check port number arguments before use
[jleu-quagga.git] / zebra / main.c
blob61750f1d07d67c0997eae1dd906a522871a28440
1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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.
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.
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.
22 #include <zebra.h>
24 #include <lib/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 "plist.h"
33 #include "privs.h"
34 #include "sigevent.h"
36 #include "zebra/rib.h"
37 #include "zebra/zserv.h"
38 #include "zebra/debug.h"
39 #include "zebra/router-id.h"
40 #include "zebra/irdp.h"
41 #include "zebra/rtadv.h"
43 /* Zebra instance */
44 struct zebra_t zebrad =
46 .rtm_table_default = 0,
49 /* process id. */
50 pid_t pid;
52 /* Pacify zclient.o in libzebra, which expects this variable. */
53 struct thread_master *master;
55 /* Route retain mode flag. */
56 int retain_mode = 0;
58 /* Don't delete kernel route. */
59 int keep_kernel_mode = 0;
61 #ifdef HAVE_NETLINK
62 /* Receive buffer size for netlink socket */
63 u_int32_t nl_rcvbufsize = 0;
64 #endif /* HAVE_NETLINK */
66 /* Command line options. */
67 struct option longopts[] =
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'},
79 { "dryrun", no_argument, NULL, 'C'},
80 #ifdef HAVE_NETLINK
81 { "nl-bufsize", required_argument, NULL, 's'},
82 #endif /* HAVE_NETLINK */
83 { "user", required_argument, NULL, 'u'},
84 { "group", required_argument, NULL, 'g'},
85 { "version", no_argument, NULL, 'v'},
86 { 0 }
89 zebra_capabilities_t _caps_p [] =
91 ZCAP_NET_ADMIN,
92 ZCAP_SYS_ADMIN,
93 ZCAP_NET_RAW,
96 /* zebra privileges to run with */
97 struct zebra_privs_t zserv_privs =
99 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
100 .user = QUAGGA_USER,
101 .group = QUAGGA_GROUP,
102 #endif
103 #ifdef VTY_GROUP
104 .vty_group = VTY_GROUP,
105 #endif
106 .caps_p = _caps_p,
107 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
108 .cap_num_i = 0
111 /* Default configuration file path. */
112 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
114 /* Process ID saved for use by init system */
115 const char *pid_file = PATH_ZEBRA_PID;
117 /* Help information display. */
118 static void
119 usage (char *progname, int status)
121 if (status != 0)
122 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
123 else
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"\
132 "-k, --keep_kernel Don't delete old routes which installed by "\
133 "zebra.\n"\
134 "-l, --log_mode Set verbose log mode flag\n"\
135 "-C, --dryrun Check configuration for validity and exit\n"\
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"\
140 "-u, --user User to run as\n"\
141 "-g, --group Group to run as\n", progname);
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);
151 exit (status);
154 /* SIGHUP handler. */
155 static void
156 sighup (void)
158 zlog_info ("SIGHUP received");
160 /* Reload of config file. */
164 /* SIGINT handler. */
165 static void
166 sigint (void)
168 zlog_notice ("Terminating on signal");
170 if (!retain_mode)
171 rib_close ();
172 #ifdef HAVE_IRDP
173 irdp_finish();
174 #endif
176 exit (0);
179 /* SIGUSR1 handler. */
180 static void
181 sigusr1 (void)
183 zlog_rotate (NULL);
186 struct quagga_signal_t zebra_signals[] =
189 .signal = SIGHUP,
190 .handler = &sighup,
193 .signal = SIGUSR1,
194 .handler = &sigusr1,
197 .signal = SIGINT,
198 .handler = &sigint,
201 .signal = SIGTERM,
202 .handler = &sigint,
206 /* Main startup routine. */
208 main (int argc, char **argv)
210 char *p;
211 char *vty_addr = NULL;
212 int vty_port = ZEBRA_VTY_PORT;
213 int dryrun = 0;
214 int batch_mode = 0;
215 int daemon_mode = 0;
216 char *config_file = NULL;
217 char *progname;
218 struct thread thread;
220 /* Set umask before anything for security */
221 umask (0027);
223 /* preserve my name */
224 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
226 zlog_default = openzlog (progname, ZLOG_ZEBRA,
227 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
229 while (1)
231 int opt;
233 #ifdef HAVE_NETLINK
234 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:vs:C", longopts, 0);
235 #else
236 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:vC", longopts, 0);
237 #endif /* HAVE_NETLINK */
239 if (opt == EOF)
240 break;
242 switch (opt)
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 'C':
255 dryrun = 1;
256 break;
257 case 'l':
258 /* log_mode = 1; */
259 break;
260 case 'f':
261 config_file = optarg;
262 break;
263 case 'A':
264 vty_addr = optarg;
265 break;
266 case 'i':
267 pid_file = optarg;
268 break;
269 case 'P':
270 /* Deal with atoi() returning 0 on failure, and zebra not
271 listening on zebra port... */
272 if (strcmp(optarg, "0") == 0)
274 vty_port = 0;
275 break;
277 vty_port = atoi (optarg);
278 if (vty_port <= 0 || vty_port > 0xffff)
279 vty_port = ZEBRA_VTY_PORT;
280 break;
281 case 'r':
282 retain_mode = 1;
283 break;
284 #ifdef HAVE_NETLINK
285 case 's':
286 nl_rcvbufsize = atoi (optarg);
287 break;
288 #endif /* HAVE_NETLINK */
289 case 'u':
290 zserv_privs.user = optarg;
291 break;
292 case 'g':
293 zserv_privs.group = optarg;
294 break;
295 case 'v':
296 print_version (progname);
297 exit (0);
298 break;
299 case 'h':
300 usage (progname, 0);
301 break;
302 default:
303 usage (progname, 1);
304 break;
308 /* Make master thread emulator. */
309 zebrad.master = thread_master_create ();
311 /* privs initialise */
312 zprivs_init (&zserv_privs);
314 /* Vty related initialize. */
315 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
316 cmd_init (1);
317 vty_init (zebrad.master);
318 memory_init ();
320 /* Zebra related initialize. */
321 zebra_init ();
322 rib_init ();
323 zebra_if_init ();
324 zebra_debug_init ();
325 router_id_init();
326 zebra_vty_init ();
327 access_list_init ();
328 prefix_list_init ();
329 rtadv_init ();
330 #ifdef HAVE_IRDP
331 irdp_init();
332 #endif
334 /* For debug purpose. */
335 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
337 /* Make kernel routing socket. */
338 kernel_init ();
339 interface_list ();
340 route_read ();
342 /* Sort VTY commands. */
343 sort_node ();
345 #ifdef HAVE_SNMP
346 zebra_snmp_init ();
347 #endif /* HAVE_SNMP */
349 /* Process the configuration file. Among other configuration
350 * directives we can meet those installing static routes. Such
351 * requests will not be executed immediately, but queued in
352 * zebra->ribq structure until we enter the main execution loop.
353 * The notifications from kernel will show originating PID equal
354 * to that after daemon() completes (if ever called).
356 vty_read_config (config_file, config_default);
358 /* Don't start execution if we are in dry-run mode */
359 if (dryrun)
360 return(0);
362 /* Clean up rib. */
363 rib_weed_tables ();
365 /* Exit when zebra is working in batch mode. */
366 if (batch_mode)
367 exit (0);
369 /* Daemonize. */
370 if (daemon_mode)
371 daemon (0, 0);
373 /* Output pid of zebra. */
374 pid_output (pid_file);
376 /* After we have successfully acquired the pidfile, we can be sure
377 * about being the only copy of zebra process, which is submitting
378 * changes to the FIB.
379 * Clean up zebra-originated routes. The requests will be sent to OS
380 * immediately, so originating PID in notifications from kernel
381 * will be equal to the current getpid(). To know about such routes,
382 * we have to have route_read() called before.
384 if (! keep_kernel_mode)
385 rib_sweep_route ();
387 /* Needed for BSD routing socket. */
388 pid = getpid ();
390 /* Make vty server socket. */
391 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
393 /* Print banner. */
394 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
396 while (thread_fetch (zebrad.master, &thread))
397 thread_call (&thread);
399 /* Not reached... */
400 return 0;