usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / zebra / ospf6d / ospf6_main.c
blob7225d9949d4ae2750f5709a25b0ec88a25a1d4ec
1 /*
2 * Copyright (C) 1999 Yasuhiro Ohara
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
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <zebra.h>
24 #include "getopt.h"
25 #include "thread.h"
26 #include "log.h"
27 #include "version.h"
28 #include "command.h"
29 #include "vty.h"
30 #include "memory.h"
31 #include "if.h"
32 #include "filter.h"
33 #include "prefix.h"
34 #include "plist.h"
36 #include "ospf6d.h"
38 /* Default configuration file name for ospf6d. */
39 #define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
41 /* Default port values. */
42 #define OSPF6_VTY_PORT 2606
43 #define OSPF6_VTYSH_PATH "/tmp/.ospf6d"
45 /* ospf6d options, we use GNU getopt library. */
46 struct option longopts[] =
48 { "daemon", no_argument, NULL, 'd'},
49 { "config_file", required_argument, NULL, 'f'},
50 { "pid_file", required_argument, NULL, 'i'},
51 { "vty_addr", required_argument, NULL, 'A'},
52 { "vty_port", required_argument, NULL, 'P'},
53 { "version", no_argument, NULL, 'v'},
54 { "help", no_argument, NULL, 'h'},
55 { 0 }
58 /* Configuration file and directory. */
59 char config_current[] = OSPF6_DEFAULT_CONFIG;
60 char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
62 /* ospf6d program name. */
63 char *progname;
65 /* is daemon? */
66 int daemon_mode = 0;
68 /* Master of threads. */
69 struct thread_master *master;
71 /* Process ID saved for use by init system */
72 char *pid_file = PATH_OSPF6D_PID;
74 /* Help information display. */
75 static void
76 usage (char *progname, int status)
78 if (status != 0)
79 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
80 else
82 printf ("Usage : %s [OPTION...]\n\n\
83 Daemon which manages OSPF version 3.\n\n\
84 -d, --daemon Runs in daemon mode\n\
85 -f, --config_file Set configuration file name\n\
86 -i, --pid_file Set process identifier file name\n\
87 -A, --vty_addr Set vty's bind address\n\
88 -P, --vty_port Set vty's port number\n\
89 -v, --version Print program version\n\
90 -h, --help Display this help and exit\n\
91 \n\
92 Report bugs to zebra@zebra.org\n", progname);
95 exit (status);
98 /* SIGHUP handler. */
99 void
100 sighup (int sig)
102 zlog_info ("SIGHUP received");
105 /* SIGINT handler. */
106 void
107 sigint (int sig)
109 zlog_info ("SIGINT received");
110 exit (0);
113 /* SIGTERM handler. */
114 void
115 sigterm (int sig)
117 zlog_info ("SIGTERM received");
118 exit (0);
121 /* SIGUSR1 handler. */
122 void
123 sigusr1 (int sig)
125 zlog_info ("SIGUSR1 received");
126 zlog_rotate (NULL);
129 /* Signale wrapper. */
130 RETSIGTYPE *
131 signal_set (int signo, void (*func)(int))
133 int ret;
134 struct sigaction sig;
135 struct sigaction osig;
137 sig.sa_handler = func;
138 sigemptyset (&sig.sa_mask);
139 sig.sa_flags = 0;
140 #ifdef SA_RESTART
141 sig.sa_flags |= SA_RESTART;
142 #endif /* SA_RESTART */
144 ret = sigaction (signo, &sig, &osig);
146 if (ret < 0)
147 return (SIG_ERR);
148 else
149 return (osig.sa_handler);
152 /* Initialization of signal handles. */
153 void
154 signal_init ()
156 signal_set (SIGHUP, sighup);
157 signal_set (SIGINT, sigint);
158 signal_set (SIGTERM, sigterm);
159 signal_set (SIGPIPE, SIG_IGN);
160 #ifdef SIGTSTP
161 signal_set (SIGTSTP, SIG_IGN);
162 #endif
163 #ifdef SIGTTIN
164 signal_set (SIGTTIN, SIG_IGN);
165 #endif
166 #ifdef SIGTTOU
167 signal_set (SIGTTOU, SIG_IGN);
168 #endif
169 signal_set (SIGUSR1, sigusr1);
172 /* Main routine of ospf6d. Treatment of argument and starting ospf finite
173 state machine is handled here. */
175 main (int argc, char *argv[], char *envp[])
177 char *p;
178 int opt;
179 char *vty_addr = NULL;
180 int vty_port = 0;
181 char *config_file = NULL;
182 struct thread thread;
183 int flag;
185 /* Set umask before anything for security */
186 umask (0027);
188 /* Preserve name of myself. */
189 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
191 /* Command line argument treatment. */
192 while (1)
194 opt = getopt_long (argc, argv, "df:hp:A:P:v", longopts, 0);
196 if (opt == EOF)
197 break;
199 switch (opt)
201 case 0:
202 break;
203 case 'd':
204 daemon_mode = 1;
205 break;
206 case 'f':
207 config_file = optarg;
208 break;
209 case 'A':
210 vty_addr = optarg;
211 break;
212 case 'i':
213 pid_file = optarg;
214 break;
215 case 'P':
216 vty_port = atoi (optarg);
217 break;
218 case 'v':
219 print_version (progname);
220 exit (0);
221 break;
222 case 'h':
223 usage (progname, 0);
224 break;
225 default:
226 usage (progname, 1);
227 break;
231 /* thread master */
232 master = thread_master_create ();
234 /* Initializations. */
235 if (! daemon_mode)
236 flag = ZLOG_STDOUT;
237 else
238 flag = 0;
240 zlog_default = openzlog (progname, flag, ZLOG_OSPF6,
241 LOG_CONS|LOG_NDELAY|LOG_PERROR|LOG_PID,
242 LOG_DAEMON);
244 /* initialize zebra libraries */
245 signal_init ();
246 cmd_init (1);
247 vty_init ();
248 memory_init ();
249 if_init ();
250 access_list_init ();
251 prefix_list_init ();
253 /* initialize ospf6 */
254 ospf6_init ();
256 /* sort command vector */
257 sort_node ();
259 /* parse config file */
260 vty_read_config (config_file, config_current, config_default);
262 if (daemon_mode)
263 daemon (0, 0);
265 /* pid file create */
266 #if 0
267 pid_output_lock (pid_file);
268 #else
269 pid_output (pid_file);
270 #endif
272 /* Make ospf6 vty socket. */
273 vty_serv_sock (vty_addr,
274 vty_port ? vty_port : OSPF6_VTY_PORT, OSPF6_VTYSH_PATH);
276 /* Print start message */
277 zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) starts",
278 ZEBRA_VERSION, OSPF6_DAEMON_VERSION);
280 /* Start finite state machine, here we go! */
281 while (thread_fetch (master, &thread))
282 thread_call (&thread);
284 /* Log in case thread failed */
285 zlog_warn ("Thread failed");
287 /* Not reached. */
288 exit (0);