pim_main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <zebra.h>
  19. #include "log.h"
  20. #include "privs.h"
  21. #include "version.h"
  22. #include <getopt.h>
  23. #include "command.h"
  24. #include "thread.h"
  25. #include <signal.h>
  26. #include "memory.h"
  27. #include "vrf.h"
  28. #include "filter.h"
  29. #include "vty.h"
  30. #include "sigevent.h"
  31. #include "version.h"
  32. #include "prefix.h"
  33. #include "plist.h"
  34. #include "pimd.h"
  35. #include "pim_version.h"
  36. #include "pim_signals.h"
  37. #include "pim_zebra.h"
  38. #ifdef PIM_ZCLIENT_DEBUG
  39. extern int zclient_debug;
  40. #endif
  41. extern struct host host;
  42. char config_default[] = SYSCONFDIR PIMD_DEFAULT_CONFIG;
  43. struct option longopts[] = {
  44. { "daemon", no_argument, NULL, 'd'},
  45. { "config_file", required_argument, NULL, 'f'},
  46. { "pid_file", required_argument, NULL, 'i'},
  47. { "vty_addr", required_argument, NULL, 'A'},
  48. { "vty_port", required_argument, NULL, 'P'},
  49. { "version", no_argument, NULL, 'v'},
  50. { "debug_zclient", no_argument, NULL, 'Z'},
  51. { "help", no_argument, NULL, 'h'},
  52. { 0 }
  53. };
  54. /* pimd privileges */
  55. zebra_capabilities_t _caps_p [] =
  56. {
  57. ZCAP_NET_ADMIN,
  58. ZCAP_SYS_ADMIN,
  59. ZCAP_NET_RAW,
  60. };
  61. /* pimd privileges to run with */
  62. struct zebra_privs_t pimd_privs =
  63. {
  64. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  65. .user = QUAGGA_USER,
  66. .group = QUAGGA_GROUP,
  67. #endif
  68. #ifdef VTY_GROUP
  69. .vty_group = VTY_GROUP,
  70. #endif
  71. .caps_p = _caps_p,
  72. .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
  73. .cap_num_i = 0
  74. };
  75. char* progname;
  76. const char *pid_file = PATH_PIMD_PID;
  77. static void usage(int status)
  78. {
  79. if (status != 0)
  80. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  81. else {
  82. printf ("Usage : %s [OPTION...]\n\
  83. Daemon which manages PIM.\n\n\
  84. -d, --daemon Run in daemon mode\n\
  85. -f, --config_file Set configuration file name\n\
  86. -i, --pid_file Set process identifier file name\n\
  87. -z, --socket Set path of zebra socket\n\
  88. -A, --vty_addr Set vty's bind address\n\
  89. -P, --vty_port Set vty's port number\n\
  90. -v, --version Print program version\n\
  91. "
  92. #ifdef PIM_ZCLIENT_DEBUG
  93. "\
  94. -Z, --debug_zclient Enable zclient debugging\n\
  95. "
  96. #endif
  97. "\
  98. -h, --help Display this help and exit\n\
  99. \n\
  100. Report bugs to %s\n", progname, PIMD_BUG_ADDRESS);
  101. }
  102. exit (status);
  103. }
  104. int main(int argc, char** argv, char** envp) {
  105. char *p;
  106. char *vty_addr = NULL;
  107. int vty_port = -1;
  108. int daemon_mode = 0;
  109. char *config_file = NULL;
  110. char *zebra_sock_path = NULL;
  111. umask(0027);
  112. progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
  113. zlog_default = openzlog(progname, ZLOG_PIM,
  114. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  115. /* this while just reads the options */
  116. while (1) {
  117. int opt;
  118. opt = getopt_long (argc, argv, "df:i:z:A:P:vZh", longopts, 0);
  119. if (opt == EOF)
  120. break;
  121. switch (opt) {
  122. case 0:
  123. break;
  124. case 'd':
  125. daemon_mode = 1;
  126. break;
  127. case 'f':
  128. config_file = optarg;
  129. break;
  130. case 'i':
  131. pid_file = optarg;
  132. break;
  133. case 'z':
  134. zebra_sock_path = optarg;
  135. break;
  136. case 'A':
  137. vty_addr = optarg;
  138. break;
  139. case 'P':
  140. vty_port = atoi (optarg);
  141. break;
  142. case 'v':
  143. printf(PIMD_PROGNAME " version %s\n", PIMD_VERSION);
  144. print_version(QUAGGA_PROGNAME);
  145. exit (0);
  146. break;
  147. #ifdef PIM_ZCLIENT_DEBUG
  148. case 'Z':
  149. zclient_debug = 1;
  150. break;
  151. #endif
  152. case 'h':
  153. usage (0);
  154. break;
  155. default:
  156. usage (1);
  157. break;
  158. }
  159. }
  160. master = thread_master_create();
  161. zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting",
  162. QUAGGA_VERSION, PIMD_VERSION);
  163. /*
  164. * Initializations
  165. */
  166. zprivs_init (&pimd_privs);
  167. pim_signals_init();
  168. cmd_init(1);
  169. vty_init(master);
  170. memory_init();
  171. vrf_init();
  172. access_list_init();
  173. prefix_list_init ();
  174. pim_route_map_init ();
  175. pim_init();
  176. /*
  177. * Initialize zclient "update" and "lookup" sockets
  178. */
  179. pim_zebra_init (master, zebra_sock_path);
  180. zlog_notice("Loading configuration - begin");
  181. /* Get configuration file. */
  182. vty_read_config(config_file, config_default);
  183. /*
  184. Starting from here zlog_* functions will log according configuration
  185. */
  186. zlog_notice("Loading configuration - end");
  187. /* Change to the daemon program. */
  188. if (daemon_mode) {
  189. if (daemon(0, 0)) {
  190. zlog_warn("failed to daemonize");
  191. }
  192. }
  193. /* Process ID file creation. */
  194. pid_output(pid_file);
  195. /* Create pimd VTY socket */
  196. if (vty_port < 0)
  197. vty_port = PIMD_VTY_PORT;
  198. vty_serv_sock(vty_addr, vty_port, PIM_VTYSH_PATH);
  199. zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d",
  200. QUAGGA_VERSION, PIMD_VERSION, vty_port);
  201. #ifdef PIM_DEBUG_BYDEFAULT
  202. zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands");
  203. PIM_DO_DEBUG_PIM_EVENTS;
  204. PIM_DO_DEBUG_PIM_PACKETS;
  205. PIM_DO_DEBUG_PIM_TRACE;
  206. PIM_DO_DEBUG_IGMP_EVENTS;
  207. PIM_DO_DEBUG_IGMP_PACKETS;
  208. PIM_DO_DEBUG_IGMP_TRACE;
  209. PIM_DO_DEBUG_ZEBRA;
  210. #endif
  211. #ifdef PIM_ZCLIENT_DEBUG
  212. zlog_notice("PIM_ZCLIENT_DEBUG: zclient debugging is supported, mode is %s (see option -Z)",
  213. zclient_debug ? "ON" : "OFF");
  214. #endif
  215. #ifdef PIM_CHECK_RECV_IFINDEX_SANITY
  216. zlog_notice("PIM_CHECK_RECV_IFINDEX_SANITY: will match sock/recv ifindex");
  217. #ifdef PIM_REPORT_RECV_IFINDEX_MISMATCH
  218. zlog_notice("PIM_REPORT_RECV_IFINDEX_MISMATCH: will report sock/recv ifindex mismatch");
  219. #endif
  220. #endif
  221. #ifdef PIM_UNEXPECTED_KERNEL_UPCALL
  222. zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall");
  223. #endif
  224. #ifdef HAVE_CLOCK_MONOTONIC
  225. zlog_notice("HAVE_CLOCK_MONOTONIC");
  226. #else
  227. zlog_notice("!HAVE_CLOCK_MONOTONIC");
  228. #endif
  229. thread_main (master);
  230. zlog_err("%s %s: thread_fetch() returned NULL, exiting",
  231. __FILE__, __PRETTY_FUNCTION__);
  232. /* never reached */
  233. return 0;
  234. }