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