main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. router_id_init (zvrf);
  194. }
  195. return 0;
  196. }
  197. /* Zebra VRF initialization. */
  198. static void
  199. zebra_vrf_init (void)
  200. {
  201. vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
  202. vrf_init ();
  203. }
  204. /* Main startup routine. */
  205. int
  206. main (int argc, char **argv)
  207. {
  208. char *p;
  209. char *vty_addr = NULL;
  210. int vty_port = ZEBRA_VTY_PORT;
  211. int dryrun = 0;
  212. int batch_mode = 0;
  213. int daemon_mode = 0;
  214. char *config_file = NULL;
  215. char *progname;
  216. struct thread thread;
  217. char *zserv_path = NULL;
  218. /* Set umask before anything for security */
  219. umask (0027);
  220. /* preserve my name */
  221. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  222. zlog_default = openzlog (progname, ZLOG_ZEBRA,
  223. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  224. while (1)
  225. {
  226. int opt;
  227. #ifdef HAVE_NETLINK
  228. opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vs:C", longopts, 0);
  229. #else
  230. opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vC", longopts, 0);
  231. #endif /* HAVE_NETLINK */
  232. if (opt == EOF)
  233. break;
  234. switch (opt)
  235. {
  236. case 0:
  237. break;
  238. case 'b':
  239. batch_mode = 1;
  240. case 'd':
  241. daemon_mode = 1;
  242. break;
  243. case 'k':
  244. keep_kernel_mode = 1;
  245. break;
  246. case 'C':
  247. dryrun = 1;
  248. break;
  249. case 'f':
  250. config_file = optarg;
  251. break;
  252. case 'A':
  253. vty_addr = optarg;
  254. break;
  255. case 'i':
  256. pid_file = optarg;
  257. break;
  258. case 'z':
  259. zserv_path = optarg;
  260. break;
  261. case 'P':
  262. /* Deal with atoi() returning 0 on failure, and zebra not
  263. listening on zebra port... */
  264. if (strcmp(optarg, "0") == 0)
  265. {
  266. vty_port = 0;
  267. break;
  268. }
  269. vty_port = atoi (optarg);
  270. if (vty_port <= 0 || vty_port > 0xffff)
  271. vty_port = ZEBRA_VTY_PORT;
  272. break;
  273. case 'r':
  274. retain_mode = 1;
  275. break;
  276. #ifdef HAVE_NETLINK
  277. case 's':
  278. nl_rcvbufsize = atoi (optarg);
  279. break;
  280. #endif /* HAVE_NETLINK */
  281. case 'u':
  282. zserv_privs.user = optarg;
  283. break;
  284. case 'g':
  285. zserv_privs.group = optarg;
  286. break;
  287. case 'v':
  288. print_version (progname);
  289. exit (0);
  290. break;
  291. case 'h':
  292. usage (progname, 0);
  293. break;
  294. default:
  295. usage (progname, 1);
  296. break;
  297. }
  298. }
  299. /* Make master thread emulator. */
  300. zebrad.master = thread_master_create ();
  301. /* privs initialise */
  302. zprivs_init (&zserv_privs);
  303. /* Vty related initialize. */
  304. signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
  305. cmd_init (1);
  306. vty_init (zebrad.master);
  307. memory_init ();
  308. /* Zebra related initialize. */
  309. zebra_init ();
  310. rib_init ();
  311. zebra_if_init ();
  312. zebra_debug_init ();
  313. router_id_cmd_init ();
  314. zebra_vty_init ();
  315. access_list_init ();
  316. prefix_list_init ();
  317. #ifdef RTADV
  318. rtadv_init ();
  319. #endif
  320. #ifdef HAVE_IRDP
  321. irdp_init();
  322. #endif
  323. /* For debug purpose. */
  324. /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
  325. /* Initialize VRF module, and make kernel routing socket. */
  326. zebra_vrf_init ();
  327. kernel_init ();
  328. interface_list ();
  329. route_read ();
  330. #ifdef HAVE_SNMP
  331. zebra_snmp_init ();
  332. #endif /* HAVE_SNMP */
  333. #ifdef HAVE_FPM
  334. zfpm_init (zebrad.master, 1, 0);
  335. #else
  336. zfpm_init (zebrad.master, 0, 0);
  337. #endif
  338. /* Process the configuration file. Among other configuration
  339. * directives we can meet those installing static routes. Such
  340. * requests will not be executed immediately, but queued in
  341. * zebra->ribq structure until we enter the main execution loop.
  342. * The notifications from kernel will show originating PID equal
  343. * to that after daemon() completes (if ever called).
  344. */
  345. vty_read_config (config_file, config_default);
  346. /* Don't start execution if we are in dry-run mode */
  347. if (dryrun)
  348. return(0);
  349. /* Clean up rib. */
  350. rib_weed_tables ();
  351. /* Exit when zebra is working in batch mode. */
  352. if (batch_mode)
  353. exit (0);
  354. /* Daemonize. */
  355. if (daemon_mode && daemon (0, 0) < 0)
  356. {
  357. zlog_err("Zebra daemon failed: %s", strerror(errno));
  358. exit (1);
  359. }
  360. /* Output pid of zebra. */
  361. pid_output (pid_file);
  362. /* After we have successfully acquired the pidfile, we can be sure
  363. * about being the only copy of zebra process, which is submitting
  364. * changes to the FIB.
  365. * Clean up zebra-originated routes. The requests will be sent to OS
  366. * immediately, so originating PID in notifications from kernel
  367. * will be equal to the current getpid(). To know about such routes,
  368. * we have to have route_read() called before.
  369. */
  370. if (! keep_kernel_mode)
  371. rib_sweep_route ();
  372. /* Needed for BSD routing socket. */
  373. pid = getpid ();
  374. /* This must be done only after locking pidfile (bug #403). */
  375. zebra_zserv_socket_init (zserv_path);
  376. /* Make vty server socket. */
  377. vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
  378. /* Print banner. */
  379. zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  380. while (thread_fetch (zebrad.master, &thread))
  381. thread_call (&thread);
  382. /* Not reached... */
  383. return 0;
  384. }