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