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