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