ospf6_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_NET_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. { "user", required_argument, NULL, 'u'},
  70. { "group", required_argument, NULL, 'g'},
  71. { "version", no_argument, NULL, 'v'},
  72. { "dryrun", no_argument, NULL, 'C'},
  73. { "help", no_argument, NULL, 'h'},
  74. { 0 }
  75. };
  76. /* Configuration file and directory. */
  77. char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
  78. /* ospf6d program name. */
  79. char *progname;
  80. /* is daemon? */
  81. int daemon_mode = 0;
  82. /* Master of threads. */
  83. struct thread_master *master;
  84. /* Process ID saved for use by init system */
  85. const char *pid_file = PATH_OSPF6D_PID;
  86. /* Help information display. */
  87. static void
  88. usage (char *progname, int status)
  89. {
  90. if (status != 0)
  91. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  92. else
  93. {
  94. printf ("Usage : %s [OPTION...]\n\n\
  95. Daemon which manages OSPF version 3.\n\n\
  96. -d, --daemon Runs in daemon mode\n\
  97. -f, --config_file Set configuration file name\n\
  98. -i, --pid_file Set process identifier file name\n\
  99. -A, --vty_addr Set vty's bind address\n\
  100. -P, --vty_port Set vty's port number\n\
  101. -u, --user User to run as\n\
  102. -g, --group Group to run as\n\
  103. -v, --version Print program version\n\
  104. -C, --dryrun Check configuration for validity and exit\n\
  105. -h, --help Display this help and exit\n\
  106. \n\
  107. Report bugs to zebra@zebra.org\n", progname);
  108. }
  109. exit (status);
  110. }
  111. /* SIGHUP handler. */
  112. static void
  113. sighup (void)
  114. {
  115. zlog_info ("SIGHUP received");
  116. }
  117. /* SIGINT handler. */
  118. static void
  119. sigint (void)
  120. {
  121. zlog_notice ("Terminating on signal SIGINT");
  122. exit (0);
  123. }
  124. /* SIGTERM handler. */
  125. static void
  126. sigterm (void)
  127. {
  128. zlog_notice ("Terminating on signal SIGTERM");
  129. exit (0);
  130. }
  131. /* SIGUSR1 handler. */
  132. static void
  133. sigusr1 (void)
  134. {
  135. zlog_info ("SIGUSR1 received");
  136. zlog_rotate (NULL);
  137. }
  138. struct quagga_signal_t ospf6_signals[] =
  139. {
  140. {
  141. .signal = SIGHUP,
  142. .handler = &sighup,
  143. },
  144. {
  145. .signal = SIGINT,
  146. .handler = &sigint,
  147. },
  148. {
  149. .signal = SIGTERM,
  150. .handler = &sigterm,
  151. },
  152. {
  153. .signal = SIGUSR1,
  154. .handler = &sigusr1,
  155. },
  156. };
  157. /* Main routine of ospf6d. Treatment of argument and starting ospf finite
  158. state machine is handled here. */
  159. int
  160. main (int argc, char *argv[], char *envp[])
  161. {
  162. char *p;
  163. int opt;
  164. char *vty_addr = NULL;
  165. int vty_port = 0;
  166. char *config_file = NULL;
  167. struct thread thread;
  168. int dryrun = 0;
  169. /* Set umask before anything for security */
  170. umask (0027);
  171. /* Preserve name of myself. */
  172. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  173. /* Command line argument treatment. */
  174. while (1)
  175. {
  176. opt = getopt_long (argc, argv, "df:i:hp:A:P:u:g:vC", longopts, 0);
  177. if (opt == EOF)
  178. break;
  179. switch (opt)
  180. {
  181. case 0:
  182. break;
  183. case 'd':
  184. daemon_mode = 1;
  185. break;
  186. case 'f':
  187. config_file = optarg;
  188. break;
  189. case 'A':
  190. vty_addr = optarg;
  191. break;
  192. case 'i':
  193. pid_file = optarg;
  194. break;
  195. case 'P':
  196. /* Deal with atoi() returning 0 on failure, and ospf6d not
  197. listening on ospf6d port... */
  198. if (strcmp(optarg, "0") == 0)
  199. {
  200. vty_port = 0;
  201. break;
  202. }
  203. vty_port = atoi (optarg);
  204. if (vty_port <= 0 || vty_port > 0xffff)
  205. vty_port = OSPF6_VTY_PORT;
  206. break;
  207. case 'u':
  208. ospf6d_privs.user = optarg;
  209. break;
  210. case 'g':
  211. ospf6d_privs.group = optarg;
  212. break;
  213. case 'v':
  214. print_version (progname);
  215. exit (0);
  216. break;
  217. case 'C':
  218. dryrun = 1;
  219. break;
  220. case 'h':
  221. usage (progname, 0);
  222. break;
  223. default:
  224. usage (progname, 1);
  225. break;
  226. }
  227. }
  228. /* thread master */
  229. master = thread_master_create ();
  230. /* Initializations. */
  231. zlog_default = openzlog (progname, ZLOG_OSPF6,
  232. LOG_CONS|LOG_NDELAY|LOG_PID,
  233. LOG_DAEMON);
  234. zprivs_init (&ospf6d_privs);
  235. /* initialize zebra libraries */
  236. signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals);
  237. cmd_init (1);
  238. vty_init (master);
  239. memory_init ();
  240. if_init ();
  241. access_list_init ();
  242. prefix_list_init ();
  243. /* initialize ospf6 */
  244. ospf6_init ();
  245. /* sort command vector */
  246. sort_node ();
  247. /* parse config file */
  248. vty_read_config (config_file, config_default);
  249. /* Start execution only if not in dry-run mode */
  250. if (dryrun)
  251. return(0);
  252. if (daemon_mode)
  253. daemon (0, 0);
  254. /* pid file create */
  255. pid_output (pid_file);
  256. /* Make ospf6 vty socket. */
  257. if (!vty_port)
  258. vty_port = OSPF6_VTY_PORT;
  259. vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH);
  260. /* Print start message */
  261. zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d",
  262. QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port);
  263. /* Start finite state machine, here we go! */
  264. while (thread_fetch (master, &thread))
  265. thread_call (&thread);
  266. /* Log in case thread failed */
  267. zlog_warn ("Thread failed");
  268. /* Not reached. */
  269. exit (0);
  270. }