blob: 4a315a5c856f0c0f257bb96d431af4b7a3eef443 [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;
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +010045char history_file[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +000046
paul718e3742002-12-13 20:15:29 +000047/* Flag for indicate executing child command. */
48int execute_flag = 0;
49
50/* For sigsetjmp() & siglongjmp(). */
51static sigjmp_buf jmpbuf;
52
53/* Flag for avoid recursive siglongjmp() call. */
54static int jmpflag = 0;
55
56/* A static variable for holding the line. */
57static char *line_read;
58
59/* Master of threads. */
60struct thread_master *master;
hassob094d262004-08-25 12:22:00 +000061
Stephen Hemminger57fb9742008-07-28 12:19:04 -070062/* Command logging */
63FILE *logfile;
64
paul718e3742002-12-13 20:15:29 +000065/* SIGTSTP handler. This function care user's ^Z input. */
66void
67sigtstp (int sig)
68{
69 /* Execute "end" command. */
70 vtysh_execute ("end");
71
72 /* Initialize readline. */
73 rl_initialize ();
74 printf ("\n");
75
76 /* Check jmpflag for duplicate siglongjmp(). */
77 if (! jmpflag)
78 return;
79
80 jmpflag = 0;
81
82 /* Back to main command loop. */
83 siglongjmp (jmpbuf, 1);
84}
85
86/* SIGINT handler. This function care user's ^Z input. */
87void
88sigint (int sig)
89{
90 /* Check this process is not child process. */
91 if (! execute_flag)
92 {
93 rl_initialize ();
94 printf ("\n");
95 rl_forced_update_display ();
96 }
97}
98
hassoe42f5a32004-08-28 17:04:33 +000099/* Signale wrapper for vtysh. We don't use sigevent because
100 * vtysh doesn't use threads. TODO */
paul718e3742002-12-13 20:15:29 +0000101RETSIGTYPE *
hassoe42f5a32004-08-28 17:04:33 +0000102vtysh_signal_set (int signo, void (*func)(int))
paul718e3742002-12-13 20:15:29 +0000103{
104 int ret;
105 struct sigaction sig;
106 struct sigaction osig;
107
108 sig.sa_handler = func;
109 sigemptyset (&sig.sa_mask);
110 sig.sa_flags = 0;
111#ifdef SA_RESTART
112 sig.sa_flags |= SA_RESTART;
113#endif /* SA_RESTART */
114
115 ret = sigaction (signo, &sig, &osig);
116
117 if (ret < 0)
118 return (SIG_ERR);
119 else
120 return (osig.sa_handler);
121}
122
123/* Initialization of signal handles. */
124void
hassoe42f5a32004-08-28 17:04:33 +0000125vtysh_signal_init ()
paul718e3742002-12-13 20:15:29 +0000126{
hassoe42f5a32004-08-28 17:04:33 +0000127 vtysh_signal_set (SIGINT, sigint);
128 vtysh_signal_set (SIGTSTP, sigtstp);
129 vtysh_signal_set (SIGPIPE, SIG_IGN);
paul718e3742002-12-13 20:15:29 +0000130}
hassob094d262004-08-25 12:22:00 +0000131
paul718e3742002-12-13 20:15:29 +0000132/* Help information display. */
133static void
134usage (int status)
135{
136 if (status != 0)
137 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
138 else
hasso95e735b2004-08-26 13:08:30 +0000139 printf ("Usage : %s [OPTION...]\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000140 "Integrated shell for Quagga routing software suite. \n\n" \
hasso95e735b2004-08-26 13:08:30 +0000141 "-b, --boot Execute boot startup configuration\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000142 "-c, --command Execute argument as command\n" \
143 "-d, --daemon Connect only to the specified daemon\n" \
144 "-E, --echo Echo prompt and command in -c mode\n" \
Paul Jakma876b8be2006-10-15 23:35:57 +0000145 "-C, --dryrun Check configuration for validity and exit\n" \
hasso95e735b2004-08-26 13:08:30 +0000146 "-h, --help Display this help and exit\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000147 "Note that multiple commands may be executed from the command\n" \
148 "line by passing multiple -c args, or by embedding linefeed\n" \
149 "characters in one or more of the commands.\n\n" \
hasso95e735b2004-08-26 13:08:30 +0000150 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
151
paul718e3742002-12-13 20:15:29 +0000152 exit (status);
153}
154
155/* VTY shell options, we use GNU getopt library. */
156struct option longopts[] =
157{
hassob094d262004-08-25 12:22:00 +0000158 { "boot", no_argument, NULL, 'b'},
hasso4991f6c2004-04-06 11:36:17 +0000159 /* For compatibility with older zebra/quagga versions */
paul718e3742002-12-13 20:15:29 +0000160 { "eval", required_argument, NULL, 'e'},
hasso4991f6c2004-04-06 11:36:17 +0000161 { "command", required_argument, NULL, 'c'},
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000162 { "daemon", required_argument, NULL, 'd'},
163 { "echo", no_argument, NULL, 'E'},
Paul Jakma876b8be2006-10-15 23:35:57 +0000164 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +0000165 { "help", no_argument, NULL, 'h'},
Stephen Hemminger914131f2008-07-30 14:16:47 -0700166 { "noerror", no_argument, NULL, 'n'},
paul718e3742002-12-13 20:15:29 +0000167 { 0 }
168};
hassob094d262004-08-25 12:22:00 +0000169
paul718e3742002-12-13 20:15:29 +0000170/* Read a string, and return a pointer to it. Returns NULL on EOF. */
171char *
172vtysh_rl_gets ()
173{
hasso4991f6c2004-04-06 11:36:17 +0000174 HIST_ENTRY *last;
paul718e3742002-12-13 20:15:29 +0000175 /* If the buffer has already been allocated, return the memory
hasso95e735b2004-08-26 13:08:30 +0000176 * to the free pool. */
paul718e3742002-12-13 20:15:29 +0000177 if (line_read)
178 {
179 free (line_read);
180 line_read = NULL;
181 }
182
183 /* Get a line from the user. Change prompt according to node. XXX. */
184 line_read = readline (vtysh_prompt ());
185
hasso4991f6c2004-04-06 11:36:17 +0000186 /* If the line has any text in it, save it on the history. But only if
hasso95e735b2004-08-26 13:08:30 +0000187 * last command in history isn't the same one. */
paul718e3742002-12-13 20:15:29 +0000188 if (line_read && *line_read)
hasso4991f6c2004-04-06 11:36:17 +0000189 {
190 using_history();
191 last = previous_history();
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +0100192 if (!last || strcmp (last->line, line_read) != 0) {
hasso4991f6c2004-04-06 11:36:17 +0000193 add_history (line_read);
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +0100194 append_history(1,history_file);
195 }
hasso4991f6c2004-04-06 11:36:17 +0000196 }
paul718e3742002-12-13 20:15:29 +0000197
198 return (line_read);
199}
hassob094d262004-08-25 12:22:00 +0000200
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700201static void log_it(const char *line)
202{
203 time_t t = time(NULL);
204 struct tm *tmp = localtime(&t);
205 char *user = getenv("USER") ? : "boot";
206 char tod[64];
207
208 strftime(tod, sizeof tod, "%Y%m%d-%H:%M.%S", tmp);
209
210 fprintf(logfile, "%s:%s %s\n", tod, user, line);
211}
212
paul718e3742002-12-13 20:15:29 +0000213/* VTY shell main routine. */
214int
215main (int argc, char **argv, char **env)
216{
217 char *p;
218 int opt;
Paul Jakma876b8be2006-10-15 23:35:57 +0000219 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000220 int boot_flag = 0;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000221 const char *daemon_name = NULL;
222 struct cmd_rec {
223 const char *line;
224 struct cmd_rec *next;
225 } *cmd = NULL;
226 struct cmd_rec *tail = NULL;
227 int echo_command = 0;
Stephen Hemminger914131f2008-07-30 14:16:47 -0700228 int no_error = 0;
paul718e3742002-12-13 20:15:29 +0000229
230 /* Preserve name of myself. */
231 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
232
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700233 /* if logging open now */
234 if ((p = getenv("VTYSH_LOG")) != NULL)
235 logfile = fopen(p, "a");
236
paul718e3742002-12-13 20:15:29 +0000237 /* Option handling. */
238 while (1)
239 {
Stephen Hemminger914131f2008-07-30 14:16:47 -0700240 opt = getopt_long (argc, argv, "be:c:d:nEhC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000241
242 if (opt == EOF)
243 break;
244
245 switch (opt)
246 {
247 case 0:
248 break;
249 case 'b':
250 boot_flag = 1;
251 break;
252 case 'e':
hasso4991f6c2004-04-06 11:36:17 +0000253 case 'c':
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000254 {
255 struct cmd_rec *cr;
256 cr = XMALLOC(0, sizeof(*cr));
257 cr->line = optarg;
258 cr->next = NULL;
259 if (tail)
260 tail->next = cr;
261 else
262 cmd = cr;
263 tail = cr;
264 }
265 break;
266 case 'd':
267 daemon_name = optarg;
268 break;
Stephen Hemminger914131f2008-07-30 14:16:47 -0700269 case 'n':
270 no_error = 1;
271 break;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000272 case 'E':
273 echo_command = 1;
paul718e3742002-12-13 20:15:29 +0000274 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000275 case 'C':
276 dryrun = 1;
277 break;
paul718e3742002-12-13 20:15:29 +0000278 case 'h':
279 usage (0);
280 break;
paul718e3742002-12-13 20:15:29 +0000281 default:
282 usage (1);
283 break;
284 }
285 }
286
287 /* Initialize user input buffer. */
288 line_read = NULL;
Stephen Hemminger0fbd62a2008-05-02 09:25:02 -0700289 setlinebuf(stdout);
paul718e3742002-12-13 20:15:29 +0000290
291 /* Signal and others. */
hassoe42f5a32004-08-28 17:04:33 +0000292 vtysh_signal_init ();
paul718e3742002-12-13 20:15:29 +0000293
294 /* Make vty structure and register commands. */
295 vtysh_init_vty ();
296 vtysh_init_cmd ();
297 vtysh_user_init ();
298 vtysh_config_init ();
299
300 vty_init_vtysh ();
301
302 sort_node ();
303
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000304 /* Read vtysh configuration file before connecting to daemons. */
hassoe7168df2004-10-03 20:11:32 +0000305 vtysh_read_config (config_default);
paul718e3742002-12-13 20:15:29 +0000306
Paul Jakma876b8be2006-10-15 23:35:57 +0000307 /* Start execution only if not in dry-run mode */
308 if(dryrun)
309 return(0);
310
Stephen Hemminger914131f2008-07-30 14:16:47 -0700311 /* Ignore error messages */
312 if (no_error)
313 freopen("/dev/null", "w", stdout);
314
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000315 /* Make sure we pass authentication before proceeding. */
316 vtysh_auth ();
317
318 /* Do not connect until we have passed authentication. */
319 if (vtysh_connect_all (daemon_name) <= 0)
paul718e3742002-12-13 20:15:29 +0000320 {
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000321 fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
322 exit(1);
323 }
324
325 /* If eval mode. */
326 if (cmd)
327 {
328 /* Enter into enable node. */
329 vtysh_execute ("enable");
330
331 while (cmd != NULL)
332 {
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700333 int ret;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000334 char *eol;
335
336 while ((eol = strchr(cmd->line, '\n')) != NULL)
337 {
338 *eol = '\0';
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700339
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000340 if (echo_command)
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700341 printf("%s%s\n", vtysh_prompt(), cmd->line);
342
343 if (logfile)
344 log_it(cmd->line);
345
346 ret = vtysh_execute_no_pager(cmd->line);
Stephen Hemminger914131f2008-07-30 14:16:47 -0700347 if (!no_error &&
348 ! (ret == CMD_SUCCESS ||
349 ret == CMD_SUCCESS_DAEMON ||
350 ret == CMD_WARNING))
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700351 exit(1);
352
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000353 cmd->line = eol+1;
354 }
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700355
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000356 if (echo_command)
357 printf("%s%s\n", vtysh_prompt(), cmd->line);
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700358
359 if (logfile)
360 log_it(cmd->line);
361
362 ret = vtysh_execute_no_pager(cmd->line);
Stephen Hemminger914131f2008-07-30 14:16:47 -0700363 if (!no_error &&
364 ! (ret == CMD_SUCCESS ||
365 ret == CMD_SUCCESS_DAEMON ||
366 ret == CMD_WARNING))
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700367 exit(1);
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000368
369 {
370 struct cmd_rec *cr;
371 cr = cmd;
372 cmd = cmd->next;
373 XFREE(0, cr);
374 }
375 }
paul718e3742002-12-13 20:15:29 +0000376 exit (0);
377 }
378
379 /* Boot startup configuration file. */
380 if (boot_flag)
381 {
hassoe7168df2004-10-03 20:11:32 +0000382 if (vtysh_read_config (integrate_default))
383 {
384 fprintf (stderr, "Can't open configuration file [%s]\n",
385 integrate_default);
386 exit (1);
387 }
388 else
389 exit (0);
paul718e3742002-12-13 20:15:29 +0000390 }
391
392 vtysh_pager_init ();
393
394 vtysh_readline_init ();
395
396 vty_hello (vty);
397
hassoe7168df2004-10-03 20:11:32 +0000398 /* Enter into enable node. */
399 vtysh_execute ("enable");
400
paul718e3742002-12-13 20:15:29 +0000401 /* Preparation for longjmp() in sigtstp(). */
402 sigsetjmp (jmpbuf, 1);
403 jmpflag = 1;
404
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +0100405 snprintf(history_file, sizeof(history_file), "%s/.history_quagga", getenv("HOME"));
406 read_history(history_file);
paul718e3742002-12-13 20:15:29 +0000407 /* Main command loop. */
408 while (vtysh_rl_gets ())
409 vtysh_execute (line_read);
410
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +0100411 history_truncate_file(history_file,1000);
paul718e3742002-12-13 20:15:29 +0000412 printf ("\n");
413
414 /* Rest in peace. */
415 exit (0);
416}