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