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