ospf6_main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "ospf6d.h"
  35. /* Default configuration file name for ospf6d. */
  36. #define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
  37. /* Default port values. */
  38. #define OSPF6_VTY_PORT 2606
  39. /* ospf6d privileges */
  40. zebra_capabilities_t _caps_p [] =
  41. {
  42. ZCAP_RAW,
  43. ZCAP_BIND
  44. };
  45. struct zebra_privs_t ospf6d_privs =
  46. {
  47. #if defined(QUAGGA_USER)
  48. .user = QUAGGA_USER,
  49. #endif
  50. #if defined QUAGGA_GROUP
  51. .group = QUAGGA_GROUP,
  52. #endif
  53. #ifdef VTY_GROUP
  54. .vty_group = VTY_GROUP,
  55. #endif
  56. .caps_p = _caps_p,
  57. .cap_num_p = 2,
  58. .cap_num_i = 0
  59. };
  60. /* ospf6d options, we use GNU getopt library. */
  61. struct option longopts[] =
  62. {
  63. { "daemon", no_argument, NULL, 'd'},
  64. { "config_file", required_argument, NULL, 'f'},
  65. { "pid_file", required_argument, NULL, 'i'},
  66. { "vty_addr", required_argument, NULL, 'A'},
  67. { "vty_port", required_argument, NULL, 'P'},
  68. { "version", no_argument, NULL, 'v'},
  69. { "help", no_argument, NULL, 'h'},
  70. { 0 }
  71. };
  72. /* Configuration file and directory. */
  73. char config_current[] = OSPF6_DEFAULT_CONFIG;
  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. 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 (int sig)
  108. {
  109. zlog_info ("SIGHUP received");
  110. }
  111. /* SIGINT handler. */
  112. void
  113. sigint (int sig)
  114. {
  115. zlog_info ("SIGINT received");
  116. exit (0);
  117. }
  118. /* SIGTERM handler. */
  119. void
  120. sigterm (int sig)
  121. {
  122. zlog_info ("SIGTERM received");
  123. exit (0);
  124. }
  125. /* SIGUSR1 handler. */
  126. void
  127. sigusr1 (int sig)
  128. {
  129. zlog_info ("SIGUSR1 received");
  130. zlog_rotate (NULL);
  131. }
  132. /* Signale wrapper. */
  133. RETSIGTYPE *
  134. signal_set (int signo, void (*func)(int))
  135. {
  136. int ret;
  137. struct sigaction sig;
  138. struct sigaction osig;
  139. sig.sa_handler = func;
  140. sigemptyset (&sig.sa_mask);
  141. sig.sa_flags = 0;
  142. #ifdef SA_RESTART
  143. sig.sa_flags |= SA_RESTART;
  144. #endif /* SA_RESTART */
  145. ret = sigaction (signo, &sig, &osig);
  146. if (ret < 0)
  147. return (SIG_ERR);
  148. else
  149. return (osig.sa_handler);
  150. }
  151. /* Initialization of signal handles. */
  152. void
  153. signal_init ()
  154. {
  155. signal_set (SIGHUP, sighup);
  156. signal_set (SIGINT, sigint);
  157. signal_set (SIGTERM, sigterm);
  158. signal_set (SIGPIPE, SIG_IGN);
  159. #ifdef SIGTSTP
  160. signal_set (SIGTSTP, SIG_IGN);
  161. #endif
  162. #ifdef SIGTTIN
  163. signal_set (SIGTTIN, SIG_IGN);
  164. #endif
  165. #ifdef SIGTTOU
  166. signal_set (SIGTTOU, SIG_IGN);
  167. #endif
  168. signal_set (SIGUSR1, sigusr1);
  169. }
  170. /* Main routine of ospf6d. Treatment of argument and starting ospf finite
  171. state machine is handled here. */
  172. int
  173. main (int argc, char *argv[], char *envp[])
  174. {
  175. char *p;
  176. int opt;
  177. char *vty_addr = NULL;
  178. int vty_port = 0;
  179. char *config_file = NULL;
  180. struct thread thread;
  181. int flag;
  182. /* Set umask before anything for security */
  183. umask (0027);
  184. /* Preserve name of myself. */
  185. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  186. /* Command line argument treatment. */
  187. while (1)
  188. {
  189. opt = getopt_long (argc, argv, "df:i:hp:A:P:u:v", longopts, 0);
  190. if (opt == EOF)
  191. break;
  192. switch (opt)
  193. {
  194. case 0:
  195. break;
  196. case 'd':
  197. daemon_mode = 1;
  198. break;
  199. case 'f':
  200. config_file = optarg;
  201. break;
  202. case 'A':
  203. vty_addr = optarg;
  204. break;
  205. case 'i':
  206. pid_file = optarg;
  207. break;
  208. case 'P':
  209. /* Deal with atoi() returning 0 on failure, and ospf6d not
  210. listening on ospf6d port... */
  211. if (strcmp(optarg, "0") == 0)
  212. {
  213. vty_port = 0;
  214. break;
  215. }
  216. vty_port = atoi (optarg);
  217. vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT);
  218. break;
  219. case 'u':
  220. ospf6d_privs.user = ospf6d_privs.group = optarg;
  221. break;
  222. case 'v':
  223. print_version (progname);
  224. exit (0);
  225. break;
  226. case 'h':
  227. usage (progname, 0);
  228. break;
  229. default:
  230. usage (progname, 1);
  231. break;
  232. }
  233. }
  234. /* thread master */
  235. master = thread_master_create ();
  236. /* Initializations. */
  237. if (! daemon_mode)
  238. flag = ZLOG_STDOUT;
  239. else
  240. flag = 0;
  241. zlog_default = openzlog (progname, flag, ZLOG_OSPF6,
  242. LOG_CONS|LOG_NDELAY|LOG_PERROR|LOG_PID,
  243. LOG_DAEMON);
  244. zprivs_init (&ospf6d_privs);
  245. /* initialize zebra libraries */
  246. signal_init ();
  247. cmd_init (1);
  248. vty_init (master);
  249. memory_init ();
  250. if_init ();
  251. access_list_init ();
  252. prefix_list_init ();
  253. /* initialize ospf6 */
  254. ospf6_init ();
  255. /* sort command vector */
  256. sort_node ();
  257. /* parse config file */
  258. vty_read_config (config_file, config_current, config_default);
  259. if (daemon_mode)
  260. daemon (0, 0);
  261. /* pid file create */
  262. #if 0
  263. pid_output_lock (pid_file);
  264. #else
  265. pid_output (pid_file);
  266. #endif
  267. /* Make ospf6 vty socket. */
  268. vty_serv_sock (vty_addr,
  269. vty_port ? vty_port : OSPF6_VTY_PORT, OSPF6_VTYSH_PATH);
  270. /* Print start message */
  271. zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts",
  272. QUAGGA_VERSION, OSPF6_DAEMON_VERSION);
  273. /* Start finite state machine, here we go! */
  274. while (thread_fetch (master, &thread))
  275. thread_call (&thread);
  276. /* Log in case thread failed */
  277. zlog_warn ("Thread failed");
  278. /* Not reached. */
  279. exit (0);
  280. }