rip_main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* RIPd main routine.
  2. * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
  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 Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include <lib/version.h>
  23. #include "getopt.h"
  24. #include "thread.h"
  25. #include "command.h"
  26. #include "memory.h"
  27. #include "prefix.h"
  28. #include "filter.h"
  29. #include "keychain.h"
  30. #include "log.h"
  31. #include "privs.h"
  32. #include "sigevent.h"
  33. #include "ripd/ripd.h"
  34. /* ripd options. */
  35. static struct option longopts[] =
  36. {
  37. { "daemon", no_argument, NULL, 'd'},
  38. { "config_file", required_argument, NULL, 'f'},
  39. { "pid_file", required_argument, NULL, 'i'},
  40. { "help", no_argument, NULL, 'h'},
  41. { "vty_addr", required_argument, NULL, 'A'},
  42. { "vty_port", required_argument, NULL, 'P'},
  43. { "retain", no_argument, NULL, 'r'},
  44. { "user", required_argument, NULL, 'u'},
  45. { "group", required_argument, NULL, 'g'},
  46. { "version", no_argument, NULL, 'v'},
  47. { 0 }
  48. };
  49. /* ripd privileges */
  50. zebra_capabilities_t _caps_p [] =
  51. {
  52. ZCAP_NET_RAW,
  53. ZCAP_BIND
  54. };
  55. struct zebra_privs_t ripd_privs =
  56. {
  57. #if defined(QUAGGA_USER)
  58. .user = QUAGGA_USER,
  59. #endif
  60. #if defined QUAGGA_GROUP
  61. .group = QUAGGA_GROUP,
  62. #endif
  63. #ifdef VTY_GROUP
  64. .vty_group = VTY_GROUP,
  65. #endif
  66. .caps_p = _caps_p,
  67. .cap_num_p = 2,
  68. .cap_num_i = 0
  69. };
  70. /* Configuration file and directory. */
  71. char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
  72. char *config_file = NULL;
  73. /* ripd program name */
  74. /* Route retain mode flag. */
  75. int retain_mode = 0;
  76. /* RIP VTY bind address. */
  77. char *vty_addr = NULL;
  78. /* RIP VTY connection port. */
  79. int vty_port = RIP_VTY_PORT;
  80. /* Master of threads. */
  81. struct thread_master *master;
  82. /* Process ID saved for use by init system */
  83. const char *pid_file = PATH_RIPD_PID;
  84. /* Help information display. */
  85. static void
  86. usage (char *progname, int status)
  87. {
  88. if (status != 0)
  89. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  90. else
  91. {
  92. printf ("Usage : %s [OPTION...]\n\
  93. Daemon which manages RIP version 1 and 2.\n\n\
  94. -d, --daemon Runs in daemon mode\n\
  95. -f, --config_file Set configuration file name\n\
  96. -i, --pid_file Set process identifier file name\n\
  97. -A, --vty_addr Set vty's bind address\n\
  98. -P, --vty_port Set vty's port number\n\
  99. -r, --retain When program terminates, retain added route by ripd.\n\
  100. -u, --user User to run as\n\
  101. -g, --group Group to run as\n\
  102. -v, --version Print program version\n\
  103. -h, --help Display this help and exit\n\
  104. \n\
  105. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  106. }
  107. exit (status);
  108. }
  109. /* SIGHUP handler. */
  110. static void
  111. sighup (void)
  112. {
  113. zlog_info ("SIGHUP received");
  114. rip_clean ();
  115. rip_reset ();
  116. zlog_info ("ripd restarting!");
  117. /* Reload config file. */
  118. vty_read_config (config_file, config_default);
  119. /* Create VTY's socket */
  120. vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
  121. /* Try to return to normal operation. */
  122. }
  123. /* SIGINT handler. */
  124. static void
  125. sigint (void)
  126. {
  127. zlog_notice ("Terminating on signal");
  128. if (! retain_mode)
  129. rip_clean ();
  130. exit (0);
  131. }
  132. /* SIGUSR1 handler. */
  133. static void
  134. sigusr1 (void)
  135. {
  136. zlog_rotate (NULL);
  137. }
  138. static struct quagga_signal_t ripd_signals[] =
  139. {
  140. {
  141. .signal = SIGHUP,
  142. .handler = &sighup,
  143. },
  144. {
  145. .signal = SIGUSR1,
  146. .handler = &sigusr1,
  147. },
  148. {
  149. .signal = SIGINT,
  150. .handler = &sigint,
  151. },
  152. {
  153. .signal = SIGTERM,
  154. .handler = &sigint,
  155. },
  156. };
  157. /* Main routine of ripd. */
  158. int
  159. main (int argc, char **argv)
  160. {
  161. char *p;
  162. int daemon_mode = 0;
  163. char *progname;
  164. struct thread thread;
  165. /* Set umask before anything for security */
  166. umask (0027);
  167. /* Get program name. */
  168. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  169. /* First of all we need logging init. */
  170. zlog_default = openzlog (progname, ZLOG_RIP,
  171. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  172. /* Command line option parse. */
  173. while (1)
  174. {
  175. int opt;
  176. opt = getopt_long (argc, argv, "df:i:hA:P:u:g:rv", longopts, 0);
  177. if (opt == EOF)
  178. break;
  179. switch (opt)
  180. {
  181. case 0:
  182. break;
  183. case 'd':
  184. daemon_mode = 1;
  185. break;
  186. case 'f':
  187. config_file = optarg;
  188. break;
  189. case 'A':
  190. vty_addr = optarg;
  191. break;
  192. case 'i':
  193. pid_file = optarg;
  194. break;
  195. case 'P':
  196. /* Deal with atoi() returning 0 on failure, and ripd not
  197. listening on rip port... */
  198. if (strcmp(optarg, "0") == 0)
  199. {
  200. vty_port = 0;
  201. break;
  202. }
  203. vty_port = atoi (optarg);
  204. vty_port = (vty_port ? vty_port : RIP_VTY_PORT);
  205. break;
  206. case 'r':
  207. retain_mode = 1;
  208. break;
  209. case 'u':
  210. ripd_privs.user = optarg;
  211. break;
  212. case 'g':
  213. ripd_privs.group = optarg;
  214. break;
  215. case 'v':
  216. print_version (progname);
  217. exit (0);
  218. break;
  219. case 'h':
  220. usage (progname, 0);
  221. break;
  222. default:
  223. usage (progname, 1);
  224. break;
  225. }
  226. }
  227. /* Prepare master thread. */
  228. master = thread_master_create ();
  229. /* Library initialization. */
  230. zprivs_init (&ripd_privs);
  231. signal_init (master, Q_SIGC(ripd_signals), ripd_signals);
  232. cmd_init (1);
  233. vty_init (master);
  234. memory_init ();
  235. keychain_init ();
  236. /* RIP related initialization. */
  237. rip_init ();
  238. rip_if_init ();
  239. rip_zclient_init ();
  240. rip_peer_init ();
  241. /* Sort all installed commands. */
  242. sort_node ();
  243. /* Get configuration file. */
  244. vty_read_config (config_file, config_default);
  245. /* Change to the daemon program. */
  246. if (daemon_mode)
  247. daemon (0, 0);
  248. /* Pid file create. */
  249. pid_output (pid_file);
  250. /* Create VTY's socket */
  251. vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
  252. /* Print banner. */
  253. zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  254. /* Execute each thread. */
  255. while (thread_fetch (master, &thread))
  256. thread_call (&thread);
  257. /* Not reached. */
  258. exit (0);
  259. }