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> |
| 23 | #include "getopt.h" |
| 24 | #include "thread.h" |
| 25 | #include "log.h" |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 26 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 27 | #include "command.h" |
| 28 | #include "vty.h" |
| 29 | #include "memory.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 30 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | |
| 32 | #include "ospf6d.h" |
| 33 | #include "ospf6_network.h" |
| 34 | |
| 35 | void ospf6_init (); |
| 36 | void ospf6_terminate (); |
| 37 | void nexthop_init (); |
| 38 | int ospf6_receive (struct thread *); |
| 39 | |
| 40 | extern int ospf6_sock; |
| 41 | |
| 42 | /* Default configuration file name for ospf6d. */ |
| 43 | #define OSPF6_DEFAULT_CONFIG "ospf6d.conf" |
| 44 | /* Default port values. */ |
| 45 | #define OSPF6_VTY_PORT 2606 |
| 46 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 47 | /* ospf6d privileges */ |
| 48 | zebra_capabilities_t _caps_p [] = |
| 49 | { |
| 50 | ZCAP_RAW, |
| 51 | ZCAP_BIND |
| 52 | }; |
| 53 | |
| 54 | struct zebra_privs_t ospf6d_privs = |
| 55 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 56 | #if defined(QUAGGA_USER) |
| 57 | .user = QUAGGA_USER, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 58 | #endif |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 59 | #if defined QUAGGA_GROUP |
| 60 | .group = QUAGGA_GROUP, |
| 61 | #endif |
| 62 | #ifdef VTY_GROUP |
| 63 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 64 | #endif |
| 65 | .caps_p = _caps_p, |
| 66 | .cap_num_p = 2, |
| 67 | .cap_num_i = 0 |
| 68 | }; |
| 69 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 70 | /* ospf6d options, we use GNU getopt library. */ |
| 71 | struct option longopts[] = |
| 72 | { |
| 73 | { "daemon", no_argument, NULL, 'd'}, |
| 74 | { "config_file", required_argument, NULL, 'f'}, |
| 75 | { "pid_file", required_argument, NULL, 'i'}, |
| 76 | { "vty_addr", required_argument, NULL, 'A'}, |
| 77 | { "vty_port", required_argument, NULL, 'P'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 78 | { "user", required_argument, NULL, 'u'}, |
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. */ |
| 85 | char config_current[] = OSPF6_DEFAULT_CONFIG; |
| 86 | char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG; |
| 87 | |
| 88 | /* ospf6d program name. */ |
| 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 */ |
| 97 | char *pid_file = PATH_OSPF6D_PID; |
| 98 | |
| 99 | /* for reload */ |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 100 | |
| 101 | #ifdef MAXPATHLEN |
| 102 | char _cwd[MAXPATHLEN]; |
| 103 | #else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | char _cwd[64]; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | char _progpath[64]; |
| 108 | int _argc; |
| 109 | char **_argv; |
| 110 | char **_envp; |
| 111 | |
| 112 | /* Help information display. */ |
| 113 | static void |
| 114 | usage (char *progname, int status) |
| 115 | { |
| 116 | if (status != 0) |
| 117 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 118 | else |
| 119 | { |
| 120 | printf ("Usage : %s [OPTION...]\n\n\ |
| 121 | Daemon which manages OSPF version 3.\n\n\ |
| 122 | -d, --daemon Runs in daemon mode\n\ |
| 123 | -f, --config_file Set configuration file name\n\ |
| 124 | -i, --pid_file Set process identifier file name\n\ |
| 125 | -A, --vty_addr Set vty's bind address\n\ |
| 126 | -P, --vty_port Set vty's port number\n\ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 127 | -u, --user User and group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | -v, --version Print program version\n\ |
| 129 | -h, --help Display this help and exit\n\ |
| 130 | \n\ |
| 131 | Report bugs to yasu@sfc.wide.ad.jp\n", progname); |
| 132 | } |
| 133 | |
| 134 | exit (status); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void |
| 139 | _reload () |
| 140 | { |
| 141 | zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) reloaded", |
paul | e8f2984 | 2003-08-12 13:08:31 +0000 | [diff] [blame] | 142 | QUAGGA_VERSION, OSPF6_DAEMON_VERSION); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 143 | ospf6_zebra_finish (); |
| 144 | vty_finish (); |
| 145 | execve (_progpath, _argv, _envp); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | terminate (int i) |
| 150 | { |
| 151 | ospf6_delete (ospf6); |
| 152 | unlink (PATH_OSPF6D_PID); |
| 153 | zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) terminated", |
paul | e8f2984 | 2003-08-12 13:08:31 +0000 | [diff] [blame] | 154 | QUAGGA_VERSION, OSPF6_DAEMON_VERSION); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | exit (i); |
| 156 | } |
| 157 | |
| 158 | /* SIGHUP handler. */ |
| 159 | void |
| 160 | sighup (int sig) |
| 161 | { |
| 162 | zlog_info ("SIGHUP received"); |
| 163 | _reload (); |
| 164 | } |
| 165 | |
| 166 | /* SIGINT handler. */ |
| 167 | void |
| 168 | sigint (int sig) |
| 169 | { |
| 170 | zlog_info ("SIGINT received"); |
| 171 | terminate (0); |
| 172 | } |
| 173 | |
| 174 | /* SIGTERM handler. */ |
| 175 | void |
| 176 | sigterm (int sig) |
| 177 | { |
| 178 | zlog_info ("SIGTERM received"); |
| 179 | terminate (0); |
| 180 | } |
| 181 | |
| 182 | /* SIGUSR1 handler. */ |
| 183 | void |
| 184 | sigusr1 (int sig) |
| 185 | { |
| 186 | zlog_info ("SIGUSR1 received"); |
| 187 | zlog_rotate (NULL); |
| 188 | } |
| 189 | |
| 190 | /* Signale wrapper. */ |
| 191 | RETSIGTYPE * |
| 192 | signal_set (int signo, void (*func)(int)) |
| 193 | { |
| 194 | int ret; |
| 195 | struct sigaction sig; |
| 196 | struct sigaction osig; |
| 197 | |
| 198 | sig.sa_handler = func; |
| 199 | sigemptyset (&sig.sa_mask); |
| 200 | sig.sa_flags = 0; |
| 201 | #ifdef SA_RESTART |
| 202 | sig.sa_flags |= SA_RESTART; |
| 203 | #endif /* SA_RESTART */ |
| 204 | |
| 205 | ret = sigaction (signo, &sig, &osig); |
| 206 | |
| 207 | if (ret < 0) |
| 208 | return (SIG_ERR); |
| 209 | else |
| 210 | return (osig.sa_handler); |
| 211 | } |
| 212 | |
| 213 | /* Initialization of signal handles. */ |
| 214 | void |
| 215 | signal_init () |
| 216 | { |
| 217 | signal_set (SIGHUP, sighup); |
| 218 | signal_set (SIGINT, sigint); |
| 219 | signal_set (SIGTERM, sigterm); |
| 220 | signal_set (SIGPIPE, SIG_IGN); |
| 221 | #ifdef SIGTSTP |
| 222 | signal_set (SIGTSTP, SIG_IGN); |
| 223 | #endif |
| 224 | #ifdef SIGTTIN |
| 225 | signal_set (SIGTTIN, SIG_IGN); |
| 226 | #endif |
| 227 | #ifdef SIGTTOU |
| 228 | signal_set (SIGTTOU, SIG_IGN); |
| 229 | #endif |
| 230 | signal_set (SIGUSR1, sigusr1); |
| 231 | } |
| 232 | |
| 233 | /* Main routine of ospf6d. Treatment of argument and start ospf finite |
| 234 | state machine is handled here. */ |
| 235 | int |
| 236 | main (int argc, char *argv[], char *envp[]) |
| 237 | { |
| 238 | char *p; |
| 239 | int opt; |
| 240 | char *vty_addr = NULL; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 241 | int vty_port = OSPF6_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 242 | char *config_file = NULL; |
| 243 | char *progname; |
| 244 | struct thread thread; |
| 245 | int flag; |
| 246 | |
| 247 | /* Set umask before anything for security */ |
| 248 | umask (0027); |
| 249 | |
| 250 | /* Preserve name of myself. */ |
| 251 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 252 | |
| 253 | /* for reload */ |
| 254 | _argc = argc; |
| 255 | _argv = argv; |
| 256 | _envp = envp; |
| 257 | getcwd (_cwd, sizeof (_cwd)); |
| 258 | if (*argv[0] == '.') |
| 259 | snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]); |
| 260 | else |
| 261 | snprintf (_progpath, sizeof (_progpath), "%s", argv[0]); |
| 262 | |
| 263 | /* Command line argument treatment. */ |
| 264 | while (1) |
| 265 | { |
paul | 96735ee | 2003-08-10 02:51:22 +0000 | [diff] [blame] | 266 | 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] | 267 | |
| 268 | if (opt == EOF) |
| 269 | break; |
| 270 | |
| 271 | switch (opt) |
| 272 | { |
| 273 | case 0: |
| 274 | break; |
| 275 | case 'd': |
| 276 | daemon_mode = 1; |
| 277 | break; |
| 278 | case 'f': |
| 279 | config_file = optarg; |
| 280 | break; |
| 281 | case 'A': |
| 282 | vty_addr = optarg; |
| 283 | break; |
| 284 | case 'i': |
| 285 | pid_file = optarg; |
| 286 | break; |
| 287 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 288 | /* Deal with atoi() returning 0 on failure, and ospf6d not |
| 289 | listening on ospf6d port... */ |
| 290 | if (strcmp(optarg, "0") == 0) |
| 291 | { |
| 292 | vty_port = 0; |
| 293 | break; |
| 294 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | vty_port = atoi (optarg); |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 296 | vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT); |
| 297 | break; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 298 | case 'u': |
| 299 | ospf6d_privs.user = ospf6d_privs.group = optarg; |
| 300 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | case 'v': |
| 302 | print_version (progname); |
| 303 | exit (0); |
| 304 | break; |
| 305 | case 'h': |
| 306 | usage (progname, 0); |
| 307 | break; |
| 308 | default: |
| 309 | usage (progname, 1); |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /* thread master */ |
| 315 | master = thread_master_create (); |
| 316 | |
| 317 | /* Initializations. */ |
| 318 | if (! daemon_mode) |
| 319 | flag = ZLOG_STDOUT; |
| 320 | else |
hasso | e26bbeb | 2003-05-25 21:39:29 +0000 | [diff] [blame] | 321 | flag = ZLOG_NOLOG; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | |
| 323 | zlog_default = openzlog (progname, flag, ZLOG_OSPF6, |
hasso | e26bbeb | 2003-05-25 21:39:29 +0000 | [diff] [blame] | 324 | LOG_CONS|LOG_NDELAY|LOG_PID, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 325 | LOG_DAEMON); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 326 | zprivs_init (&ospf6d_privs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | signal_init (); |
| 328 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 329 | vty_init (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | ospf6_init (); |
| 331 | memory_init (); |
| 332 | sort_node (); |
| 333 | |
| 334 | /* parse config file */ |
| 335 | vty_read_config (config_file, config_current, config_default); |
| 336 | |
| 337 | if (daemon_mode) |
| 338 | daemon (0, 0); |
| 339 | |
| 340 | /* pid file create */ |
| 341 | #if 0 |
| 342 | pid_output_lock (pid_file); |
| 343 | #else |
| 344 | pid_output (pid_file); |
| 345 | #endif |
| 346 | |
| 347 | /* Make ospf protocol socket. */ |
| 348 | ospf6_serv_sock (); |
| 349 | thread_add_read (master, ospf6_receive, NULL, ospf6_sock); |
| 350 | |
| 351 | /* Make ospf vty socket. */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 352 | vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 353 | |
hasso | e26bbeb | 2003-05-25 21:39:29 +0000 | [diff] [blame] | 354 | #ifdef DEBUG |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 355 | /* Print start message */ |
| 356 | zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) starts", |
paul | e8f2984 | 2003-08-12 13:08:31 +0000 | [diff] [blame] | 357 | QUAGGA_VERSION, OSPF6_DAEMON_VERSION); |
hasso | e26bbeb | 2003-05-25 21:39:29 +0000 | [diff] [blame] | 358 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 359 | |
| 360 | /* Start finite state machine, here we go! */ |
| 361 | while (thread_fetch (master, &thread)) |
| 362 | thread_call (&thread); |
| 363 | |
| 364 | /* Log in case thread failed */ |
| 365 | zlog_warn ("Thread failed"); |
| 366 | terminate (0); |
| 367 | |
| 368 | /* Not reached. */ |
| 369 | exit (0); |
| 370 | } |
| 371 | |