paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RIPngd main routine. |
| 3 | * Copyright (C) 1998, 1999 Kunihiro Ishiguro |
| 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 | |
| 25 | #include "version.h" |
| 26 | #include "getopt.h" |
| 27 | #include "vector.h" |
| 28 | #include "vty.h" |
| 29 | #include "command.h" |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame^] | 30 | #include "memory.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | #include "thread.h" |
| 32 | #include "log.h" |
| 33 | #include "prefix.h" |
| 34 | #include "if.h" |
| 35 | |
| 36 | #include "ripngd/ripngd.h" |
| 37 | |
| 38 | /* Configuration filename and directory. */ |
| 39 | char config_current[] = RIPNG_DEFAULT_CONFIG; |
| 40 | char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG; |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame^] | 41 | char *config_file = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | |
| 43 | /* RIPngd options. */ |
| 44 | struct option longopts[] = |
| 45 | { |
| 46 | { "daemon", no_argument, NULL, 'd'}, |
| 47 | { "config_file", required_argument, NULL, 'f'}, |
| 48 | { "pid_file", required_argument, NULL, 'i'}, |
| 49 | { "log_mode", no_argument, NULL, 'l'}, |
| 50 | { "help", no_argument, NULL, 'h'}, |
| 51 | { "vty_addr", required_argument, NULL, 'A'}, |
| 52 | { "vty_port", required_argument, NULL, 'P'}, |
| 53 | { "retain", no_argument, NULL, 'r'}, |
| 54 | { "version", no_argument, NULL, 'v'}, |
| 55 | { 0 } |
| 56 | }; |
| 57 | |
| 58 | /* RIPngd program name */ |
| 59 | |
| 60 | /* Route retain mode flag. */ |
| 61 | int retain_mode = 0; |
| 62 | |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame^] | 63 | /* RIPng VTY bind address. */ |
| 64 | char *vty_addr = NULL; |
| 65 | |
| 66 | /* RIPng VTY connection port. */ |
| 67 | int vty_port = RIPNG_VTY_PORT; |
| 68 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | /* Master of threads. */ |
| 70 | struct thread_master *master; |
| 71 | |
| 72 | /* Process ID saved for use by init system */ |
| 73 | char *pid_file = PATH_RIPNGD_PID; |
| 74 | |
| 75 | /* Help information display. */ |
| 76 | static void |
| 77 | usage (char *progname, int status) |
| 78 | { |
| 79 | if (status != 0) |
| 80 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 81 | else |
| 82 | { |
| 83 | printf ("Usage : %s [OPTION...]\n\ |
| 84 | Daemon which manages RIPng.\n\n\ |
| 85 | -d, --daemon Runs in daemon mode\n\ |
| 86 | -f, --config_file Set configuration file name\n\ |
| 87 | -i, --pid_file Set process identifier file name\n\ |
| 88 | -l. --log_mode Set verbose log mode flag\n\ |
| 89 | -A, --vty_addr Set vty's bind address\n\ |
| 90 | -P, --vty_port Set vty's port number\n\ |
| 91 | -r, --retain When program terminates, retain added route by ripngd.\n\ |
| 92 | -v, --version Print program version\n\ |
| 93 | -h, --help Display this help and exit\n\ |
| 94 | \n\ |
| 95 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 96 | } |
| 97 | exit (status); |
| 98 | } |
| 99 | |
| 100 | /* SIGHUP handler. */ |
| 101 | void |
| 102 | sighup (int sig) |
| 103 | { |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame^] | 104 | zlog_info ("SIGHUP received"); |
| 105 | ripng_clean (); |
| 106 | ripng_reset (); |
| 107 | zlog_info ("Terminating on signal"); |
| 108 | |
| 109 | /* Reload config file. */ |
| 110 | vty_read_config (config_file, config_current, config_default); |
| 111 | /* Create VTY's socket */ |
| 112 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); |
| 113 | |
| 114 | /* Try to return to normal operation. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* SIGINT handler. */ |
| 118 | void |
| 119 | sigint (int sig) |
| 120 | { |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame^] | 121 | zlog_info ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 122 | |
| 123 | if (! retain_mode) |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame^] | 124 | ripng_clean (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | |
| 126 | exit (0); |
| 127 | } |
| 128 | |
| 129 | /* SIGUSR1 handler. */ |
| 130 | void |
| 131 | sigusr1 (int sig) |
| 132 | { |
| 133 | zlog_rotate (NULL); |
| 134 | } |
| 135 | |
| 136 | /* Signale wrapper. */ |
| 137 | RETSIGTYPE * |
| 138 | signal_set (int signo, void (*func)(int)) |
| 139 | { |
| 140 | int ret; |
| 141 | struct sigaction sig; |
| 142 | struct sigaction osig; |
| 143 | |
| 144 | sig.sa_handler = func; |
| 145 | sigemptyset (&sig.sa_mask); |
| 146 | sig.sa_flags = 0; |
| 147 | #ifdef SA_RESTART |
| 148 | sig.sa_flags |= SA_RESTART; |
| 149 | #endif /* SA_RESTART */ |
| 150 | |
| 151 | ret = sigaction (signo, &sig, &osig); |
| 152 | |
| 153 | if (ret < 0) |
| 154 | return (SIG_ERR); |
| 155 | else |
| 156 | return (osig.sa_handler); |
| 157 | } |
| 158 | |
| 159 | /* Initialization of signal handles. */ |
| 160 | void |
| 161 | signal_init () |
| 162 | { |
| 163 | signal_set (SIGHUP, sighup); |
| 164 | signal_set (SIGINT, sigint); |
| 165 | signal_set (SIGTERM, sigint); |
| 166 | signal_set (SIGPIPE, SIG_IGN); |
| 167 | signal_set (SIGUSR1, sigusr1); |
| 168 | } |
| 169 | |
| 170 | /* RIPngd main routine. */ |
| 171 | int |
| 172 | main (int argc, char **argv) |
| 173 | { |
| 174 | char *p; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 175 | int vty_port = RIPNG_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 176 | int daemon_mode = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | char *progname; |
| 178 | struct thread thread; |
| 179 | |
| 180 | /* Set umask before anything for security */ |
| 181 | umask (0027); |
| 182 | |
| 183 | /* get program name */ |
| 184 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 185 | |
| 186 | zlog_default = openzlog(progname, ZLOG_NOLOG, ZLOG_RIPNG, |
| 187 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 188 | |
| 189 | while (1) |
| 190 | { |
| 191 | int opt; |
| 192 | |
| 193 | opt = getopt_long (argc, argv, "dlf:hA:P:v", longopts, 0); |
| 194 | |
| 195 | if (opt == EOF) |
| 196 | break; |
| 197 | |
| 198 | switch (opt) |
| 199 | { |
| 200 | case 0: |
| 201 | break; |
| 202 | case 'd': |
| 203 | daemon_mode = 1; |
| 204 | break; |
| 205 | case 'l': |
| 206 | /* log_mode = 1; */ |
| 207 | break; |
| 208 | case 'f': |
| 209 | config_file = optarg; |
| 210 | break; |
| 211 | case 'A': |
| 212 | vty_addr = optarg; |
| 213 | break; |
| 214 | case 'i': |
| 215 | pid_file = optarg; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 216 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 218 | /* Deal with atoi() returning 0 on failure, and ripngd not |
| 219 | listening on ripngd port... */ |
| 220 | if (strcmp(optarg, "0") == 0) |
| 221 | { |
| 222 | vty_port = 0; |
| 223 | break; |
| 224 | } |
| 225 | vty_port = atoi (optarg); |
| 226 | vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT); |
| 227 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 228 | case 'r': |
| 229 | retain_mode = 1; |
| 230 | break; |
| 231 | case 'v': |
| 232 | print_version (progname); |
| 233 | exit (0); |
| 234 | break; |
| 235 | case 'h': |
| 236 | usage (progname, 0); |
| 237 | break; |
| 238 | default: |
| 239 | usage (progname, 1); |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | master = thread_master_create (); |
| 245 | |
| 246 | /* Library inits. */ |
| 247 | signal_init (); |
| 248 | cmd_init (1); |
| 249 | vty_init (); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame^] | 250 | memory_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | |
| 252 | /* RIPngd inits. */ |
| 253 | ripng_init (); |
| 254 | zebra_init (); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame^] | 255 | ripng_peer_init (); |
| 256 | |
| 257 | /* Sort all installed commands. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 258 | sort_node (); |
| 259 | |
| 260 | /* Get configuration file. */ |
| 261 | vty_read_config (config_file, config_current, config_default); |
| 262 | |
| 263 | /* Change to the daemon program. */ |
| 264 | if (daemon_mode) |
| 265 | daemon (0, 0); |
| 266 | |
| 267 | /* Create VTY socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 268 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 269 | |
| 270 | /* Process id file create. */ |
| 271 | pid_output (pid_file); |
| 272 | |
| 273 | /* Fetch next active thread. */ |
| 274 | while (thread_fetch (master, &thread)) |
| 275 | thread_call (&thread); |
| 276 | |
| 277 | /* Not reached. */ |
| 278 | exit (0); |
| 279 | } |