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" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
| 36 | #include "zebra/rib.h" |
| 37 | #include "zebra/zserv.h" |
| 38 | #include "zebra/debug.h" |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 39 | #include "zebra/router-id.h" |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 40 | #include "zebra/irdp.h" |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 41 | #include "zebra/rtadv.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 43 | /* Zebra instance */ |
| 44 | struct zebra_t zebrad = |
| 45 | { |
| 46 | .rtm_table_default = 0, |
| 47 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 48 | |
| 49 | /* process id. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | pid_t pid; |
| 51 | |
gdt | 87efd64 | 2004-06-30 17:36:11 +0000 | [diff] [blame] | 52 | /* Pacify zclient.o in libzebra, which expects this variable. */ |
| 53 | struct thread_master *master; |
| 54 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | /* Route retain mode flag. */ |
| 56 | int retain_mode = 0; |
| 57 | |
| 58 | /* Don't delete kernel route. */ |
| 59 | int keep_kernel_mode = 0; |
| 60 | |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 61 | #ifdef HAVE_NETLINK |
| 62 | /* Receive buffer size for netlink socket */ |
| 63 | u_int32_t nl_rcvbufsize = 0; |
| 64 | #endif /* HAVE_NETLINK */ |
| 65 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 66 | /* Command line options. */ |
| 67 | struct option longopts[] = |
| 68 | { |
| 69 | { "batch", no_argument, NULL, 'b'}, |
| 70 | { "daemon", no_argument, NULL, 'd'}, |
| 71 | { "keep_kernel", no_argument, NULL, 'k'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | { "config_file", required_argument, NULL, 'f'}, |
| 73 | { "pid_file", required_argument, NULL, 'i'}, |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 74 | { "socket", required_argument, NULL, 'z'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | { "help", no_argument, NULL, 'h'}, |
| 76 | { "vty_addr", required_argument, NULL, 'A'}, |
| 77 | { "vty_port", required_argument, NULL, 'P'}, |
| 78 | { "retain", no_argument, NULL, 'r'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 79 | { "dryrun", no_argument, NULL, 'C'}, |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 80 | #ifdef HAVE_NETLINK |
hasso | 583d800 | 2005-01-16 23:34:02 +0000 | [diff] [blame] | 81 | { "nl-bufsize", required_argument, NULL, 's'}, |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 82 | #endif /* HAVE_NETLINK */ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 83 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 84 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | { "version", no_argument, NULL, 'v'}, |
| 86 | { 0 } |
| 87 | }; |
| 88 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 89 | zebra_capabilities_t _caps_p [] = |
| 90 | { |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 91 | ZCAP_NET_ADMIN, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 92 | ZCAP_SYS_ADMIN, |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 93 | ZCAP_NET_RAW, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /* zebra privileges to run with */ |
| 97 | struct zebra_privs_t zserv_privs = |
| 98 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 99 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 100 | .user = QUAGGA_USER, |
| 101 | .group = QUAGGA_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 102 | #endif |
| 103 | #ifdef VTY_GROUP |
| 104 | .vty_group = VTY_GROUP, |
| 105 | #endif |
| 106 | .caps_p = _caps_p, |
| 107 | .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]), |
| 108 | .cap_num_i = 0 |
| 109 | }; |
| 110 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 111 | /* Default configuration file path. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 112 | char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE; |
| 113 | |
| 114 | /* Process ID saved for use by init system */ |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 115 | const char *pid_file = PATH_ZEBRA_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | |
| 117 | /* Help information display. */ |
| 118 | static void |
| 119 | usage (char *progname, int status) |
| 120 | { |
| 121 | if (status != 0) |
| 122 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 123 | else |
| 124 | { |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 125 | printf ("Usage : %s [OPTION...]\n\n"\ |
| 126 | "Daemon which manages kernel routing table management and "\ |
| 127 | "redistribution between different routing protocols.\n\n"\ |
| 128 | "-b, --batch Runs in batch mode\n"\ |
| 129 | "-d, --daemon Runs in daemon mode\n"\ |
| 130 | "-f, --config_file Set configuration file name\n"\ |
| 131 | "-i, --pid_file Set process identifier file name\n"\ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 132 | "-z, --socket Set path of zebra socket\n"\ |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 133 | "-k, --keep_kernel Don't delete old routes which installed by "\ |
| 134 | "zebra.\n"\ |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 135 | "-C, --dryrun Check configuration for validity and exit\n"\ |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 136 | "-A, --vty_addr Set vty's bind address\n"\ |
| 137 | "-P, --vty_port Set vty's port number\n"\ |
| 138 | "-r, --retain When program terminates, retain added route "\ |
| 139 | "by zebra.\n"\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 140 | "-u, --user User to run as\n"\ |
| 141 | "-g, --group Group to run as\n", progname); |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 142 | #ifdef HAVE_NETLINK |
| 143 | printf ("-s, --nl-bufsize Set netlink receive buffer size\n"); |
| 144 | #endif /* HAVE_NETLINK */ |
| 145 | printf ("-v, --version Print program version\n"\ |
| 146 | "-h, --help Display this help and exit\n"\ |
| 147 | "\n"\ |
| 148 | "Report bugs to %s\n", ZEBRA_BUG_ADDRESS); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | exit (status); |
| 152 | } |
| 153 | |
| 154 | /* SIGHUP handler. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 155 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 156 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | { |
| 158 | zlog_info ("SIGHUP received"); |
| 159 | |
| 160 | /* Reload of config file. */ |
| 161 | ; |
| 162 | } |
| 163 | |
| 164 | /* SIGINT handler. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 165 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 166 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 168 | zlog_notice ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 180 | static 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 Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 213 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 214 | int batch_mode = 0; |
| 215 | int daemon_mode = 0; |
| 216 | char *config_file = NULL; |
| 217 | char *progname; |
| 218 | struct thread thread; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 219 | char *zserv_path = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 227 | zlog_default = openzlog (progname, ZLOG_ZEBRA, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 235 | 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] | 236 | #else |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 237 | 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] | 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; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 255 | case 'C': |
| 256 | dryrun = 1; |
| 257 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 267 | case 'z': |
| 268 | zserv_path = optarg; |
| 269 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 270 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 271 | /* Deal with atoi() returning 0 on failure, and zebra not |
| 272 | listening on zebra port... */ |
| 273 | if (strcmp(optarg, "0") == 0) |
| 274 | { |
| 275 | vty_port = 0; |
| 276 | break; |
| 277 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | vty_port = atoi (optarg); |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 279 | if (vty_port <= 0 || vty_port > 0xffff) |
| 280 | vty_port = ZEBRA_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | break; |
| 282 | case 'r': |
| 283 | retain_mode = 1; |
| 284 | break; |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 285 | #ifdef HAVE_NETLINK |
| 286 | case 's': |
| 287 | nl_rcvbufsize = atoi (optarg); |
| 288 | break; |
| 289 | #endif /* HAVE_NETLINK */ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 290 | case 'u': |
| 291 | zserv_privs.user = optarg; |
| 292 | break; |
| 293 | case 'g': |
| 294 | zserv_privs.group = optarg; |
| 295 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 296 | case 'v': |
| 297 | print_version (progname); |
| 298 | exit (0); |
| 299 | break; |
| 300 | case 'h': |
| 301 | usage (progname, 0); |
| 302 | break; |
| 303 | default: |
| 304 | usage (progname, 1); |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* Make master thread emulator. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 310 | zebrad.master = thread_master_create (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 311 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 312 | /* privs initialise */ |
| 313 | zprivs_init (&zserv_privs); |
| 314 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 315 | /* Vty related initialize. */ |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 316 | signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 317 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 318 | vty_init (zebrad.master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 319 | memory_init (); |
| 320 | |
| 321 | /* Zebra related initialize. */ |
| 322 | zebra_init (); |
| 323 | rib_init (); |
| 324 | zebra_if_init (); |
| 325 | zebra_debug_init (); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 326 | router_id_init(); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | zebra_vty_init (); |
| 328 | access_list_init (); |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 329 | prefix_list_init (); |
Joachim Nilsson | 36735ed | 2012-05-09 13:38:36 +0200 | [diff] [blame] | 330 | #ifdef RTADV |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 331 | rtadv_init (); |
Joachim Nilsson | 36735ed | 2012-05-09 13:38:36 +0200 | [diff] [blame] | 332 | #endif |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 333 | #ifdef HAVE_IRDP |
| 334 | irdp_init(); |
| 335 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | |
| 337 | /* For debug purpose. */ |
| 338 | /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */ |
| 339 | |
| 340 | /* Make kernel routing socket. */ |
| 341 | kernel_init (); |
| 342 | interface_list (); |
| 343 | route_read (); |
| 344 | |
| 345 | /* Sort VTY commands. */ |
| 346 | sort_node (); |
| 347 | |
| 348 | #ifdef HAVE_SNMP |
| 349 | zebra_snmp_init (); |
| 350 | #endif /* HAVE_SNMP */ |
| 351 | |
Denis Ovsienko | 91b7351 | 2007-09-14 13:31:52 +0000 | [diff] [blame] | 352 | /* Process the configuration file. Among other configuration |
| 353 | * directives we can meet those installing static routes. Such |
| 354 | * requests will not be executed immediately, but queued in |
| 355 | * zebra->ribq structure until we enter the main execution loop. |
| 356 | * The notifications from kernel will show originating PID equal |
| 357 | * to that after daemon() completes (if ever called). |
| 358 | */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 359 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 360 | |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 361 | /* Don't start execution if we are in dry-run mode */ |
| 362 | if (dryrun) |
| 363 | return(0); |
| 364 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 365 | /* Clean up rib. */ |
| 366 | rib_weed_tables (); |
| 367 | |
| 368 | /* Exit when zebra is working in batch mode. */ |
| 369 | if (batch_mode) |
| 370 | exit (0); |
| 371 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 372 | /* Daemonize. */ |
Stephen Hemminger | 065de90 | 2009-08-07 11:13:49 -0700 | [diff] [blame] | 373 | if (daemon_mode && daemon (0, 0) < 0) |
| 374 | { |
| 375 | zlog_err("Zebra daemon failed: %s", strerror(errno)); |
| 376 | exit (1); |
| 377 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | |
| 379 | /* Output pid of zebra. */ |
| 380 | pid_output (pid_file); |
| 381 | |
Denis Ovsienko | 91b7351 | 2007-09-14 13:31:52 +0000 | [diff] [blame] | 382 | /* After we have successfully acquired the pidfile, we can be sure |
| 383 | * about being the only copy of zebra process, which is submitting |
| 384 | * changes to the FIB. |
| 385 | * Clean up zebra-originated routes. The requests will be sent to OS |
| 386 | * immediately, so originating PID in notifications from kernel |
| 387 | * will be equal to the current getpid(). To know about such routes, |
| 388 | * we have to have route_read() called before. |
| 389 | */ |
| 390 | if (! keep_kernel_mode) |
| 391 | rib_sweep_route (); |
| 392 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 393 | /* Needed for BSD routing socket. */ |
| 394 | pid = getpid (); |
| 395 | |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 396 | /* This must be done only after locking pidfile (bug #403). */ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 397 | zebra_zserv_socket_init (zserv_path); |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 398 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 399 | /* Make vty server socket. */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 400 | vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 401 | |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 402 | /* Print banner. */ |
| 403 | zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port); |
| 404 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 405 | while (thread_fetch (zebrad.master, &thread)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 406 | thread_call (&thread); |
| 407 | |
| 408 | /* Not reached... */ |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 409 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 410 | } |