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