pim_main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "pimd.h"
  33. #include "pim_version.h"
  34. #include "pim_signals.h"
  35. #include "pim_zebra.h"
  36. #ifdef PIM_ZCLIENT_DEBUG
  37. extern int zclient_debug;
  38. #endif
  39. extern struct host host;
  40. char config_default[] = SYSCONFDIR PIMD_DEFAULT_CONFIG;
  41. struct option longopts[] = {
  42. { "daemon", no_argument, NULL, 'd'},
  43. { "config_file", required_argument, NULL, 'f'},
  44. { "pid_file", required_argument, NULL, 'i'},
  45. { "vty_addr", required_argument, NULL, 'A'},
  46. { "vty_port", required_argument, NULL, 'P'},
  47. { "version", no_argument, NULL, 'v'},
  48. { "debug_zclient", no_argument, NULL, 'Z'},
  49. { "help", no_argument, NULL, 'h'},
  50. { 0 }
  51. };
  52. /* pimd privileges */
  53. zebra_capabilities_t _caps_p [] =
  54. {
  55. ZCAP_NET_ADMIN,
  56. ZCAP_SYS_ADMIN,
  57. ZCAP_NET_RAW,
  58. };
  59. /* pimd privileges to run with */
  60. struct zebra_privs_t pimd_privs =
  61. {
  62. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  63. .user = QUAGGA_USER,
  64. .group = QUAGGA_GROUP,
  65. #endif
  66. #ifdef VTY_GROUP
  67. .vty_group = VTY_GROUP,
  68. #endif
  69. .caps_p = _caps_p,
  70. .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
  71. .cap_num_i = 0
  72. };
  73. char* progname;
  74. const char *pid_file = PATH_PIMD_PID;
  75. static void usage(int status)
  76. {
  77. if (status != 0)
  78. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  79. else {
  80. printf ("Usage : %s [OPTION...]\n\
  81. Daemon which manages PIM.\n\n\
  82. -d, --daemon Run in daemon mode\n\
  83. -f, --config_file Set configuration file name\n\
  84. -i, --pid_file Set process identifier file name\n\
  85. -z, --socket Set path of zebra socket\n\
  86. -A, --vty_addr Set vty's bind address\n\
  87. -P, --vty_port Set vty's port number\n\
  88. -v, --version Print program version\n\
  89. "
  90. #ifdef PIM_ZCLIENT_DEBUG
  91. "\
  92. -Z, --debug_zclient Enable zclient debugging\n\
  93. "
  94. #endif
  95. "\
  96. -h, --help Display this help and exit\n\
  97. \n\
  98. Report bugs to %s\n", progname, PIMD_BUG_ADDRESS);
  99. }
  100. exit (status);
  101. }
  102. int main(int argc, char** argv, char** envp) {
  103. char *p;
  104. char *vty_addr = NULL;
  105. int vty_port = -1;
  106. int daemon_mode = 0;
  107. char *config_file = NULL;
  108. char *zebra_sock_path = NULL;
  109. struct thread thread;
  110. umask(0027);
  111. progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
  112. zlog_default = openzlog(progname, ZLOG_PIM,
  113. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  114. /* this while just reads the options */
  115. while (1) {
  116. int opt;
  117. opt = getopt_long (argc, argv, "df:i:z:A:P:vZh", longopts, 0);
  118. if (opt == EOF)
  119. break;
  120. switch (opt) {
  121. case 0:
  122. break;
  123. case 'd':
  124. daemon_mode = 1;
  125. break;
  126. case 'f':
  127. config_file = optarg;
  128. break;
  129. case 'i':
  130. pid_file = optarg;
  131. break;
  132. case 'z':
  133. zebra_sock_path = optarg;
  134. break;
  135. case 'A':
  136. vty_addr = optarg;
  137. break;
  138. case 'P':
  139. vty_port = atoi (optarg);
  140. break;
  141. case 'v':
  142. printf(PIMD_PROGNAME " version %s\n", PIMD_VERSION);
  143. print_version(QUAGGA_PROGNAME);
  144. exit (0);
  145. break;
  146. #ifdef PIM_ZCLIENT_DEBUG
  147. case 'Z':
  148. zclient_debug = 1;
  149. break;
  150. #endif
  151. case 'h':
  152. usage (0);
  153. break;
  154. default:
  155. usage (1);
  156. break;
  157. }
  158. }
  159. master = thread_master_create();
  160. zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting",
  161. QUAGGA_VERSION, PIMD_VERSION);
  162. /*
  163. * Initializations
  164. */
  165. zprivs_init (&pimd_privs);
  166. pim_signals_init();
  167. cmd_init(1);
  168. vty_init(master);
  169. memory_init();
  170. vrf_init();
  171. access_list_init();
  172. pim_init();
  173. /*
  174. * Initialize zclient "update" and "lookup" sockets
  175. */
  176. pim_zebra_init (master, zebra_sock_path);
  177. zlog_notice("Loading configuration - begin");
  178. /* Get configuration file. */
  179. vty_read_config(config_file, config_default);
  180. /*
  181. Starting from here zlog_* functions will log according configuration
  182. */
  183. zlog_notice("Loading configuration - end");
  184. /* Change to the daemon program. */
  185. if (daemon_mode) {
  186. if (daemon(0, 0)) {
  187. zlog_warn("failed to daemonize");
  188. }
  189. }
  190. /* Process ID file creation. */
  191. pid_output(pid_file);
  192. /* Create pimd VTY socket */
  193. if (vty_port < 0)
  194. vty_port = PIMD_VTY_PORT;
  195. vty_serv_sock(vty_addr, vty_port, PIM_VTYSH_PATH);
  196. zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d",
  197. QUAGGA_VERSION, PIMD_VERSION, vty_port);
  198. #ifdef PIM_DEBUG_BYDEFAULT
  199. zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands");
  200. PIM_DO_DEBUG_PIM_EVENTS;
  201. PIM_DO_DEBUG_PIM_PACKETS;
  202. PIM_DO_DEBUG_PIM_TRACE;
  203. PIM_DO_DEBUG_IGMP_EVENTS;
  204. PIM_DO_DEBUG_IGMP_PACKETS;
  205. PIM_DO_DEBUG_IGMP_TRACE;
  206. PIM_DO_DEBUG_ZEBRA;
  207. #endif
  208. #ifdef PIM_ZCLIENT_DEBUG
  209. zlog_notice("PIM_ZCLIENT_DEBUG: zclient debugging is supported, mode is %s (see option -Z)",
  210. zclient_debug ? "ON" : "OFF");
  211. #endif
  212. #ifdef PIM_CHECK_RECV_IFINDEX_SANITY
  213. zlog_notice("PIM_CHECK_RECV_IFINDEX_SANITY: will match sock/recv ifindex");
  214. #ifdef PIM_REPORT_RECV_IFINDEX_MISMATCH
  215. zlog_notice("PIM_REPORT_RECV_IFINDEX_MISMATCH: will report sock/recv ifindex mismatch");
  216. #endif
  217. #endif
  218. #ifdef PIM_UNEXPECTED_KERNEL_UPCALL
  219. zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall");
  220. #endif
  221. #ifdef HAVE_CLOCK_MONOTONIC
  222. zlog_notice("HAVE_CLOCK_MONOTONIC");
  223. #else
  224. zlog_notice("!HAVE_CLOCK_MONOTONIC");
  225. #endif
  226. while (thread_fetch(master, &thread))
  227. thread_call(&thread);
  228. zlog_err("%s %s: thread_fetch() returned NULL, exiting",
  229. __FILE__, __PRETTY_FUNCTION__);
  230. /* never reached */
  231. return 0;
  232. }