paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1 | /* zebra daemon main routine. |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2 | * Copyright (C) 1997, 98 Kunihiro Ishiguro |
| 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 "command.h" |
| 27 | #include "thread.h" |
| 28 | #include "filter.h" |
| 29 | #include "memory.h" |
| 30 | #include "prefix.h" |
| 31 | #include "log.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 32 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 33 | #include "sigevent.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | |
| 35 | #include "zebra/rib.h" |
| 36 | #include "zebra/zserv.h" |
| 37 | #include "zebra/debug.h" |
| 38 | #include "zebra/rib.h" |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 39 | #include "zebra/irdp.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 41 | /* Zebra instance */ |
| 42 | struct zebra_t zebrad = |
| 43 | { |
| 44 | .rtm_table_default = 0, |
| 45 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 46 | |
| 47 | /* process id. */ |
| 48 | pid_t old_pid; |
| 49 | pid_t pid; |
| 50 | |
gdt | 87efd64 | 2004-06-30 17:36:11 +0000 | [diff] [blame^] | 51 | /* Pacify zclient.o in libzebra, which expects this variable. */ |
| 52 | struct thread_master *master; |
| 53 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | /* Route retain mode flag. */ |
| 55 | int retain_mode = 0; |
| 56 | |
| 57 | /* Don't delete kernel route. */ |
| 58 | int keep_kernel_mode = 0; |
| 59 | |
| 60 | /* Command line options. */ |
| 61 | struct option longopts[] = |
| 62 | { |
| 63 | { "batch", no_argument, NULL, 'b'}, |
| 64 | { "daemon", no_argument, NULL, 'd'}, |
| 65 | { "keep_kernel", no_argument, NULL, 'k'}, |
| 66 | { "log_mode", no_argument, NULL, 'l'}, |
| 67 | { "config_file", required_argument, NULL, 'f'}, |
| 68 | { "pid_file", required_argument, NULL, 'i'}, |
| 69 | { "help", no_argument, NULL, 'h'}, |
| 70 | { "vty_addr", required_argument, NULL, 'A'}, |
| 71 | { "vty_port", required_argument, NULL, 'P'}, |
| 72 | { "retain", no_argument, NULL, 'r'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 73 | { "user", required_argument, NULL, 'u'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 74 | { "version", no_argument, NULL, 'v'}, |
| 75 | { 0 } |
| 76 | }; |
| 77 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 78 | zebra_capabilities_t _caps_p [] = |
| 79 | { |
| 80 | ZCAP_ADMIN, |
| 81 | ZCAP_SYS_ADMIN, |
hasso | 4190881 | 2003-06-05 11:33:10 +0000 | [diff] [blame] | 82 | ZCAP_RAW, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /* zebra privileges to run with */ |
| 86 | struct zebra_privs_t zserv_privs = |
| 87 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 88 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 89 | .user = QUAGGA_USER, |
| 90 | .group = QUAGGA_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 91 | #endif |
| 92 | #ifdef VTY_GROUP |
| 93 | .vty_group = VTY_GROUP, |
| 94 | #endif |
| 95 | .caps_p = _caps_p, |
| 96 | .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]), |
| 97 | .cap_num_i = 0 |
| 98 | }; |
| 99 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 100 | /* Default configuration file path. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 101 | char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE; |
| 102 | |
| 103 | /* Process ID saved for use by init system */ |
| 104 | char *pid_file = PATH_ZEBRA_PID; |
| 105 | |
| 106 | /* Help information display. */ |
| 107 | static void |
| 108 | usage (char *progname, int status) |
| 109 | { |
| 110 | if (status != 0) |
| 111 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 112 | else |
| 113 | { |
| 114 | printf ("Usage : %s [OPTION...]\n\n\ |
| 115 | Daemon which manages kernel routing table management and \ |
| 116 | redistribution between different routing protocols.\n\n\ |
| 117 | -b, --batch Runs in batch mode\n\ |
| 118 | -d, --daemon Runs in daemon mode\n\ |
| 119 | -f, --config_file Set configuration file name\n\ |
| 120 | -i, --pid_file Set process identifier file name\n\ |
| 121 | -k, --keep_kernel Don't delete old routes which installed by zebra.\n\ |
| 122 | -l, --log_mode Set verbose log mode flag\n\ |
| 123 | -A, --vty_addr Set vty's bind address\n\ |
| 124 | -P, --vty_port Set vty's port number\n\ |
| 125 | -r, --retain When program terminates, retain added route by zebra.\n\ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 126 | -u, --user User and group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | -v, --version Print program version\n\ |
| 128 | -h, --help Display this help and exit\n\ |
| 129 | \n\ |
| 130 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 131 | } |
| 132 | |
| 133 | exit (status); |
| 134 | } |
| 135 | |
| 136 | /* SIGHUP handler. */ |
| 137 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 138 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 139 | { |
| 140 | zlog_info ("SIGHUP received"); |
| 141 | |
| 142 | /* Reload of config file. */ |
| 143 | ; |
| 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 | { |
| 150 | /* Decrared in rib.c */ |
| 151 | void rib_close (); |
| 152 | |
| 153 | zlog_info ("Terminating on signal"); |
| 154 | |
| 155 | if (!retain_mode) |
| 156 | rib_close (); |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 157 | #ifdef HAVE_IRDP |
| 158 | irdp_finish(); |
| 159 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 160 | |
| 161 | exit (0); |
| 162 | } |
| 163 | |
| 164 | /* SIGUSR1 handler. */ |
| 165 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 166 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | { |
| 168 | zlog_rotate (NULL); |
| 169 | } |
| 170 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 171 | struct quagga_signal_t zebra_signals[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | { |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 173 | { |
| 174 | .signal = SIGHUP, |
| 175 | .handler = &sighup, |
| 176 | }, |
| 177 | { |
| 178 | .signal = SIGUSR1, |
| 179 | .handler = &sigusr1, |
| 180 | }, |
| 181 | { |
| 182 | .signal = SIGINT, |
hasso | 8c903fb | 2004-03-17 20:39:18 +0000 | [diff] [blame] | 183 | .handler = &sigint, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 184 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 185 | { |
| 186 | .signal = SIGTERM, |
| 187 | .handler = &sigint, |
| 188 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 189 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | |
| 191 | /* Main startup routine. */ |
| 192 | int |
| 193 | main (int argc, char **argv) |
| 194 | { |
| 195 | char *p; |
| 196 | char *vty_addr = NULL; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 197 | int vty_port = ZEBRA_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | int batch_mode = 0; |
| 199 | int daemon_mode = 0; |
| 200 | char *config_file = NULL; |
| 201 | char *progname; |
| 202 | struct thread thread; |
| 203 | void rib_weed_tables (); |
| 204 | void zebra_vty_init (); |
| 205 | |
| 206 | /* Set umask before anything for security */ |
| 207 | umask (0027); |
| 208 | |
| 209 | /* preserve my name */ |
| 210 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 211 | |
| 212 | zlog_default = openzlog (progname, ZLOG_STDOUT, ZLOG_ZEBRA, |
| 213 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 214 | |
| 215 | while (1) |
| 216 | { |
| 217 | int opt; |
| 218 | |
paul | 96735ee | 2003-08-10 02:51:22 +0000 | [diff] [blame] | 219 | opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:v", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | |
| 221 | if (opt == EOF) |
| 222 | break; |
| 223 | |
| 224 | switch (opt) |
| 225 | { |
| 226 | case 0: |
| 227 | break; |
| 228 | case 'b': |
| 229 | batch_mode = 1; |
| 230 | case 'd': |
| 231 | daemon_mode = 1; |
| 232 | break; |
| 233 | case 'k': |
| 234 | keep_kernel_mode = 1; |
| 235 | break; |
| 236 | case 'l': |
| 237 | /* log_mode = 1; */ |
| 238 | break; |
| 239 | case 'f': |
| 240 | config_file = optarg; |
| 241 | break; |
| 242 | case 'A': |
| 243 | vty_addr = optarg; |
| 244 | break; |
| 245 | case 'i': |
| 246 | pid_file = optarg; |
| 247 | break; |
| 248 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 249 | /* Deal with atoi() returning 0 on failure, and zebra not |
| 250 | listening on zebra port... */ |
| 251 | if (strcmp(optarg, "0") == 0) |
| 252 | { |
| 253 | vty_port = 0; |
| 254 | break; |
| 255 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 256 | vty_port = atoi (optarg); |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 257 | vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 258 | break; |
| 259 | case 'r': |
| 260 | retain_mode = 1; |
| 261 | break; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 262 | case 'u': |
| 263 | zserv_privs.user = zserv_privs.group = optarg; |
| 264 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 265 | case 'v': |
| 266 | print_version (progname); |
| 267 | exit (0); |
| 268 | break; |
| 269 | case 'h': |
| 270 | usage (progname, 0); |
| 271 | break; |
| 272 | default: |
| 273 | usage (progname, 1); |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* Make master thread emulator. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 279 | zebrad.master = thread_master_create (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 281 | /* privs initialise */ |
| 282 | zprivs_init (&zserv_privs); |
| 283 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 284 | /* Vty related initialize. */ |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 285 | signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 286 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 287 | vty_init (zebrad.master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | memory_init (); |
| 289 | |
| 290 | /* Zebra related initialize. */ |
| 291 | zebra_init (); |
| 292 | rib_init (); |
| 293 | zebra_if_init (); |
| 294 | zebra_debug_init (); |
| 295 | zebra_vty_init (); |
| 296 | access_list_init (); |
| 297 | rtadv_init (); |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 298 | #ifdef HAVE_IRDP |
| 299 | irdp_init(); |
| 300 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | |
| 302 | /* For debug purpose. */ |
| 303 | /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */ |
| 304 | |
| 305 | /* Make kernel routing socket. */ |
| 306 | kernel_init (); |
| 307 | interface_list (); |
| 308 | route_read (); |
| 309 | |
| 310 | /* Sort VTY commands. */ |
| 311 | sort_node (); |
| 312 | |
| 313 | #ifdef HAVE_SNMP |
| 314 | zebra_snmp_init (); |
| 315 | #endif /* HAVE_SNMP */ |
| 316 | |
| 317 | /* Clean up self inserted route. */ |
| 318 | if (! keep_kernel_mode) |
| 319 | rib_sweep_route (); |
| 320 | |
| 321 | /* Configuration file read*/ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 322 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | |
| 324 | /* Clean up rib. */ |
| 325 | rib_weed_tables (); |
| 326 | |
| 327 | /* Exit when zebra is working in batch mode. */ |
| 328 | if (batch_mode) |
| 329 | exit (0); |
| 330 | |
| 331 | /* Needed for BSD routing socket. */ |
| 332 | old_pid = getpid (); |
| 333 | |
| 334 | /* Daemonize. */ |
| 335 | if (daemon_mode) |
| 336 | daemon (0, 0); |
| 337 | |
| 338 | /* Output pid of zebra. */ |
| 339 | pid_output (pid_file); |
| 340 | |
| 341 | /* Needed for BSD routing socket. */ |
| 342 | pid = getpid (); |
| 343 | |
| 344 | /* Make vty server socket. */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 345 | vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 347 | while (thread_fetch (zebrad.master, &thread)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 348 | thread_call (&thread); |
| 349 | |
| 350 | /* Not reached... */ |
| 351 | exit (0); |
| 352 | } |