pim_main.c 7.0 KB

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