]>
Commit | Line | Data |
---|---|---|
457eb9af PJ |
1 | /* 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" | |
4a1ab8e4 | 28 | #include "zebra_memory.h" |
fc7948fa | 29 | #include "memory_vty.h" |
457eb9af PJ |
30 | #include "prefix.h" |
31 | #include "log.h" | |
32 | #include "privs.h" | |
33 | #include "sigevent.h" | |
b72ede27 | 34 | #include "vrf.h" |
457eb9af PJ |
35 | |
36 | #include "zebra/rib.h" | |
7c551956 | 37 | #include "zebra/zebra_ns.h" |
457eb9af | 38 | #include "zebra/zserv.h" |
7c551956 | 39 | #include "zebra/zebra_vrf.h" |
457eb9af PJ |
40 | #include "zebra/debug.h" |
41 | #include "zebra/router-id.h" | |
42 | #include "zebra/interface.h" | |
43 | ||
44 | /* Zebra instance */ | |
45 | struct zebra_t zebrad = | |
46 | { | |
47 | .rtm_table_default = 0, | |
48 | }; | |
49 | ||
50 | /* process id. */ | |
457eb9af PJ |
51 | pid_t pid; |
52 | ||
6baf7bb8 DS |
53 | /* Allow non-quagga entities to delete quagga routes */ |
54 | int allow_delete = 0; | |
55 | ||
457eb9af PJ |
56 | /* zebra_rib's workqueue hold time. Private export for use by test code only */ |
57 | extern int rib_process_hold_time; | |
58 | ||
55c72803 | 59 | /* Pacify zclient.o in libfrr, which expects this variable. */ |
457eb9af PJ |
60 | struct thread_master *master; |
61 | ||
62 | /* Command line options. */ | |
63 | struct option longopts[] = | |
64 | { | |
6baf7bb8 DS |
65 | { "batch", no_argument, NULL, 'b'}, |
66 | { "daemon", no_argument, NULL, 'd'}, | |
67 | { "allow_delete", no_argument, NULL, 'a'}, | |
68 | { "config_file", required_argument, NULL, 'f'}, | |
69 | { "help", no_argument, NULL, 'h'}, | |
70 | { "vty_addr", required_argument, NULL, 'A'}, | |
71 | { "vty_port", required_argument, NULL, 'P'}, | |
72 | { "version", no_argument, NULL, 'v'}, | |
73 | { "rib_hold", required_argument, NULL, 'r'}, | |
457eb9af PJ |
74 | { 0 } |
75 | }; | |
76 | ||
77 | zebra_capabilities_t _caps_p [] = | |
78 | { | |
79 | ZCAP_NET_ADMIN, | |
80 | ZCAP_SYS_ADMIN, | |
81 | ZCAP_NET_RAW, | |
82 | }; | |
83 | ||
84 | /* Default configuration file path. */ | |
85 | char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE; | |
86 | ||
87 | /* Process ID saved for use by init system */ | |
eb05883f | 88 | const char *pid_file = "testzebra.pid"; |
457eb9af PJ |
89 | |
90 | /* Help information display. */ | |
91 | static void | |
92 | usage (char *progname, int status) | |
93 | { | |
94 | if (status != 0) | |
95 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); | |
96 | else | |
97 | { | |
98 | printf ("Usage : %s [OPTION...]\n\n"\ | |
99 | "Daemon which manages kernel routing table management and "\ | |
100 | "redistribution between different routing protocols.\n\n"\ | |
101 | "-b, --batch Runs in batch mode\n"\ | |
102 | "-d, --daemon Runs in daemon mode\n"\ | |
4487fc74 | 103 | "-a, --allow_delete Allow other processes to delete zebra routes\n" \ |
457eb9af | 104 | "-f, --config_file Set configuration file name\n"\ |
457eb9af PJ |
105 | "-A, --vty_addr Set vty's bind address\n"\ |
106 | "-P, --vty_port Set vty's port number\n"\ | |
107 | "-r, --rib_hold Set rib-queue hold time\n"\ | |
108 | "-v, --version Print program version\n"\ | |
109 | "-h, --help Display this help and exit\n"\ | |
110 | "\n"\ | |
b2f36157 | 111 | "Report bugs to %s\n", progname, FRR_BUG_ADDRESS); |
457eb9af PJ |
112 | } |
113 | ||
114 | exit (status); | |
115 | } | |
6b0655a2 | 116 | |
b892f1dd | 117 | static ifindex_t test_ifindex = 0; |
457eb9af PJ |
118 | |
119 | /* testrib commands */ | |
120 | DEFUN (test_interface_state, | |
121 | test_interface_state_cmd, | |
6147e2c6 | 122 | "state <up|down>", |
457eb9af PJ |
123 | "configure interface\n" |
124 | "up\n" | |
125 | "down\n") | |
126 | { | |
7c022376 | 127 | int idx_up_down = 1; |
3ddccf18 | 128 | VTY_DECLVAR_CONTEXT (interface, ifp); |
457eb9af | 129 | |
457eb9af PJ |
130 | if (ifp->ifindex == IFINDEX_INTERNAL) |
131 | { | |
132 | ifp->ifindex = ++test_ifindex; | |
133 | ifp->mtu = 1500; | |
134 | ifp->flags = IFF_BROADCAST|IFF_MULTICAST; | |
135 | } | |
136 | ||
7c022376 | 137 | switch (argv[idx_up_down]->arg[0]) |
457eb9af PJ |
138 | { |
139 | case 'u': | |
140 | SET_FLAG (ifp->flags, IFF_UP); | |
141 | if_add_update (ifp); | |
142 | printf ("up\n"); | |
143 | break; | |
144 | case 'd': | |
145 | UNSET_FLAG (ifp->flags, IFF_UP); | |
146 | if_delete_update (ifp); | |
147 | printf ("down\n"); | |
148 | break; | |
149 | default: | |
150 | return CMD_WARNING; | |
151 | } | |
152 | return CMD_SUCCESS; | |
153 | } | |
154 | ||
155 | static void | |
156 | test_cmd_init (void) | |
157 | { | |
158 | install_element (INTERFACE_NODE, &test_interface_state_cmd); | |
159 | } | |
6b0655a2 | 160 | |
457eb9af PJ |
161 | /* SIGHUP handler. */ |
162 | static void | |
163 | sighup (void) | |
164 | { | |
165 | zlog_info ("SIGHUP received"); | |
166 | ||
167 | /* Reload of config file. */ | |
168 | ; | |
169 | } | |
170 | ||
171 | /* SIGINT handler. */ | |
172 | static void | |
173 | sigint (void) | |
174 | { | |
175 | zlog_notice ("Terminating on signal"); | |
176 | ||
177 | exit (0); | |
178 | } | |
179 | ||
180 | /* SIGUSR1 handler. */ | |
181 | static void | |
182 | sigusr1 (void) | |
183 | { | |
dd8376fe | 184 | zlog_rotate(); |
457eb9af PJ |
185 | } |
186 | ||
187 | struct quagga_signal_t zebra_signals[] = | |
188 | { | |
189 | { | |
190 | .signal = SIGHUP, | |
191 | .handler = &sighup, | |
192 | }, | |
193 | { | |
194 | .signal = SIGUSR1, | |
195 | .handler = &sigusr1, | |
196 | }, | |
197 | { | |
198 | .signal = SIGINT, | |
199 | .handler = &sigint, | |
200 | }, | |
201 | { | |
202 | .signal = SIGTERM, | |
203 | .handler = &sigint, | |
204 | }, | |
205 | }; | |
457eb9af PJ |
206 | /* Main startup routine. */ |
207 | int | |
208 | main (int argc, char **argv) | |
209 | { | |
210 | char *p; | |
211 | char *vty_addr = NULL; | |
212 | int vty_port = 0; | |
213 | int batch_mode = 0; | |
214 | int daemon_mode = 0; | |
215 | char *config_file = NULL; | |
216 | char *progname; | |
217 | struct thread thread; | |
218 | ||
219 | /* Set umask before anything for security */ | |
220 | umask (0027); | |
221 | ||
222 | /* preserve my name */ | |
223 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); | |
224 | ||
bf1013e6 | 225 | openzlog(progname, "ZEBRA", 0, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); |
457eb9af PJ |
226 | |
227 | while (1) | |
228 | { | |
229 | int opt; | |
230 | ||
6baf7bb8 | 231 | opt = getopt_long (argc, argv, "bdaf:hA:P:r:v", longopts, 0); |
457eb9af PJ |
232 | |
233 | if (opt == EOF) | |
234 | break; | |
235 | ||
236 | switch (opt) | |
237 | { | |
238 | case 0: | |
239 | break; | |
240 | case 'b': | |
241 | batch_mode = 1; | |
242 | case 'd': | |
243 | daemon_mode = 1; | |
244 | break; | |
6baf7bb8 DS |
245 | case 'a': |
246 | allow_delete =1; | |
247 | break; | |
457eb9af PJ |
248 | case 'f': |
249 | config_file = optarg; | |
250 | break; | |
251 | case 'A': | |
252 | vty_addr = optarg; | |
253 | break; | |
254 | case 'P': | |
255 | /* Deal with atoi() returning 0 on failure, and zebra not | |
256 | listening on zebra port... */ | |
257 | if (strcmp(optarg, "0") == 0) | |
258 | { | |
259 | vty_port = 0; | |
260 | break; | |
261 | } | |
262 | vty_port = atoi (optarg); | |
263 | break; | |
264 | case 'r': | |
265 | rib_process_hold_time = atoi(optarg); | |
266 | break; | |
267 | case 'v': | |
268 | print_version (progname); | |
269 | exit (0); | |
270 | break; | |
271 | case 'h': | |
272 | usage (progname, 0); | |
273 | break; | |
274 | default: | |
275 | usage (progname, 1); | |
276 | break; | |
277 | } | |
278 | } | |
279 | ||
280 | /* port and conf file mandatory */ | |
281 | if (!vty_port || !config_file) | |
302d53f7 PJ |
282 | { |
283 | fprintf (stderr, "Error: --vty_port and --config_file arguments" | |
284 | " are both required\n"); | |
285 | usage (progname, 1); | |
286 | } | |
457eb9af PJ |
287 | |
288 | /* Make master thread emulator. */ | |
289 | zebrad.master = thread_master_create (); | |
290 | ||
291 | /* Vty related initialize. */ | |
837d16cc | 292 | signal_init (zebrad.master, array_size(zebra_signals), zebra_signals); |
457eb9af | 293 | cmd_init (1); |
3ddccf18 | 294 | vty_config_lockless (); |
457eb9af PJ |
295 | vty_init (zebrad.master); |
296 | memory_init (); | |
457eb9af PJ |
297 | zebra_debug_init (); |
298 | zebra_if_init (); | |
299 | test_cmd_init (); | |
300 | ||
301 | /* Zebra related initialize. */ | |
302 | rib_init (); | |
303 | access_list_init (); | |
304 | ||
305 | /* Make kernel routing socket. */ | |
b72ede27 | 306 | zebra_vrf_init (); |
457eb9af PJ |
307 | zebra_vty_init(); |
308 | ||
457eb9af PJ |
309 | /* Configuration file read*/ |
310 | vty_read_config (config_file, config_default); | |
311 | ||
312 | /* Clean up rib. */ | |
313 | rib_weed_tables (); | |
314 | ||
315 | /* Exit when zebra is working in batch mode. */ | |
316 | if (batch_mode) | |
317 | exit (0); | |
318 | ||
457eb9af | 319 | /* Daemonize. */ |
065de903 SH |
320 | if (daemon_mode && daemon (0, 0) < 0) |
321 | { | |
322 | perror("daemon start failed"); | |
323 | exit (1); | |
324 | } | |
457eb9af PJ |
325 | |
326 | /* Needed for BSD routing socket. */ | |
327 | pid = getpid (); | |
328 | ||
329 | /* Make vty server socket. */ | |
330 | vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra"); | |
331 | ||
332 | /* Print banner. */ | |
b2f36157 | 333 | zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION, vty_port); |
457eb9af PJ |
334 | |
335 | while (thread_fetch (zebrad.master, &thread)) | |
336 | thread_call (&thread); | |
337 | ||
338 | /* Not reached... */ | |
339 | return 0; | |
340 | } |