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