isis_main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "zclient.h"
  35. #include "vrf.h"
  36. #include "isisd/dict.h"
  37. #include "include-netbsd/iso.h"
  38. #include "isisd/isis_constants.h"
  39. #include "isisd/isis_common.h"
  40. #include "isisd/isis_flags.h"
  41. #include "isisd/isis_circuit.h"
  42. #include "isisd/isisd.h"
  43. #include "isisd/isis_dynhn.h"
  44. #include "isisd/isis_spf.h"
  45. #include "isisd/isis_route.h"
  46. #include "isisd/isis_zebra.h"
  47. /* Default configuration file name */
  48. #define ISISD_DEFAULT_CONFIG "isisd.conf"
  49. /* Default vty port */
  50. #define ISISD_VTY_PORT 2608
  51. /* isisd privileges */
  52. zebra_capabilities_t _caps_p[] = {
  53. ZCAP_NET_RAW,
  54. ZCAP_BIND
  55. };
  56. struct zebra_privs_t isisd_privs = {
  57. #if defined(QUAGGA_USER)
  58. .user = QUAGGA_USER,
  59. #endif
  60. #if defined QUAGGA_GROUP
  61. .group = QUAGGA_GROUP,
  62. #endif
  63. #ifdef VTY_GROUP
  64. .vty_group = VTY_GROUP,
  65. #endif
  66. .caps_p = _caps_p,
  67. .cap_num_p = sizeof (_caps_p) / sizeof (*_caps_p),
  68. .cap_num_i = 0
  69. };
  70. /* isisd options */
  71. struct option longopts[] = {
  72. {"daemon", no_argument, NULL, 'd'},
  73. {"config_file", required_argument, NULL, 'f'},
  74. {"pid_file", required_argument, NULL, 'i'},
  75. {"socket", required_argument, NULL, 'z'},
  76. {"vty_addr", required_argument, NULL, 'A'},
  77. {"vty_port", required_argument, NULL, 'P'},
  78. {"user", required_argument, NULL, 'u'},
  79. {"group", required_argument, NULL, 'g'},
  80. {"version", no_argument, NULL, 'v'},
  81. {"dryrun", no_argument, NULL, 'C'},
  82. {"help", no_argument, NULL, 'h'},
  83. {0}
  84. };
  85. /* Configuration file and directory. */
  86. char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
  87. char *config_file = NULL;
  88. /* isisd program name. */
  89. char *progname;
  90. int daemon_mode = 0;
  91. /* Master of threads. */
  92. struct thread_master *master;
  93. /* Process ID saved for use by init system */
  94. const char *pid_file = PATH_ISISD_PID;
  95. /* for reload */
  96. char _cwd[MAXPATHLEN];
  97. char _progpath[MAXPATHLEN];
  98. int _argc;
  99. char **_argv;
  100. char **_envp;
  101. /*
  102. * Prototypes.
  103. */
  104. void reload(void);
  105. void sighup(void);
  106. void sigint(void);
  107. void sigterm(void);
  108. void sigusr1(void);
  109. /* Help information display. */
  110. static void
  111. usage (int status)
  112. {
  113. if (status != 0)
  114. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  115. else
  116. {
  117. printf ("Usage : %s [OPTION...]\n\n\
  118. Daemon which manages IS-IS routing\n\n\
  119. -d, --daemon Runs in daemon mode\n\
  120. -f, --config_file Set configuration file name\n\
  121. -i, --pid_file Set process identifier file name\n\
  122. -z, --socket Set path of zebra socket\n\
  123. -A, --vty_addr Set vty's bind address\n\
  124. -P, --vty_port Set vty's port number\n\
  125. -u, --user User to run as\n\
  126. -g, --group Group to run as\n\
  127. -v, --version Print program version\n\
  128. -C, --dryrun Check configuration for validity and exit\n\
  129. -h, --help Display this help and exit\n\
  130. \n\
  131. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  132. }
  133. exit (status);
  134. }
  135. void
  136. reload ()
  137. {
  138. zlog_debug ("Reload");
  139. /* FIXME: Clean up func call here */
  140. vty_reset ();
  141. (void) isisd_privs.change (ZPRIVS_RAISE);
  142. execve (_progpath, _argv, _envp);
  143. zlog_err ("Reload failed: cannot exec %s: %s", _progpath,
  144. safe_strerror (errno));
  145. }
  146. static void
  147. terminate (int i)
  148. {
  149. exit (i);
  150. }
  151. /*
  152. * Signal handlers
  153. */
  154. void
  155. sighup (void)
  156. {
  157. zlog_debug ("SIGHUP received");
  158. reload ();
  159. return;
  160. }
  161. void
  162. sigint (void)
  163. {
  164. zlog_notice ("Terminating on signal SIGINT");
  165. terminate (0);
  166. }
  167. void
  168. sigterm (void)
  169. {
  170. zlog_notice ("Terminating on signal SIGTERM");
  171. terminate (0);
  172. }
  173. void
  174. sigusr1 (void)
  175. {
  176. zlog_debug ("SIGUSR1 received");
  177. zlog_rotate (NULL);
  178. }
  179. struct quagga_signal_t isisd_signals[] =
  180. {
  181. {
  182. .signal = SIGHUP,
  183. .handler = &sighup,
  184. },
  185. {
  186. .signal = SIGUSR1,
  187. .handler = &sigusr1,
  188. },
  189. {
  190. .signal = SIGINT,
  191. .handler = &sigint,
  192. },
  193. {
  194. .signal = SIGTERM,
  195. .handler = &sigterm,
  196. },
  197. };
  198. /*
  199. * Main routine of isisd. Parse arguments and handle IS-IS state machine.
  200. */
  201. int
  202. main (int argc, char **argv, char **envp)
  203. {
  204. char *p;
  205. int opt, vty_port = ISISD_VTY_PORT;
  206. struct thread thread;
  207. char *config_file = NULL;
  208. char *vty_addr = NULL;
  209. int dryrun = 0;
  210. /* Get the programname without the preceding path. */
  211. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  212. zlog_default = openzlog (progname, ZLOG_ISIS,
  213. LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
  214. /* for reload */
  215. _argc = argc;
  216. _argv = argv;
  217. _envp = envp;
  218. getcwd (_cwd, sizeof (_cwd));
  219. if (*argv[0] == '.')
  220. snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
  221. else
  222. snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
  223. /* Command line argument treatment. */
  224. while (1)
  225. {
  226. opt = getopt_long (argc, argv, "df:i:z:hA:p:P:u:g:vC", longopts, 0);
  227. if (opt == EOF)
  228. break;
  229. switch (opt)
  230. {
  231. case 0:
  232. break;
  233. case 'd':
  234. daemon_mode = 1;
  235. break;
  236. case 'f':
  237. config_file = optarg;
  238. break;
  239. case 'i':
  240. pid_file = optarg;
  241. break;
  242. case 'z':
  243. zclient_serv_path_set (optarg);
  244. break;
  245. case 'A':
  246. vty_addr = optarg;
  247. break;
  248. case 'P':
  249. /* Deal with atoi() returning 0 on failure, and isisd not
  250. listening on isisd port... */
  251. if (strcmp (optarg, "0") == 0)
  252. {
  253. vty_port = 0;
  254. break;
  255. }
  256. vty_port = atoi (optarg);
  257. vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
  258. break;
  259. case 'u':
  260. isisd_privs.user = optarg;
  261. break;
  262. case 'g':
  263. isisd_privs.group = optarg;
  264. break;
  265. case 'v':
  266. printf ("ISISd version %s\n", ISISD_VERSION);
  267. printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
  268. " Ofer Wald and Hannes Gredler\n");
  269. print_version ("Zebra");
  270. exit (0);
  271. break;
  272. case 'C':
  273. dryrun = 1;
  274. break;
  275. case 'h':
  276. usage (0);
  277. break;
  278. default:
  279. usage (1);
  280. break;
  281. }
  282. }
  283. /* thread master */
  284. master = thread_master_create ();
  285. /* random seed from time */
  286. srandom (time (NULL));
  287. /*
  288. * initializations
  289. */
  290. zprivs_init (&isisd_privs);
  291. signal_init (master, array_size (isisd_signals), isisd_signals);
  292. cmd_init (1);
  293. vty_init (master);
  294. memory_init ();
  295. access_list_init();
  296. vrf_init ();
  297. isis_init ();
  298. isis_circuit_init ();
  299. isis_spf_cmds_init ();
  300. /* create the global 'isis' instance */
  301. isis_new (1);
  302. isis_zebra_init (master);
  303. /* parse config file */
  304. /* this is needed three times! because we have interfaces before the areas */
  305. vty_read_config (config_file, config_default);
  306. /* Start execution only if not in dry-run mode */
  307. if (dryrun)
  308. return(0);
  309. /* demonize */
  310. if (daemon_mode)
  311. daemon (0, 0);
  312. /* Process ID file creation. */
  313. if (pid_file[0] != '\0')
  314. pid_output (pid_file);
  315. /* Make isis vty socket. */
  316. vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
  317. /* Print banner. */
  318. zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  319. /* Start finite state machine. */
  320. while (thread_fetch (master, &thread))
  321. thread_call (&thread);
  322. /* Not reached. */
  323. exit (0);
  324. }