rip_main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "zclient.h"
  34. #include "vrf.h"
  35. #include "ripd/ripd.h"
  36. /* ripd options. */
  37. static struct option longopts[] =
  38. {
  39. { "daemon", no_argument, NULL, 'd'},
  40. { "config_file", required_argument, NULL, 'f'},
  41. { "pid_file", required_argument, NULL, 'i'},
  42. { "socket", required_argument, NULL, 'z'},
  43. { "help", no_argument, NULL, 'h'},
  44. { "dryrun", no_argument, NULL, 'C'},
  45. { "vty_addr", required_argument, NULL, 'A'},
  46. { "vty_port", required_argument, NULL, 'P'},
  47. { "retain", no_argument, NULL, 'r'},
  48. { "user", required_argument, NULL, 'u'},
  49. { "group", required_argument, NULL, 'g'},
  50. { "version", no_argument, NULL, 'v'},
  51. { 0 }
  52. };
  53. /* ripd privileges */
  54. zebra_capabilities_t _caps_p [] =
  55. {
  56. ZCAP_NET_RAW,
  57. ZCAP_BIND
  58. };
  59. struct zebra_privs_t ripd_privs =
  60. {
  61. #if defined(QUAGGA_USER)
  62. .user = QUAGGA_USER,
  63. #endif
  64. #if defined QUAGGA_GROUP
  65. .group = QUAGGA_GROUP,
  66. #endif
  67. #ifdef VTY_GROUP
  68. .vty_group = VTY_GROUP,
  69. #endif
  70. .caps_p = _caps_p,
  71. .cap_num_p = 2,
  72. .cap_num_i = 0
  73. };
  74. /* Configuration file and directory. */
  75. char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
  76. char *config_file = NULL;
  77. /* ripd program name */
  78. /* Route retain mode flag. */
  79. int retain_mode = 0;
  80. /* RIP VTY bind address. */
  81. char *vty_addr = NULL;
  82. /* RIP VTY connection port. */
  83. int vty_port = RIP_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_RIPD_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 RIP version 1 and 2.\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. -z, --socket Set path of zebra socket\n\
  102. -A, --vty_addr Set vty's bind address\n\
  103. -P, --vty_port Set vty's port number\n\
  104. -C, --dryrun Check configuration for validity and exit\n\
  105. -r, --retain When program terminates, retain added route by ripd.\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. -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. rip_clean ();
  121. rip_reset ();
  122. zlog_info ("ripd restarting!");
  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, RIP_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. rip_clean ();
  136. exit (0);
  137. }
  138. /* SIGUSR1 handler. */
  139. static void
  140. sigusr1 (void)
  141. {
  142. zlog_rotate (NULL);
  143. }
  144. static struct quagga_signal_t ripd_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. /* Main routine of ripd. */
  164. int
  165. main (int argc, char **argv)
  166. {
  167. char *p;
  168. int daemon_mode = 0;
  169. int dryrun = 0;
  170. char *progname;
  171. /* Set umask before anything for security */
  172. umask (0027);
  173. /* Get program name. */
  174. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  175. /* First of all we need logging init. */
  176. zlog_default = openzlog (progname, ZLOG_RIP,
  177. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  178. /* Command line option parse. */
  179. while (1)
  180. {
  181. int opt;
  182. opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:rvC", longopts, 0);
  183. if (opt == EOF)
  184. break;
  185. switch (opt)
  186. {
  187. case 0:
  188. break;
  189. case 'd':
  190. daemon_mode = 1;
  191. break;
  192. case 'f':
  193. config_file = optarg;
  194. break;
  195. case 'A':
  196. vty_addr = optarg;
  197. break;
  198. case 'i':
  199. pid_file = optarg;
  200. break;
  201. case 'z':
  202. zclient_serv_path_set (optarg);
  203. break;
  204. case 'P':
  205. /* Deal with atoi() returning 0 on failure, and ripd not
  206. listening on rip port... */
  207. if (strcmp(optarg, "0") == 0)
  208. {
  209. vty_port = 0;
  210. break;
  211. }
  212. vty_port = atoi (optarg);
  213. if (vty_port <= 0 || vty_port > 0xffff)
  214. vty_port = RIP_VTY_PORT;
  215. break;
  216. case 'r':
  217. retain_mode = 1;
  218. break;
  219. case 'C':
  220. dryrun = 1;
  221. break;
  222. case 'u':
  223. ripd_privs.user = optarg;
  224. break;
  225. case 'g':
  226. ripd_privs.group = optarg;
  227. break;
  228. case 'v':
  229. print_version (progname);
  230. exit (0);
  231. break;
  232. case 'h':
  233. usage (progname, 0);
  234. break;
  235. default:
  236. usage (progname, 1);
  237. break;
  238. }
  239. }
  240. /* Prepare master thread. */
  241. master = thread_master_create ();
  242. /* Library initialization. */
  243. zprivs_init (&ripd_privs);
  244. signal_init (master, array_size(ripd_signals), ripd_signals);
  245. cmd_init (1);
  246. vty_init (master);
  247. memory_init ();
  248. keychain_init ();
  249. vrf_init ();
  250. /* RIP related initialization. */
  251. rip_init ();
  252. rip_if_init ();
  253. rip_zclient_init (master);
  254. rip_peer_init ();
  255. /* Get configuration file. */
  256. vty_read_config (config_file, config_default);
  257. /* Start execution only if not in dry-run mode */
  258. if(dryrun)
  259. return (0);
  260. /* Change to the daemon program. */
  261. if (daemon_mode && daemon (0, 0) < 0)
  262. {
  263. zlog_err("RIPd daemon failed: %s", strerror(errno));
  264. exit (1);
  265. }
  266. /* Pid file create. */
  267. pid_output (pid_file);
  268. /* Create VTY's socket */
  269. vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
  270. /* Print banner. */
  271. zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  272. /* Execute each thread. */
  273. thread_main (master);
  274. /* Not reached. */
  275. return (0);
  276. }