ripng_main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. { "log_mode", no_argument, NULL, 'l'},
  46. { "dryrun", no_argument, NULL, 'C'},
  47. { "help", no_argument, NULL, 'h'},
  48. { "vty_addr", required_argument, NULL, 'A'},
  49. { "vty_port", required_argument, NULL, 'P'},
  50. { "retain", no_argument, NULL, 'r'},
  51. { "user", required_argument, NULL, 'u'},
  52. { "group", required_argument, NULL, 'g'},
  53. { "version", no_argument, NULL, 'v'},
  54. { 0 }
  55. };
  56. /* ripngd privileges */
  57. zebra_capabilities_t _caps_p [] =
  58. {
  59. ZCAP_NET_RAW,
  60. ZCAP_BIND
  61. };
  62. struct zebra_privs_t ripngd_privs =
  63. {
  64. #if defined(QUAGGA_USER)
  65. .user = QUAGGA_USER,
  66. #endif
  67. #if defined QUAGGA_GROUP
  68. .group = QUAGGA_GROUP,
  69. #endif
  70. #ifdef VTY_GROUP
  71. .vty_group = VTY_GROUP,
  72. #endif
  73. .caps_p = _caps_p,
  74. .cap_num_p = 2,
  75. .cap_num_i = 0
  76. };
  77. /* RIPngd program name */
  78. /* Route retain mode flag. */
  79. int retain_mode = 0;
  80. /* RIPng VTY bind address. */
  81. char *vty_addr = NULL;
  82. /* RIPng VTY connection port. */
  83. int vty_port = RIPNG_VTY_PORT;
  84. /* Master of threads. */
  85. struct thread_master *master;
  86. /* Process ID saved for use by init system */
  87. const char *pid_file = PATH_RIPNGD_PID;
  88. /* Help information display. */
  89. static void
  90. usage (char *progname, int status)
  91. {
  92. if (status != 0)
  93. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  94. else
  95. {
  96. printf ("Usage : %s [OPTION...]\n\
  97. Daemon which manages RIPng.\n\n\
  98. -d, --daemon Runs in daemon mode\n\
  99. -f, --config_file Set configuration file name\n\
  100. -i, --pid_file Set process identifier file name\n\
  101. -l. --log_mode Set verbose log mode flag\n\
  102. -A, --vty_addr Set vty's bind address\n\
  103. -P, --vty_port Set vty's port number\n\
  104. -r, --retain When program terminates, retain added route by ripngd.\n\
  105. -u, --user User to run as\n\
  106. -g, --group Group to run as\n\
  107. -v, --version Print program version\n\
  108. -C, --dryrun Check configuration for validity and exit\n\
  109. -h, --help Display this help and exit\n\
  110. \n\
  111. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  112. }
  113. exit (status);
  114. }
  115. /* SIGHUP handler. */
  116. static void
  117. sighup (void)
  118. {
  119. zlog_info ("SIGHUP received");
  120. ripng_clean ();
  121. ripng_reset ();
  122. /* Reload config file. */
  123. vty_read_config (config_file, config_default);
  124. /* Create VTY's socket */
  125. vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
  126. /* Try to return to normal operation. */
  127. }
  128. /* SIGINT handler. */
  129. static void
  130. sigint (void)
  131. {
  132. zlog_notice ("Terminating on signal");
  133. if (! retain_mode)
  134. ripng_clean ();
  135. exit (0);
  136. }
  137. /* SIGUSR1 handler. */
  138. static void
  139. sigusr1 (void)
  140. {
  141. zlog_rotate (NULL);
  142. }
  143. struct quagga_signal_t ripng_signals[] =
  144. {
  145. {
  146. .signal = SIGHUP,
  147. .handler = &sighup,
  148. },
  149. {
  150. .signal = SIGUSR1,
  151. .handler = &sigusr1,
  152. },
  153. {
  154. .signal = SIGINT,
  155. .handler = &sigint,
  156. },
  157. {
  158. .signal = SIGTERM,
  159. .handler = &sigint,
  160. },
  161. };
  162. /* RIPngd main routine. */
  163. int
  164. main (int argc, char **argv)
  165. {
  166. char *p;
  167. int vty_port = RIPNG_VTY_PORT;
  168. int daemon_mode = 0;
  169. char *progname;
  170. struct thread thread;
  171. int dryrun = 0;
  172. /* Set umask before anything for security */
  173. umask (0027);
  174. /* get program name */
  175. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  176. zlog_default = openzlog(progname, ZLOG_RIPNG,
  177. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  178. while (1)
  179. {
  180. int opt;
  181. opt = getopt_long (argc, argv, "dlf:i:hA:P:u:g:vC", longopts, 0);
  182. if (opt == EOF)
  183. break;
  184. switch (opt)
  185. {
  186. case 0:
  187. break;
  188. case 'd':
  189. daemon_mode = 1;
  190. break;
  191. case 'l':
  192. /* log_mode = 1; */
  193. break;
  194. case 'f':
  195. config_file = optarg;
  196. break;
  197. case 'A':
  198. vty_addr = optarg;
  199. break;
  200. case 'i':
  201. pid_file = optarg;
  202. break;
  203. case 'P':
  204. /* Deal with atoi() returning 0 on failure, and ripngd not
  205. listening on ripngd port... */
  206. if (strcmp(optarg, "0") == 0)
  207. {
  208. vty_port = 0;
  209. break;
  210. }
  211. vty_port = atoi (optarg);
  212. if (vty_port <= 0 || vty_port > 0xffff)
  213. vty_port = RIPNG_VTY_PORT;
  214. break;
  215. case 'r':
  216. retain_mode = 1;
  217. break;
  218. case 'u':
  219. ripngd_privs.user = optarg;
  220. break;
  221. case 'g':
  222. ripngd_privs.group = optarg;
  223. break;
  224. case 'v':
  225. print_version (progname);
  226. exit (0);
  227. break;
  228. case 'C':
  229. dryrun = 1;
  230. break;
  231. case 'h':
  232. usage (progname, 0);
  233. break;
  234. default:
  235. usage (progname, 1);
  236. break;
  237. }
  238. }
  239. master = thread_master_create ();
  240. /* Library inits. */
  241. zprivs_init (&ripngd_privs);
  242. signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
  243. cmd_init (1);
  244. vty_init (master);
  245. memory_init ();
  246. /* RIPngd inits. */
  247. ripng_init ();
  248. zebra_init ();
  249. ripng_peer_init ();
  250. /* Sort all installed commands. */
  251. sort_node ();
  252. /* Get configuration file. */
  253. vty_read_config (config_file, config_default);
  254. /* Start execution only if not in dry-run mode */
  255. if(dryrun)
  256. return(0);
  257. /* Change to the daemon program. */
  258. if (daemon_mode)
  259. daemon (0, 0);
  260. /* Create VTY socket */
  261. vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
  262. /* Process id file create. */
  263. pid_output (pid_file);
  264. /* Print banner. */
  265. zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  266. /* Fetch next active thread. */
  267. while (thread_fetch (master, &thread))
  268. thread_call (&thread);
  269. /* Not reached. */
  270. return 0;
  271. }