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'}, |
| 50 | { "log_mode", no_argument, NULL, 'l'}, |
| 51 | { "help", no_argument, NULL, 'h'}, |
| 52 | { "vty_addr", required_argument, NULL, 'A'}, |
| 53 | { "vty_port", required_argument, NULL, 'P'}, |
| 54 | { "retain", no_argument, NULL, 'r'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 55 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame^] | 56 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | { "version", no_argument, NULL, 'v'}, |
| 58 | { 0 } |
| 59 | }; |
| 60 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 61 | /* ripngd privileges */ |
| 62 | zebra_capabilities_t _caps_p [] = |
| 63 | { |
| 64 | ZCAP_RAW, |
| 65 | ZCAP_BIND |
| 66 | }; |
| 67 | |
| 68 | struct zebra_privs_t ripngd_privs = |
| 69 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 70 | #if defined(QUAGGA_USER) |
| 71 | .user = QUAGGA_USER, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 72 | #endif |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 73 | #if defined QUAGGA_GROUP |
| 74 | .group = QUAGGA_GROUP, |
| 75 | #endif |
| 76 | #ifdef VTY_GROUP |
| 77 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 78 | #endif |
| 79 | .caps_p = _caps_p, |
| 80 | .cap_num_p = 2, |
| 81 | .cap_num_i = 0 |
| 82 | }; |
| 83 | |
| 84 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | /* RIPngd program name */ |
| 86 | |
| 87 | /* Route retain mode flag. */ |
| 88 | int retain_mode = 0; |
| 89 | |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 90 | /* RIPng VTY bind address. */ |
| 91 | char *vty_addr = NULL; |
| 92 | |
| 93 | /* RIPng VTY connection port. */ |
| 94 | int vty_port = RIPNG_VTY_PORT; |
| 95 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | /* Master of threads. */ |
| 97 | struct thread_master *master; |
| 98 | |
| 99 | /* Process ID saved for use by init system */ |
hasso | 7a1d583 | 2004-10-08 06:32:23 +0000 | [diff] [blame] | 100 | const char *pid_file = PATH_RIPNGD_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 RIPng.\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\ |
| 115 | -l. --log_mode Set verbose log mode flag\n\ |
| 116 | -A, --vty_addr Set vty's bind address\n\ |
| 117 | -P, --vty_port Set vty's port number\n\ |
| 118 | -r, --retain When program terminates, retain added route by ripngd.\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 | exit (status); |
| 127 | } |
| 128 | |
| 129 | /* SIGHUP handler. */ |
| 130 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 131 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | { |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 133 | zlog_info ("SIGHUP received"); |
| 134 | ripng_clean (); |
| 135 | ripng_reset (); |
| 136 | zlog_info ("Terminating on signal"); |
| 137 | |
| 138 | /* Reload config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 139 | vty_read_config (config_file, config_default); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 140 | /* Create VTY's socket */ |
| 141 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); |
| 142 | |
| 143 | /* Try to return to normal operation. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* SIGINT handler. */ |
| 147 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 148 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | { |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 150 | zlog_info ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | |
| 152 | if (! retain_mode) |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 153 | ripng_clean (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 154 | |
| 155 | exit (0); |
| 156 | } |
| 157 | |
| 158 | /* SIGUSR1 handler. */ |
| 159 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 160 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | { |
| 162 | zlog_rotate (NULL); |
| 163 | } |
| 164 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 165 | struct quagga_signal_t ripng_signals[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | { |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 167 | { |
| 168 | .signal = SIGHUP, |
| 169 | .handler = &sighup, |
| 170 | }, |
| 171 | { |
| 172 | .signal = SIGUSR1, |
| 173 | .handler = &sigusr1, |
| 174 | }, |
| 175 | { |
| 176 | .signal = SIGINT, |
| 177 | .handler = &sigint, |
| 178 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 179 | { |
| 180 | .signal = SIGTERM, |
| 181 | .handler = &sigint, |
| 182 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 183 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 184 | |
| 185 | /* RIPngd main routine. */ |
| 186 | int |
| 187 | main (int argc, char **argv) |
| 188 | { |
| 189 | char *p; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 190 | int vty_port = RIPNG_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 191 | int daemon_mode = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 192 | char *progname; |
| 193 | struct thread thread; |
| 194 | |
| 195 | /* Set umask before anything for security */ |
| 196 | umask (0027); |
| 197 | |
| 198 | /* get program name */ |
| 199 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 200 | |
| 201 | zlog_default = openzlog(progname, ZLOG_NOLOG, ZLOG_RIPNG, |
| 202 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 203 | |
| 204 | while (1) |
| 205 | { |
| 206 | int opt; |
| 207 | |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame^] | 208 | opt = getopt_long (argc, argv, "dlf:i:hA:P:u:g:v", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | |
| 210 | if (opt == EOF) |
| 211 | break; |
| 212 | |
| 213 | switch (opt) |
| 214 | { |
| 215 | case 0: |
| 216 | break; |
| 217 | case 'd': |
| 218 | daemon_mode = 1; |
| 219 | break; |
| 220 | case 'l': |
| 221 | /* log_mode = 1; */ |
| 222 | break; |
| 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; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 231 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 232 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 233 | /* Deal with atoi() returning 0 on failure, and ripngd not |
| 234 | listening on ripngd port... */ |
| 235 | if (strcmp(optarg, "0") == 0) |
| 236 | { |
| 237 | vty_port = 0; |
| 238 | break; |
| 239 | } |
| 240 | vty_port = atoi (optarg); |
| 241 | vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT); |
| 242 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | case 'r': |
| 244 | retain_mode = 1; |
| 245 | break; |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame^] | 246 | case 'u': |
| 247 | ripngd_privs.user = optarg; |
| 248 | break; |
| 249 | case 'g': |
| 250 | ripngd_privs.group = optarg; |
| 251 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 252 | case 'v': |
| 253 | print_version (progname); |
| 254 | exit (0); |
| 255 | break; |
| 256 | case 'h': |
| 257 | usage (progname, 0); |
| 258 | break; |
| 259 | default: |
| 260 | usage (progname, 1); |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | master = thread_master_create (); |
| 266 | |
| 267 | /* Library inits. */ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 268 | zprivs_init (&ripngd_privs); |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 269 | signal_init (master, Q_SIGC(ripng_signals), ripng_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 270 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 271 | vty_init (master); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 272 | memory_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | |
| 274 | /* RIPngd inits. */ |
| 275 | ripng_init (); |
| 276 | zebra_init (); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 277 | ripng_peer_init (); |
| 278 | |
| 279 | /* Sort all installed commands. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | sort_node (); |
| 281 | |
| 282 | /* Get configuration file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 283 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 284 | |
| 285 | /* Change to the daemon program. */ |
| 286 | if (daemon_mode) |
| 287 | daemon (0, 0); |
| 288 | |
| 289 | /* Create VTY socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 290 | vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 291 | |
| 292 | /* Process id file create. */ |
| 293 | pid_output (pid_file); |
| 294 | |
| 295 | /* Fetch next active thread. */ |
| 296 | while (thread_fetch (master, &thread)) |
| 297 | thread_call (&thread); |
| 298 | |
| 299 | /* Not reached. */ |
| 300 | exit (0); |
| 301 | } |