main.c 9.7 KB

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