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