ospf6_main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 "getopt.h"
  23. #include "thread.h"
  24. #include "log.h"
  25. #include "version.h"
  26. #include "command.h"
  27. #include "vty.h"
  28. #include "memory.h"
  29. #include "privs.h"
  30. #include "ospf6d.h"
  31. #include "ospf6_network.h"
  32. void ospf6_init ();
  33. void ospf6_terminate ();
  34. void nexthop_init ();
  35. int ospf6_receive (struct thread *);
  36. extern int ospf6_sock;
  37. /* Default configuration file name for ospf6d. */
  38. #define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
  39. /* Default port values. */
  40. #define OSPF6_VTY_PORT 2606
  41. /* ospf6d privileges */
  42. zebra_capabilities_t _caps_p [] =
  43. {
  44. ZCAP_RAW,
  45. ZCAP_BIND
  46. };
  47. struct zebra_privs_t ospf6d_privs =
  48. {
  49. #if defined(QUAGGA_USER)
  50. .user = QUAGGA_USER,
  51. #endif
  52. #if defined QUAGGA_GROUP
  53. .group = QUAGGA_GROUP,
  54. #endif
  55. #ifdef VTY_GROUP
  56. .vty_group = VTY_GROUP,
  57. #endif
  58. .caps_p = _caps_p,
  59. .cap_num_p = 2,
  60. .cap_num_i = 0
  61. };
  62. /* ospf6d options, we use GNU getopt library. */
  63. struct option longopts[] =
  64. {
  65. { "daemon", no_argument, NULL, 'd'},
  66. { "config_file", required_argument, NULL, 'f'},
  67. { "pid_file", required_argument, NULL, 'i'},
  68. { "vty_addr", required_argument, NULL, 'A'},
  69. { "vty_port", required_argument, NULL, 'P'},
  70. { "user", required_argument, NULL, 'u'},
  71. { "version", no_argument, NULL, 'v'},
  72. { "help", no_argument, NULL, 'h'},
  73. { 0 }
  74. };
  75. /* Configuration file and directory. */
  76. char config_current[] = OSPF6_DEFAULT_CONFIG;
  77. char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
  78. /* ospf6d program name. */
  79. /* is daemon? */
  80. int daemon_mode = 0;
  81. /* Master of threads. */
  82. struct thread_master *master;
  83. /* Process ID saved for use by init system */
  84. char *pid_file = PATH_OSPF6D_PID;
  85. /* for reload */
  86. #ifdef MAXPATHLEN
  87. char _cwd[MAXPATHLEN];
  88. #else
  89. char _cwd[64];
  90. #endif
  91. char _progpath[64];
  92. int _argc;
  93. char **_argv;
  94. char **_envp;
  95. /* Help information display. */
  96. static void
  97. usage (char *progname, int status)
  98. {
  99. if (status != 0)
  100. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  101. else
  102. {
  103. printf ("Usage : %s [OPTION...]\n\n\
  104. Daemon which manages OSPF version 3.\n\n\
  105. -d, --daemon Runs in daemon mode\n\
  106. -f, --config_file Set configuration file name\n\
  107. -i, --pid_file Set process identifier file name\n\
  108. -A, --vty_addr Set vty's bind address\n\
  109. -P, --vty_port Set vty's port number\n\
  110. -u, --user User and group to run as\n\
  111. -v, --version Print program version\n\
  112. -h, --help Display this help and exit\n\
  113. \n\
  114. Report bugs to yasu@sfc.wide.ad.jp\n", progname);
  115. }
  116. exit (status);
  117. }
  118. void
  119. _reload ()
  120. {
  121. zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) reloaded",
  122. QUAGGA_VERSION, OSPF6_DAEMON_VERSION);
  123. ospf6_zebra_finish ();
  124. vty_finish ();
  125. execve (_progpath, _argv, _envp);
  126. }
  127. void
  128. terminate (int i)
  129. {
  130. ospf6_delete (ospf6);
  131. unlink (PATH_OSPF6D_PID);
  132. zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) terminated",
  133. QUAGGA_VERSION, OSPF6_DAEMON_VERSION);
  134. exit (i);
  135. }
  136. /* SIGHUP handler. */
  137. void
  138. sighup (int sig)
  139. {
  140. zlog_info ("SIGHUP received");
  141. _reload ();
  142. }
  143. /* SIGINT handler. */
  144. void
  145. sigint (int sig)
  146. {
  147. zlog_info ("SIGINT received");
  148. terminate (0);
  149. }
  150. /* SIGTERM handler. */
  151. void
  152. sigterm (int sig)
  153. {
  154. zlog_info ("SIGTERM received");
  155. terminate (0);
  156. }
  157. /* SIGUSR1 handler. */
  158. void
  159. sigusr1 (int sig)
  160. {
  161. zlog_info ("SIGUSR1 received");
  162. zlog_rotate (NULL);
  163. }
  164. /* Signale wrapper. */
  165. RETSIGTYPE *
  166. signal_set (int signo, void (*func)(int))
  167. {
  168. int ret;
  169. struct sigaction sig;
  170. struct sigaction osig;
  171. sig.sa_handler = func;
  172. sigemptyset (&sig.sa_mask);
  173. sig.sa_flags = 0;
  174. #ifdef SA_RESTART
  175. sig.sa_flags |= SA_RESTART;
  176. #endif /* SA_RESTART */
  177. ret = sigaction (signo, &sig, &osig);
  178. if (ret < 0)
  179. return (SIG_ERR);
  180. else
  181. return (osig.sa_handler);
  182. }
  183. /* Initialization of signal handles. */
  184. void
  185. signal_init ()
  186. {
  187. signal_set (SIGHUP, sighup);
  188. signal_set (SIGINT, sigint);
  189. signal_set (SIGTERM, sigterm);
  190. signal_set (SIGPIPE, SIG_IGN);
  191. #ifdef SIGTSTP
  192. signal_set (SIGTSTP, SIG_IGN);
  193. #endif
  194. #ifdef SIGTTIN
  195. signal_set (SIGTTIN, SIG_IGN);
  196. #endif
  197. #ifdef SIGTTOU
  198. signal_set (SIGTTOU, SIG_IGN);
  199. #endif
  200. signal_set (SIGUSR1, sigusr1);
  201. }
  202. /* Main routine of ospf6d. Treatment of argument and start ospf finite
  203. state machine is handled here. */
  204. int
  205. main (int argc, char *argv[], char *envp[])
  206. {
  207. char *p;
  208. int opt;
  209. char *vty_addr = NULL;
  210. int vty_port = OSPF6_VTY_PORT;
  211. char *config_file = NULL;
  212. char *progname;
  213. struct thread thread;
  214. int flag;
  215. /* Set umask before anything for security */
  216. umask (0027);
  217. /* Preserve name of myself. */
  218. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  219. /* for reload */
  220. _argc = argc;
  221. _argv = argv;
  222. _envp = envp;
  223. getcwd (_cwd, sizeof (_cwd));
  224. if (*argv[0] == '.')
  225. snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
  226. else
  227. snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
  228. /* Command line argument treatment. */
  229. while (1)
  230. {
  231. opt = getopt_long (argc, argv, "df:i:hp:A:P:u:v", longopts, 0);
  232. if (opt == EOF)
  233. break;
  234. switch (opt)
  235. {
  236. case 0:
  237. break;
  238. case 'd':
  239. daemon_mode = 1;
  240. break;
  241. case 'f':
  242. config_file = optarg;
  243. break;
  244. case 'A':
  245. vty_addr = optarg;
  246. break;
  247. case 'i':
  248. pid_file = optarg;
  249. break;
  250. case 'P':
  251. /* Deal with atoi() returning 0 on failure, and ospf6d not
  252. listening on ospf6d port... */
  253. if (strcmp(optarg, "0") == 0)
  254. {
  255. vty_port = 0;
  256. break;
  257. }
  258. vty_port = atoi (optarg);
  259. vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT);
  260. break;
  261. case 'u':
  262. ospf6d_privs.user = ospf6d_privs.group = optarg;
  263. break;
  264. case 'v':
  265. print_version (progname);
  266. exit (0);
  267. break;
  268. case 'h':
  269. usage (progname, 0);
  270. break;
  271. default:
  272. usage (progname, 1);
  273. break;
  274. }
  275. }
  276. /* thread master */
  277. master = thread_master_create ();
  278. /* Initializations. */
  279. if (! daemon_mode)
  280. flag = ZLOG_STDOUT;
  281. else
  282. flag = ZLOG_NOLOG;
  283. zlog_default = openzlog (progname, flag, ZLOG_OSPF6,
  284. LOG_CONS|LOG_NDELAY|LOG_PID,
  285. LOG_DAEMON);
  286. zprivs_init (&ospf6d_privs);
  287. signal_init ();
  288. cmd_init (1);
  289. vty_init (master);
  290. ospf6_init ();
  291. memory_init ();
  292. sort_node ();
  293. /* parse config file */
  294. vty_read_config (config_file, config_current, config_default);
  295. if (daemon_mode)
  296. daemon (0, 0);
  297. /* pid file create */
  298. #if 0
  299. pid_output_lock (pid_file);
  300. #else
  301. pid_output (pid_file);
  302. #endif
  303. /* Make ospf protocol socket. */
  304. ospf6_serv_sock ();
  305. thread_add_read (master, ospf6_receive, NULL, ospf6_sock);
  306. /* Make ospf vty socket. */
  307. vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH);
  308. #ifdef DEBUG
  309. /* Print start message */
  310. zlog_notice ("OSPF6d (Zebra-%s ospf6d-%s) starts",
  311. QUAGGA_VERSION, OSPF6_DAEMON_VERSION);
  312. #endif
  313. /* Start finite state machine, here we go! */
  314. while (thread_fetch (master, &thread))
  315. thread_call (&thread);
  316. /* Log in case thread failed */
  317. zlog_warn ("Thread failed");
  318. terminate (0);
  319. /* Not reached. */
  320. exit (0);
  321. }