main.c 9.4 KB

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