]>
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> | |
23 | #include "getopt.h" | |
24 | #include "thread.h" | |
25 | #include "log.h" | |
26 | #include "version.h" | |
27 | #include "command.h" | |
28 | #include "vty.h" | |
29 | #include "memory.h" | |
edd7c245 | 30 | #include "privs.h" |
718e3744 | 31 | |
32 | #include "ospf6d.h" | |
33 | #include "ospf6_network.h" | |
34 | ||
35 | void ospf6_init (); | |
36 | void ospf6_terminate (); | |
37 | void nexthop_init (); | |
38 | int ospf6_receive (struct thread *); | |
39 | ||
40 | extern int ospf6_sock; | |
41 | ||
42 | /* Default configuration file name for ospf6d. */ | |
43 | #define OSPF6_DEFAULT_CONFIG "ospf6d.conf" | |
44 | /* Default port values. */ | |
45 | #define OSPF6_VTY_PORT 2606 | |
46 | ||
edd7c245 | 47 | /* ospf6d privileges */ |
48 | zebra_capabilities_t _caps_p [] = | |
49 | { | |
50 | ZCAP_RAW, | |
51 | ZCAP_BIND | |
52 | }; | |
53 | ||
54 | struct zebra_privs_t ospf6d_privs = | |
55 | { | |
56 | #if defined(ZEBRA_USER) | |
57 | .user = ZEBRA_USER, | |
58 | #endif | |
59 | #if defined ZEBRA_GROUP | |
60 | .group = ZEBRA_GROUP, | |
61 | #endif | |
62 | .caps_p = _caps_p, | |
63 | .cap_num_p = 2, | |
64 | .cap_num_i = 0 | |
65 | }; | |
66 | ||
718e3744 | 67 | /* ospf6d options, we use GNU getopt library. */ |
68 | struct option longopts[] = | |
69 | { | |
70 | { "daemon", no_argument, NULL, 'd'}, | |
71 | { "config_file", required_argument, NULL, 'f'}, | |
72 | { "pid_file", required_argument, NULL, 'i'}, | |
73 | { "vty_addr", required_argument, NULL, 'A'}, | |
74 | { "vty_port", required_argument, NULL, 'P'}, | |
edd7c245 | 75 | { "user", required_argument, NULL, 'u'}, |
718e3744 | 76 | { "version", no_argument, NULL, 'v'}, |
77 | { "help", no_argument, NULL, 'h'}, | |
78 | { 0 } | |
79 | }; | |
80 | ||
81 | /* Configuration file and directory. */ | |
82 | char config_current[] = OSPF6_DEFAULT_CONFIG; | |
83 | char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG; | |
84 | ||
85 | /* ospf6d program name. */ | |
86 | ||
87 | /* is daemon? */ | |
88 | int daemon_mode = 0; | |
89 | ||
90 | /* Master of threads. */ | |
91 | struct thread_master *master; | |
92 | ||
93 | /* Process ID saved for use by init system */ | |
94 | char *pid_file = PATH_OSPF6D_PID; | |
95 | ||
96 | /* for reload */ | |
97 | char _cwd[64]; | |
98 | char _progpath[64]; | |
99 | int _argc; | |
100 | char **_argv; | |
101 | char **_envp; | |
102 | ||
103 | /* Help information display. */ | |
104 | static void | |
105 | usage (char *progname, int status) | |
106 | { | |
107 | if (status != 0) | |
108 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); | |
109 | else | |
110 | { | |
111 | printf ("Usage : %s [OPTION...]\n\n\ | |
112 | Daemon which manages OSPF version 3.\n\n\ | |
113 | -d, --daemon Runs in daemon mode\n\ | |
114 | -f, --config_file Set configuration file name\n\ | |
115 | -i, --pid_file Set process identifier file name\n\ | |
116 | -A, --vty_addr Set vty's bind address\n\ | |
117 | -P, --vty_port Set vty's port number\n\ | |
edd7c245 | 118 | -u, --user User and group to run as\n\ |
718e3744 | 119 | -v, --version Print program version\n\ |
120 | -h, --help Display this help and exit\n\ | |
121 | \n\ | |
122 | Report bugs to yasu@sfc.wide.ad.jp\n", progname); | |
123 | } | |
124 | ||
125 | exit (status); | |
126 | } | |
127 | \f | |
128 | ||
129 | void | |
130 | _reload () | |
131 | { | |
132 | zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) reloaded", | |
133 | ZEBRA_VERSION, OSPF6_DAEMON_VERSION); | |
134 | ospf6_zebra_finish (); | |
135 | vty_finish (); | |
136 | execve (_progpath, _argv, _envp); | |
137 | } | |
138 | ||
139 | void | |
140 | terminate (int i) | |
141 | { | |
142 | ospf6_delete (ospf6); | |
143 | unlink (PATH_OSPF6D_PID); | |
144 | zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) terminated", | |
145 | ZEBRA_VERSION, OSPF6_DAEMON_VERSION); | |
146 | exit (i); | |
147 | } | |
148 | ||
149 | /* SIGHUP handler. */ | |
150 | void | |
151 | sighup (int sig) | |
152 | { | |
153 | zlog_info ("SIGHUP received"); | |
154 | _reload (); | |
155 | } | |
156 | ||
157 | /* SIGINT handler. */ | |
158 | void | |
159 | sigint (int sig) | |
160 | { | |
161 | zlog_info ("SIGINT received"); | |
162 | terminate (0); | |
163 | } | |
164 | ||
165 | /* SIGTERM handler. */ | |
166 | void | |
167 | sigterm (int sig) | |
168 | { | |
169 | zlog_info ("SIGTERM received"); | |
170 | terminate (0); | |
171 | } | |
172 | ||
173 | /* SIGUSR1 handler. */ | |
174 | void | |
175 | sigusr1 (int sig) | |
176 | { | |
177 | zlog_info ("SIGUSR1 received"); | |
178 | zlog_rotate (NULL); | |
179 | } | |
180 | ||
181 | /* Signale wrapper. */ | |
182 | RETSIGTYPE * | |
183 | signal_set (int signo, void (*func)(int)) | |
184 | { | |
185 | int ret; | |
186 | struct sigaction sig; | |
187 | struct sigaction osig; | |
188 | ||
189 | sig.sa_handler = func; | |
190 | sigemptyset (&sig.sa_mask); | |
191 | sig.sa_flags = 0; | |
192 | #ifdef SA_RESTART | |
193 | sig.sa_flags |= SA_RESTART; | |
194 | #endif /* SA_RESTART */ | |
195 | ||
196 | ret = sigaction (signo, &sig, &osig); | |
197 | ||
198 | if (ret < 0) | |
199 | return (SIG_ERR); | |
200 | else | |
201 | return (osig.sa_handler); | |
202 | } | |
203 | ||
204 | /* Initialization of signal handles. */ | |
205 | void | |
206 | signal_init () | |
207 | { | |
208 | signal_set (SIGHUP, sighup); | |
209 | signal_set (SIGINT, sigint); | |
210 | signal_set (SIGTERM, sigterm); | |
211 | signal_set (SIGPIPE, SIG_IGN); | |
212 | #ifdef SIGTSTP | |
213 | signal_set (SIGTSTP, SIG_IGN); | |
214 | #endif | |
215 | #ifdef SIGTTIN | |
216 | signal_set (SIGTTIN, SIG_IGN); | |
217 | #endif | |
218 | #ifdef SIGTTOU | |
219 | signal_set (SIGTTOU, SIG_IGN); | |
220 | #endif | |
221 | signal_set (SIGUSR1, sigusr1); | |
222 | } | |
223 | \f | |
224 | /* Main routine of ospf6d. Treatment of argument and start ospf finite | |
225 | state machine is handled here. */ | |
226 | int | |
227 | main (int argc, char *argv[], char *envp[]) | |
228 | { | |
229 | char *p; | |
230 | int opt; | |
231 | char *vty_addr = NULL; | |
4fc4e7ab | 232 | int vty_port = OSPF6_VTY_PORT; |
718e3744 | 233 | char *config_file = NULL; |
234 | char *progname; | |
235 | struct thread thread; | |
236 | int flag; | |
237 | ||
238 | /* Set umask before anything for security */ | |
239 | umask (0027); | |
240 | ||
241 | /* Preserve name of myself. */ | |
242 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); | |
243 | ||
244 | /* for reload */ | |
245 | _argc = argc; | |
246 | _argv = argv; | |
247 | _envp = envp; | |
248 | getcwd (_cwd, sizeof (_cwd)); | |
249 | if (*argv[0] == '.') | |
250 | snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]); | |
251 | else | |
252 | snprintf (_progpath, sizeof (_progpath), "%s", argv[0]); | |
253 | ||
254 | /* Command line argument treatment. */ | |
255 | while (1) | |
256 | { | |
edd7c245 | 257 | opt = getopt_long (argc, argv, "df:hp:A:P:u:v", longopts, 0); |
718e3744 | 258 | |
259 | if (opt == EOF) | |
260 | break; | |
261 | ||
262 | switch (opt) | |
263 | { | |
264 | case 0: | |
265 | break; | |
266 | case 'd': | |
267 | daemon_mode = 1; | |
268 | break; | |
269 | case 'f': | |
270 | config_file = optarg; | |
271 | break; | |
272 | case 'A': | |
273 | vty_addr = optarg; | |
274 | break; | |
275 | case 'i': | |
276 | pid_file = optarg; | |
277 | break; | |
278 | case 'P': | |
4fc4e7ab | 279 | /* Deal with atoi() returning 0 on failure, and ospf6d not |
280 | listening on ospf6d port... */ | |
281 | if (strcmp(optarg, "0") == 0) | |
282 | { | |
283 | vty_port = 0; | |
284 | break; | |
285 | } | |
718e3744 | 286 | vty_port = atoi (optarg); |
4fc4e7ab | 287 | vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT); |
288 | break; | |
edd7c245 | 289 | case 'u': |
290 | ospf6d_privs.user = ospf6d_privs.group = optarg; | |
291 | break; | |
718e3744 | 292 | case 'v': |
293 | print_version (progname); | |
294 | exit (0); | |
295 | break; | |
296 | case 'h': | |
297 | usage (progname, 0); | |
298 | break; | |
299 | default: | |
300 | usage (progname, 1); | |
301 | break; | |
302 | } | |
303 | } | |
304 | ||
305 | /* thread master */ | |
306 | master = thread_master_create (); | |
307 | ||
308 | /* Initializations. */ | |
309 | if (! daemon_mode) | |
310 | flag = ZLOG_STDOUT; | |
311 | else | |
e26bbeba | 312 | flag = ZLOG_NOLOG; |
718e3744 | 313 | |
314 | zlog_default = openzlog (progname, flag, ZLOG_OSPF6, | |
e26bbeba | 315 | LOG_CONS|LOG_NDELAY|LOG_PID, |
718e3744 | 316 | LOG_DAEMON); |
edd7c245 | 317 | zprivs_init (&ospf6d_privs); |
718e3744 | 318 | signal_init (); |
319 | cmd_init (1); | |
320 | vty_init (); | |
321 | ospf6_init (); | |
322 | memory_init (); | |
323 | sort_node (); | |
324 | ||
325 | /* parse config file */ | |
326 | vty_read_config (config_file, config_current, config_default); | |
327 | ||
328 | if (daemon_mode) | |
329 | daemon (0, 0); | |
330 | ||
331 | /* pid file create */ | |
332 | #if 0 | |
333 | pid_output_lock (pid_file); | |
334 | #else | |
335 | pid_output (pid_file); | |
336 | #endif | |
337 | ||
338 | /* Make ospf protocol socket. */ | |
339 | ospf6_serv_sock (); | |
340 | thread_add_read (master, ospf6_receive, NULL, ospf6_sock); | |
341 | ||
342 | /* Make ospf vty socket. */ | |
4fc4e7ab | 343 | vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH); |
718e3744 | 344 | |
e26bbeba | 345 | #ifdef DEBUG |
718e3744 | 346 | /* Print start message */ |
347 | zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) starts", | |
348 | ZEBRA_VERSION, OSPF6_DAEMON_VERSION); | |
e26bbeba | 349 | #endif |
718e3744 | 350 | |
351 | /* Start finite state machine, here we go! */ | |
352 | while (thread_fetch (master, &thread)) | |
353 | thread_call (&thread); | |
354 | ||
355 | /* Log in case thread failed */ | |
356 | zlog_warn ("Thread failed"); | |
357 | terminate (0); | |
358 | ||
359 | /* Not reached. */ | |
360 | exit (0); | |
361 | } | |
362 |