main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "privs.h"
  31. #include "sigevent.h"
  32. #include "zebra/rib.h"
  33. #include "zebra/zserv.h"
  34. #include "zebra/debug.h"
  35. #include "zebra/rib.h"
  36. #include "zebra/irdp.h"
  37. /* Zebra instance */
  38. struct zebra_t zebrad =
  39. {
  40. .rtm_table_default = 0,
  41. };
  42. /* process id. */
  43. pid_t old_pid;
  44. pid_t pid;
  45. /* Pacify zclient.o in libzebra, which expects this variable. */
  46. struct thread_master *master;
  47. /* Route retain mode flag. */
  48. int retain_mode = 0;
  49. /* Don't delete kernel route. */
  50. int keep_kernel_mode = 0;
  51. /* Command line options. */
  52. struct option longopts[] =
  53. {
  54. { "batch", no_argument, NULL, 'b'},
  55. { "daemon", no_argument, NULL, 'd'},
  56. { "keep_kernel", no_argument, NULL, 'k'},
  57. { "log_mode", no_argument, NULL, 'l'},
  58. { "config_file", required_argument, NULL, 'f'},
  59. { "pid_file", required_argument, NULL, 'i'},
  60. { "help", no_argument, NULL, 'h'},
  61. { "vty_addr", required_argument, NULL, 'A'},
  62. { "vty_port", required_argument, NULL, 'P'},
  63. { "retain", no_argument, NULL, 'r'},
  64. { "user", required_argument, NULL, 'u'},
  65. { "version", no_argument, NULL, 'v'},
  66. { 0 }
  67. };
  68. zebra_capabilities_t _caps_p [] =
  69. {
  70. ZCAP_ADMIN,
  71. ZCAP_SYS_ADMIN,
  72. ZCAP_RAW,
  73. };
  74. /* zebra privileges to run with */
  75. struct zebra_privs_t zserv_privs =
  76. {
  77. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  78. .user = QUAGGA_USER,
  79. .group = QUAGGA_GROUP,
  80. #endif
  81. #ifdef VTY_GROUP
  82. .vty_group = VTY_GROUP,
  83. #endif
  84. .caps_p = _caps_p,
  85. .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
  86. .cap_num_i = 0
  87. };
  88. /* Default configuration file path. */
  89. char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
  90. /* Process ID saved for use by init system */
  91. char *pid_file = PATH_ZEBRA_PID;
  92. /* Help information display. */
  93. static void
  94. usage (char *progname, int status)
  95. {
  96. if (status != 0)
  97. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  98. else
  99. {
  100. printf ("Usage : %s [OPTION...]\n\n\
  101. Daemon which manages kernel routing table management and \
  102. redistribution between different routing protocols.\n\n\
  103. -b, --batch Runs in batch mode\n\
  104. -d, --daemon Runs in daemon mode\n\
  105. -f, --config_file Set configuration file name\n\
  106. -i, --pid_file Set process identifier file name\n\
  107. -k, --keep_kernel Don't delete old routes which installed by zebra.\n\
  108. -l, --log_mode Set verbose log mode flag\n\
  109. -A, --vty_addr Set vty's bind address\n\
  110. -P, --vty_port Set vty's port number\n\
  111. -r, --retain When program terminates, retain added route by zebra.\n\
  112. -u, --user User and group to run as\n\
  113. -v, --version Print program version\n\
  114. -h, --help Display this help and exit\n\
  115. \n\
  116. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  117. }
  118. exit (status);
  119. }
  120. /* SIGHUP handler. */
  121. void
  122. sighup (void)
  123. {
  124. zlog_info ("SIGHUP received");
  125. /* Reload of config file. */
  126. ;
  127. }
  128. /* SIGINT handler. */
  129. void
  130. sigint (void)
  131. {
  132. /* Decrared in rib.c */
  133. void rib_close ();
  134. zlog_info ("Terminating on signal");
  135. if (!retain_mode)
  136. rib_close ();
  137. #ifdef HAVE_IRDP
  138. irdp_finish();
  139. #endif
  140. exit (0);
  141. }
  142. /* SIGUSR1 handler. */
  143. void
  144. sigusr1 (void)
  145. {
  146. zlog_rotate (NULL);
  147. }
  148. struct quagga_signal_t zebra_signals[] =
  149. {
  150. {
  151. .signal = SIGHUP,
  152. .handler = &sighup,
  153. },
  154. {
  155. .signal = SIGUSR1,
  156. .handler = &sigusr1,
  157. },
  158. {
  159. .signal = SIGINT,
  160. .handler = &sigint,
  161. },
  162. {
  163. .signal = SIGTERM,
  164. .handler = &sigint,
  165. },
  166. };
  167. /* Main startup routine. */
  168. int
  169. main (int argc, char **argv)
  170. {
  171. char *p;
  172. char *vty_addr = NULL;
  173. int vty_port = ZEBRA_VTY_PORT;
  174. int batch_mode = 0;
  175. int daemon_mode = 0;
  176. char *config_file = NULL;
  177. char *progname;
  178. struct thread thread;
  179. void rib_weed_tables ();
  180. void zebra_vty_init ();
  181. /* Set umask before anything for security */
  182. umask (0027);
  183. /* preserve my name */
  184. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  185. zlog_default = openzlog (progname, ZLOG_STDOUT, ZLOG_ZEBRA,
  186. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  187. while (1)
  188. {
  189. int opt;
  190. opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:v", longopts, 0);
  191. if (opt == EOF)
  192. break;
  193. switch (opt)
  194. {
  195. case 0:
  196. break;
  197. case 'b':
  198. batch_mode = 1;
  199. case 'd':
  200. daemon_mode = 1;
  201. break;
  202. case 'k':
  203. keep_kernel_mode = 1;
  204. break;
  205. case 'l':
  206. /* log_mode = 1; */
  207. break;
  208. case 'f':
  209. config_file = optarg;
  210. break;
  211. case 'A':
  212. vty_addr = optarg;
  213. break;
  214. case 'i':
  215. pid_file = optarg;
  216. break;
  217. case 'P':
  218. /* Deal with atoi() returning 0 on failure, and zebra not
  219. listening on zebra port... */
  220. if (strcmp(optarg, "0") == 0)
  221. {
  222. vty_port = 0;
  223. break;
  224. }
  225. vty_port = atoi (optarg);
  226. vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT);
  227. break;
  228. case 'r':
  229. retain_mode = 1;
  230. break;
  231. case 'u':
  232. zserv_privs.user = zserv_privs.group = optarg;
  233. break;
  234. case 'v':
  235. print_version (progname);
  236. exit (0);
  237. break;
  238. case 'h':
  239. usage (progname, 0);
  240. break;
  241. default:
  242. usage (progname, 1);
  243. break;
  244. }
  245. }
  246. /* Make master thread emulator. */
  247. zebrad.master = thread_master_create ();
  248. /* privs initialise */
  249. zprivs_init (&zserv_privs);
  250. /* Vty related initialize. */
  251. signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
  252. cmd_init (1);
  253. vty_init (zebrad.master);
  254. memory_init ();
  255. /* Zebra related initialize. */
  256. zebra_init ();
  257. rib_init ();
  258. zebra_if_init ();
  259. zebra_debug_init ();
  260. zebra_vty_init ();
  261. access_list_init ();
  262. rtadv_init ();
  263. #ifdef HAVE_IRDP
  264. irdp_init();
  265. #endif
  266. /* For debug purpose. */
  267. /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
  268. /* Make kernel routing socket. */
  269. kernel_init ();
  270. interface_list ();
  271. route_read ();
  272. /* Sort VTY commands. */
  273. sort_node ();
  274. #ifdef HAVE_SNMP
  275. zebra_snmp_init ();
  276. #endif /* HAVE_SNMP */
  277. /* Clean up self inserted route. */
  278. if (! keep_kernel_mode)
  279. rib_sweep_route ();
  280. /* Configuration file read*/
  281. vty_read_config (config_file, config_default);
  282. /* Clean up rib. */
  283. rib_weed_tables ();
  284. /* Exit when zebra is working in batch mode. */
  285. if (batch_mode)
  286. exit (0);
  287. /* Needed for BSD routing socket. */
  288. old_pid = getpid ();
  289. /* Daemonize. */
  290. if (daemon_mode)
  291. daemon (0, 0);
  292. /* Output pid of zebra. */
  293. pid_output (pid_file);
  294. /* Needed for BSD routing socket. */
  295. pid = getpid ();
  296. /* Make vty server socket. */
  297. vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
  298. while (thread_fetch (zebrad.master, &thread))
  299. thread_call (&thread);
  300. /* Not reached... */
  301. exit (0);
  302. }