isis_main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * IS-IS Rout(e)ing protocol - isis_main.c
  3. *
  4. * Copyright (C) 2001,2002 Sampo Saaristo
  5. * Tampere University of Technology
  6. * Institute of Communications Engineering
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public Licenseas published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "getopt.h"
  23. #include "thread.h"
  24. #include "log.h"
  25. #include <lib/version.h>
  26. #include "command.h"
  27. #include "vty.h"
  28. #include "memory.h"
  29. #include "stream.h"
  30. #include "if.h"
  31. #include "privs.h"
  32. #include "sigevent.h"
  33. #include "filter.h"
  34. #include "plist.h"
  35. #include "zclient.h"
  36. #include "vrf.h"
  37. #include "isisd/dict.h"
  38. #include "include-netbsd/iso.h"
  39. #include "isisd/isis_constants.h"
  40. #include "isisd/isis_common.h"
  41. #include "isisd/isis_flags.h"
  42. #include "isisd/isis_circuit.h"
  43. #include "isisd/isisd.h"
  44. #include "isisd/isis_dynhn.h"
  45. #include "isisd/isis_spf.h"
  46. #include "isisd/isis_route.h"
  47. #include "isisd/isis_routemap.h"
  48. #include "isisd/isis_zebra.h"
  49. #include "isisd/isis_tlv.h"
  50. #include "isisd/isis_te.h"
  51. /* Default configuration file name */
  52. #define ISISD_DEFAULT_CONFIG "isisd.conf"
  53. /* Default vty port */
  54. #define ISISD_VTY_PORT 2608
  55. /* isisd privileges */
  56. zebra_capabilities_t _caps_p[] = {
  57. ZCAP_NET_RAW,
  58. ZCAP_BIND
  59. };
  60. struct zebra_privs_t isisd_privs = {
  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 = sizeof (_caps_p) / sizeof (*_caps_p),
  72. .cap_num_i = 0
  73. };
  74. /* isisd options */
  75. struct option longopts[] = {
  76. {"daemon", no_argument, NULL, 'd'},
  77. {"config_file", required_argument, NULL, 'f'},
  78. {"pid_file", required_argument, NULL, 'i'},
  79. {"socket", required_argument, NULL, 'z'},
  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. {"version", no_argument, NULL, 'v'},
  85. {"dryrun", no_argument, NULL, 'C'},
  86. {"help", no_argument, NULL, 'h'},
  87. {0}
  88. };
  89. /* Configuration file and directory. */
  90. char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
  91. char *config_file = NULL;
  92. /* isisd program name. */
  93. char *progname;
  94. int daemon_mode = 0;
  95. /* Master of threads. */
  96. struct thread_master *master;
  97. /* Process ID saved for use by init system */
  98. const char *pid_file = PATH_ISISD_PID;
  99. /* for reload */
  100. char _cwd[MAXPATHLEN];
  101. char _progpath[MAXPATHLEN];
  102. int _argc;
  103. char **_argv;
  104. char **_envp;
  105. /*
  106. * Prototypes.
  107. */
  108. void reload(void);
  109. void sighup(void);
  110. void sigint(void);
  111. void sigterm(void);
  112. void sigusr1(void);
  113. /* Help information display. */
  114. static void
  115. usage (int status)
  116. {
  117. if (status != 0)
  118. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  119. else
  120. {
  121. printf ("Usage : %s [OPTION...]\n\n\
  122. Daemon which manages IS-IS routing\n\n\
  123. -d, --daemon Runs in daemon mode\n\
  124. -f, --config_file Set configuration file name\n\
  125. -i, --pid_file Set process identifier file name\n\
  126. -z, --socket Set path of zebra socket\n\
  127. -A, --vty_addr Set vty's bind address\n\
  128. -P, --vty_port Set vty's port number\n\
  129. -u, --user User to run as\n\
  130. -g, --group Group to run as\n\
  131. -v, --version Print program version\n\
  132. -C, --dryrun Check configuration for validity and exit\n\
  133. -h, --help Display this help and exit\n\
  134. \n\
  135. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  136. }
  137. exit (status);
  138. }
  139. void
  140. reload ()
  141. {
  142. zlog_debug ("Reload");
  143. /* FIXME: Clean up func call here */
  144. vty_reset ();
  145. (void) isisd_privs.change (ZPRIVS_RAISE);
  146. execve (_progpath, _argv, _envp);
  147. zlog_err ("Reload failed: cannot exec %s: %s", _progpath,
  148. safe_strerror (errno));
  149. }
  150. static void
  151. terminate (int i)
  152. {
  153. exit (i);
  154. }
  155. /*
  156. * Signal handlers
  157. */
  158. void
  159. sighup (void)
  160. {
  161. zlog_debug ("SIGHUP received");
  162. reload ();
  163. return;
  164. }
  165. void
  166. sigint (void)
  167. {
  168. zlog_notice ("Terminating on signal SIGINT");
  169. terminate (0);
  170. }
  171. void
  172. sigterm (void)
  173. {
  174. zlog_notice ("Terminating on signal SIGTERM");
  175. terminate (0);
  176. }
  177. void
  178. sigusr1 (void)
  179. {
  180. zlog_debug ("SIGUSR1 received");
  181. zlog_rotate (NULL);
  182. }
  183. struct quagga_signal_t isisd_signals[] =
  184. {
  185. {
  186. .signal = SIGHUP,
  187. .handler = &sighup,
  188. },
  189. {
  190. .signal = SIGUSR1,
  191. .handler = &sigusr1,
  192. },
  193. {
  194. .signal = SIGINT,
  195. .handler = &sigint,
  196. },
  197. {
  198. .signal = SIGTERM,
  199. .handler = &sigterm,
  200. },
  201. };
  202. /*
  203. * Main routine of isisd. Parse arguments and handle IS-IS state machine.
  204. */
  205. int
  206. main (int argc, char **argv, char **envp)
  207. {
  208. char *p;
  209. int opt, vty_port = ISISD_VTY_PORT;
  210. char *config_file = NULL;
  211. char *vty_addr = NULL;
  212. int dryrun = 0;
  213. /* Get the programname without the preceding path. */
  214. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  215. zlog_default = openzlog (progname, ZLOG_ISIS,
  216. LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
  217. /* for reload */
  218. _argc = argc;
  219. _argv = argv;
  220. _envp = envp;
  221. getcwd (_cwd, sizeof (_cwd));
  222. if (*argv[0] == '.')
  223. snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
  224. else
  225. snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
  226. /* Command line argument treatment. */
  227. while (1)
  228. {
  229. opt = getopt_long (argc, argv, "df:i:z:hA:p:P:u:g:vC", longopts, 0);
  230. if (opt == EOF)
  231. break;
  232. switch (opt)
  233. {
  234. case 0:
  235. break;
  236. case 'd':
  237. daemon_mode = 1;
  238. break;
  239. case 'f':
  240. config_file = optarg;
  241. break;
  242. case 'i':
  243. pid_file = optarg;
  244. break;
  245. case 'z':
  246. zclient_serv_path_set (optarg);
  247. break;
  248. case 'A':
  249. vty_addr = optarg;
  250. break;
  251. case 'P':
  252. /* Deal with atoi() returning 0 on failure, and isisd not
  253. listening on isisd port... */
  254. if (strcmp (optarg, "0") == 0)
  255. {
  256. vty_port = 0;
  257. break;
  258. }
  259. vty_port = atoi (optarg);
  260. vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
  261. break;
  262. case 'u':
  263. isisd_privs.user = optarg;
  264. break;
  265. case 'g':
  266. isisd_privs.group = optarg;
  267. break;
  268. case 'v':
  269. printf ("ISISd version %s\n", ISISD_VERSION);
  270. printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
  271. " Ofer Wald and Hannes Gredler\n");
  272. print_version ("Zebra");
  273. exit (0);
  274. break;
  275. case 'C':
  276. dryrun = 1;
  277. break;
  278. case 'h':
  279. usage (0);
  280. break;
  281. default:
  282. usage (1);
  283. break;
  284. }
  285. }
  286. /* thread master */
  287. master = thread_master_create ();
  288. /* random seed from time */
  289. srandom (time (NULL));
  290. /*
  291. * initializations
  292. */
  293. zprivs_init (&isisd_privs);
  294. signal_init (master, array_size (isisd_signals), isisd_signals);
  295. cmd_init (1);
  296. vty_init (master);
  297. memory_init ();
  298. access_list_init();
  299. vrf_init ();
  300. prefix_list_init();
  301. isis_init ();
  302. isis_circuit_init ();
  303. isis_spf_cmds_init ();
  304. isis_redist_init ();
  305. isis_route_map_init();
  306. isis_mpls_te_init();
  307. /* create the global 'isis' instance */
  308. isis_new (1);
  309. isis_zebra_init (master);
  310. /* parse config file */
  311. /* this is needed three times! because we have interfaces before the areas */
  312. vty_read_config (config_file, config_default);
  313. /* Start execution only if not in dry-run mode */
  314. if (dryrun)
  315. return(0);
  316. /* demonize */
  317. if (daemon_mode && daemon (0, 0) < 0)
  318. {
  319. zlog_err("IS-IS daemon failed: %s", strerror(errno));
  320. exit (1);
  321. }
  322. /* Process ID file creation. */
  323. if (pid_file[0] != '\0')
  324. pid_output (pid_file);
  325. /* Make isis vty socket. */
  326. vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
  327. /* Print banner. */
  328. zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  329. /* Start finite state machine. */
  330. thread_main (master);
  331. /* Not reached. */
  332. exit (0);
  333. }