blob: 70a1a3a650d9131d9f22e942b3d2c042122981b2 [file] [log] [blame]
Paul Jakma457eb9a2006-07-27 19:59:58 +00001/* main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
4 * GNU Zebra is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2, or (at your option) any
7 * later version.
8 *
9 * GNU Zebra is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with GNU Zebra; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 */
19
20#include <zebra.h>
21
22#include <lib/version.h>
23#include "getopt.h"
24#include "command.h"
25#include "thread.h"
26#include "filter.h"
27#include "memory.h"
28#include "prefix.h"
29#include "log.h"
30#include "privs.h"
31#include "sigevent.h"
32
33#include "zebra/rib.h"
34#include "zebra/zserv.h"
35#include "zebra/debug.h"
36#include "zebra/router-id.h"
37#include "zebra/interface.h"
38
39/* Zebra instance */
40struct zebra_t zebrad =
41{
42 .rtm_table_default = 0,
43};
44
45/* process id. */
Paul Jakma457eb9a2006-07-27 19:59:58 +000046pid_t pid;
47
48/* zebra_rib's workqueue hold time. Private export for use by test code only */
49extern int rib_process_hold_time;
50
51/* Pacify zclient.o in libzebra, which expects this variable. */
52struct thread_master *master;
53
54/* Command line options. */
55struct option longopts[] =
56{
57 { "batch", no_argument, NULL, 'b'},
58 { "daemon", no_argument, NULL, 'd'},
Paul Jakma457eb9a2006-07-27 19:59:58 +000059 { "config_file", required_argument, NULL, 'f'},
60 { "help", no_argument, NULL, 'h'},
61 { "vty_addr", required_argument, NULL, 'A'},
62 { "vty_port", required_argument, NULL, 'P'},
63 { "version", no_argument, NULL, 'v'},
64 { "rib_hold", required_argument, NULL, 'r'},
65 { 0 }
66};
67
68zebra_capabilities_t _caps_p [] =
69{
70 ZCAP_NET_ADMIN,
71 ZCAP_SYS_ADMIN,
72 ZCAP_NET_RAW,
73};
74
75/* Default configuration file path. */
76char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
77
78/* Process ID saved for use by init system */
79const char *pid_file = PATH_ZEBRA_PID;
80
81/* Help information display. */
82static void
83usage (char *progname, int status)
84{
85 if (status != 0)
86 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
87 else
88 {
89 printf ("Usage : %s [OPTION...]\n\n"\
90 "Daemon which manages kernel routing table management and "\
91 "redistribution between different routing protocols.\n\n"\
92 "-b, --batch Runs in batch mode\n"\
93 "-d, --daemon Runs in daemon mode\n"\
94 "-f, --config_file Set configuration file name\n"\
Paul Jakma457eb9a2006-07-27 19:59:58 +000095 "-A, --vty_addr Set vty's bind address\n"\
96 "-P, --vty_port Set vty's port number\n"\
97 "-r, --rib_hold Set rib-queue hold time\n"\
98 "-v, --version Print program version\n"\
99 "-h, --help Display this help and exit\n"\
100 "\n"\
101 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
102 }
103
104 exit (status);
105}
106
107static unsigned int test_ifindex = 0;
108
109/* testrib commands */
110DEFUN (test_interface_state,
111 test_interface_state_cmd,
112 "state (up|down)",
113 "configure interface\n"
114 "up\n"
115 "down\n")
116{
117 struct interface *ifp;
118 if (argc < 1)
119 return CMD_WARNING;
120
121 ifp = vty->index;
122 if (ifp->ifindex == IFINDEX_INTERNAL)
123 {
124 ifp->ifindex = ++test_ifindex;
125 ifp->mtu = 1500;
126 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
127 }
128
129 switch (argv[0][0])
130 {
131 case 'u':
132 SET_FLAG (ifp->flags, IFF_UP);
133 if_add_update (ifp);
134 printf ("up\n");
135 break;
136 case 'd':
137 UNSET_FLAG (ifp->flags, IFF_UP);
138 if_delete_update (ifp);
139 printf ("down\n");
140 break;
141 default:
142 return CMD_WARNING;
143 }
144 return CMD_SUCCESS;
145}
146
147static void
148test_cmd_init (void)
149{
150 install_element (INTERFACE_NODE, &test_interface_state_cmd);
151}
152
153/* SIGHUP handler. */
154static void
155sighup (void)
156{
157 zlog_info ("SIGHUP received");
158
159 /* Reload of config file. */
160 ;
161}
162
163/* SIGINT handler. */
164static void
165sigint (void)
166{
167 zlog_notice ("Terminating on signal");
168
169 exit (0);
170}
171
172/* SIGUSR1 handler. */
173static void
174sigusr1 (void)
175{
176 zlog_rotate (NULL);
177}
178
179struct quagga_signal_t zebra_signals[] =
180{
181 {
182 .signal = SIGHUP,
183 .handler = &sighup,
184 },
185 {
186 .signal = SIGUSR1,
187 .handler = &sigusr1,
188 },
189 {
190 .signal = SIGINT,
191 .handler = &sigint,
192 },
193 {
194 .signal = SIGTERM,
195 .handler = &sigint,
196 },
197};
198
199/* Main startup routine. */
200int
201main (int argc, char **argv)
202{
203 char *p;
204 char *vty_addr = NULL;
205 int vty_port = 0;
206 int batch_mode = 0;
207 int daemon_mode = 0;
208 char *config_file = NULL;
209 char *progname;
210 struct thread thread;
211
212 /* Set umask before anything for security */
213 umask (0027);
214
215 /* preserve my name */
216 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
217
218 zlog_default = openzlog (progname, ZLOG_ZEBRA,
219 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
220
221 while (1)
222 {
223 int opt;
224
Jeremy Jacksonc77cffd2008-12-28 12:57:42 -0500225 opt = getopt_long (argc, argv, "bdf:hA:P:r:v", longopts, 0);
Paul Jakma457eb9a2006-07-27 19:59:58 +0000226
227 if (opt == EOF)
228 break;
229
230 switch (opt)
231 {
232 case 0:
233 break;
234 case 'b':
235 batch_mode = 1;
236 case 'd':
237 daemon_mode = 1;
238 break;
Paul Jakma457eb9a2006-07-27 19:59:58 +0000239 case 'f':
240 config_file = optarg;
241 break;
242 case 'A':
243 vty_addr = optarg;
244 break;
245 case 'P':
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 }
253 vty_port = atoi (optarg);
254 break;
255 case 'r':
256 rib_process_hold_time = atoi(optarg);
257 break;
258 case 'v':
259 print_version (progname);
260 exit (0);
261 break;
262 case 'h':
263 usage (progname, 0);
264 break;
265 default:
266 usage (progname, 1);
267 break;
268 }
269 }
270
271 /* port and conf file mandatory */
272 if (!vty_port || !config_file)
Paul Jakma302d53f2009-08-06 11:48:48 +0100273 {
274 fprintf (stderr, "Error: --vty_port and --config_file arguments"
275 " are both required\n");
276 usage (progname, 1);
277 }
Paul Jakma457eb9a2006-07-27 19:59:58 +0000278
279 /* Make master thread emulator. */
280 zebrad.master = thread_master_create ();
281
282 /* Vty related initialize. */
283 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
284 cmd_init (1);
285 vty_init (zebrad.master);
286 memory_init ();
287 if_init();
288 zebra_debug_init ();
289 zebra_if_init ();
290 test_cmd_init ();
291
292 /* Zebra related initialize. */
293 rib_init ();
294 access_list_init ();
295
296 /* Make kernel routing socket. */
297 kernel_init ();
298 route_read ();
299 zebra_vty_init();
300
301 /* Sort VTY commands. */
302 sort_node ();
303
304 /* Configuration file read*/
305 vty_read_config (config_file, config_default);
306
307 /* Clean up rib. */
308 rib_weed_tables ();
309
310 /* Exit when zebra is working in batch mode. */
311 if (batch_mode)
312 exit (0);
313
Paul Jakma457eb9a2006-07-27 19:59:58 +0000314 /* Daemonize. */
Stephen Hemminger065de902009-08-07 11:13:49 -0700315 if (daemon_mode && daemon (0, 0) < 0)
316 {
317 perror("daemon start failed");
318 exit (1);
319 }
Paul Jakma457eb9a2006-07-27 19:59:58 +0000320
321 /* Needed for BSD routing socket. */
322 pid = getpid ();
323
324 /* Make vty server socket. */
325 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
326
327 /* Print banner. */
328 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
329
330 while (thread_fetch (zebrad.master, &thread))
331 thread_call (&thread);
332
333 /* Not reached... */
334 return 0;
335}