pim_main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. extern const char *default_motd;
  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. -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. struct thread thread;
  108. umask(0027);
  109. progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
  110. zlog_default = openzlog(progname, ZLOG_PIM,
  111. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  112. /* this while just reads the options */
  113. while (1) {
  114. int opt;
  115. opt = getopt_long (argc, argv, "df:i:A:P:vZh", longopts, 0);
  116. if (opt == EOF)
  117. break;
  118. switch (opt) {
  119. case 0:
  120. break;
  121. case 'd':
  122. daemon_mode = 1;
  123. break;
  124. case 'f':
  125. config_file = optarg;
  126. break;
  127. case 'i':
  128. pid_file = optarg;
  129. break;
  130. case 'A':
  131. vty_addr = optarg;
  132. break;
  133. case 'P':
  134. vty_port = atoi (optarg);
  135. break;
  136. case 'v':
  137. printf(PIMD_PROGNAME " version %s\n", PIMD_VERSION);
  138. print_version(QUAGGA_PROGNAME);
  139. exit (0);
  140. break;
  141. #ifdef PIM_ZCLIENT_DEBUG
  142. case 'Z':
  143. zclient_debug = 1;
  144. break;
  145. #endif
  146. case 'h':
  147. usage (0);
  148. break;
  149. default:
  150. usage (1);
  151. break;
  152. }
  153. }
  154. master = thread_master_create();
  155. /*
  156. * Temporarily send zlog to stdout
  157. */
  158. zlog_default->maxlvl[ZLOG_DEST_STDOUT] = zlog_default->default_lvl;
  159. zlog_notice("Boot logging temporarily directed to stdout - begin");
  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. access_list_init();
  171. pim_init();
  172. sort_node();
  173. /*
  174. * reset zlog default, then will obey configuration file
  175. */
  176. zlog_notice("Boot logging temporarily directed to stdout - end");
  177. #if 0
  178. /* this would disable logging to stdout, but config has not been
  179. loaded yet to reconfig the logging output */
  180. zlog_default->maxlvl[ZLOG_DEST_STDOUT] = ZLOG_DISABLED;
  181. #endif
  182. zlog_notice("Loading configuration - begin");
  183. /* Get configuration file. */
  184. vty_read_config(config_file, config_default);
  185. /*
  186. Starting from here zlog_* functions will log according configuration
  187. */
  188. zlog_notice("Loading configuration - end");
  189. /* Change to the daemon program. */
  190. if (daemon_mode) {
  191. if (daemon(0, 0)) {
  192. zlog_warn("failed to daemonize");
  193. }
  194. }
  195. /* Process ID file creation. */
  196. pid_output(pid_file);
  197. /* Create pimd VTY socket */
  198. if (vty_port < 0)
  199. vty_port = PIMD_VTY_PORT;
  200. vty_serv_sock(vty_addr, vty_port, PIM_VTYSH_PATH);
  201. zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d",
  202. QUAGGA_VERSION, PIMD_VERSION, vty_port);
  203. #ifdef PIM_MOTD_VERSION
  204. /* Tweak default MOTD to include pimd version */
  205. zlog_notice("PIM_MOTD_VERSION: adding pimd version to default MOTD");
  206. if (host.motd == default_motd) {
  207. host.motd =
  208. "\r\n\
  209. Hello, this is " QUAGGA_PROGNAME " " QUAGGA_VERSION " " PIMD_PROGNAME " " PIMD_VERSION_STR "\r\n\
  210. " QUAGGA_COPYRIGHT "\r\n\
  211. \r\n";
  212. }
  213. #endif
  214. #ifdef PIM_DEBUG_BYDEFAULT
  215. zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands");
  216. PIM_DO_DEBUG_PIM_EVENTS;
  217. PIM_DO_DEBUG_PIM_PACKETS;
  218. PIM_DO_DEBUG_PIM_TRACE;
  219. PIM_DO_DEBUG_IGMP_EVENTS;
  220. PIM_DO_DEBUG_IGMP_PACKETS;
  221. PIM_DO_DEBUG_IGMP_TRACE;
  222. PIM_DO_DEBUG_ZEBRA;
  223. #endif
  224. #ifdef PIM_ZCLIENT_DEBUG
  225. zlog_notice("PIM_ZCLIENT_DEBUG: zclient debugging is supported, mode is %s (see option -Z)",
  226. zclient_debug ? "ON" : "OFF");
  227. #endif
  228. #ifdef PIM_CHECK_RECV_IFINDEX_SANITY
  229. zlog_notice("PIM_CHECK_RECV_IFINDEX_SANITY: will match sock/recv ifindex");
  230. #ifdef PIM_REPORT_RECV_IFINDEX_MISMATCH
  231. zlog_notice("PIM_REPORT_RECV_IFINDEX_MISMATCH: will report sock/recv ifindex mismatch");
  232. #endif
  233. #endif
  234. #ifdef PIM_USE_QUAGGA_INET_CHECKSUM
  235. zlog_notice("PIM_USE_QUAGGA_INET_CHECKSUM: using Quagga's builtin checksum");
  236. #endif
  237. #ifdef PIM_UNEXPECTED_KERNEL_UPCALL
  238. zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall");
  239. #endif
  240. /*
  241. Initialize zclient "update" and "lookup" sockets
  242. */
  243. pim_zebra_init();
  244. while (thread_fetch(master, &thread))
  245. thread_call(&thread);
  246. zlog_err("%s %s: thread_fetch() returned NULL, exiting",
  247. __FILE__, __PRETTY_FUNCTION__);
  248. /* never reached */
  249. return 0;
  250. }