paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 39 | #include "privs.h" |
| 40 | #include "debug.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 41 | |
| 42 | #include "ospfd/ospfd.h" |
| 43 | #include "ospfd/ospf_interface.h" |
| 44 | #include "ospfd/ospf_asbr.h" |
| 45 | #include "ospfd/ospf_lsa.h" |
| 46 | #include "ospfd/ospf_lsdb.h" |
| 47 | #include "ospfd/ospf_neighbor.h" |
| 48 | #include "ospfd/ospf_dump.h" |
| 49 | #include "ospfd/ospf_zebra.h" |
| 50 | #include "ospfd/ospf_vty.h" |
| 51 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 52 | /* ospfd privileges */ |
| 53 | zebra_capabilities_t _caps_p [] = |
| 54 | { |
| 55 | ZCAP_RAW, |
| 56 | ZCAP_BIND, |
| 57 | ZCAP_BROADCAST, |
| 58 | ZCAP_ADMIN, |
| 59 | }; |
| 60 | |
| 61 | struct zebra_privs_t ospfd_privs = |
| 62 | { |
| 63 | #if defined(ZEBRA_USER) && defined(ZEBRA_GROUP) |
| 64 | .user = ZEBRA_USER, |
| 65 | .group = ZEBRA_GROUP, |
| 66 | #endif |
| 67 | #if defined(VTY_GROUP) |
| 68 | .vty_group = VTY_GROUP, |
| 69 | #endif |
| 70 | .caps_p = _caps_p, |
| 71 | .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]), |
| 72 | .cap_num_i = 0 |
| 73 | }; |
| 74 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | /* Configuration filename and directory. */ |
| 76 | char config_current[] = OSPF_DEFAULT_CONFIG; |
| 77 | char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG; |
| 78 | |
| 79 | /* OSPFd options. */ |
| 80 | struct option longopts[] = |
| 81 | { |
| 82 | { "daemon", no_argument, NULL, 'd'}, |
| 83 | { "config_file", required_argument, NULL, 'f'}, |
| 84 | { "pid_file", required_argument, NULL, 'i'}, |
| 85 | { "log_mode", no_argument, NULL, 'l'}, |
| 86 | { "help", no_argument, NULL, 'h'}, |
| 87 | { "vty_addr", required_argument, NULL, 'A'}, |
| 88 | { "vty_port", required_argument, NULL, 'P'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 89 | { "user", required_argument, NULL, 'u'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 90 | { "version", no_argument, NULL, 'v'}, |
| 91 | { 0 } |
| 92 | }; |
| 93 | |
| 94 | /* OSPFd program name */ |
| 95 | |
| 96 | /* Master of threads. */ |
| 97 | struct thread_master *master; |
| 98 | |
| 99 | /* Process ID saved for use by init system */ |
| 100 | char *pid_file = PATH_OSPFD_PID; |
| 101 | |
| 102 | /* Help information display. */ |
| 103 | static void |
| 104 | usage (char *progname, int status) |
| 105 | { |
| 106 | if (status != 0) |
| 107 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 108 | else |
| 109 | { |
| 110 | printf ("Usage : %s [OPTION...]\n\ |
| 111 | Daemon which manages OSPF.\n\n\ |
| 112 | -d, --daemon Runs in daemon mode\n\ |
| 113 | -f, --config_file Set configuration file name\n\ |
| 114 | -i, --pid_file Set process identifier file name\n\ |
| 115 | -A, --vty_addr Set vty's bind address\n\ |
| 116 | -P, --vty_port Set vty's port number\n\ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 117 | -u, --user User and group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 118 | -v, --version Print program version\n\ |
| 119 | -h, --help Display this help and exit\n\ |
| 120 | \n\ |
| 121 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 122 | } |
| 123 | exit (status); |
| 124 | } |
| 125 | |
| 126 | /* SIGHUP handler. */ |
| 127 | void |
| 128 | sighup (int sig) |
| 129 | { |
| 130 | zlog (NULL, LOG_INFO, "SIGHUP received"); |
| 131 | } |
| 132 | |
| 133 | /* SIGINT handler. */ |
| 134 | void |
| 135 | sigint (int sig) |
| 136 | { |
| 137 | zlog (NULL, LOG_INFO, "Terminating on signal"); |
| 138 | |
| 139 | ospf_terminate (); |
| 140 | |
| 141 | exit (0); |
| 142 | } |
| 143 | |
| 144 | /* SIGUSR1 handler. */ |
| 145 | void |
| 146 | sigusr1 (int sig) |
| 147 | { |
| 148 | zlog_rotate (NULL); |
| 149 | } |
| 150 | |
| 151 | /* Signal wrapper. */ |
| 152 | RETSIGTYPE * |
| 153 | signal_set (int signo, void (*func)(int)) |
| 154 | { |
| 155 | int ret; |
| 156 | struct sigaction sig; |
| 157 | struct sigaction osig; |
| 158 | |
| 159 | sig.sa_handler = func; |
| 160 | sigemptyset (&sig.sa_mask); |
| 161 | sig.sa_flags = 0; |
| 162 | #ifdef SA_RESTART |
| 163 | sig.sa_flags |= SA_RESTART; |
| 164 | #endif /* SA_RESTART */ |
| 165 | |
| 166 | ret = sigaction (signo, &sig, &osig); |
| 167 | |
| 168 | if (ret < 0) |
| 169 | return (SIG_ERR); |
| 170 | else |
| 171 | return (osig.sa_handler); |
| 172 | } |
| 173 | |
| 174 | /* Initialization of signal handles. */ |
| 175 | void |
| 176 | signal_init () |
| 177 | { |
| 178 | signal_set (SIGHUP, sighup); |
| 179 | signal_set (SIGINT, sigint); |
| 180 | signal_set (SIGTERM, sigint); |
| 181 | signal_set (SIGPIPE, SIG_IGN); |
| 182 | #ifdef SIGTSTP |
| 183 | signal_set (SIGTSTP, SIG_IGN); |
| 184 | #endif |
| 185 | #ifdef SIGTTIN |
| 186 | signal_set (SIGTTIN, SIG_IGN); |
| 187 | #endif |
| 188 | #ifdef SIGTTOU |
| 189 | signal_set (SIGTTOU, SIG_IGN); |
| 190 | #endif |
| 191 | signal_set (SIGUSR1, sigusr1); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 192 | #ifdef HAVE_GLIBC_BACKTRACE |
| 193 | signal_set (SIGBUS, debug_print_trace); |
| 194 | signal_set (SIGSEGV, debug_print_trace); |
| 195 | signal_set (SIGILL, debug_print_trace); |
| 196 | #endif /* HAVE_GLIBC_BACKTRACE */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* OSPFd main routine. */ |
| 200 | int |
| 201 | main (int argc, char **argv) |
| 202 | { |
| 203 | char *p; |
| 204 | char *vty_addr = NULL; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 205 | int vty_port = OSPF_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | int daemon_mode = 0; |
| 207 | char *config_file = NULL; |
| 208 | char *progname; |
| 209 | struct thread thread; |
| 210 | |
| 211 | /* Set umask before anything for security */ |
| 212 | umask (0027); |
| 213 | |
| 214 | /* get program name */ |
| 215 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 216 | |
| 217 | /* Invoked by a priviledged user? -- endo. */ |
hasso | b3516a7 | 2003-05-25 22:11:22 +0000 | [diff] [blame] | 218 | if (geteuid () != 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | { |
| 220 | errno = EPERM; |
| 221 | perror (progname); |
| 222 | exit (1); |
| 223 | } |
| 224 | |
| 225 | zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_OSPF, |
| 226 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 227 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 228 | /* OSPF master init. */ |
| 229 | ospf_master_init (); |
| 230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | while (1) |
| 232 | { |
| 233 | int opt; |
| 234 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 235 | opt = getopt_long (argc, argv, "dlf:hA:P:u:v", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 236 | |
| 237 | if (opt == EOF) |
| 238 | break; |
| 239 | |
| 240 | switch (opt) |
| 241 | { |
| 242 | case 0: |
| 243 | break; |
| 244 | case 'd': |
| 245 | daemon_mode = 1; |
| 246 | break; |
| 247 | case 'f': |
| 248 | config_file = optarg; |
| 249 | break; |
| 250 | case 'A': |
| 251 | vty_addr = optarg; |
| 252 | break; |
| 253 | case 'i': |
| 254 | pid_file = optarg; |
| 255 | break; |
| 256 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 257 | /* Deal with atoi() returning 0 on failure, and ospfd not |
| 258 | listening on ospfd port... */ |
| 259 | if (strcmp(optarg, "0") == 0) |
| 260 | { |
| 261 | vty_port = 0; |
| 262 | break; |
| 263 | } |
| 264 | vty_port = atoi (optarg); |
| 265 | vty_port = (vty_port ? vty_port : OSPF_VTY_PORT); |
| 266 | break; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 267 | case 'u': |
| 268 | ospfd_privs.group = ospfd_privs.user = optarg; |
| 269 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 270 | case 'v': |
| 271 | print_version (progname); |
| 272 | exit (0); |
| 273 | break; |
| 274 | case 'h': |
| 275 | usage (progname, 0); |
| 276 | break; |
| 277 | default: |
| 278 | usage (progname, 1); |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* Initializations. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 284 | master = om->master; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 285 | |
| 286 | /* Library inits. */ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 287 | zprivs_init (&ospfd_privs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | signal_init (); |
| 289 | cmd_init (1); |
| 290 | debug_init (); |
| 291 | vty_init (); |
| 292 | memory_init (); |
| 293 | |
| 294 | access_list_init (); |
| 295 | prefix_list_init (); |
| 296 | |
| 297 | /* OSPFd inits. */ |
| 298 | ospf_init (); |
| 299 | ospf_if_init (); |
| 300 | ospf_zebra_init (); |
| 301 | |
| 302 | /* OSPF vty inits. */ |
| 303 | ospf_vty_init (); |
| 304 | ospf_vty_show_init (); |
| 305 | |
| 306 | ospf_route_map_init (); |
| 307 | #ifdef HAVE_SNMP |
| 308 | ospf_snmp_init (); |
| 309 | #endif /* HAVE_SNMP */ |
| 310 | #ifdef HAVE_OPAQUE_LSA |
| 311 | ospf_opaque_init (); |
| 312 | #endif /* HAVE_OPAQUE_LSA */ |
| 313 | |
| 314 | sort_node (); |
| 315 | |
| 316 | /* Get configuration file. */ |
| 317 | vty_read_config (config_file, config_current, config_default); |
| 318 | |
| 319 | /* Change to the daemon program. */ |
| 320 | if (daemon_mode) |
| 321 | daemon (0, 0); |
| 322 | |
| 323 | /* Process id file create. */ |
| 324 | pid_output (pid_file); |
| 325 | |
| 326 | /* Create VTY socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 327 | vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | |
hasso | b3516a7 | 2003-05-25 22:11:22 +0000 | [diff] [blame] | 329 | #ifdef DEBUG |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | /* Print banner. */ |
| 331 | zlog (NULL, LOG_INFO, "OSPFd (%s) starts", ZEBRA_VERSION); |
hasso | b3516a7 | 2003-05-25 22:11:22 +0000 | [diff] [blame] | 332 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 333 | |
| 334 | /* Fetch next active thread. */ |
| 335 | while (thread_fetch (master, &thread)) |
| 336 | thread_call (&thread); |
| 337 | |
| 338 | /* Not reached. */ |
| 339 | exit (0); |
| 340 | } |
| 341 | |