ripng_main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 "vrf.h"
  36. #include "ripngd/ripngd.h"
  37. /* Configuration filename and directory. */
  38. char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG;
  39. char *config_file = NULL;
  40. /* RIPngd options. */
  41. struct option longopts[] =
  42. {
  43. { "daemon", no_argument, NULL, 'd'},
  44. { "config_file", required_argument, NULL, 'f'},
  45. { "pid_file", required_argument, NULL, 'i'},
  46. { "socket", required_argument, NULL, 'z'},
  47. { "dryrun", no_argument, NULL, 'C'},
  48. { "help", no_argument, NULL, 'h'},
  49. { "vty_addr", required_argument, NULL, 'A'},
  50. { "vty_port", required_argument, NULL, 'P'},
  51. { "retain", no_argument, NULL, 'r'},
  52. { "user", required_argument, NULL, 'u'},
  53. { "group", required_argument, NULL, 'g'},
  54. { "version", no_argument, NULL, 'v'},
  55. { 0 }
  56. };
  57. /* ripngd privileges */
  58. zebra_capabilities_t _caps_p [] =
  59. {
  60. ZCAP_NET_RAW,
  61. ZCAP_BIND
  62. };
  63. struct zebra_privs_t ripngd_privs =
  64. {
  65. #if defined(QUAGGA_USER)
  66. .user = QUAGGA_USER,
  67. #endif
  68. #if defined QUAGGA_GROUP
  69. .group = QUAGGA_GROUP,
  70. #endif
  71. #ifdef VTY_GROUP
  72. .vty_group = VTY_GROUP,
  73. #endif
  74. .caps_p = _caps_p,
  75. .cap_num_p = 2,
  76. .cap_num_i = 0
  77. };
  78. /* RIPngd program name */
  79. /* Route retain mode flag. */
  80. int retain_mode = 0;
  81. /* RIPng VTY bind address. */
  82. char *vty_addr = NULL;
  83. /* RIPng VTY connection port. */
  84. int vty_port = RIPNG_VTY_PORT;
  85. /* Master of threads. */
  86. struct thread_master *master;
  87. /* Process ID saved for use by init system */
  88. const char *pid_file = PATH_RIPNGD_PID;
  89. /* Help information display. */
  90. static void
  91. usage (char *progname, int status)
  92. {
  93. if (status != 0)
  94. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  95. else
  96. {
  97. printf ("Usage : %s [OPTION...]\n\
  98. Daemon which manages RIPng.\n\n\
  99. -d, --daemon Runs in daemon mode\n\
  100. -f, --config_file Set configuration file name\n\
  101. -i, --pid_file Set process identifier file name\n\
  102. -z, --socket Set path of zebra socket\n\
  103. -A, --vty_addr Set vty's bind address\n\
  104. -P, --vty_port Set vty's port number\n\
  105. -r, --retain When program terminates, retain added route by ripngd.\n\
  106. -u, --user User to run as\n\
  107. -g, --group Group to run as\n\
  108. -v, --version Print program version\n\
  109. -C, --dryrun Check configuration for validity and exit\n\
  110. -h, --help Display this help and exit\n\
  111. \n\
  112. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  113. }
  114. exit (status);
  115. }
  116. /* SIGHUP handler. */
  117. static void
  118. sighup (void)
  119. {
  120. zlog_info ("SIGHUP received");
  121. ripng_clean ();
  122. ripng_reset ();
  123. /* Reload config file. */
  124. vty_read_config (config_file, config_default);
  125. /* Create VTY's socket */
  126. vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
  127. /* Try to return to normal operation. */
  128. }
  129. /* SIGINT handler. */
  130. static void
  131. sigint (void)
  132. {
  133. zlog_notice ("Terminating on signal");
  134. if (! retain_mode)
  135. ripng_clean ();
  136. exit (0);
  137. }
  138. /* SIGUSR1 handler. */
  139. static void
  140. sigusr1 (void)
  141. {
  142. zlog_rotate (NULL);
  143. }
  144. struct quagga_signal_t ripng_signals[] =
  145. {
  146. {
  147. .signal = SIGHUP,
  148. .handler = &sighup,
  149. },
  150. {
  151. .signal = SIGUSR1,
  152. .handler = &sigusr1,
  153. },
  154. {
  155. .signal = SIGINT,
  156. .handler = &sigint,
  157. },
  158. {
  159. .signal = SIGTERM,
  160. .handler = &sigint,
  161. },
  162. };
  163. /* RIPngd main routine. */
  164. int
  165. main (int argc, char **argv)
  166. {
  167. char *p;
  168. int vty_port = RIPNG_VTY_PORT;
  169. int daemon_mode = 0;
  170. char *progname;
  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, "df:i:z: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 'f':
  192. config_file = optarg;
  193. break;
  194. case 'A':
  195. vty_addr = optarg;
  196. break;
  197. case 'i':
  198. pid_file = optarg;
  199. break;
  200. case 'z':
  201. zclient_serv_path_set (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, array_size(ripng_signals), ripng_signals);
  243. cmd_init (1);
  244. vty_init (master);
  245. memory_init ();
  246. vrf_init ();
  247. /* RIPngd inits. */
  248. ripng_init ();
  249. zebra_init (master);
  250. ripng_peer_init ();
  251. /* Get configuration file. */
  252. vty_read_config (config_file, config_default);
  253. /* Start execution only if not in dry-run mode */
  254. if(dryrun)
  255. return(0);
  256. /* Change to the daemon program. */
  257. if (daemon_mode && daemon (0, 0) < 0)
  258. {
  259. zlog_err("RIPNGd daemon failed: %s", strerror(errno));
  260. exit (1);
  261. }
  262. /* Create VTY socket */
  263. vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
  264. /* Process id file create. */
  265. pid_output (pid_file);
  266. /* Print banner. */
  267. zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  268. /* Fetch next active thread. */
  269. thread_main (master);
  270. /* Not reached. */
  271. return 0;
  272. }