main.c 10 KB

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