ripng_main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * RIPngd main routine.
  3. * Copyright (C) 1998, 1999 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include <lib/version.h>
  24. #include "getopt.h"
  25. #include "vector.h"
  26. #include "vty.h"
  27. #include "command.h"
  28. #include "memory.h"
  29. #include "thread.h"
  30. #include "log.h"
  31. #include "prefix.h"
  32. #include "if.h"
  33. #include "privs.h"
  34. #include "sigevent.h"
  35. #include "ripngd/ripngd.h"
  36. /* Configuration filename and directory. */
  37. char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG;
  38. char *config_file = NULL;
  39. /* RIPngd options. */
  40. struct option longopts[] =
  41. {
  42. { "daemon", no_argument, NULL, 'd'},
  43. { "config_file", required_argument, NULL, 'f'},
  44. { "pid_file", required_argument, NULL, 'i'},
  45. { "dryrun", no_argument, NULL, 'C'},
  46. { "help", no_argument, NULL, 'h'},
  47. { "vty_addr", required_argument, NULL, 'A'},
  48. { "vty_port", required_argument, NULL, 'P'},
  49. { "retain", no_argument, NULL, 'r'},
  50. { "user", required_argument, NULL, 'u'},
  51. { "group", required_argument, NULL, 'g'},
  52. { "version", no_argument, NULL, 'v'},
  53. { 0 }
  54. };
  55. /* ripngd privileges */
  56. zebra_capabilities_t _caps_p [] =
  57. {
  58. ZCAP_NET_RAW,
  59. ZCAP_BIND
  60. };
  61. struct zebra_privs_t ripngd_privs =
  62. {
  63. #if defined(QUAGGA_USER)
  64. .user = QUAGGA_USER,
  65. #endif
  66. #if defined QUAGGA_GROUP
  67. .group = QUAGGA_GROUP,
  68. #endif
  69. #ifdef VTY_GROUP
  70. .vty_group = VTY_GROUP,
  71. #endif
  72. .caps_p = _caps_p,
  73. .cap_num_p = 2,
  74. .cap_num_i = 0
  75. };
  76. /* RIPngd program name */
  77. /* Route retain mode flag. */
  78. int retain_mode = 0;
  79. /* RIPng VTY bind address. */
  80. char *vty_addr = NULL;
  81. /* RIPng VTY connection port. */
  82. int vty_port = RIPNG_VTY_PORT;
  83. /* Master of threads. */
  84. struct thread_master *master;
  85. /* Process ID saved for use by init system */
  86. const char *pid_file = PATH_RIPNGD_PID;
  87. /* Help information display. */
  88. static void
  89. usage (char *progname, int status)
  90. {
  91. if (status != 0)
  92. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  93. else
  94. {
  95. printf ("Usage : %s [OPTION...]\n\
  96. Daemon which manages RIPng.\n\n\
  97. -d, --daemon Runs in daemon mode\n\
  98. -f, --config_file Set configuration file name\n\
  99. -i, --pid_file Set process identifier file name\n\
  100. -A, --vty_addr Set vty's bind address\n\
  101. -P, --vty_port Set vty's port number\n\
  102. -r, --retain When program terminates, retain added route by ripngd.\n\
  103. -u, --user User to run as\n\
  104. -g, --group Group to run as\n\
  105. -v, --version Print program version\n\
  106. -C, --dryrun Check configuration for validity and exit\n\
  107. -h, --help Display this help and exit\n\
  108. \n\
  109. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  110. }
  111. exit (status);
  112. }
  113. /* SIGHUP handler. */
  114. static void
  115. sighup (void)
  116. {
  117. zlog_info ("SIGHUP received");
  118. ripng_clean ();
  119. ripng_reset ();
  120. /* Reload config file. */
  121. vty_read_config (config_file, config_default);
  122. /* Create VTY's socket */
  123. vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
  124. /* Try to return to normal operation. */
  125. }
  126. /* SIGINT handler. */
  127. static void
  128. sigint (void)
  129. {
  130. zlog_notice ("Terminating on signal");
  131. if (! retain_mode)
  132. ripng_clean ();
  133. exit (0);
  134. }
  135. /* SIGUSR1 handler. */
  136. static void
  137. sigusr1 (void)
  138. {
  139. zlog_rotate (NULL);
  140. }
  141. struct quagga_signal_t ripng_signals[] =
  142. {
  143. {
  144. .signal = SIGHUP,
  145. .handler = &sighup,
  146. },
  147. {
  148. .signal = SIGUSR1,
  149. .handler = &sigusr1,
  150. },
  151. {
  152. .signal = SIGINT,
  153. .handler = &sigint,
  154. },
  155. {
  156. .signal = SIGTERM,
  157. .handler = &sigint,
  158. },
  159. };
  160. /* RIPngd main routine. */
  161. int
  162. main (int argc, char **argv)
  163. {
  164. char *p;
  165. int vty_port = RIPNG_VTY_PORT;
  166. int daemon_mode = 0;
  167. char *progname;
  168. struct thread thread;
  169. int dryrun = 0;
  170. /* Set umask before anything for security */
  171. umask (0027);
  172. /* get program name */
  173. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  174. zlog_default = openzlog(progname, ZLOG_RIPNG,
  175. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  176. while (1)
  177. {
  178. int opt;
  179. opt = getopt_long (argc, argv, "df:i:hA:P:u:g:vC", longopts, 0);
  180. if (opt == EOF)
  181. break;
  182. switch (opt)
  183. {
  184. case 0:
  185. break;
  186. case 'd':
  187. daemon_mode = 1;
  188. break;
  189. case 'f':
  190. config_file = optarg;
  191. break;
  192. case 'A':
  193. vty_addr = optarg;
  194. break;
  195. case 'i':
  196. pid_file = optarg;
  197. break;
  198. case 'P':
  199. /* Deal with atoi() returning 0 on failure, and ripngd not
  200. listening on ripngd port... */
  201. if (strcmp(optarg, "0") == 0)
  202. {
  203. vty_port = 0;
  204. break;
  205. }
  206. vty_port = atoi (optarg);
  207. if (vty_port <= 0 || vty_port > 0xffff)
  208. vty_port = RIPNG_VTY_PORT;
  209. break;
  210. case 'r':
  211. retain_mode = 1;
  212. break;
  213. case 'u':
  214. ripngd_privs.user = optarg;
  215. break;
  216. case 'g':
  217. ripngd_privs.group = optarg;
  218. break;
  219. case 'v':
  220. print_version (progname);
  221. exit (0);
  222. break;
  223. case 'C':
  224. dryrun = 1;
  225. break;
  226. case 'h':
  227. usage (progname, 0);
  228. break;
  229. default:
  230. usage (progname, 1);
  231. break;
  232. }
  233. }
  234. master = thread_master_create ();
  235. /* Library inits. */
  236. zprivs_init (&ripngd_privs);
  237. signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
  238. cmd_init (1);
  239. vty_init (master);
  240. memory_init ();
  241. /* RIPngd inits. */
  242. ripng_init ();
  243. zebra_init ();
  244. ripng_peer_init ();
  245. /* Sort all installed commands. */
  246. sort_node ();
  247. /* Get configuration 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. /* Change to the daemon program. */
  253. if (daemon_mode && daemon (0, 0) < 0)
  254. {
  255. zlog_err("RIPNGd daemon failed: %s", strerror(errno));
  256. exit (1);
  257. }
  258. /* Create VTY socket */
  259. vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
  260. /* Process id file create. */
  261. pid_output (pid_file);
  262. /* Print banner. */
  263. zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  264. /* Fetch next active thread. */
  265. while (thread_fetch (master, &thread))
  266. thread_call (&thread);
  267. /* Not reached. */
  268. return 0;
  269. }