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