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