blob: 55a430d5b7689338d3825c49a03e993ed5aa0a90 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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
22#include <zebra.h>
23
24#include <sys/un.h>
25#include <setjmp.h>
26#include <sys/wait.h>
27#include <pwd.h>
28
29#include <readline/readline.h>
30#include <readline/history.h>
31
gdt5e4fa162004-03-16 14:38:36 +000032#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000033#include "getopt.h"
34#include "command.h"
Andrew J. Schorrf366ad32006-07-27 18:01:41 +000035#include "memory.h"
paul718e3742002-12-13 20:15:29 +000036
37#include "vtysh/vtysh.h"
38#include "vtysh/vtysh_user.h"
hassob094d262004-08-25 12:22:00 +000039
paul718e3742002-12-13 20:15:29 +000040/* VTY shell program name. */
41char *progname;
42
hasso67e29ab2004-08-26 22:21:31 +000043/* Configuration file name and directory. */
paul718e3742002-12-13 20:15:29 +000044char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG;
45
paul718e3742002-12-13 20:15:29 +000046/* Flag for indicate executing child command. */
47int execute_flag = 0;
48
49/* For sigsetjmp() & siglongjmp(). */
50static sigjmp_buf jmpbuf;
51
52/* Flag for avoid recursive siglongjmp() call. */
53static int jmpflag = 0;
54
55/* A static variable for holding the line. */
56static char *line_read;
57
58/* Master of threads. */
59struct thread_master *master;
hassob094d262004-08-25 12:22:00 +000060
Stephen Hemminger57fb9742008-07-28 12:19:04 -070061/* Command logging */
62FILE *logfile;
63
paul718e3742002-12-13 20:15:29 +000064/* SIGTSTP handler. This function care user's ^Z input. */
65void
66sigtstp (int sig)
67{
68 /* Execute "end" command. */
69 vtysh_execute ("end");
70
71 /* Initialize readline. */
72 rl_initialize ();
73 printf ("\n");
74
75 /* Check jmpflag for duplicate siglongjmp(). */
76 if (! jmpflag)
77 return;
78
79 jmpflag = 0;
80
81 /* Back to main command loop. */
82 siglongjmp (jmpbuf, 1);
83}
84
85/* SIGINT handler. This function care user's ^Z input. */
86void
87sigint (int sig)
88{
89 /* Check this process is not child process. */
90 if (! execute_flag)
91 {
92 rl_initialize ();
93 printf ("\n");
94 rl_forced_update_display ();
95 }
96}
97
hassoe42f5a32004-08-28 17:04:33 +000098/* Signale wrapper for vtysh. We don't use sigevent because
99 * vtysh doesn't use threads. TODO */
paul718e3742002-12-13 20:15:29 +0000100RETSIGTYPE *
hassoe42f5a32004-08-28 17:04:33 +0000101vtysh_signal_set (int signo, void (*func)(int))
paul718e3742002-12-13 20:15:29 +0000102{
103 int ret;
104 struct sigaction sig;
105 struct sigaction osig;
106
107 sig.sa_handler = func;
108 sigemptyset (&sig.sa_mask);
109 sig.sa_flags = 0;
110#ifdef SA_RESTART
111 sig.sa_flags |= SA_RESTART;
112#endif /* SA_RESTART */
113
114 ret = sigaction (signo, &sig, &osig);
115
116 if (ret < 0)
117 return (SIG_ERR);
118 else
119 return (osig.sa_handler);
120}
121
122/* Initialization of signal handles. */
123void
hassoe42f5a32004-08-28 17:04:33 +0000124vtysh_signal_init ()
paul718e3742002-12-13 20:15:29 +0000125{
hassoe42f5a32004-08-28 17:04:33 +0000126 vtysh_signal_set (SIGINT, sigint);
127 vtysh_signal_set (SIGTSTP, sigtstp);
128 vtysh_signal_set (SIGPIPE, SIG_IGN);
paul718e3742002-12-13 20:15:29 +0000129}
hassob094d262004-08-25 12:22:00 +0000130
paul718e3742002-12-13 20:15:29 +0000131/* Help information display. */
132static void
133usage (int status)
134{
135 if (status != 0)
136 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
137 else
hasso95e735b2004-08-26 13:08:30 +0000138 printf ("Usage : %s [OPTION...]\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000139 "Integrated shell for Quagga routing software suite. \n\n" \
hasso95e735b2004-08-26 13:08:30 +0000140 "-b, --boot Execute boot startup configuration\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000141 "-c, --command Execute argument as command\n" \
142 "-d, --daemon Connect only to the specified daemon\n" \
143 "-E, --echo Echo prompt and command in -c mode\n" \
Paul Jakma876b8be2006-10-15 23:35:57 +0000144 "-C, --dryrun Check configuration for validity and exit\n" \
hasso95e735b2004-08-26 13:08:30 +0000145 "-h, --help Display this help and exit\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000146 "Note that multiple commands may be executed from the command\n" \
147 "line by passing multiple -c args, or by embedding linefeed\n" \
148 "characters in one or more of the commands.\n\n" \
hasso95e735b2004-08-26 13:08:30 +0000149 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
150
paul718e3742002-12-13 20:15:29 +0000151 exit (status);
152}
153
154/* VTY shell options, we use GNU getopt library. */
155struct option longopts[] =
156{
hassob094d262004-08-25 12:22:00 +0000157 { "boot", no_argument, NULL, 'b'},
hasso4991f6c2004-04-06 11:36:17 +0000158 /* For compatibility with older zebra/quagga versions */
paul718e3742002-12-13 20:15:29 +0000159 { "eval", required_argument, NULL, 'e'},
hasso4991f6c2004-04-06 11:36:17 +0000160 { "command", required_argument, NULL, 'c'},
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000161 { "daemon", required_argument, NULL, 'd'},
162 { "echo", no_argument, NULL, 'E'},
Paul Jakma876b8be2006-10-15 23:35:57 +0000163 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +0000164 { "help", no_argument, NULL, 'h'},
165 { 0 }
166};
hassob094d262004-08-25 12:22:00 +0000167
paul718e3742002-12-13 20:15:29 +0000168/* Read a string, and return a pointer to it. Returns NULL on EOF. */
169char *
170vtysh_rl_gets ()
171{
hasso4991f6c2004-04-06 11:36:17 +0000172 HIST_ENTRY *last;
paul718e3742002-12-13 20:15:29 +0000173 /* If the buffer has already been allocated, return the memory
hasso95e735b2004-08-26 13:08:30 +0000174 * to the free pool. */
paul718e3742002-12-13 20:15:29 +0000175 if (line_read)
176 {
177 free (line_read);
178 line_read = NULL;
179 }
180
181 /* Get a line from the user. Change prompt according to node. XXX. */
182 line_read = readline (vtysh_prompt ());
183
hasso4991f6c2004-04-06 11:36:17 +0000184 /* If the line has any text in it, save it on the history. But only if
hasso95e735b2004-08-26 13:08:30 +0000185 * last command in history isn't the same one. */
paul718e3742002-12-13 20:15:29 +0000186 if (line_read && *line_read)
hasso4991f6c2004-04-06 11:36:17 +0000187 {
188 using_history();
189 last = previous_history();
190 if (!last || strcmp (last->line, line_read) != 0)
191 add_history (line_read);
192 }
paul718e3742002-12-13 20:15:29 +0000193
194 return (line_read);
195}
hassob094d262004-08-25 12:22:00 +0000196
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700197static void log_it(const char *line)
198{
199 time_t t = time(NULL);
200 struct tm *tmp = localtime(&t);
201 char *user = getenv("USER") ? : "boot";
202 char tod[64];
203
204 strftime(tod, sizeof tod, "%Y%m%d-%H:%M.%S", tmp);
205
206 fprintf(logfile, "%s:%s %s\n", tod, user, line);
207}
208
paul718e3742002-12-13 20:15:29 +0000209/* VTY shell main routine. */
210int
211main (int argc, char **argv, char **env)
212{
213 char *p;
214 int opt;
Paul Jakma876b8be2006-10-15 23:35:57 +0000215 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000216 int boot_flag = 0;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000217 const char *daemon_name = NULL;
218 struct cmd_rec {
219 const char *line;
220 struct cmd_rec *next;
221 } *cmd = NULL;
222 struct cmd_rec *tail = NULL;
223 int echo_command = 0;
paul718e3742002-12-13 20:15:29 +0000224
225 /* Preserve name of myself. */
226 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
227
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700228 /* if logging open now */
229 if ((p = getenv("VTYSH_LOG")) != NULL)
230 logfile = fopen(p, "a");
231
paul718e3742002-12-13 20:15:29 +0000232 /* Option handling. */
233 while (1)
234 {
Paul Jakma876b8be2006-10-15 23:35:57 +0000235 opt = getopt_long (argc, argv, "be:c:d:EhC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000236
237 if (opt == EOF)
238 break;
239
240 switch (opt)
241 {
242 case 0:
243 break;
244 case 'b':
245 boot_flag = 1;
246 break;
247 case 'e':
hasso4991f6c2004-04-06 11:36:17 +0000248 case 'c':
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000249 {
250 struct cmd_rec *cr;
251 cr = XMALLOC(0, sizeof(*cr));
252 cr->line = optarg;
253 cr->next = NULL;
254 if (tail)
255 tail->next = cr;
256 else
257 cmd = cr;
258 tail = cr;
259 }
260 break;
261 case 'd':
262 daemon_name = optarg;
263 break;
264 case 'E':
265 echo_command = 1;
paul718e3742002-12-13 20:15:29 +0000266 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000267 case 'C':
268 dryrun = 1;
269 break;
paul718e3742002-12-13 20:15:29 +0000270 case 'h':
271 usage (0);
272 break;
paul718e3742002-12-13 20:15:29 +0000273 default:
274 usage (1);
275 break;
276 }
277 }
278
279 /* Initialize user input buffer. */
280 line_read = NULL;
Stephen Hemminger0fbd62a2008-05-02 09:25:02 -0700281 setlinebuf(stdout);
paul718e3742002-12-13 20:15:29 +0000282
283 /* Signal and others. */
hassoe42f5a32004-08-28 17:04:33 +0000284 vtysh_signal_init ();
paul718e3742002-12-13 20:15:29 +0000285
286 /* Make vty structure and register commands. */
287 vtysh_init_vty ();
288 vtysh_init_cmd ();
289 vtysh_user_init ();
290 vtysh_config_init ();
291
292 vty_init_vtysh ();
293
294 sort_node ();
295
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000296 /* Read vtysh configuration file before connecting to daemons. */
hassoe7168df2004-10-03 20:11:32 +0000297 vtysh_read_config (config_default);
paul718e3742002-12-13 20:15:29 +0000298
Paul Jakma876b8be2006-10-15 23:35:57 +0000299 /* Start execution only if not in dry-run mode */
300 if(dryrun)
301 return(0);
302
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000303 /* Make sure we pass authentication before proceeding. */
304 vtysh_auth ();
305
306 /* Do not connect until we have passed authentication. */
307 if (vtysh_connect_all (daemon_name) <= 0)
paul718e3742002-12-13 20:15:29 +0000308 {
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000309 fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
310 exit(1);
311 }
312
313 /* If eval mode. */
314 if (cmd)
315 {
316 /* Enter into enable node. */
317 vtysh_execute ("enable");
318
319 while (cmd != NULL)
320 {
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700321 int ret;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000322 char *eol;
323
324 while ((eol = strchr(cmd->line, '\n')) != NULL)
325 {
326 *eol = '\0';
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700327
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000328 if (echo_command)
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700329 printf("%s%s\n", vtysh_prompt(), cmd->line);
330
331 if (logfile)
332 log_it(cmd->line);
333
334 ret = vtysh_execute_no_pager(cmd->line);
335 if (ret != CMD_SUCCESS
336 && ret != CMD_SUCCESS_DAEMON
337 && ret != CMD_WARNING)
338 exit(1);
339
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000340 cmd->line = eol+1;
341 }
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700342
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000343 if (echo_command)
344 printf("%s%s\n", vtysh_prompt(), cmd->line);
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700345
346 if (logfile)
347 log_it(cmd->line);
348
349 ret = vtysh_execute_no_pager(cmd->line);
350 if (ret != CMD_SUCCESS
351 && ret != CMD_SUCCESS_DAEMON
352 && ret != CMD_WARNING)
353 exit(1);
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000354
355 {
356 struct cmd_rec *cr;
357 cr = cmd;
358 cmd = cmd->next;
359 XFREE(0, cr);
360 }
361 }
paul718e3742002-12-13 20:15:29 +0000362 exit (0);
363 }
364
365 /* Boot startup configuration file. */
366 if (boot_flag)
367 {
hassoe7168df2004-10-03 20:11:32 +0000368 if (vtysh_read_config (integrate_default))
369 {
370 fprintf (stderr, "Can't open configuration file [%s]\n",
371 integrate_default);
372 exit (1);
373 }
374 else
375 exit (0);
paul718e3742002-12-13 20:15:29 +0000376 }
377
378 vtysh_pager_init ();
379
380 vtysh_readline_init ();
381
382 vty_hello (vty);
383
hassoe7168df2004-10-03 20:11:32 +0000384 /* Enter into enable node. */
385 vtysh_execute ("enable");
386
paul718e3742002-12-13 20:15:29 +0000387 /* Preparation for longjmp() in sigtstp(). */
388 sigsetjmp (jmpbuf, 1);
389 jmpflag = 1;
390
391 /* Main command loop. */
392 while (vtysh_rl_gets ())
393 vtysh_execute (line_read);
394
395 printf ("\n");
396
397 /* Rest in peace. */
398 exit (0);
399}