paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RIPngd main routine. |
| 3 | * Copyright (C) 1998, 1999 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 25 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 26 | #include "getopt.h" |
| 27 | #include "vector.h" |
| 28 | #include "vty.h" |
| 29 | #include "command.h" |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 30 | #include "memory.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | #include "thread.h" |
| 32 | #include "log.h" |
| 33 | #include "prefix.h" |
| 34 | #include "if.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 35 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 36 | #include "sigevent.h" |
Feng Lu | 126215c | 2015-05-22 11:39:58 +0200 | [diff] [blame] | 37 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | |
| 39 | #include "ripngd/ripngd.h" |
| 40 | |
| 41 | /* Configuration filename and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG; |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 43 | char *config_file = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | |
| 45 | /* RIPngd options. */ |
| 46 | struct option longopts[] = |
| 47 | { |
| 48 | { "daemon", no_argument, NULL, 'd'}, |
| 49 | { "config_file", required_argument, NULL, 'f'}, |
| 50 | { "pid_file", required_argument, NULL, 'i'}, |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 51 | { "socket", required_argument, NULL, 'z'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 52 | { "dryrun", no_argument, NULL, 'C'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 53 | { "help", no_argument, NULL, 'h'}, |
| 54 | { "vty_addr", required_argument, NULL, 'A'}, |
| 55 | { "vty_port", required_argument, NULL, 'P'}, |
| 56 | { "retain", no_argument, NULL, 'r'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 57 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 58 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 59 | { "version", no_argument, NULL, 'v'}, |
| 60 | { 0 } |
| 61 | }; |
| 62 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 63 | /* ripngd privileges */ |
| 64 | zebra_capabilities_t _caps_p [] = |
| 65 | { |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 66 | ZCAP_NET_RAW, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 67 | ZCAP_BIND |
| 68 | }; |
| 69 | |
| 70 | struct zebra_privs_t ripngd_privs = |
| 71 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 72 | #if defined(QUAGGA_USER) |
| 73 | .user = QUAGGA_USER, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 74 | #endif |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 75 | #if defined QUAGGA_GROUP |
| 76 | .group = QUAGGA_GROUP, |
| 77 | #endif |
| 78 | #ifdef VTY_GROUP |
| 79 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 80 | #endif |
| 81 | .caps_p = _caps_p, |
| 82 | .cap_num_p = 2, |
| 83 | .cap_num_i = 0 |
| 84 | }; |
| 85 | |
| 86 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | /* RIPngd program name */ |
| 88 | |
| 89 | /* Route retain mode flag. */ |
| 90 | int retain_mode = 0; |
| 91 | |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 92 | /* RIPng VTY bind address. */ |
| 93 | char *vty_addr = NULL; |
| 94 | |
| 95 | /* RIPng VTY connection port. */ |
| 96 | int vty_port = RIPNG_VTY_PORT; |
| 97 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | /* Master of threads. */ |
| 99 | struct thread_master *master; |
| 100 | |
| 101 | /* Process ID saved for use by init system */ |
hasso | 7a1d583 | 2004-10-08 06:32:23 +0000 | [diff] [blame] | 102 | const char *pid_file = PATH_RIPNGD_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | |
| 104 | /* Help information display. */ |
| 105 | static void |
| 106 | usage (char *progname, int status) |
| 107 | { |
| 108 | if (status != 0) |
| 109 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 110 | else |
| 111 | { |
| 112 | printf ("Usage : %s [OPTION...]\n\ |
| 113 | Daemon which manages RIPng.\n\n\ |
| 114 | -d, --daemon Runs in daemon mode\n\ |
| 115 | -f, --config_file Set configuration file name\n\ |
| 116 | -i, --pid_file Set process identifier file name\n\ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 117 | -z, --socket Set path of zebra socket\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 118 | -A, --vty_addr Set vty's bind address\n\ |
| 119 | -P, --vty_port Set vty's port number\n\ |
| 120 | -r, --retain When program terminates, retain added route by ripngd.\n\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 121 | -u, --user User to run as\n\ |
| 122 | -g, --group Group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | -v, --version Print program version\n\ |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 124 | -C, --dryrun Check configuration for validity and exit\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | -h, --help Display this help and exit\n\ |
| 126 | \n\ |
| 127 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 128 | } |
| 129 | exit (status); |
| 130 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 131 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | /* SIGHUP handler. */ |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 133 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 134 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | { |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 136 | zlog_info ("SIGHUP received"); |
| 137 | ripng_clean (); |
| 138 | ripng_reset (); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 139 | |
| 140 | /* Reload config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 141 | vty_read_config (config_file, config_default); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 142 | /* Create VTY's socket */ |
| 143 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); |
| 144 | |
| 145 | /* Try to return to normal operation. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* SIGINT handler. */ |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [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) |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 155 | ripng_clean (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | |
| 157 | exit (0); |
| 158 | } |
| 159 | |
| 160 | /* SIGUSR1 handler. */ |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [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 | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 167 | struct quagga_signal_t ripng_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, |
| 179 | .handler = &sigint, |
| 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 | /* RIPngd main routine. */ |
| 188 | int |
| 189 | main (int argc, char **argv) |
| 190 | { |
| 191 | char *p; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 192 | int vty_port = RIPNG_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | int daemon_mode = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | char *progname; |
| 195 | struct thread thread; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 196 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 204 | zlog_default = openzlog(progname, ZLOG_RIPNG, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 206 | |
| 207 | while (1) |
| 208 | { |
| 209 | int opt; |
| 210 | |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 211 | opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:vC", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 212 | |
| 213 | if (opt == EOF) |
| 214 | break; |
| 215 | |
| 216 | switch (opt) |
| 217 | { |
| 218 | case 0: |
| 219 | break; |
| 220 | case 'd': |
| 221 | daemon_mode = 1; |
| 222 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | case 'f': |
| 224 | config_file = optarg; |
| 225 | break; |
| 226 | case 'A': |
| 227 | vty_addr = optarg; |
| 228 | break; |
| 229 | case 'i': |
| 230 | pid_file = optarg; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 231 | break; |
| 232 | case 'z': |
| 233 | zclient_serv_path_set (optarg); |
| 234 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 235 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 236 | /* Deal with atoi() returning 0 on failure, and ripngd not |
| 237 | listening on ripngd port... */ |
| 238 | if (strcmp(optarg, "0") == 0) |
| 239 | { |
| 240 | vty_port = 0; |
| 241 | break; |
| 242 | } |
| 243 | vty_port = atoi (optarg); |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 244 | if (vty_port <= 0 || vty_port > 0xffff) |
| 245 | vty_port = RIPNG_VTY_PORT; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 246 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 247 | case 'r': |
| 248 | retain_mode = 1; |
| 249 | break; |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 250 | case 'u': |
| 251 | ripngd_privs.user = optarg; |
| 252 | break; |
| 253 | case 'g': |
| 254 | ripngd_privs.group = optarg; |
| 255 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 256 | case 'v': |
| 257 | print_version (progname); |
| 258 | exit (0); |
| 259 | break; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 260 | case 'C': |
| 261 | dryrun = 1; |
| 262 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | case 'h': |
| 264 | usage (progname, 0); |
| 265 | break; |
| 266 | default: |
| 267 | usage (progname, 1); |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | master = thread_master_create (); |
| 273 | |
| 274 | /* Library inits. */ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 275 | zprivs_init (&ripngd_privs); |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 276 | signal_init (master, array_size(ripng_signals), ripng_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 277 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 278 | vty_init (master); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 279 | memory_init (); |
Feng Lu | 126215c | 2015-05-22 11:39:58 +0200 | [diff] [blame] | 280 | vrf_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | |
| 282 | /* RIPngd inits. */ |
| 283 | ripng_init (); |
| 284 | zebra_init (); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 285 | ripng_peer_init (); |
| 286 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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("RIPNGd daemon failed: %s", strerror(errno)); |
| 298 | exit (1); |
| 299 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 300 | |
| 301 | /* Create VTY socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 302 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 303 | |
| 304 | /* Process id file create. */ |
| 305 | pid_output (pid_file); |
| 306 | |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 307 | /* Print banner. */ |
| 308 | zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port); |
| 309 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 310 | /* Fetch next active 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 | } |