test_main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* main routine.
  2. * Copyright (C) 1997, 98 Kunihiro Ishiguro
  3. *
  4. * GNU Zebra is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2, or (at your option) any
  7. * later version.
  8. *
  9. * GNU Zebra is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  16. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  17. * 02111-1307, USA.
  18. */
  19. #include <zebra.h>
  20. #include <lib/version.h>
  21. #include "getopt.h"
  22. #include "command.h"
  23. #include "thread.h"
  24. #include "filter.h"
  25. #include "memory.h"
  26. #include "prefix.h"
  27. #include "log.h"
  28. #include "privs.h"
  29. #include "sigevent.h"
  30. #include "zebra/rib.h"
  31. #include "zebra/zserv.h"
  32. #include "zebra/debug.h"
  33. #include "zebra/router-id.h"
  34. #include "zebra/interface.h"
  35. /* Zebra instance */
  36. struct zebra_t zebrad =
  37. {
  38. .rtm_table_default = 0,
  39. };
  40. /* process id. */
  41. pid_t pid;
  42. /* zebra_rib's workqueue hold time. Private export for use by test code only */
  43. extern int rib_process_hold_time;
  44. /* Pacify zclient.o in libzebra, which expects this variable. */
  45. struct thread_master *master;
  46. /* Command line options. */
  47. struct option longopts[] =
  48. {
  49. { "batch", no_argument, NULL, 'b'},
  50. { "daemon", no_argument, NULL, 'd'},
  51. { "config_file", required_argument, NULL, 'f'},
  52. { "help", no_argument, NULL, 'h'},
  53. { "vty_addr", required_argument, NULL, 'A'},
  54. { "vty_port", required_argument, NULL, 'P'},
  55. { "version", no_argument, NULL, 'v'},
  56. { "rib_hold", required_argument, NULL, 'r'},
  57. { 0 }
  58. };
  59. zebra_capabilities_t _caps_p [] =
  60. {
  61. ZCAP_NET_ADMIN,
  62. ZCAP_SYS_ADMIN,
  63. ZCAP_NET_RAW,
  64. };
  65. /* Default configuration file path. */
  66. char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
  67. /* Process ID saved for use by init system */
  68. const char *pid_file = PATH_ZEBRA_PID;
  69. /* Help information display. */
  70. static void
  71. usage (char *progname, int status)
  72. {
  73. if (status != 0)
  74. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  75. else
  76. {
  77. printf ("Usage : %s [OPTION...]\n\n"\
  78. "Daemon which manages kernel routing table management and "\
  79. "redistribution between different routing protocols.\n\n"\
  80. "-b, --batch Runs in batch mode\n"\
  81. "-d, --daemon Runs in daemon mode\n"\
  82. "-f, --config_file Set configuration file name\n"\
  83. "-A, --vty_addr Set vty's bind address\n"\
  84. "-P, --vty_port Set vty's port number\n"\
  85. "-r, --rib_hold Set rib-queue hold time\n"\
  86. "-v, --version Print program version\n"\
  87. "-h, --help Display this help and exit\n"\
  88. "\n"\
  89. "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  90. }
  91. exit (status);
  92. }
  93. static unsigned int test_ifindex = 0;
  94. /* testrib commands */
  95. DEFUN (test_interface_state,
  96. test_interface_state_cmd,
  97. "state (up|down)",
  98. "configure interface\n"
  99. "up\n"
  100. "down\n")
  101. {
  102. struct interface *ifp;
  103. if (argc < 1)
  104. return CMD_WARNING;
  105. ifp = vty->index;
  106. if (ifp->ifindex == IFINDEX_INTERNAL)
  107. {
  108. ifp->ifindex = ++test_ifindex;
  109. ifp->mtu = 1500;
  110. ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
  111. }
  112. switch (argv[0][0])
  113. {
  114. case 'u':
  115. SET_FLAG (ifp->flags, IFF_UP);
  116. if_add_update (ifp);
  117. printf ("up\n");
  118. break;
  119. case 'd':
  120. UNSET_FLAG (ifp->flags, IFF_UP);
  121. if_delete_update (ifp);
  122. printf ("down\n");
  123. break;
  124. default:
  125. return CMD_WARNING;
  126. }
  127. return CMD_SUCCESS;
  128. }
  129. static void
  130. test_cmd_init (void)
  131. {
  132. install_element (INTERFACE_NODE, &test_interface_state_cmd);
  133. }
  134. /* SIGHUP handler. */
  135. static void
  136. sighup (void)
  137. {
  138. zlog_info ("SIGHUP received");
  139. /* Reload of config file. */
  140. ;
  141. }
  142. /* SIGINT handler. */
  143. static void
  144. sigint (void)
  145. {
  146. zlog_notice ("Terminating on signal");
  147. exit (0);
  148. }
  149. /* SIGUSR1 handler. */
  150. static void
  151. sigusr1 (void)
  152. {
  153. zlog_rotate (NULL);
  154. }
  155. struct quagga_signal_t zebra_signals[] =
  156. {
  157. {
  158. .signal = SIGHUP,
  159. .handler = &sighup,
  160. },
  161. {
  162. .signal = SIGUSR1,
  163. .handler = &sigusr1,
  164. },
  165. {
  166. .signal = SIGINT,
  167. .handler = &sigint,
  168. },
  169. {
  170. .signal = SIGTERM,
  171. .handler = &sigint,
  172. },
  173. };
  174. /* Main startup routine. */
  175. int
  176. main (int argc, char **argv)
  177. {
  178. char *p;
  179. char *vty_addr = NULL;
  180. int vty_port = 0;
  181. int batch_mode = 0;
  182. int daemon_mode = 0;
  183. char *config_file = NULL;
  184. char *progname;
  185. struct thread thread;
  186. /* Set umask before anything for security */
  187. umask (0027);
  188. /* preserve my name */
  189. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  190. zlog_default = openzlog (progname, ZLOG_ZEBRA,
  191. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  192. while (1)
  193. {
  194. int opt;
  195. opt = getopt_long (argc, argv, "bdf:hA:P:r:v", longopts, 0);
  196. if (opt == EOF)
  197. break;
  198. switch (opt)
  199. {
  200. case 0:
  201. break;
  202. case 'b':
  203. batch_mode = 1;
  204. case 'd':
  205. daemon_mode = 1;
  206. break;
  207. case 'f':
  208. config_file = optarg;
  209. break;
  210. case 'A':
  211. vty_addr = optarg;
  212. break;
  213. case 'P':
  214. /* Deal with atoi() returning 0 on failure, and zebra not
  215. listening on zebra port... */
  216. if (strcmp(optarg, "0") == 0)
  217. {
  218. vty_port = 0;
  219. break;
  220. }
  221. vty_port = atoi (optarg);
  222. break;
  223. case 'r':
  224. rib_process_hold_time = atoi(optarg);
  225. break;
  226. case 'v':
  227. print_version (progname);
  228. exit (0);
  229. break;
  230. case 'h':
  231. usage (progname, 0);
  232. break;
  233. default:
  234. usage (progname, 1);
  235. break;
  236. }
  237. }
  238. /* port and conf file mandatory */
  239. if (!vty_port || !config_file)
  240. {
  241. fprintf (stderr, "Error: --vty_port and --config_file arguments"
  242. " are both required\n");
  243. usage (progname, 1);
  244. }
  245. /* Make master thread emulator. */
  246. zebrad.master = thread_master_create ();
  247. /* Vty related initialize. */
  248. signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
  249. cmd_init (1);
  250. vty_init (zebrad.master);
  251. memory_init ();
  252. if_init();
  253. zebra_debug_init ();
  254. zebra_if_init ();
  255. test_cmd_init ();
  256. /* Zebra related initialize. */
  257. rib_init ();
  258. access_list_init ();
  259. /* Make kernel routing socket. */
  260. kernel_init ();
  261. route_read ();
  262. zebra_vty_init();
  263. /* Sort VTY commands. */
  264. sort_node ();
  265. /* Configuration file read*/
  266. vty_read_config (config_file, config_default);
  267. /* Clean up rib. */
  268. rib_weed_tables ();
  269. /* Exit when zebra is working in batch mode. */
  270. if (batch_mode)
  271. exit (0);
  272. /* Daemonize. */
  273. if (daemon_mode && daemon (0, 0) < 0)
  274. {
  275. perror("daemon start failed");
  276. exit (1);
  277. }
  278. /* Needed for BSD routing socket. */
  279. pid = getpid ();
  280. /* Make vty server socket. */
  281. vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
  282. /* Print banner. */
  283. zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  284. while (thread_fetch (zebrad.master, &thread))
  285. thread_call (&thread);
  286. /* Not reached... */
  287. return 0;
  288. }