]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * Copyright (C) 1999 Yasuhiro Ohara | |
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 | |
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | * Boston, MA 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
3b4cd3a9 | 23 | #include <lib/version.h> |
508e53e2 | 24 | |
718e3744 | 25 | #include "getopt.h" |
26 | #include "thread.h" | |
27 | #include "log.h" | |
718e3744 | 28 | #include "command.h" |
29 | #include "vty.h" | |
30 | #include "memory.h" | |
508e53e2 | 31 | #include "if.h" |
32 | #include "filter.h" | |
33 | #include "prefix.h" | |
34 | #include "plist.h" | |
edd7c245 | 35 | #include "privs.h" |
e42f5a37 | 36 | #include "sigevent.h" |
718e3744 | 37 | |
38 | #include "ospf6d.h" | |
718e3744 | 39 | |
40 | /* Default configuration file name for ospf6d. */ | |
41 | #define OSPF6_DEFAULT_CONFIG "ospf6d.conf" | |
508e53e2 | 42 | |
718e3744 | 43 | /* Default port values. */ |
44 | #define OSPF6_VTY_PORT 2606 | |
45 | ||
edd7c245 | 46 | /* ospf6d privileges */ |
508e53e2 | 47 | zebra_capabilities_t _caps_p [] = |
edd7c245 | 48 | { |
ceacedba | 49 | ZCAP_NET_RAW, |
edd7c245 | 50 | ZCAP_BIND |
51 | }; | |
52 | ||
53 | struct zebra_privs_t ospf6d_privs = | |
54 | { | |
d81fadfd | 55 | #if defined(QUAGGA_USER) |
56 | .user = QUAGGA_USER, | |
edd7c245 | 57 | #endif |
d81fadfd | 58 | #if defined QUAGGA_GROUP |
59 | .group = QUAGGA_GROUP, | |
60 | #endif | |
61 | #ifdef VTY_GROUP | |
62 | .vty_group = VTY_GROUP, | |
edd7c245 | 63 | #endif |
64 | .caps_p = _caps_p, | |
65 | .cap_num_p = 2, | |
66 | .cap_num_i = 0 | |
67 | }; | |
68 | ||
718e3744 | 69 | /* ospf6d options, we use GNU getopt library. */ |
70 | struct option longopts[] = | |
71 | { | |
72 | { "daemon", no_argument, NULL, 'd'}, | |
73 | { "config_file", required_argument, NULL, 'f'}, | |
74 | { "pid_file", required_argument, NULL, 'i'}, | |
75 | { "vty_addr", required_argument, NULL, 'A'}, | |
76 | { "vty_port", required_argument, NULL, 'P'}, | |
c065230a | 77 | { "user", required_argument, NULL, 'u'}, |
78 | { "group", required_argument, NULL, 'g'}, | |
718e3744 | 79 | { "version", no_argument, NULL, 'v'}, |
876b8be0 | 80 | { "dryrun", no_argument, NULL, 'C'}, |
718e3744 | 81 | { "help", no_argument, NULL, 'h'}, |
82 | { 0 } | |
83 | }; | |
84 | ||
85 | /* Configuration file and directory. */ | |
718e3744 | 86 | char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG; |
87 | ||
88 | /* ospf6d program name. */ | |
508e53e2 | 89 | char *progname; |
718e3744 | 90 | |
91 | /* is daemon? */ | |
92 | int daemon_mode = 0; | |
93 | ||
94 | /* Master of threads. */ | |
95 | struct thread_master *master; | |
96 | ||
97 | /* Process ID saved for use by init system */ | |
0c083ee9 | 98 | const char *pid_file = PATH_OSPF6D_PID; |
718e3744 | 99 | |
718e3744 | 100 | /* Help information display. */ |
101 | static void | |
102 | usage (char *progname, int status) | |
103 | { | |
104 | if (status != 0) | |
105 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); | |
106 | else | |
107 | { | |
108 | printf ("Usage : %s [OPTION...]\n\n\ | |
109 | Daemon which manages OSPF version 3.\n\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 | -A, --vty_addr Set vty's bind address\n\ | |
114 | -P, --vty_port Set vty's port number\n\ | |
c065230a | 115 | -u, --user User to run as\n\ |
116 | -g, --group Group to run as\n\ | |
718e3744 | 117 | -v, --version Print program version\n\ |
876b8be0 | 118 | -C, --dryrun Check configuration for validity and exit\n\ |
718e3744 | 119 | -h, --help Display this help and exit\n\ |
120 | \n\ | |
508e53e2 | 121 | Report bugs to zebra@zebra.org\n", progname); |
718e3744 | 122 | } |
123 | ||
124 | exit (status); | |
125 | } | |
718e3744 | 126 | |
ae2254aa TG |
127 | static void |
128 | ospf6_exit (int status) | |
129 | { | |
130 | extern struct ospf6 *ospf6; | |
131 | extern struct zclient *zclient; | |
132 | ||
133 | if (ospf6) | |
134 | ospf6_delete (ospf6); | |
135 | ||
136 | ospf6_message_terminate (); | |
137 | ospf6_asbr_terminate (); | |
138 | ospf6_lsa_terminate (); | |
139 | ||
140 | if_terminate (); | |
141 | vty_terminate (); | |
142 | cmd_terminate (); | |
143 | ||
144 | if (zclient) | |
145 | zclient_free (zclient); | |
146 | ||
147 | if (master) | |
148 | thread_master_free (master); | |
149 | ||
150 | if (zlog_default) | |
151 | closezlog (zlog_default); | |
152 | ||
153 | exit (status); | |
154 | } | |
155 | ||
718e3744 | 156 | /* SIGHUP handler. */ |
6ac29a51 | 157 | static void |
e42f5a37 | 158 | sighup (void) |
718e3744 | 159 | { |
160 | zlog_info ("SIGHUP received"); | |
718e3744 | 161 | } |
162 | ||
163 | /* SIGINT handler. */ | |
6ac29a51 | 164 | static void |
e42f5a37 | 165 | sigint (void) |
718e3744 | 166 | { |
887c44a4 | 167 | zlog_notice ("Terminating on signal SIGINT"); |
ae2254aa | 168 | ospf6_exit (0); |
718e3744 | 169 | } |
170 | ||
171 | /* SIGTERM handler. */ | |
6ac29a51 | 172 | static void |
e42f5a37 | 173 | sigterm (void) |
718e3744 | 174 | { |
887c44a4 | 175 | zlog_notice ("Terminating on signal SIGTERM"); |
ae2254aa | 176 | ospf6_exit (0); |
718e3744 | 177 | } |
178 | ||
179 | /* SIGUSR1 handler. */ | |
6ac29a51 | 180 | static void |
e42f5a37 | 181 | sigusr1 (void) |
718e3744 | 182 | { |
183 | zlog_info ("SIGUSR1 received"); | |
184 | zlog_rotate (NULL); | |
185 | } | |
186 | ||
e42f5a37 | 187 | struct quagga_signal_t ospf6_signals[] = |
718e3744 | 188 | { |
e42f5a37 | 189 | { |
190 | .signal = SIGHUP, | |
191 | .handler = &sighup, | |
192 | }, | |
193 | { | |
194 | .signal = SIGINT, | |
195 | .handler = &sigint, | |
196 | }, | |
197 | { | |
198 | .signal = SIGTERM, | |
199 | .handler = &sigterm, | |
200 | }, | |
201 | { | |
202 | .signal = SIGUSR1, | |
203 | .handler = &sigusr1, | |
204 | }, | |
205 | }; | |
508e53e2 | 206 | |
207 | /* Main routine of ospf6d. Treatment of argument and starting ospf finite | |
718e3744 | 208 | state machine is handled here. */ |
209 | int | |
210 | main (int argc, char *argv[], char *envp[]) | |
211 | { | |
212 | char *p; | |
213 | int opt; | |
214 | char *vty_addr = NULL; | |
508e53e2 | 215 | int vty_port = 0; |
718e3744 | 216 | char *config_file = NULL; |
718e3744 | 217 | struct thread thread; |
876b8be0 | 218 | int dryrun = 0; |
718e3744 | 219 | |
220 | /* Set umask before anything for security */ | |
221 | umask (0027); | |
222 | ||
223 | /* Preserve name of myself. */ | |
224 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); | |
225 | ||
718e3744 | 226 | /* Command line argument treatment. */ |
227 | while (1) | |
228 | { | |
876b8be0 | 229 | opt = getopt_long (argc, argv, "df:i:hp:A:P:u:g:vC", longopts, 0); |
718e3744 | 230 | |
231 | if (opt == EOF) | |
232 | break; | |
233 | ||
234 | switch (opt) | |
235 | { | |
236 | case 0: | |
237 | break; | |
238 | case 'd': | |
239 | daemon_mode = 1; | |
240 | break; | |
241 | case 'f': | |
242 | config_file = optarg; | |
243 | break; | |
244 | case 'A': | |
245 | vty_addr = optarg; | |
246 | break; | |
247 | case 'i': | |
248 | pid_file = optarg; | |
249 | break; | |
250 | case 'P': | |
4fc4e7ab | 251 | /* Deal with atoi() returning 0 on failure, and ospf6d not |
252 | listening on ospf6d port... */ | |
508e53e2 | 253 | if (strcmp(optarg, "0") == 0) |
4fc4e7ab | 254 | { |
255 | vty_port = 0; | |
256 | break; | |
508e53e2 | 257 | } |
718e3744 | 258 | vty_port = atoi (optarg); |
0d6b2ee2 PJ |
259 | if (vty_port <= 0 || vty_port > 0xffff) |
260 | vty_port = OSPF6_VTY_PORT; | |
508e53e2 | 261 | break; |
edd7c245 | 262 | case 'u': |
c065230a | 263 | ospf6d_privs.user = optarg; |
edd7c245 | 264 | break; |
c065230a | 265 | case 'g': |
266 | ospf6d_privs.group = optarg; | |
267 | break; | |
718e3744 | 268 | case 'v': |
269 | print_version (progname); | |
270 | exit (0); | |
271 | break; | |
876b8be0 PJ |
272 | case 'C': |
273 | dryrun = 1; | |
274 | break; | |
718e3744 | 275 | case 'h': |
276 | usage (progname, 0); | |
277 | break; | |
278 | default: | |
279 | usage (progname, 1); | |
280 | break; | |
281 | } | |
282 | } | |
283 | ||
284 | /* thread master */ | |
285 | master = thread_master_create (); | |
286 | ||
287 | /* Initializations. */ | |
274a4a44 | 288 | zlog_default = openzlog (progname, ZLOG_OSPF6, |
79dc373a | 289 | LOG_CONS|LOG_NDELAY|LOG_PID, |
508e53e2 | 290 | LOG_DAEMON); |
291 | zprivs_init (&ospf6d_privs); | |
292 | /* initialize zebra libraries */ | |
e42f5a37 | 293 | signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals); |
718e3744 | 294 | cmd_init (1); |
b21b19c5 | 295 | vty_init (master); |
718e3744 | 296 | memory_init (); |
508e53e2 | 297 | if_init (); |
298 | access_list_init (); | |
299 | prefix_list_init (); | |
300 | ||
301 | /* initialize ospf6 */ | |
302 | ospf6_init (); | |
303 | ||
304 | /* sort command vector */ | |
718e3744 | 305 | sort_node (); |
306 | ||
307 | /* parse config file */ | |
320ec10a | 308 | vty_read_config (config_file, config_default); |
718e3744 | 309 | |
876b8be0 PJ |
310 | /* Start execution only if not in dry-run mode */ |
311 | if (dryrun) | |
312 | return(0); | |
313 | ||
065de903 SH |
314 | if (daemon_mode && daemon (0, 0) < 0) |
315 | { | |
316 | zlog_err("OSPF6d daemon failed: %s", strerror(errno)); | |
317 | exit (1); | |
318 | } | |
718e3744 | 319 | |
320 | /* pid file create */ | |
718e3744 | 321 | pid_output (pid_file); |
718e3744 | 322 | |
508e53e2 | 323 | /* Make ospf6 vty socket. */ |
887c44a4 | 324 | if (!vty_port) |
325 | vty_port = OSPF6_VTY_PORT; | |
326 | vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH); | |
718e3744 | 327 | |
328 | /* Print start message */ | |
887c44a4 | 329 | zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d", |
330 | QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port); | |
718e3744 | 331 | |
332 | /* Start finite state machine, here we go! */ | |
333 | while (thread_fetch (master, &thread)) | |
334 | thread_call (&thread); | |
335 | ||
336 | /* Log in case thread failed */ | |
337 | zlog_warn ("Thread failed"); | |
718e3744 | 338 | |
339 | /* Not reached. */ | |
ae2254aa | 340 | ospf6_exit (0); |
718e3744 | 341 | } |
342 | ||
508e53e2 | 343 |