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