ospf_main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * OSPFd main routine.
  3. * Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada
  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 "thread.h"
  26. #include "prefix.h"
  27. #include "linklist.h"
  28. #include "if.h"
  29. #include "vector.h"
  30. #include "vty.h"
  31. #include "command.h"
  32. #include "filter.h"
  33. #include "plist.h"
  34. #include "stream.h"
  35. #include "log.h"
  36. #include "memory.h"
  37. #include "privs.h"
  38. #include "sigevent.h"
  39. #include "zclient.h"
  40. #include "vrf.h"
  41. #include "ospfd/ospfd.h"
  42. #include "ospfd/ospf_interface.h"
  43. #include "ospfd/ospf_asbr.h"
  44. #include "ospfd/ospf_lsa.h"
  45. #include "ospfd/ospf_lsdb.h"
  46. #include "ospfd/ospf_neighbor.h"
  47. #include "ospfd/ospf_dump.h"
  48. #include "ospfd/ospf_zebra.h"
  49. #include "ospfd/ospf_vty.h"
  50. /* ospfd privileges */
  51. zebra_capabilities_t _caps_p [] =
  52. {
  53. ZCAP_NET_RAW,
  54. ZCAP_BIND,
  55. ZCAP_NET_ADMIN,
  56. };
  57. struct zebra_privs_t ospfd_privs =
  58. {
  59. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  60. .user = QUAGGA_USER,
  61. .group = QUAGGA_GROUP,
  62. #endif
  63. #if defined(VTY_GROUP)
  64. .vty_group = VTY_GROUP,
  65. #endif
  66. .caps_p = _caps_p,
  67. .cap_num_p = array_size(_caps_p),
  68. .cap_num_i = 0
  69. };
  70. /* Configuration filename and directory. */
  71. char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
  72. /* OSPFd options. */
  73. struct option longopts[] =
  74. {
  75. { "daemon", no_argument, NULL, 'd'},
  76. { "config_file", required_argument, NULL, 'f'},
  77. { "pid_file", required_argument, NULL, 'i'},
  78. { "socket", required_argument, NULL, 'z'},
  79. { "dryrun", no_argument, NULL, 'C'},
  80. { "help", no_argument, NULL, 'h'},
  81. { "vty_addr", required_argument, NULL, 'A'},
  82. { "vty_port", required_argument, NULL, 'P'},
  83. { "user", required_argument, NULL, 'u'},
  84. { "group", required_argument, NULL, 'g'},
  85. { "apiserver", no_argument, NULL, 'a'},
  86. { "version", no_argument, NULL, 'v'},
  87. { 0 }
  88. };
  89. /* OSPFd program name */
  90. /* Master of threads. */
  91. struct thread_master *master;
  92. /* Process ID saved for use by init system */
  93. const char *pid_file = PATH_OSPFD_PID;
  94. #ifdef SUPPORT_OSPF_API
  95. extern int ospf_apiserver_enable;
  96. #endif /* SUPPORT_OSPF_API */
  97. /* Help information display. */
  98. static void __attribute__ ((noreturn))
  99. usage (char *progname, int status)
  100. {
  101. if (status != 0)
  102. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  103. else
  104. {
  105. printf ("Usage : %s [OPTION...]\n\
  106. Daemon which manages OSPF.\n\n\
  107. -d, --daemon Runs in daemon mode\n\
  108. -f, --config_file Set configuration file name\n\
  109. -i, --pid_file Set process identifier file name\n\
  110. -z, --socket Set path of zebra socket\n\
  111. -A, --vty_addr Set vty's bind address\n\
  112. -P, --vty_port Set vty's port number\n\
  113. -u, --user User to run as\n\
  114. -g, --group Group to run as\n\
  115. -a. --apiserver Enable OSPF apiserver\n\
  116. -v, --version Print program version\n\
  117. -C, --dryrun Check configuration for validity and exit\n\
  118. -h, --help Display this help and exit\n\
  119. \n\
  120. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  121. }
  122. exit (status);
  123. }
  124. /* SIGHUP handler. */
  125. static void
  126. sighup (void)
  127. {
  128. zlog (NULL, LOG_INFO, "SIGHUP received");
  129. }
  130. /* SIGINT / SIGTERM handler. */
  131. static void
  132. sigint (void)
  133. {
  134. zlog_notice ("Terminating on signal");
  135. ospf_terminate ();
  136. }
  137. /* SIGUSR1 handler. */
  138. static void
  139. sigusr1 (void)
  140. {
  141. zlog_rotate (NULL);
  142. }
  143. struct quagga_signal_t ospf_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. /* OSPFd main routine. */
  163. int
  164. main (int argc, char **argv)
  165. {
  166. char *p;
  167. char *vty_addr = NULL;
  168. int vty_port = OSPF_VTY_PORT;
  169. int daemon_mode = 0;
  170. char *config_file = NULL;
  171. char *progname;
  172. int dryrun = 0;
  173. /* Set umask before anything for security */
  174. umask (0027);
  175. #ifdef SUPPORT_OSPF_API
  176. /* OSPF apiserver is disabled by default. */
  177. ospf_apiserver_enable = 0;
  178. #endif /* SUPPORT_OSPF_API */
  179. /* get program name */
  180. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  181. while (1)
  182. {
  183. int opt;
  184. opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:avC", longopts, 0);
  185. if (opt == EOF)
  186. break;
  187. switch (opt)
  188. {
  189. case 0:
  190. break;
  191. case 'd':
  192. daemon_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 'z':
  204. zclient_serv_path_set (optarg);
  205. break;
  206. case 'P':
  207. /* Deal with atoi() returning 0 on failure, and ospfd not
  208. listening on ospfd port... */
  209. if (strcmp(optarg, "0") == 0)
  210. {
  211. vty_port = 0;
  212. break;
  213. }
  214. vty_port = atoi (optarg);
  215. if (vty_port <= 0 || vty_port > 0xffff)
  216. vty_port = OSPF_VTY_PORT;
  217. break;
  218. case 'u':
  219. ospfd_privs.user = optarg;
  220. break;
  221. case 'g':
  222. ospfd_privs.group = optarg;
  223. break;
  224. #ifdef SUPPORT_OSPF_API
  225. case 'a':
  226. ospf_apiserver_enable = 1;
  227. break;
  228. #endif /* SUPPORT_OSPF_API */
  229. case 'v':
  230. print_version (progname);
  231. exit (0);
  232. break;
  233. case 'C':
  234. dryrun = 1;
  235. break;
  236. case 'h':
  237. usage (progname, 0);
  238. break;
  239. default:
  240. usage (progname, 1);
  241. break;
  242. }
  243. }
  244. /* Invoked by a priviledged user? -- endo. */
  245. if (geteuid () != 0)
  246. {
  247. errno = EPERM;
  248. perror (progname);
  249. exit (1);
  250. }
  251. zlog_default = openzlog (progname, ZLOG_OSPF,
  252. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  253. /* OSPF master init. */
  254. ospf_master_init ();
  255. /* Initializations. */
  256. master = om->master;
  257. /* Library inits. */
  258. zprivs_init (&ospfd_privs);
  259. signal_init (master, array_size(ospf_signals), ospf_signals);
  260. cmd_init (1);
  261. debug_init ();
  262. vty_init (master);
  263. memory_init ();
  264. vrf_init ();
  265. access_list_init ();
  266. prefix_list_init ();
  267. /* OSPFd inits. */
  268. ospf_if_init ();
  269. ospf_zebra_init (master);
  270. /* OSPF vty inits. */
  271. ospf_vty_init ();
  272. ospf_vty_show_init ();
  273. ospf_vty_clear_init ();
  274. ospf_route_map_init ();
  275. #ifdef HAVE_SNMP
  276. ospf_snmp_init ();
  277. #endif /* HAVE_SNMP */
  278. ospf_opaque_init ();
  279. /* Get configuration file. */
  280. vty_read_config (config_file, config_default);
  281. /* Start execution only if not in dry-run mode */
  282. if (dryrun)
  283. return(0);
  284. /* Change to the daemon program. */
  285. if (daemon_mode && daemon (0, 0) < 0)
  286. {
  287. zlog_err("OSPFd daemon failed: %s", strerror(errno));
  288. exit (1);
  289. }
  290. /* Process id file create. */
  291. pid_output (pid_file);
  292. /* Create VTY socket */
  293. vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
  294. /* Print banner. */
  295. zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  296. thread_main (master);
  297. /* Not reached. */
  298. return (0);
  299. }