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" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | |
| 38 | #include "ospf6d.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 39 | |
| 40 | /* Default configuration file name for ospf6d. */ |
| 41 | #define OSPF6_DEFAULT_CONFIG "ospf6d.conf" |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 42 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | /* Default port values. */ |
| 44 | #define OSPF6_VTY_PORT 2606 |
| 45 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 46 | /* ospf6d privileges */ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 47 | zebra_capabilities_t _caps_p [] = |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 48 | { |
| 49 | ZCAP_RAW, |
| 50 | ZCAP_BIND |
| 51 | }; |
| 52 | |
| 53 | struct zebra_privs_t ospf6d_privs = |
| 54 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 55 | #if defined(QUAGGA_USER) |
| 56 | .user = QUAGGA_USER, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 57 | #endif |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 58 | #if defined QUAGGA_GROUP |
| 59 | .group = QUAGGA_GROUP, |
| 60 | #endif |
| 61 | #ifdef VTY_GROUP |
| 62 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 63 | #endif |
| 64 | .caps_p = _caps_p, |
| 65 | .cap_num_p = 2, |
| 66 | .cap_num_i = 0 |
| 67 | }; |
| 68 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | /* ospf6d options, we use GNU getopt library. */ |
| 70 | struct option longopts[] = |
| 71 | { |
| 72 | { "daemon", no_argument, NULL, 'd'}, |
| 73 | { "config_file", required_argument, NULL, 'f'}, |
| 74 | { "pid_file", required_argument, NULL, 'i'}, |
| 75 | { "vty_addr", required_argument, NULL, 'A'}, |
| 76 | { "vty_port", required_argument, NULL, 'P'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 77 | { "user", required_argument, NULL, 'u'}, |
| 78 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 79 | { "version", no_argument, NULL, 'v'}, |
| 80 | { "help", no_argument, NULL, 'h'}, |
| 81 | { 0 } |
| 82 | }; |
| 83 | |
| 84 | /* Configuration file and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG; |
| 86 | |
| 87 | /* ospf6d program name. */ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 88 | char *progname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | |
| 90 | /* is daemon? */ |
| 91 | int daemon_mode = 0; |
| 92 | |
| 93 | /* Master of threads. */ |
| 94 | struct thread_master *master; |
| 95 | |
| 96 | /* Process ID saved for use by init system */ |
paul | 0c083ee | 2004-10-10 12:54:58 +0000 | [diff] [blame] | 97 | const char *pid_file = PATH_OSPF6D_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | /* Help information display. */ |
| 100 | static void |
| 101 | usage (char *progname, int status) |
| 102 | { |
| 103 | if (status != 0) |
| 104 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 105 | else |
| 106 | { |
| 107 | printf ("Usage : %s [OPTION...]\n\n\ |
| 108 | Daemon which manages OSPF version 3.\n\n\ |
| 109 | -d, --daemon Runs in daemon mode\n\ |
| 110 | -f, --config_file Set configuration file name\n\ |
| 111 | -i, --pid_file Set process identifier file name\n\ |
| 112 | -A, --vty_addr Set vty's bind address\n\ |
| 113 | -P, --vty_port Set vty's port number\n\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 114 | -u, --user User to run as\n\ |
| 115 | -g, --group Group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | -v, --version Print program version\n\ |
| 117 | -h, --help Display this help and exit\n\ |
| 118 | \n\ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 119 | Report bugs to zebra@zebra.org\n", progname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | exit (status); |
| 123 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | |
| 125 | /* SIGHUP handler. */ |
| 126 | void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 127 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | { |
| 129 | zlog_info ("SIGHUP received"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* SIGINT handler. */ |
| 133 | void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 134 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 136 | zlog_notice ("Terminating on signal SIGINT"); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 137 | exit (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* SIGTERM handler. */ |
| 141 | void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 142 | sigterm (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 SIGTERM"); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 145 | exit (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* SIGUSR1 handler. */ |
| 149 | void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 150 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | { |
| 152 | zlog_info ("SIGUSR1 received"); |
| 153 | zlog_rotate (NULL); |
| 154 | } |
| 155 | |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 156 | struct quagga_signal_t ospf6_signals[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | { |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 158 | { |
| 159 | .signal = SIGHUP, |
| 160 | .handler = &sighup, |
| 161 | }, |
| 162 | { |
| 163 | .signal = SIGINT, |
| 164 | .handler = &sigint, |
| 165 | }, |
| 166 | { |
| 167 | .signal = SIGTERM, |
| 168 | .handler = &sigterm, |
| 169 | }, |
| 170 | { |
| 171 | .signal = SIGUSR1, |
| 172 | .handler = &sigusr1, |
| 173 | }, |
| 174 | }; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 175 | |
| 176 | /* Main routine of ospf6d. Treatment of argument and starting ospf finite |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | state machine is handled here. */ |
| 178 | int |
| 179 | main (int argc, char *argv[], char *envp[]) |
| 180 | { |
| 181 | char *p; |
| 182 | int opt; |
| 183 | char *vty_addr = NULL; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 184 | int vty_port = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 185 | char *config_file = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | struct thread thread; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | |
| 188 | /* Set umask before anything for security */ |
| 189 | umask (0027); |
| 190 | |
| 191 | /* Preserve name of myself. */ |
| 192 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 193 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | /* Command line argument treatment. */ |
| 195 | while (1) |
| 196 | { |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 197 | opt = getopt_long (argc, argv, "df:i:hp:A:P:u:g:v", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | |
| 199 | if (opt == EOF) |
| 200 | break; |
| 201 | |
| 202 | switch (opt) |
| 203 | { |
| 204 | case 0: |
| 205 | break; |
| 206 | case 'd': |
| 207 | daemon_mode = 1; |
| 208 | break; |
| 209 | case 'f': |
| 210 | config_file = optarg; |
| 211 | break; |
| 212 | case 'A': |
| 213 | vty_addr = optarg; |
| 214 | break; |
| 215 | case 'i': |
| 216 | pid_file = optarg; |
| 217 | break; |
| 218 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 219 | /* Deal with atoi() returning 0 on failure, and ospf6d not |
| 220 | listening on ospf6d port... */ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 221 | if (strcmp(optarg, "0") == 0) |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 222 | { |
| 223 | vty_port = 0; |
| 224 | break; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 225 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 226 | vty_port = atoi (optarg); |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 227 | vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 228 | break; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 229 | case 'u': |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 230 | ospf6d_privs.user = optarg; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 231 | break; |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 232 | case 'g': |
| 233 | ospf6d_privs.group = optarg; |
| 234 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 235 | case 'v': |
| 236 | print_version (progname); |
| 237 | exit (0); |
| 238 | break; |
| 239 | case 'h': |
| 240 | usage (progname, 0); |
| 241 | break; |
| 242 | default: |
| 243 | usage (progname, 1); |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* thread master */ |
| 249 | master = thread_master_create (); |
| 250 | |
| 251 | /* Initializations. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 252 | zlog_default = openzlog (progname, ZLOG_OSPF6, |
paul | 79dc373 | 2004-07-23 15:17:45 +0000 | [diff] [blame] | 253 | LOG_CONS|LOG_NDELAY|LOG_PID, |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 254 | LOG_DAEMON); |
| 255 | zprivs_init (&ospf6d_privs); |
| 256 | /* initialize zebra libraries */ |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 257 | signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 258 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 259 | vty_init (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 260 | memory_init (); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 261 | if_init (); |
| 262 | access_list_init (); |
| 263 | prefix_list_init (); |
| 264 | |
| 265 | /* initialize ospf6 */ |
| 266 | ospf6_init (); |
| 267 | |
| 268 | /* sort command vector */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 269 | sort_node (); |
| 270 | |
| 271 | /* parse config file */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 272 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | |
| 274 | if (daemon_mode) |
| 275 | daemon (0, 0); |
| 276 | |
| 277 | /* pid file create */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | pid_output (pid_file); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 280 | /* Make ospf6 vty socket. */ |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 281 | if (!vty_port) |
| 282 | vty_port = OSPF6_VTY_PORT; |
| 283 | vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 284 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 285 | /* Print start message */ |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 286 | zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d", |
| 287 | QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | |
| 289 | /* Start finite state machine, here we go! */ |
| 290 | while (thread_fetch (master, &thread)) |
| 291 | thread_call (&thread); |
| 292 | |
| 293 | /* Log in case thread failed */ |
| 294 | zlog_warn ("Thread failed"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | |
| 296 | /* Not reached. */ |
| 297 | exit (0); |
| 298 | } |
| 299 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 300 | |