ospf_main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "ospfd/ospfd.h"
  41. #include "ospfd/ospf_interface.h"
  42. #include "ospfd/ospf_asbr.h"
  43. #include "ospfd/ospf_lsa.h"
  44. #include "ospfd/ospf_lsdb.h"
  45. #include "ospfd/ospf_neighbor.h"
  46. #include "ospfd/ospf_dump.h"
  47. #include "ospfd/ospf_zebra.h"
  48. #include "ospfd/ospf_vty.h"
  49. /* ospfd privileges */
  50. zebra_capabilities_t _caps_p [] =
  51. {
  52. ZCAP_NET_RAW,
  53. ZCAP_BIND,
  54. ZCAP_NET_ADMIN,
  55. };
  56. struct zebra_privs_t ospfd_privs =
  57. {
  58. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  59. .user = QUAGGA_USER,
  60. .group = QUAGGA_GROUP,
  61. #endif
  62. #if defined(VTY_GROUP)
  63. .vty_group = VTY_GROUP,
  64. #endif
  65. .caps_p = _caps_p,
  66. .cap_num_p = array_size(_caps_p),
  67. .cap_num_i = 0
  68. };
  69. /* Configuration filename and directory. */
  70. char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
  71. /* OSPFd options. */
  72. struct option longopts[] =
  73. {
  74. { "daemon", no_argument, NULL, 'd'},
  75. { "config_file", required_argument, NULL, 'f'},
  76. { "pid_file", required_argument, NULL, 'i'},
  77. { "socket", required_argument, NULL, 'z'},
  78. { "dryrun", no_argument, NULL, 'C'},
  79. { "help", no_argument, NULL, 'h'},
  80. { "vty_addr", required_argument, NULL, 'A'},
  81. { "vty_port", required_argument, NULL, 'P'},
  82. { "user", required_argument, NULL, 'u'},
  83. { "group", required_argument, NULL, 'g'},
  84. { "apiserver", no_argument, NULL, 'a'},
  85. { "version", no_argument, NULL, 'v'},
  86. { 0 }
  87. };
  88. /* OSPFd program name */
  89. /* Master of threads. */
  90. struct thread_master *master;
  91. /* Process ID saved for use by init system */
  92. const char *pid_file = PATH_OSPFD_PID;
  93. #ifdef SUPPORT_OSPF_API
  94. extern int ospf_apiserver_enable;
  95. #endif /* SUPPORT_OSPF_API */
  96. /* Help information display. */
  97. static void __attribute__ ((noreturn))
  98. usage (char *progname, int status)
  99. {
  100. if (status != 0)
  101. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  102. else
  103. {
  104. printf ("Usage : %s [OPTION...]\n\
  105. Daemon which manages OSPF.\n\n\
  106. -d, --daemon Runs in daemon mode\n\
  107. -f, --config_file Set configuration file name\n\
  108. -i, --pid_file Set process identifier file name\n\
  109. -z, --socket Set path of zebra socket\n\
  110. -A, --vty_addr Set vty's bind address\n\
  111. -P, --vty_port Set vty's port number\n\
  112. -u, --user User to run as\n\
  113. -g, --group Group to run as\n\
  114. -a. --apiserver Enable OSPF apiserver\n\
  115. -v, --version Print program version\n\
  116. -C, --dryrun Check configuration for validity and exit\n\
  117. -h, --help Display this help and exit\n\
  118. \n\
  119. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  120. }
  121. exit (status);
  122. }
  123. /* SIGHUP handler. */
  124. static void
  125. sighup (void)
  126. {
  127. zlog (NULL, LOG_INFO, "SIGHUP received");
  128. }
  129. /* SIGINT / SIGTERM handler. */
  130. static void
  131. sigint (void)
  132. {
  133. zlog_notice ("Terminating on signal");
  134. ospf_terminate ();
  135. }
  136. /* SIGUSR1 handler. */
  137. static void
  138. sigusr1 (void)
  139. {
  140. zlog_rotate (NULL);
  141. }
  142. struct quagga_signal_t ospf_signals[] =
  143. {
  144. {
  145. .signal = SIGHUP,
  146. .handler = &sighup,
  147. },
  148. {
  149. .signal = SIGUSR1,
  150. .handler = &sigusr1,
  151. },
  152. {
  153. .signal = SIGINT,
  154. .handler = &sigint,
  155. },
  156. {
  157. .signal = SIGTERM,
  158. .handler = &sigint,
  159. },
  160. };
  161. /* OSPFd main routine. */
  162. int
  163. main (int argc, char **argv)
  164. {
  165. char *p;
  166. char *vty_addr = NULL;
  167. int vty_port = OSPF_VTY_PORT;
  168. int daemon_mode = 0;
  169. char *config_file = NULL;
  170. char *progname;
  171. struct thread thread;
  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. access_list_init ();
  265. prefix_list_init ();
  266. /* OSPFd inits. */
  267. ospf_if_init ();
  268. ospf_zebra_init ();
  269. /* OSPF vty inits. */
  270. ospf_vty_init ();
  271. ospf_vty_show_init ();
  272. ospf_route_map_init ();
  273. #ifdef HAVE_SNMP
  274. ospf_snmp_init ();
  275. #endif /* HAVE_SNMP */
  276. #ifdef HAVE_OPAQUE_LSA
  277. ospf_opaque_init ();
  278. #endif /* HAVE_OPAQUE_LSA */
  279. /* Need to initialize the default ospf structure, so the interface mode
  280. commands can be duly processed if they are received before 'router ospf',
  281. when quagga(ospfd) is restarted */
  282. if (!ospf_get())
  283. {
  284. zlog_err("OSPF instance init failed: %s", strerror(errno));
  285. exit (1);
  286. }
  287. /* Get configuration file. */
  288. vty_read_config (config_file, config_default);
  289. /* Start execution only if not in dry-run mode */
  290. if (dryrun)
  291. return(0);
  292. /* Change to the daemon program. */
  293. if (daemon_mode && daemon (0, 0) < 0)
  294. {
  295. zlog_err("OSPFd daemon failed: %s", strerror(errno));
  296. exit (1);
  297. }
  298. /* Process id file create. */
  299. pid_output (pid_file);
  300. /* Create VTY socket */
  301. vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
  302. /* Print banner. */
  303. zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  304. /* Fetch next active thread. */
  305. while (thread_fetch (master, &thread))
  306. thread_call (&thread);
  307. /* Not reached. */
  308. return (0);
  309. }