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