paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999 Yasuhiro Ohara |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
hasso | 3b4cd3a | 2004-05-18 19:28:32 +0000 | [diff] [blame] | 23 | #include <lib/version.h> |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 24 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 25 | #include "getopt.h" |
| 26 | #include "thread.h" |
| 27 | #include "log.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | #include "command.h" |
| 29 | #include "vty.h" |
| 30 | #include "memory.h" |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 31 | #include "if.h" |
| 32 | #include "filter.h" |
| 33 | #include "prefix.h" |
| 34 | #include "plist.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 35 | #include "privs.h" |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 36 | #include "sigevent.h" |
Denis Ovsienko | 87362ce | 2011-08-27 22:19:34 +0400 | [diff] [blame] | 37 | #include "zclient.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | |
| 39 | #include "ospf6d.h" |
Denis Ovsienko | 87362ce | 2011-08-27 22:19:34 +0400 | [diff] [blame] | 40 | #include "ospf6_top.h" |
| 41 | #include "ospf6_message.h" |
| 42 | #include "ospf6_asbr.h" |
| 43 | #include "ospf6_lsa.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | |
| 45 | /* Default configuration file name for ospf6d. */ |
| 46 | #define OSPF6_DEFAULT_CONFIG "ospf6d.conf" |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 47 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 48 | /* Default port values. */ |
| 49 | #define OSPF6_VTY_PORT 2606 |
| 50 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 51 | /* ospf6d privileges */ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 52 | zebra_capabilities_t _caps_p [] = |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 53 | { |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 54 | ZCAP_NET_RAW, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 55 | ZCAP_BIND |
| 56 | }; |
| 57 | |
| 58 | struct zebra_privs_t ospf6d_privs = |
| 59 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 60 | #if defined(QUAGGA_USER) |
| 61 | .user = QUAGGA_USER, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 62 | #endif |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 63 | #if defined QUAGGA_GROUP |
| 64 | .group = QUAGGA_GROUP, |
| 65 | #endif |
| 66 | #ifdef VTY_GROUP |
| 67 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 68 | #endif |
| 69 | .caps_p = _caps_p, |
| 70 | .cap_num_p = 2, |
| 71 | .cap_num_i = 0 |
| 72 | }; |
| 73 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 74 | /* ospf6d options, we use GNU getopt library. */ |
| 75 | struct option longopts[] = |
| 76 | { |
| 77 | { "daemon", no_argument, NULL, 'd'}, |
| 78 | { "config_file", required_argument, NULL, 'f'}, |
| 79 | { "pid_file", required_argument, NULL, 'i'}, |
| 80 | { "vty_addr", required_argument, NULL, 'A'}, |
| 81 | { "vty_port", required_argument, NULL, 'P'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 82 | { "user", required_argument, NULL, 'u'}, |
| 83 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | { "version", no_argument, NULL, 'v'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 85 | { "dryrun", no_argument, NULL, 'C'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 86 | { "help", no_argument, NULL, 'h'}, |
| 87 | { 0 } |
| 88 | }; |
| 89 | |
| 90 | /* Configuration file and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 91 | char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG; |
| 92 | |
| 93 | /* ospf6d program name. */ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 94 | char *progname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 95 | |
| 96 | /* is daemon? */ |
| 97 | int daemon_mode = 0; |
| 98 | |
| 99 | /* Master of threads. */ |
| 100 | struct thread_master *master; |
| 101 | |
| 102 | /* Process ID saved for use by init system */ |
paul | 0c083ee | 2004-10-10 12:54:58 +0000 | [diff] [blame] | 103 | const char *pid_file = PATH_OSPF6D_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +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\n\ |
| 114 | Daemon which manages OSPF version 3.\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\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 120 | -u, --user User to run as\n\ |
| 121 | -g, --group Group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 122 | -v, --version Print program version\n\ |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 123 | -C, --dryrun Check configuration for validity and exit\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | -h, --help Display this help and exit\n\ |
| 125 | \n\ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 126 | Report bugs to zebra@zebra.org\n", progname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | exit (status); |
| 130 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 131 | |
Stephen Hemminger | c143c38 | 2011-12-07 01:25:46 +0400 | [diff] [blame] | 132 | static void __attribute__ ((noreturn)) |
Tom Goff | ae2254a | 2010-11-10 13:01:41 -0800 | [diff] [blame] | 133 | ospf6_exit (int status) |
| 134 | { |
| 135 | extern struct ospf6 *ospf6; |
| 136 | extern struct zclient *zclient; |
| 137 | |
| 138 | if (ospf6) |
| 139 | ospf6_delete (ospf6); |
| 140 | |
| 141 | ospf6_message_terminate (); |
| 142 | ospf6_asbr_terminate (); |
| 143 | ospf6_lsa_terminate (); |
| 144 | |
| 145 | if_terminate (); |
| 146 | vty_terminate (); |
| 147 | cmd_terminate (); |
| 148 | |
| 149 | if (zclient) |
| 150 | zclient_free (zclient); |
| 151 | |
| 152 | if (master) |
| 153 | thread_master_free (master); |
| 154 | |
| 155 | if (zlog_default) |
| 156 | closezlog (zlog_default); |
| 157 | |
| 158 | exit (status); |
| 159 | } |
| 160 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | /* SIGHUP handler. */ |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 162 | static void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 163 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 164 | { |
| 165 | zlog_info ("SIGHUP received"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* SIGINT handler. */ |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 169 | static void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 170 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 172 | zlog_notice ("Terminating on signal SIGINT"); |
Tom Goff | ae2254a | 2010-11-10 13:01:41 -0800 | [diff] [blame] | 173 | ospf6_exit (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* SIGTERM handler. */ |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 177 | static void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 178 | sigterm (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 179 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 180 | zlog_notice ("Terminating on signal SIGTERM"); |
Tom Goff | ae2254a | 2010-11-10 13:01:41 -0800 | [diff] [blame] | 181 | ospf6_exit (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* SIGUSR1 handler. */ |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 185 | static void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 186 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | { |
| 188 | zlog_info ("SIGUSR1 received"); |
| 189 | zlog_rotate (NULL); |
| 190 | } |
| 191 | |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 192 | struct quagga_signal_t ospf6_signals[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | { |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 194 | { |
| 195 | .signal = SIGHUP, |
| 196 | .handler = &sighup, |
| 197 | }, |
| 198 | { |
| 199 | .signal = SIGINT, |
| 200 | .handler = &sigint, |
| 201 | }, |
| 202 | { |
| 203 | .signal = SIGTERM, |
| 204 | .handler = &sigterm, |
| 205 | }, |
| 206 | { |
| 207 | .signal = SIGUSR1, |
| 208 | .handler = &sigusr1, |
| 209 | }, |
| 210 | }; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 211 | |
| 212 | /* Main routine of ospf6d. Treatment of argument and starting ospf finite |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | state machine is handled here. */ |
| 214 | int |
| 215 | main (int argc, char *argv[], char *envp[]) |
| 216 | { |
| 217 | char *p; |
| 218 | int opt; |
| 219 | char *vty_addr = NULL; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 220 | int vty_port = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 221 | char *config_file = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | struct thread thread; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 223 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | |
| 225 | /* Set umask before anything for security */ |
| 226 | umask (0027); |
| 227 | |
| 228 | /* Preserve name of myself. */ |
| 229 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | /* Command line argument treatment. */ |
| 232 | while (1) |
| 233 | { |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 234 | opt = getopt_long (argc, argv, "df:i:hp:A:P:u:g:vC", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 235 | |
| 236 | if (opt == EOF) |
| 237 | break; |
| 238 | |
| 239 | switch (opt) |
| 240 | { |
| 241 | case 0: |
| 242 | break; |
| 243 | case 'd': |
| 244 | daemon_mode = 1; |
| 245 | break; |
| 246 | case 'f': |
| 247 | config_file = optarg; |
| 248 | break; |
| 249 | case 'A': |
| 250 | vty_addr = optarg; |
| 251 | break; |
| 252 | case 'i': |
| 253 | pid_file = optarg; |
| 254 | break; |
| 255 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 256 | /* Deal with atoi() returning 0 on failure, and ospf6d not |
| 257 | listening on ospf6d port... */ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 258 | if (strcmp(optarg, "0") == 0) |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 259 | { |
| 260 | vty_port = 0; |
| 261 | break; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 262 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | vty_port = atoi (optarg); |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 264 | if (vty_port <= 0 || vty_port > 0xffff) |
| 265 | vty_port = OSPF6_VTY_PORT; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 266 | break; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 267 | case 'u': |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 268 | ospf6d_privs.user = optarg; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 269 | break; |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 270 | case 'g': |
| 271 | ospf6d_privs.group = optarg; |
| 272 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | case 'v': |
| 274 | print_version (progname); |
| 275 | exit (0); |
| 276 | break; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 277 | case 'C': |
| 278 | dryrun = 1; |
| 279 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | case 'h': |
| 281 | usage (progname, 0); |
| 282 | break; |
| 283 | default: |
| 284 | usage (progname, 1); |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* thread master */ |
| 290 | master = thread_master_create (); |
| 291 | |
| 292 | /* Initializations. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 293 | zlog_default = openzlog (progname, ZLOG_OSPF6, |
paul | 79dc373 | 2004-07-23 15:17:45 +0000 | [diff] [blame] | 294 | LOG_CONS|LOG_NDELAY|LOG_PID, |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 295 | LOG_DAEMON); |
| 296 | zprivs_init (&ospf6d_privs); |
| 297 | /* initialize zebra libraries */ |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 298 | signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 299 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 300 | vty_init (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | memory_init (); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 302 | if_init (); |
| 303 | access_list_init (); |
| 304 | prefix_list_init (); |
| 305 | |
| 306 | /* initialize ospf6 */ |
| 307 | ospf6_init (); |
| 308 | |
| 309 | /* sort command vector */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 310 | sort_node (); |
| 311 | |
| 312 | /* parse config file */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 313 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 315 | /* Start execution only if not in dry-run mode */ |
| 316 | if (dryrun) |
| 317 | return(0); |
| 318 | |
Stephen Hemminger | 065de90 | 2009-08-07 11:13:49 -0700 | [diff] [blame] | 319 | if (daemon_mode && daemon (0, 0) < 0) |
| 320 | { |
| 321 | zlog_err("OSPF6d daemon failed: %s", strerror(errno)); |
| 322 | exit (1); |
| 323 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 324 | |
| 325 | /* pid file create */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | pid_output (pid_file); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 328 | /* Make ospf6 vty socket. */ |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 329 | if (!vty_port) |
| 330 | vty_port = OSPF6_VTY_PORT; |
| 331 | vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 332 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 333 | /* Print start message */ |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 334 | zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d", |
| 335 | QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | |
| 337 | /* Start finite state machine, here we go! */ |
| 338 | while (thread_fetch (master, &thread)) |
| 339 | thread_call (&thread); |
| 340 | |
| 341 | /* Log in case thread failed */ |
| 342 | zlog_warn ("Thread failed"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 343 | |
| 344 | /* Not reached. */ |
Tom Goff | ae2254a | 2010-11-10 13:01:41 -0800 | [diff] [blame] | 345 | ospf6_exit (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | } |
| 347 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 348 | |