1/* 2 * OSPFd main routine. 3 * Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada 4 * 5 * This file is part of GNU Zebra. 6 * 7 * GNU Zebra is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the 9 * Free Software Foundation; either version 2, or (at your option) any 10 * later version. 11 * 12 * GNU Zebra is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with GNU Zebra; see the file COPYING. If not, write to the Free 19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 20 * 02111-1307, USA. 21 */ 22 23#include <zebra.h> 24 25#include "version.h" 26#include "getopt.h" 27#include "thread.h" 28#include "prefix.h" 29#include "linklist.h" 30#include "if.h" 31#include "vector.h" 32#include "vty.h" 33#include "command.h" 34#include "filter.h" 35#include "plist.h" 36#include "stream.h" 37#include "log.h" 38#include "memory.h" 39 40#include "ospfd/ospfd.h" 41#include "ospfd/ospf_interface.h" 42#include "ospfd/ospf_asbr.h" 43#include "ospfd/ospf_lsa.h" 44#include "ospfd/ospf_lsdb.h" 45#include "ospfd/ospf_neighbor.h" 46#include "ospfd/ospf_dump.h" 47#include "ospfd/ospf_zebra.h" 48#include "ospfd/ospf_vty.h" 49 50/* Configuration filename and directory. */ 51char config_current[] = OSPF_DEFAULT_CONFIG; 52char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG; 53 54/* OSPFd options. */ 55struct option longopts[] = 56{ 57 { "daemon", no_argument, NULL, 'd'}, 58 { "config_file", required_argument, NULL, 'f'}, 59 { "pid_file", required_argument, NULL, 'i'}, 60 { "log_mode", no_argument, NULL, 'l'}, 61 { "help", no_argument, NULL, 'h'}, 62 { "vty_addr", required_argument, NULL, 'A'}, 63 { "vty_port", required_argument, NULL, 'P'}, 64 { "version", no_argument, NULL, 'v'}, 65 { 0 } 66}; 67 68/* OSPFd program name */ 69 70/* Master of threads. */ 71struct thread_master *master; 72 73/* Process ID saved for use by init system */ 74char *pid_file = PATH_OSPFD_PID; 75 76/* Help information display. */ 77static void 78usage (char *progname, int status) 79{ 80 if (status != 0) 81 fprintf (stderr, "Try `%s --help' for more information.\n", progname); 82 else 83 { 84 printf ("Usage : %s [OPTION...]\n\ 85Daemon which manages OSPF.\n\n\ 86-d, --daemon Runs in daemon mode\n\ 87-f, --config_file Set configuration file name\n\ 88-i, --pid_file Set process identifier file name\n\ 89-A, --vty_addr Set vty's bind address\n\ 90-P, --vty_port Set vty's port number\n\ 91-v, --version Print program version\n\ 92-h, --help Display this help and exit\n\ 93\n\ 94Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); 95 } 96 exit (status); 97} 98 99/* SIGHUP handler. */ 100void 101sighup (int sig) 102{ 103 zlog (NULL, LOG_INFO, "SIGHUP received"); 104} 105 106/* SIGINT handler. */ 107void 108sigint (int sig) 109{ 110 zlog (NULL, LOG_INFO, "Terminating on signal"); 111 112 ospf_terminate (); 113 114 exit (0); 115} 116 117/* SIGUSR1 handler. */ 118void 119sigusr1 (int sig) 120{ 121 zlog_rotate (NULL); 122} 123 124/* Signal wrapper. */ 125RETSIGTYPE * 126signal_set (int signo, void (*func)(int)) 127{ 128 int ret; 129 struct sigaction sig; 130 struct sigaction osig; 131 132 sig.sa_handler = func; 133 sigemptyset (&sig.sa_mask); 134 sig.sa_flags = 0; 135#ifdef SA_RESTART 136 sig.sa_flags |= SA_RESTART; 137#endif /* SA_RESTART */ 138 139 ret = sigaction (signo, &sig, &osig); 140 141 if (ret < 0) 142 return (SIG_ERR); 143 else 144 return (osig.sa_handler); 145} 146 147/* Initialization of signal handles. */ 148void 149signal_init () 150{ 151 signal_set (SIGHUP, sighup); 152 signal_set (SIGINT, sigint); 153 signal_set (SIGTERM, sigint); 154 signal_set (SIGPIPE, SIG_IGN); 155#ifdef SIGTSTP 156 signal_set (SIGTSTP, SIG_IGN); 157#endif 158#ifdef SIGTTIN 159 signal_set (SIGTTIN, SIG_IGN); 160#endif 161#ifdef SIGTTOU 162 signal_set (SIGTTOU, SIG_IGN); 163#endif 164 signal_set (SIGUSR1, sigusr1); 165} 166 167/* OSPFd main routine. */ 168int 169main (int argc, char **argv) 170{ 171 char *p; 172 char *vty_addr = NULL; 173 int vty_port = 0; 174 int daemon_mode = 0; 175 char *config_file = NULL; 176 char *progname; 177 struct thread thread; 178 179 /* Set umask before anything for security */ 180 umask (0027); 181 182 /* get program name */ 183 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); 184 185 /* Invoked by a priviledged user? -- endo. */ 186 if (getuid () != 0) 187 { 188 errno = EPERM; 189 perror (progname); 190 exit (1); 191 } 192 193 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_OSPF, 194 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); 195 196 while (1) 197 { 198 int opt; 199 200 opt = getopt_long (argc, argv, "dlf:hA:P:v", longopts, 0); 201 202 if (opt == EOF) 203 break; 204 205 switch (opt) 206 { 207 case 0: 208 break; 209 case 'd': 210 daemon_mode = 1; 211 break; 212 case 'f': 213 config_file = optarg; 214 break; 215 case 'A': 216 vty_addr = optarg; 217 break; 218 case 'i': 219 pid_file = optarg; 220 break; 221 case 'P': 222 vty_port = atoi (optarg); 223 break; 224 case 'v': 225 print_version (progname); 226 exit (0); 227 break; 228 case 'h': 229 usage (progname, 0); 230 break; 231 default: 232 usage (progname, 1); 233 break; 234 } 235 } 236 237 /* Initializations. */ 238 master = thread_master_create (); 239 240 /* Library inits. */ 241 signal_init (); 242 cmd_init (1); 243 debug_init (); 244 vty_init (); 245 memory_init (); 246 247 access_list_init (); 248 prefix_list_init (); 249 250 /* OSPFd inits. */ 251 ospf_init (); 252 ospf_if_init (); 253 ospf_zebra_init (); 254 255 /* OSPF vty inits. */ 256 ospf_vty_init (); 257 ospf_vty_show_init (); 258 259 ospf_route_map_init (); 260#ifdef HAVE_SNMP 261 ospf_snmp_init (); 262#endif /* HAVE_SNMP */ 263#ifdef HAVE_OPAQUE_LSA 264 ospf_opaque_init (); 265#endif /* HAVE_OPAQUE_LSA */ 266 267 sort_node (); 268 269 /* Get configuration file. */ 270 vty_read_config (config_file, config_current, config_default); 271 272 /* Change to the daemon program. */ 273 if (daemon_mode) 274 daemon (0, 0); 275 276 /* Process id file create. */ 277 pid_output (pid_file); 278 279 /* Create VTY socket */ 280 vty_serv_sock (vty_addr, 281 vty_port ? vty_port : OSPF_VTY_PORT, OSPF_VTYSH_PATH); 282 283 /* Print banner. */ 284 zlog (NULL, LOG_INFO, "OSPFd (%s) starts", ZEBRA_VERSION); 285 286 /* Fetch next active thread. */ 287 while (thread_fetch (master, &thread)) 288 thread_call (&thread); 289 290 /* Not reached. */ 291 exit (0); 292} 293 294