vtysh_main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* Virtual terminal interface shell.
  2. * Copyright (C) 2000 Kunihiro Ishiguro
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include <sys/un.h>
  23. #include <setjmp.h>
  24. #include <sys/wait.h>
  25. #include <pwd.h>
  26. #include <readline/readline.h>
  27. #include <readline/history.h>
  28. #include <lib/version.h>
  29. #include "getopt.h"
  30. #include "command.h"
  31. #include "memory.h"
  32. #include "vtysh/vtysh.h"
  33. #include "vtysh/vtysh_user.h"
  34. /* VTY shell program name. */
  35. char *progname;
  36. /* Configuration file name and directory. */
  37. char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG;
  38. char history_file[MAXPATHLEN];
  39. /* Flag for indicate executing child command. */
  40. int execute_flag = 0;
  41. /* For sigsetjmp() & siglongjmp(). */
  42. static sigjmp_buf jmpbuf;
  43. /* Flag for avoid recursive siglongjmp() call. */
  44. static int jmpflag = 0;
  45. /* A static variable for holding the line. */
  46. static char *line_read;
  47. /* Master of threads. */
  48. struct thread_master *master;
  49. /* Command logging */
  50. FILE *logfile;
  51. /* SIGTSTP handler. This function care user's ^Z input. */
  52. void
  53. sigtstp (int sig)
  54. {
  55. /* Execute "end" command. */
  56. vtysh_execute ("end");
  57. /* Initialize readline. */
  58. rl_initialize ();
  59. printf ("\n");
  60. /* Check jmpflag for duplicate siglongjmp(). */
  61. if (! jmpflag)
  62. return;
  63. jmpflag = 0;
  64. /* Back to main command loop. */
  65. siglongjmp (jmpbuf, 1);
  66. }
  67. /* SIGINT handler. This function care user's ^Z input. */
  68. void
  69. sigint (int sig)
  70. {
  71. /* Check this process is not child process. */
  72. if (! execute_flag)
  73. {
  74. rl_initialize ();
  75. printf ("\n");
  76. rl_forced_update_display ();
  77. }
  78. }
  79. /* Signale wrapper for vtysh. We don't use sigevent because
  80. * vtysh doesn't use threads. TODO */
  81. RETSIGTYPE *
  82. vtysh_signal_set (int signo, void (*func)(int))
  83. {
  84. int ret;
  85. struct sigaction sig;
  86. struct sigaction osig;
  87. sig.sa_handler = func;
  88. sigemptyset (&sig.sa_mask);
  89. sig.sa_flags = 0;
  90. #ifdef SA_RESTART
  91. sig.sa_flags |= SA_RESTART;
  92. #endif /* SA_RESTART */
  93. ret = sigaction (signo, &sig, &osig);
  94. if (ret < 0)
  95. return (SIG_ERR);
  96. else
  97. return (osig.sa_handler);
  98. }
  99. /* Initialization of signal handles. */
  100. void
  101. vtysh_signal_init ()
  102. {
  103. vtysh_signal_set (SIGINT, sigint);
  104. vtysh_signal_set (SIGTSTP, sigtstp);
  105. vtysh_signal_set (SIGPIPE, SIG_IGN);
  106. }
  107. /* Help information display. */
  108. static void
  109. usage (int status)
  110. {
  111. if (status != 0)
  112. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  113. else
  114. printf ("Usage : %s [OPTION...]\n\n" \
  115. "Integrated shell for Quagga routing software suite. \n\n" \
  116. "-b, --boot Execute boot startup configuration\n" \
  117. "-c, --command Execute argument as command\n" \
  118. "-d, --daemon Connect only to the specified daemon\n" \
  119. "-E, --echo Echo prompt and command in -c mode\n" \
  120. "-C, --dryrun Check configuration for validity and exit\n" \
  121. "-h, --help Display this help and exit\n\n" \
  122. "Note that multiple commands may be executed from the command\n" \
  123. "line by passing multiple -c args, or by embedding linefeed\n" \
  124. "characters in one or more of the commands.\n\n" \
  125. "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  126. exit (status);
  127. }
  128. /* VTY shell options, we use GNU getopt library. */
  129. struct option longopts[] =
  130. {
  131. { "boot", no_argument, NULL, 'b'},
  132. /* For compatibility with older zebra/quagga versions */
  133. { "eval", required_argument, NULL, 'e'},
  134. { "command", required_argument, NULL, 'c'},
  135. { "daemon", required_argument, NULL, 'd'},
  136. { "echo", no_argument, NULL, 'E'},
  137. { "dryrun", no_argument, NULL, 'C'},
  138. { "help", no_argument, NULL, 'h'},
  139. { "noerror", no_argument, NULL, 'n'},
  140. { 0 }
  141. };
  142. /* Read a string, and return a pointer to it. Returns NULL on EOF. */
  143. char *
  144. vtysh_rl_gets ()
  145. {
  146. HIST_ENTRY *last;
  147. /* If the buffer has already been allocated, return the memory
  148. * to the free pool. */
  149. if (line_read)
  150. {
  151. free (line_read);
  152. line_read = NULL;
  153. }
  154. /* Get a line from the user. Change prompt according to node. XXX. */
  155. line_read = readline (vtysh_prompt ());
  156. /* If the line has any text in it, save it on the history. But only if
  157. * last command in history isn't the same one. */
  158. if (line_read && *line_read)
  159. {
  160. using_history();
  161. last = previous_history();
  162. if (!last || strcmp (last->line, line_read) != 0) {
  163. add_history (line_read);
  164. append_history(1,history_file);
  165. }
  166. }
  167. return (line_read);
  168. }
  169. static void log_it(const char *line)
  170. {
  171. time_t t = time(NULL);
  172. struct tm *tmp = localtime(&t);
  173. const char *user = getenv("USER");
  174. char tod[64];
  175. if (!user)
  176. user = "boot";
  177. strftime(tod, sizeof tod, "%Y%m%d-%H:%M.%S", tmp);
  178. fprintf(logfile, "%s:%s %s\n", tod, user, line);
  179. }
  180. /* VTY shell main routine. */
  181. int
  182. main (int argc, char **argv, char **env)
  183. {
  184. char *p;
  185. int opt;
  186. int dryrun = 0;
  187. int boot_flag = 0;
  188. const char *daemon_name = NULL;
  189. struct cmd_rec {
  190. const char *line;
  191. struct cmd_rec *next;
  192. } *cmd = NULL;
  193. struct cmd_rec *tail = NULL;
  194. int echo_command = 0;
  195. int no_error = 0;
  196. /* Preserve name of myself. */
  197. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  198. /* if logging open now */
  199. if ((p = getenv("VTYSH_LOG")) != NULL)
  200. logfile = fopen(p, "a");
  201. /* Option handling. */
  202. while (1)
  203. {
  204. opt = getopt_long (argc, argv, "be:c:d:nEhC", longopts, 0);
  205. if (opt == EOF)
  206. break;
  207. switch (opt)
  208. {
  209. case 0:
  210. break;
  211. case 'b':
  212. boot_flag = 1;
  213. break;
  214. case 'e':
  215. case 'c':
  216. {
  217. struct cmd_rec *cr;
  218. cr = XMALLOC(0, sizeof(*cr));
  219. cr->line = optarg;
  220. cr->next = NULL;
  221. if (tail)
  222. tail->next = cr;
  223. else
  224. cmd = cr;
  225. tail = cr;
  226. }
  227. break;
  228. case 'd':
  229. daemon_name = optarg;
  230. break;
  231. case 'n':
  232. no_error = 1;
  233. break;
  234. case 'E':
  235. echo_command = 1;
  236. break;
  237. case 'C':
  238. dryrun = 1;
  239. break;
  240. case 'h':
  241. usage (0);
  242. break;
  243. default:
  244. usage (1);
  245. break;
  246. }
  247. }
  248. /* Initialize user input buffer. */
  249. line_read = NULL;
  250. setlinebuf(stdout);
  251. /* Signal and others. */
  252. vtysh_signal_init ();
  253. /* Make vty structure and register commands. */
  254. vtysh_init_vty ();
  255. vtysh_init_cmd ();
  256. vtysh_user_init ();
  257. vtysh_config_init ();
  258. vty_init_vtysh ();
  259. /* Read vtysh configuration file before connecting to daemons. */
  260. vtysh_read_config (config_default);
  261. /* Start execution only if not in dry-run mode */
  262. if(dryrun)
  263. return(0);
  264. /* Ignore error messages */
  265. if (no_error)
  266. freopen("/dev/null", "w", stdout);
  267. /* Make sure we pass authentication before proceeding. */
  268. vtysh_auth ();
  269. /* Do not connect until we have passed authentication. */
  270. if (vtysh_connect_all (daemon_name) <= 0)
  271. {
  272. fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
  273. exit(1);
  274. }
  275. /* If eval mode. */
  276. if (cmd)
  277. {
  278. /* Enter into enable node. */
  279. vtysh_execute ("enable");
  280. while (cmd != NULL)
  281. {
  282. int ret;
  283. char *eol;
  284. while ((eol = strchr(cmd->line, '\n')) != NULL)
  285. {
  286. *eol = '\0';
  287. if (echo_command)
  288. printf("%s%s\n", vtysh_prompt(), cmd->line);
  289. if (logfile)
  290. log_it(cmd->line);
  291. ret = vtysh_execute_no_pager(cmd->line);
  292. if (!no_error &&
  293. ! (ret == CMD_SUCCESS ||
  294. ret == CMD_SUCCESS_DAEMON ||
  295. ret == CMD_WARNING))
  296. exit(1);
  297. cmd->line = eol+1;
  298. }
  299. if (echo_command)
  300. printf("%s%s\n", vtysh_prompt(), cmd->line);
  301. if (logfile)
  302. log_it(cmd->line);
  303. ret = vtysh_execute_no_pager(cmd->line);
  304. if (!no_error &&
  305. ! (ret == CMD_SUCCESS ||
  306. ret == CMD_SUCCESS_DAEMON ||
  307. ret == CMD_WARNING))
  308. exit(1);
  309. {
  310. struct cmd_rec *cr;
  311. cr = cmd;
  312. cmd = cmd->next;
  313. XFREE(0, cr);
  314. }
  315. }
  316. exit (0);
  317. }
  318. /* Boot startup configuration file. */
  319. if (boot_flag)
  320. {
  321. if (vtysh_read_config (integrate_default))
  322. {
  323. fprintf (stderr, "Can't open configuration file [%s]\n",
  324. integrate_default);
  325. exit (1);
  326. }
  327. else
  328. exit (0);
  329. }
  330. vtysh_pager_init ();
  331. vtysh_readline_init ();
  332. vty_hello (vty);
  333. /* Enter into enable node. */
  334. vtysh_execute ("enable");
  335. /* Preparation for longjmp() in sigtstp(). */
  336. sigsetjmp (jmpbuf, 1);
  337. jmpflag = 1;
  338. snprintf(history_file, sizeof(history_file), "%s/.history_quagga", getenv("HOME"));
  339. read_history(history_file);
  340. /* Main command loop. */
  341. while (vtysh_rl_gets ())
  342. vtysh_execute (line_read);
  343. history_truncate_file(history_file,1000);
  344. printf ("\n");
  345. /* Rest in peace. */
  346. exit (0);
  347. }