main.c 9.6 KB

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