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 Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 32 | #include "plist.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 33 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 34 | #include "sigevent.h" |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 35 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | |
| 37 | #include "zebra/rib.h" |
| 38 | #include "zebra/zserv.h" |
| 39 | #include "zebra/debug.h" |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 40 | #include "zebra/router-id.h" |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 41 | #include "zebra/irdp.h" |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 42 | #include "zebra/rtadv.h" |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 43 | #include "zebra/zebra_fpm.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 45 | /* Zebra instance */ |
| 46 | struct zebra_t zebrad = |
| 47 | { |
| 48 | .rtm_table_default = 0, |
| 49 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | |
| 51 | /* process id. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | pid_t pid; |
| 53 | |
gdt | 87efd64 | 2004-06-30 17:36:11 +0000 | [diff] [blame] | 54 | /* Pacify zclient.o in libzebra, which expects this variable. */ |
| 55 | struct thread_master *master; |
| 56 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | /* Route retain mode flag. */ |
| 58 | int retain_mode = 0; |
| 59 | |
| 60 | /* Don't delete kernel route. */ |
| 61 | int keep_kernel_mode = 0; |
| 62 | |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 63 | #ifdef HAVE_NETLINK |
| 64 | /* Receive buffer size for netlink socket */ |
| 65 | u_int32_t nl_rcvbufsize = 0; |
| 66 | #endif /* HAVE_NETLINK */ |
| 67 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | /* Command line options. */ |
| 69 | struct option longopts[] = |
| 70 | { |
| 71 | { "batch", no_argument, NULL, 'b'}, |
| 72 | { "daemon", no_argument, NULL, 'd'}, |
| 73 | { "keep_kernel", no_argument, NULL, 'k'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 74 | { "config_file", required_argument, NULL, 'f'}, |
| 75 | { "pid_file", required_argument, NULL, 'i'}, |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 76 | { "socket", required_argument, NULL, 'z'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | { "help", no_argument, NULL, 'h'}, |
| 78 | { "vty_addr", required_argument, NULL, 'A'}, |
| 79 | { "vty_port", required_argument, NULL, 'P'}, |
| 80 | { "retain", no_argument, NULL, 'r'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 81 | { "dryrun", no_argument, NULL, 'C'}, |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 82 | #ifdef HAVE_NETLINK |
hasso | 583d800 | 2005-01-16 23:34:02 +0000 | [diff] [blame] | 83 | { "nl-bufsize", required_argument, NULL, 's'}, |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 84 | #endif /* HAVE_NETLINK */ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 85 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 86 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | { "version", no_argument, NULL, 'v'}, |
| 88 | { 0 } |
| 89 | }; |
| 90 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 91 | zebra_capabilities_t _caps_p [] = |
| 92 | { |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 93 | ZCAP_NET_ADMIN, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 94 | ZCAP_SYS_ADMIN, |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 95 | ZCAP_NET_RAW, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | /* zebra privileges to run with */ |
| 99 | struct zebra_privs_t zserv_privs = |
| 100 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 101 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 102 | .user = QUAGGA_USER, |
| 103 | .group = QUAGGA_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 104 | #endif |
| 105 | #ifdef VTY_GROUP |
| 106 | .vty_group = VTY_GROUP, |
| 107 | #endif |
| 108 | .caps_p = _caps_p, |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 109 | .cap_num_p = array_size(_caps_p), |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 110 | .cap_num_i = 0 |
| 111 | }; |
| 112 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | /* Default configuration file path. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE; |
| 115 | |
| 116 | /* Process ID saved for use by init system */ |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 117 | const char *pid_file = PATH_ZEBRA_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 118 | |
| 119 | /* Help information display. */ |
| 120 | static void |
| 121 | usage (char *progname, int status) |
| 122 | { |
| 123 | if (status != 0) |
| 124 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 125 | else |
| 126 | { |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 127 | printf ("Usage : %s [OPTION...]\n\n"\ |
| 128 | "Daemon which manages kernel routing table management and "\ |
| 129 | "redistribution between different routing protocols.\n\n"\ |
| 130 | "-b, --batch Runs in batch mode\n"\ |
| 131 | "-d, --daemon Runs in daemon mode\n"\ |
| 132 | "-f, --config_file Set configuration file name\n"\ |
| 133 | "-i, --pid_file Set process identifier file name\n"\ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 134 | "-z, --socket Set path of zebra socket\n"\ |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 135 | "-k, --keep_kernel Don't delete old routes which installed by "\ |
| 136 | "zebra.\n"\ |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 137 | "-C, --dryrun Check configuration for validity and exit\n"\ |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 138 | "-A, --vty_addr Set vty's bind address\n"\ |
| 139 | "-P, --vty_port Set vty's port number\n"\ |
| 140 | "-r, --retain When program terminates, retain added route "\ |
| 141 | "by zebra.\n"\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 142 | "-u, --user User to run as\n"\ |
| 143 | "-g, --group Group to run as\n", progname); |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 144 | #ifdef HAVE_NETLINK |
| 145 | printf ("-s, --nl-bufsize Set netlink receive buffer size\n"); |
| 146 | #endif /* HAVE_NETLINK */ |
| 147 | printf ("-v, --version Print program version\n"\ |
| 148 | "-h, --help Display this help and exit\n"\ |
| 149 | "\n"\ |
| 150 | "Report bugs to %s\n", ZEBRA_BUG_ADDRESS); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | exit (status); |
| 154 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 155 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | /* SIGHUP handler. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 157 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 158 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 159 | { |
| 160 | zlog_info ("SIGHUP received"); |
| 161 | |
| 162 | /* Reload of config file. */ |
| 163 | ; |
| 164 | } |
| 165 | |
| 166 | /* SIGINT handler. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 167 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 168 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 169 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 170 | zlog_notice ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | |
| 172 | if (!retain_mode) |
| 173 | rib_close (); |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 174 | #ifdef HAVE_IRDP |
| 175 | irdp_finish(); |
| 176 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | |
| 178 | exit (0); |
| 179 | } |
| 180 | |
| 181 | /* SIGUSR1 handler. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 182 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 183 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 184 | { |
| 185 | zlog_rotate (NULL); |
| 186 | } |
| 187 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 188 | struct quagga_signal_t zebra_signals[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 189 | { |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 190 | { |
| 191 | .signal = SIGHUP, |
| 192 | .handler = &sighup, |
| 193 | }, |
| 194 | { |
| 195 | .signal = SIGUSR1, |
| 196 | .handler = &sigusr1, |
| 197 | }, |
| 198 | { |
| 199 | .signal = SIGINT, |
hasso | 8c903fb | 2004-03-17 20:39:18 +0000 | [diff] [blame] | 200 | .handler = &sigint, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 201 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 202 | { |
| 203 | .signal = SIGTERM, |
| 204 | .handler = &sigint, |
| 205 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 206 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 207 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 208 | /* Callback upon creating a new VRF. */ |
| 209 | static int |
| 210 | zebra_vrf_new (vrf_id_t vrf_id, void **info) |
| 211 | { |
| 212 | struct zebra_vrf *zvrf = *info; |
| 213 | |
| 214 | if (! zvrf) |
| 215 | { |
| 216 | zvrf = zebra_vrf_alloc (vrf_id); |
| 217 | *info = (void *)zvrf; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* Zebra VRF initialization. */ |
| 224 | static void |
| 225 | zebra_vrf_init (void) |
| 226 | { |
| 227 | vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new); |
| 228 | vrf_init (); |
| 229 | } |
| 230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | /* Main startup routine. */ |
| 232 | int |
| 233 | main (int argc, char **argv) |
| 234 | { |
| 235 | char *p; |
| 236 | char *vty_addr = NULL; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 237 | int vty_port = ZEBRA_VTY_PORT; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 238 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | int batch_mode = 0; |
| 240 | int daemon_mode = 0; |
| 241 | char *config_file = NULL; |
| 242 | char *progname; |
| 243 | struct thread thread; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 244 | char *zserv_path = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 245 | |
| 246 | /* Set umask before anything for security */ |
| 247 | umask (0027); |
| 248 | |
| 249 | /* preserve my name */ |
| 250 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 251 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 252 | zlog_default = openzlog (progname, ZLOG_ZEBRA, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 253 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 254 | |
| 255 | while (1) |
| 256 | { |
| 257 | int opt; |
| 258 | |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 259 | #ifdef HAVE_NETLINK |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 260 | opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vs:C", longopts, 0); |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 261 | #else |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 262 | opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vC", longopts, 0); |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 263 | #endif /* HAVE_NETLINK */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 264 | |
| 265 | if (opt == EOF) |
| 266 | break; |
| 267 | |
| 268 | switch (opt) |
| 269 | { |
| 270 | case 0: |
| 271 | break; |
| 272 | case 'b': |
| 273 | batch_mode = 1; |
| 274 | case 'd': |
| 275 | daemon_mode = 1; |
| 276 | break; |
| 277 | case 'k': |
| 278 | keep_kernel_mode = 1; |
| 279 | break; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 280 | case 'C': |
| 281 | dryrun = 1; |
| 282 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | case 'f': |
| 284 | config_file = optarg; |
| 285 | break; |
| 286 | case 'A': |
| 287 | vty_addr = optarg; |
| 288 | break; |
| 289 | case 'i': |
| 290 | pid_file = optarg; |
| 291 | break; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 292 | case 'z': |
| 293 | zserv_path = optarg; |
| 294 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 296 | /* Deal with atoi() returning 0 on failure, and zebra not |
| 297 | listening on zebra port... */ |
| 298 | if (strcmp(optarg, "0") == 0) |
| 299 | { |
| 300 | vty_port = 0; |
| 301 | break; |
| 302 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 303 | vty_port = atoi (optarg); |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 304 | if (vty_port <= 0 || vty_port > 0xffff) |
| 305 | vty_port = ZEBRA_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 306 | break; |
| 307 | case 'r': |
| 308 | retain_mode = 1; |
| 309 | break; |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 310 | #ifdef HAVE_NETLINK |
| 311 | case 's': |
| 312 | nl_rcvbufsize = atoi (optarg); |
| 313 | break; |
| 314 | #endif /* HAVE_NETLINK */ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 315 | case 'u': |
| 316 | zserv_privs.user = optarg; |
| 317 | break; |
| 318 | case 'g': |
| 319 | zserv_privs.group = optarg; |
| 320 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 321 | case 'v': |
| 322 | print_version (progname); |
| 323 | exit (0); |
| 324 | break; |
| 325 | case 'h': |
| 326 | usage (progname, 0); |
| 327 | break; |
| 328 | default: |
| 329 | usage (progname, 1); |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* Make master thread emulator. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 335 | zebrad.master = thread_master_create (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 337 | /* privs initialise */ |
| 338 | zprivs_init (&zserv_privs); |
| 339 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | /* Vty related initialize. */ |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 341 | signal_init (zebrad.master, array_size(zebra_signals), zebra_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 342 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 343 | vty_init (zebrad.master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 344 | memory_init (); |
| 345 | |
| 346 | /* Zebra related initialize. */ |
| 347 | zebra_init (); |
| 348 | rib_init (); |
| 349 | zebra_if_init (); |
| 350 | zebra_debug_init (); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 351 | router_id_init(); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 352 | zebra_vty_init (); |
| 353 | access_list_init (); |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 354 | prefix_list_init (); |
Joachim Nilsson | 36735ed | 2012-05-09 13:38:36 +0200 | [diff] [blame] | 355 | #ifdef RTADV |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 356 | rtadv_init (); |
Joachim Nilsson | 36735ed | 2012-05-09 13:38:36 +0200 | [diff] [blame] | 357 | #endif |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 358 | #ifdef HAVE_IRDP |
| 359 | irdp_init(); |
| 360 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 361 | |
| 362 | /* For debug purpose. */ |
| 363 | /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */ |
| 364 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 365 | /* Initialize VRF module, and make kernel routing socket. */ |
| 366 | zebra_vrf_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 367 | kernel_init (); |
| 368 | interface_list (); |
| 369 | route_read (); |
| 370 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 371 | #ifdef HAVE_SNMP |
| 372 | zebra_snmp_init (); |
| 373 | #endif /* HAVE_SNMP */ |
| 374 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 375 | #ifdef HAVE_FPM |
| 376 | zfpm_init (zebrad.master, 1, 0); |
| 377 | #else |
| 378 | zfpm_init (zebrad.master, 0, 0); |
| 379 | #endif |
| 380 | |
Denis Ovsienko | 91b7351 | 2007-09-14 13:31:52 +0000 | [diff] [blame] | 381 | /* Process the configuration file. Among other configuration |
| 382 | * directives we can meet those installing static routes. Such |
| 383 | * requests will not be executed immediately, but queued in |
| 384 | * zebra->ribq structure until we enter the main execution loop. |
| 385 | * The notifications from kernel will show originating PID equal |
| 386 | * to that after daemon() completes (if ever called). |
| 387 | */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 388 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 389 | |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 390 | /* Don't start execution if we are in dry-run mode */ |
| 391 | if (dryrun) |
| 392 | return(0); |
| 393 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 394 | /* Clean up rib. */ |
| 395 | rib_weed_tables (); |
| 396 | |
| 397 | /* Exit when zebra is working in batch mode. */ |
| 398 | if (batch_mode) |
| 399 | exit (0); |
| 400 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 401 | /* Daemonize. */ |
Stephen Hemminger | 065de90 | 2009-08-07 11:13:49 -0700 | [diff] [blame] | 402 | if (daemon_mode && daemon (0, 0) < 0) |
| 403 | { |
| 404 | zlog_err("Zebra daemon failed: %s", strerror(errno)); |
| 405 | exit (1); |
| 406 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 407 | |
| 408 | /* Output pid of zebra. */ |
| 409 | pid_output (pid_file); |
| 410 | |
Denis Ovsienko | 91b7351 | 2007-09-14 13:31:52 +0000 | [diff] [blame] | 411 | /* After we have successfully acquired the pidfile, we can be sure |
| 412 | * about being the only copy of zebra process, which is submitting |
| 413 | * changes to the FIB. |
| 414 | * Clean up zebra-originated routes. The requests will be sent to OS |
| 415 | * immediately, so originating PID in notifications from kernel |
| 416 | * will be equal to the current getpid(). To know about such routes, |
| 417 | * we have to have route_read() called before. |
| 418 | */ |
| 419 | if (! keep_kernel_mode) |
| 420 | rib_sweep_route (); |
| 421 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 422 | /* Needed for BSD routing socket. */ |
| 423 | pid = getpid (); |
| 424 | |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 425 | /* This must be done only after locking pidfile (bug #403). */ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 426 | zebra_zserv_socket_init (zserv_path); |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 427 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 428 | /* Make vty server socket. */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 429 | vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 430 | |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 431 | /* Print banner. */ |
| 432 | zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port); |
| 433 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 434 | while (thread_fetch (zebrad.master, &thread)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 435 | thread_call (&thread); |
| 436 | |
| 437 | /* Not reached... */ |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 438 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 439 | } |