ospf6_main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (C) 1999 Yasuhiro Ohara
  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
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include <lib/version.h>
  23. #include "getopt.h"
  24. #include "thread.h"
  25. #include "log.h"
  26. #include "command.h"
  27. #include "vty.h"
  28. #include "memory.h"
  29. #include "if.h"
  30. #include "filter.h"
  31. #include "prefix.h"
  32. #include "plist.h"
  33. #include "privs.h"
  34. #include "sigevent.h"
  35. #include "ospf6d.h"
  36. /* Default configuration file name for ospf6d. */
  37. #define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
  38. /* Default port values. */
  39. #define OSPF6_VTY_PORT 2606
  40. /* ospf6d privileges */
  41. zebra_capabilities_t _caps_p [] =
  42. {
  43. ZCAP_RAW,
  44. ZCAP_BIND
  45. };
  46. struct zebra_privs_t ospf6d_privs =
  47. {
  48. #if defined(QUAGGA_USER)
  49. .user = QUAGGA_USER,
  50. #endif
  51. #if defined QUAGGA_GROUP
  52. .group = QUAGGA_GROUP,
  53. #endif
  54. #ifdef VTY_GROUP
  55. .vty_group = VTY_GROUP,
  56. #endif
  57. .caps_p = _caps_p,
  58. .cap_num_p = 2,
  59. .cap_num_i = 0
  60. };
  61. /* ospf6d options, we use GNU getopt library. */
  62. struct option longopts[] =
  63. {
  64. { "daemon", no_argument, NULL, 'd'},
  65. { "config_file", required_argument, NULL, 'f'},
  66. { "pid_file", required_argument, NULL, 'i'},
  67. { "vty_addr", required_argument, NULL, 'A'},
  68. { "vty_port", required_argument, NULL, 'P'},
  69. { "version", no_argument, NULL, 'v'},
  70. { "help", no_argument, NULL, 'h'},
  71. { 0 }
  72. };
  73. /* Configuration file and directory. */
  74. char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
  75. /* ospf6d program name. */
  76. char *progname;
  77. /* is daemon? */
  78. int daemon_mode = 0;
  79. /* Master of threads. */
  80. struct thread_master *master;
  81. /* Process ID saved for use by init system */
  82. const char *pid_file = PATH_OSPF6D_PID;
  83. /* Help information display. */
  84. static void
  85. usage (char *progname, int status)
  86. {
  87. if (status != 0)
  88. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  89. else
  90. {
  91. printf ("Usage : %s [OPTION...]\n\n\
  92. Daemon which manages OSPF version 3.\n\n\
  93. -d, --daemon Runs in daemon mode\n\
  94. -f, --config_file Set configuration file name\n\
  95. -i, --pid_file Set process identifier file name\n\
  96. -A, --vty_addr Set vty's bind address\n\
  97. -P, --vty_port Set vty's port number\n\
  98. -v, --version Print program version\n\
  99. -h, --help Display this help and exit\n\
  100. \n\
  101. Report bugs to zebra@zebra.org\n", progname);
  102. }
  103. exit (status);
  104. }
  105. /* SIGHUP handler. */
  106. void
  107. sighup (void)
  108. {
  109. zlog_info ("SIGHUP received");
  110. }
  111. /* SIGINT handler. */
  112. void
  113. sigint (void)
  114. {
  115. zlog_info ("SIGINT received");
  116. exit (0);
  117. }
  118. /* SIGTERM handler. */
  119. void
  120. sigterm (void)
  121. {
  122. zlog_info ("SIGTERM received");
  123. exit (0);
  124. }
  125. /* SIGUSR1 handler. */
  126. void
  127. sigusr1 (void)
  128. {
  129. zlog_info ("SIGUSR1 received");
  130. zlog_rotate (NULL);
  131. }
  132. struct quagga_signal_t ospf6_signals[] =
  133. {
  134. {
  135. .signal = SIGHUP,
  136. .handler = &sighup,
  137. },
  138. {
  139. .signal = SIGINT,
  140. .handler = &sigint,
  141. },
  142. {
  143. .signal = SIGTERM,
  144. .handler = &sigterm,
  145. },
  146. {
  147. .signal = SIGUSR1,
  148. .handler = &sigusr1,
  149. },
  150. };
  151. /* Main routine of ospf6d. Treatment of argument and starting ospf finite
  152. state machine is handled here. */
  153. int
  154. main (int argc, char *argv[], char *envp[])
  155. {
  156. char *p;
  157. int opt;
  158. char *vty_addr = NULL;
  159. int vty_port = 0;
  160. char *config_file = NULL;
  161. struct thread thread;
  162. int flag;
  163. /* Set umask before anything for security */
  164. umask (0027);
  165. /* Preserve name of myself. */
  166. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  167. /* Command line argument treatment. */
  168. while (1)
  169. {
  170. opt = getopt_long (argc, argv, "df:i:hp:A:P:u:v", longopts, 0);
  171. if (opt == EOF)
  172. break;
  173. switch (opt)
  174. {
  175. case 0:
  176. break;
  177. case 'd':
  178. daemon_mode = 1;
  179. break;
  180. case 'f':
  181. config_file = optarg;
  182. break;
  183. case 'A':
  184. vty_addr = optarg;
  185. break;
  186. case 'i':
  187. pid_file = optarg;
  188. break;
  189. case 'P':
  190. /* Deal with atoi() returning 0 on failure, and ospf6d not
  191. listening on ospf6d port... */
  192. if (strcmp(optarg, "0") == 0)
  193. {
  194. vty_port = 0;
  195. break;
  196. }
  197. vty_port = atoi (optarg);
  198. vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT);
  199. break;
  200. case 'u':
  201. ospf6d_privs.user = ospf6d_privs.group = optarg;
  202. break;
  203. case 'v':
  204. print_version (progname);
  205. exit (0);
  206. break;
  207. case 'h':
  208. usage (progname, 0);
  209. break;
  210. default:
  211. usage (progname, 1);
  212. break;
  213. }
  214. }
  215. /* thread master */
  216. master = thread_master_create ();
  217. /* Initializations. */
  218. if (! daemon_mode)
  219. flag = ZLOG_STDOUT;
  220. else
  221. flag = 0;
  222. zlog_default = openzlog (progname, flag, ZLOG_OSPF6,
  223. LOG_CONS|LOG_NDELAY|LOG_PID,
  224. LOG_DAEMON);
  225. zprivs_init (&ospf6d_privs);
  226. /* initialize zebra libraries */
  227. signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals);
  228. cmd_init (1);
  229. vty_init (master);
  230. memory_init ();
  231. if_init ();
  232. access_list_init ();
  233. prefix_list_init ();
  234. /* initialize ospf6 */
  235. ospf6_init ();
  236. /* sort command vector */
  237. sort_node ();
  238. /* parse config file */
  239. vty_read_config (config_file, config_default);
  240. if (daemon_mode)
  241. daemon (0, 0);
  242. /* pid file create */
  243. #if 0
  244. pid_output_lock (pid_file);
  245. #else
  246. pid_output (pid_file);
  247. #endif
  248. /* Make ospf6 vty socket. */
  249. vty_serv_sock (vty_addr,
  250. vty_port ? vty_port : OSPF6_VTY_PORT, OSPF6_VTYSH_PATH);
  251. /* Print start message */
  252. zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts",
  253. QUAGGA_VERSION, OSPF6_DAEMON_VERSION);
  254. /* Start finite state machine, here we go! */
  255. while (thread_fetch (master, &thread))
  256. thread_call (&thread);
  257. /* Log in case thread failed */
  258. zlog_warn ("Thread failed");
  259. /* Not reached. */
  260. exit (0);
  261. }