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