rip_main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. { "dryrun", no_argument, NULL, 'C'},
  42. { "vty_addr", required_argument, NULL, 'A'},
  43. { "vty_port", required_argument, NULL, 'P'},
  44. { "retain", no_argument, NULL, 'r'},
  45. { "user", required_argument, NULL, 'u'},
  46. { "group", required_argument, NULL, 'g'},
  47. { "version", no_argument, NULL, 'v'},
  48. { 0 }
  49. };
  50. /* ripd privileges */
  51. zebra_capabilities_t _caps_p [] =
  52. {
  53. ZCAP_NET_RAW,
  54. ZCAP_BIND
  55. };
  56. struct zebra_privs_t ripd_privs =
  57. {
  58. #if defined(QUAGGA_USER)
  59. .user = QUAGGA_USER,
  60. #endif
  61. #if defined QUAGGA_GROUP
  62. .group = QUAGGA_GROUP,
  63. #endif
  64. #ifdef VTY_GROUP
  65. .vty_group = VTY_GROUP,
  66. #endif
  67. .caps_p = _caps_p,
  68. .cap_num_p = 2,
  69. .cap_num_i = 0
  70. };
  71. /* Configuration file and directory. */
  72. char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
  73. char *config_file = NULL;
  74. /* ripd program name */
  75. /* Route retain mode flag. */
  76. int retain_mode = 0;
  77. /* RIP VTY bind address. */
  78. char *vty_addr = NULL;
  79. /* RIP VTY connection port. */
  80. int vty_port = RIP_VTY_PORT;
  81. /* Master of threads. */
  82. struct thread_master *master;
  83. /* Process ID saved for use by init system */
  84. const char *pid_file = PATH_RIPD_PID;
  85. /* Help information display. */
  86. static void
  87. usage (char *progname, int status)
  88. {
  89. if (status != 0)
  90. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  91. else
  92. {
  93. printf ("Usage : %s [OPTION...]\n\
  94. Daemon which manages RIP version 1 and 2.\n\n\
  95. -d, --daemon Runs in daemon mode\n\
  96. -f, --config_file Set configuration file name\n\
  97. -i, --pid_file Set process identifier file name\n\
  98. -A, --vty_addr Set vty's bind address\n\
  99. -P, --vty_port Set vty's port number\n\
  100. -C, --dryrun Check configuration for validity and exit\n\
  101. -r, --retain When program terminates, retain added route by ripd.\n\
  102. -u, --user User to run as\n\
  103. -g, --group Group to run as\n\
  104. -v, --version Print program version\n\
  105. -h, --help Display this help and exit\n\
  106. \n\
  107. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  108. }
  109. exit (status);
  110. }
  111. /* SIGHUP handler. */
  112. static void
  113. sighup (void)
  114. {
  115. zlog_info ("SIGHUP received");
  116. rip_clean ();
  117. rip_reset ();
  118. zlog_info ("ripd restarting!");
  119. /* Reload config file. */
  120. vty_read_config (config_file, config_default);
  121. /* Create VTY's socket */
  122. vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
  123. /* Try to return to normal operation. */
  124. }
  125. /* SIGINT handler. */
  126. static void
  127. sigint (void)
  128. {
  129. zlog_notice ("Terminating on signal");
  130. if (! retain_mode)
  131. rip_clean ();
  132. exit (0);
  133. }
  134. /* SIGUSR1 handler. */
  135. static void
  136. sigusr1 (void)
  137. {
  138. zlog_rotate (NULL);
  139. }
  140. static struct quagga_signal_t ripd_signals[] =
  141. {
  142. {
  143. .signal = SIGHUP,
  144. .handler = &sighup,
  145. },
  146. {
  147. .signal = SIGUSR1,
  148. .handler = &sigusr1,
  149. },
  150. {
  151. .signal = SIGINT,
  152. .handler = &sigint,
  153. },
  154. {
  155. .signal = SIGTERM,
  156. .handler = &sigint,
  157. },
  158. };
  159. /* Main routine of ripd. */
  160. int
  161. main (int argc, char **argv)
  162. {
  163. char *p;
  164. int daemon_mode = 0;
  165. int dryrun = 0;
  166. char *progname;
  167. struct thread thread;
  168. /* Set umask before anything for security */
  169. umask (0027);
  170. /* Get program name. */
  171. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  172. /* First of all we need logging init. */
  173. zlog_default = openzlog (progname, ZLOG_RIP,
  174. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  175. /* Command line option parse. */
  176. while (1)
  177. {
  178. int opt;
  179. opt = getopt_long (argc, argv, "df:i:hA:P:u:g:rvC", 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 ripd not
  200. listening on rip 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 = RIP_VTY_PORT;
  209. break;
  210. case 'r':
  211. retain_mode = 1;
  212. break;
  213. case 'C':
  214. dryrun = 1;
  215. break;
  216. case 'u':
  217. ripd_privs.user = optarg;
  218. break;
  219. case 'g':
  220. ripd_privs.group = optarg;
  221. break;
  222. case 'v':
  223. print_version (progname);
  224. exit (0);
  225. break;
  226. case 'h':
  227. usage (progname, 0);
  228. break;
  229. default:
  230. usage (progname, 1);
  231. break;
  232. }
  233. }
  234. /* Prepare master thread. */
  235. master = thread_master_create ();
  236. /* Library initialization. */
  237. zprivs_init (&ripd_privs);
  238. signal_init (master, Q_SIGC(ripd_signals), ripd_signals);
  239. cmd_init (1);
  240. vty_init (master);
  241. memory_init ();
  242. keychain_init ();
  243. /* RIP related initialization. */
  244. rip_init ();
  245. rip_if_init ();
  246. rip_zclient_init ();
  247. rip_peer_init ();
  248. /* Sort all installed commands. */
  249. sort_node ();
  250. /* Get configuration file. */
  251. vty_read_config (config_file, config_default);
  252. /* Start execution only if not in dry-run mode */
  253. if(dryrun)
  254. return (0);
  255. /* Change to the daemon program. */
  256. if (daemon_mode && daemon (0, 0) < 0)
  257. {
  258. zlog_err("RIPd daemon failed: %s", strerror(errno));
  259. exit (1);
  260. }
  261. /* Pid file create. */
  262. pid_output (pid_file);
  263. /* Create VTY's socket */
  264. vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
  265. /* Print banner. */
  266. zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  267. /* Execute each thread. */
  268. while (thread_fetch (master, &thread))
  269. thread_call (&thread);
  270. /* Not reached. */
  271. return (0);
  272. }