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 | |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 24 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 33 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 34 | #include "sigevent.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
| 36 | #include "ripd/ripd.h" |
| 37 | |
| 38 | /* ripd options. */ |
| 39 | static struct option longopts[] = |
| 40 | { |
| 41 | { "daemon", no_argument, NULL, 'd'}, |
| 42 | { "config_file", required_argument, NULL, 'f'}, |
| 43 | { "pid_file", required_argument, NULL, 'i'}, |
| 44 | { "help", no_argument, NULL, 'h'}, |
| 45 | { "vty_addr", required_argument, NULL, 'A'}, |
| 46 | { "vty_port", required_argument, NULL, 'P'}, |
| 47 | { "retain", no_argument, NULL, 'r'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 48 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 49 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | { "version", no_argument, NULL, 'v'}, |
| 51 | { 0 } |
| 52 | }; |
| 53 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 54 | /* ripd privileges */ |
| 55 | zebra_capabilities_t _caps_p [] = |
| 56 | { |
| 57 | ZCAP_RAW, |
| 58 | ZCAP_BIND |
| 59 | }; |
| 60 | |
| 61 | struct zebra_privs_t ripd_privs = |
| 62 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 63 | #if defined(QUAGGA_USER) |
| 64 | .user = QUAGGA_USER, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 65 | #endif |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 66 | #if defined QUAGGA_GROUP |
| 67 | .group = QUAGGA_GROUP, |
| 68 | #endif |
| 69 | #ifdef VTY_GROUP |
| 70 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 71 | #endif |
| 72 | .caps_p = _caps_p, |
| 73 | .cap_num_p = 2, |
| 74 | .cap_num_i = 0 |
| 75 | }; |
| 76 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | /* Configuration file and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG; |
| 79 | char *config_file = NULL; |
| 80 | |
| 81 | /* ripd program name */ |
| 82 | |
| 83 | /* Route retain mode flag. */ |
| 84 | int retain_mode = 0; |
| 85 | |
| 86 | /* RIP VTY bind address. */ |
| 87 | char *vty_addr = NULL; |
| 88 | |
| 89 | /* RIP VTY connection port. */ |
| 90 | int vty_port = RIP_VTY_PORT; |
| 91 | |
| 92 | /* Master of threads. */ |
| 93 | struct thread_master *master; |
| 94 | |
| 95 | /* Process ID saved for use by init system */ |
hasso | 8a676be | 2004-10-08 06:36:38 +0000 | [diff] [blame] | 96 | const char *pid_file = PATH_RIPD_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 97 | |
| 98 | /* Help information display. */ |
| 99 | static void |
| 100 | usage (char *progname, int status) |
| 101 | { |
| 102 | if (status != 0) |
| 103 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 104 | else |
| 105 | { |
| 106 | printf ("Usage : %s [OPTION...]\n\ |
| 107 | Daemon which manages RIP version 1 and 2.\n\n\ |
| 108 | -d, --daemon Runs in daemon mode\n\ |
| 109 | -f, --config_file Set configuration file name\n\ |
| 110 | -i, --pid_file Set process identifier file name\n\ |
| 111 | -A, --vty_addr Set vty's bind address\n\ |
| 112 | -P, --vty_port Set vty's port number\n\ |
| 113 | -r, --retain When program terminates, retain added route by ripd.\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\ |
| 119 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 120 | } |
| 121 | |
| 122 | exit (status); |
| 123 | } |
| 124 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | /* SIGHUP handler. */ |
| 126 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 127 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | { |
| 129 | zlog_info ("SIGHUP received"); |
| 130 | rip_clean (); |
| 131 | rip_reset (); |
| 132 | zlog_info ("ripd restarting!"); |
| 133 | |
| 134 | /* Reload config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 135 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | |
| 137 | /* Create VTY's socket */ |
| 138 | vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH); |
| 139 | |
| 140 | /* Try to return to normal operation. */ |
| 141 | } |
| 142 | |
| 143 | /* SIGINT handler. */ |
| 144 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 145 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 147 | zlog_notice ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | |
| 149 | if (! retain_mode) |
| 150 | rip_clean (); |
| 151 | |
| 152 | exit (0); |
| 153 | } |
| 154 | |
| 155 | /* SIGUSR1 handler. */ |
| 156 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 157 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | { |
| 159 | zlog_rotate (NULL); |
| 160 | } |
| 161 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 162 | struct quagga_signal_t ripd_signals[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | { |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 164 | { |
| 165 | .signal = SIGHUP, |
| 166 | .handler = &sighup, |
| 167 | }, |
| 168 | { |
| 169 | .signal = SIGUSR1, |
| 170 | .handler = &sigusr1, |
| 171 | }, |
| 172 | { |
| 173 | .signal = SIGINT, |
hasso | 8c903fb | 2004-03-17 20:39:18 +0000 | [diff] [blame] | 174 | .handler = &sigint, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 175 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 176 | { |
| 177 | .signal = SIGTERM, |
| 178 | .handler = &sigint, |
| 179 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 180 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | |
| 182 | /* Main routine of ripd. */ |
| 183 | int |
| 184 | main (int argc, char **argv) |
| 185 | { |
| 186 | char *p; |
| 187 | int daemon_mode = 0; |
| 188 | char *progname; |
| 189 | struct thread thread; |
| 190 | |
| 191 | /* Set umask before anything for security */ |
| 192 | umask (0027); |
| 193 | |
| 194 | /* Get program name. */ |
| 195 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 196 | |
| 197 | /* First of all we need logging init. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 198 | zlog_default = openzlog (progname, ZLOG_RIP, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 199 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 200 | |
| 201 | /* Command line option parse. */ |
| 202 | while (1) |
| 203 | { |
| 204 | int opt; |
| 205 | |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 206 | opt = getopt_long (argc, argv, "df:i:hA:P:u:g:rv", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 207 | |
| 208 | if (opt == EOF) |
| 209 | break; |
| 210 | |
| 211 | switch (opt) |
| 212 | { |
| 213 | case 0: |
| 214 | break; |
| 215 | case 'd': |
| 216 | daemon_mode = 1; |
| 217 | break; |
| 218 | case 'f': |
| 219 | config_file = optarg; |
| 220 | break; |
| 221 | case 'A': |
| 222 | vty_addr = optarg; |
| 223 | break; |
| 224 | case 'i': |
| 225 | pid_file = optarg; |
| 226 | break; |
| 227 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 228 | /* Deal with atoi() returning 0 on failure, and ripd not |
| 229 | listening on rip port... */ |
| 230 | if (strcmp(optarg, "0") == 0) |
| 231 | { |
| 232 | vty_port = 0; |
| 233 | break; |
| 234 | } |
| 235 | vty_port = atoi (optarg); |
| 236 | vty_port = (vty_port ? vty_port : RIP_VTY_PORT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 237 | break; |
| 238 | case 'r': |
| 239 | retain_mode = 1; |
| 240 | break; |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 241 | case 'u': |
| 242 | ripd_privs.user = optarg; |
| 243 | break; |
| 244 | case 'g': |
| 245 | ripd_privs.group = optarg; |
| 246 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 247 | case 'v': |
| 248 | print_version (progname); |
| 249 | exit (0); |
| 250 | break; |
| 251 | case 'h': |
| 252 | usage (progname, 0); |
| 253 | break; |
| 254 | default: |
| 255 | usage (progname, 1); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /* Prepare master thread. */ |
| 261 | master = thread_master_create (); |
| 262 | |
| 263 | /* Library initialization. */ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 264 | zprivs_init (&ripd_privs); |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 265 | signal_init (master, Q_SIGC(ripd_signals), ripd_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 267 | vty_init (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 268 | memory_init (); |
| 269 | keychain_init (); |
| 270 | |
| 271 | /* RIP related initialization. */ |
| 272 | rip_init (); |
| 273 | rip_if_init (); |
| 274 | rip_zclient_init (); |
| 275 | rip_peer_init (); |
| 276 | |
| 277 | /* Sort all installed commands. */ |
| 278 | sort_node (); |
| 279 | |
| 280 | /* Get configuration file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 281 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | |
| 283 | /* Change to the daemon program. */ |
| 284 | if (daemon_mode) |
| 285 | daemon (0, 0); |
| 286 | |
| 287 | /* Pid file create. */ |
| 288 | pid_output (pid_file); |
| 289 | |
| 290 | /* Create VTY's socket */ |
| 291 | vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH); |
| 292 | |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 293 | /* Print banner. */ |
| 294 | zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port); |
| 295 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 296 | /* Execute each thread. */ |
| 297 | while (thread_fetch (master, &thread)) |
| 298 | thread_call (&thread); |
| 299 | |
| 300 | /* Not reached. */ |
| 301 | exit (0); |
| 302 | } |