blob: 51f376c773fe72e9df981e35e877975df4faf2b0 [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
paul718e3742002-12-13 20:15:29 +000061/* SIGTSTP handler. This function care user's ^Z input. */
62void
63sigtstp (int sig)
64{
65 /* Execute "end" command. */
66 vtysh_execute ("end");
67
68 /* Initialize readline. */
69 rl_initialize ();
70 printf ("\n");
71
72 /* Check jmpflag for duplicate siglongjmp(). */
73 if (! jmpflag)
74 return;
75
76 jmpflag = 0;
77
78 /* Back to main command loop. */
79 siglongjmp (jmpbuf, 1);
80}
81
82/* SIGINT handler. This function care user's ^Z input. */
83void
84sigint (int sig)
85{
86 /* Check this process is not child process. */
87 if (! execute_flag)
88 {
89 rl_initialize ();
90 printf ("\n");
91 rl_forced_update_display ();
92 }
93}
94
hassoe42f5a32004-08-28 17:04:33 +000095/* Signale wrapper for vtysh. We don't use sigevent because
96 * vtysh doesn't use threads. TODO */
paul718e3742002-12-13 20:15:29 +000097RETSIGTYPE *
hassoe42f5a32004-08-28 17:04:33 +000098vtysh_signal_set (int signo, void (*func)(int))
paul718e3742002-12-13 20:15:29 +000099{
100 int ret;
101 struct sigaction sig;
102 struct sigaction osig;
103
104 sig.sa_handler = func;
105 sigemptyset (&sig.sa_mask);
106 sig.sa_flags = 0;
107#ifdef SA_RESTART
108 sig.sa_flags |= SA_RESTART;
109#endif /* SA_RESTART */
110
111 ret = sigaction (signo, &sig, &osig);
112
113 if (ret < 0)
114 return (SIG_ERR);
115 else
116 return (osig.sa_handler);
117}
118
119/* Initialization of signal handles. */
120void
hassoe42f5a32004-08-28 17:04:33 +0000121vtysh_signal_init ()
paul718e3742002-12-13 20:15:29 +0000122{
hassoe42f5a32004-08-28 17:04:33 +0000123 vtysh_signal_set (SIGINT, sigint);
124 vtysh_signal_set (SIGTSTP, sigtstp);
125 vtysh_signal_set (SIGPIPE, SIG_IGN);
paul718e3742002-12-13 20:15:29 +0000126}
hassob094d262004-08-25 12:22:00 +0000127
paul718e3742002-12-13 20:15:29 +0000128/* Help information display. */
129static void
130usage (int status)
131{
132 if (status != 0)
133 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
134 else
hasso95e735b2004-08-26 13:08:30 +0000135 printf ("Usage : %s [OPTION...]\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000136 "Integrated shell for Quagga routing software suite. \n\n" \
hasso95e735b2004-08-26 13:08:30 +0000137 "-b, --boot Execute boot startup configuration\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000138 "-c, --command Execute argument as command\n" \
139 "-d, --daemon Connect only to the specified daemon\n" \
140 "-E, --echo Echo prompt and command in -c mode\n" \
Paul Jakma876b8be2006-10-15 23:35:57 +0000141 "-C, --dryrun Check configuration for validity and exit\n" \
hasso95e735b2004-08-26 13:08:30 +0000142 "-h, --help Display this help and exit\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000143 "Note that multiple commands may be executed from the command\n" \
144 "line by passing multiple -c args, or by embedding linefeed\n" \
145 "characters in one or more of the commands.\n\n" \
hasso95e735b2004-08-26 13:08:30 +0000146 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
147
paul718e3742002-12-13 20:15:29 +0000148 exit (status);
149}
150
151/* VTY shell options, we use GNU getopt library. */
152struct option longopts[] =
153{
hassob094d262004-08-25 12:22:00 +0000154 { "boot", no_argument, NULL, 'b'},
hasso4991f6c2004-04-06 11:36:17 +0000155 /* For compatibility with older zebra/quagga versions */
paul718e3742002-12-13 20:15:29 +0000156 { "eval", required_argument, NULL, 'e'},
hasso4991f6c2004-04-06 11:36:17 +0000157 { "command", required_argument, NULL, 'c'},
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000158 { "daemon", required_argument, NULL, 'd'},
159 { "echo", no_argument, NULL, 'E'},
Paul Jakma876b8be2006-10-15 23:35:57 +0000160 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +0000161 { "help", no_argument, NULL, 'h'},
162 { 0 }
163};
hassob094d262004-08-25 12:22:00 +0000164
paul718e3742002-12-13 20:15:29 +0000165/* Read a string, and return a pointer to it. Returns NULL on EOF. */
166char *
167vtysh_rl_gets ()
168{
hasso4991f6c2004-04-06 11:36:17 +0000169 HIST_ENTRY *last;
paul718e3742002-12-13 20:15:29 +0000170 /* If the buffer has already been allocated, return the memory
hasso95e735b2004-08-26 13:08:30 +0000171 * to the free pool. */
paul718e3742002-12-13 20:15:29 +0000172 if (line_read)
173 {
174 free (line_read);
175 line_read = NULL;
176 }
177
178 /* Get a line from the user. Change prompt according to node. XXX. */
179 line_read = readline (vtysh_prompt ());
180
hasso4991f6c2004-04-06 11:36:17 +0000181 /* If the line has any text in it, save it on the history. But only if
hasso95e735b2004-08-26 13:08:30 +0000182 * last command in history isn't the same one. */
paul718e3742002-12-13 20:15:29 +0000183 if (line_read && *line_read)
hasso4991f6c2004-04-06 11:36:17 +0000184 {
185 using_history();
186 last = previous_history();
187 if (!last || strcmp (last->line, line_read) != 0)
188 add_history (line_read);
189 }
paul718e3742002-12-13 20:15:29 +0000190
191 return (line_read);
192}
hassob094d262004-08-25 12:22:00 +0000193
paul718e3742002-12-13 20:15:29 +0000194/* VTY shell main routine. */
195int
196main (int argc, char **argv, char **env)
197{
198 char *p;
199 int opt;
Paul Jakma876b8be2006-10-15 23:35:57 +0000200 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000201 int boot_flag = 0;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000202 const char *daemon_name = NULL;
203 struct cmd_rec {
204 const char *line;
205 struct cmd_rec *next;
206 } *cmd = NULL;
207 struct cmd_rec *tail = NULL;
208 int echo_command = 0;
paul718e3742002-12-13 20:15:29 +0000209
210 /* Preserve name of myself. */
211 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
212
213 /* Option handling. */
214 while (1)
215 {
Paul Jakma876b8be2006-10-15 23:35:57 +0000216 opt = getopt_long (argc, argv, "be:c:d:EhC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000217
218 if (opt == EOF)
219 break;
220
221 switch (opt)
222 {
223 case 0:
224 break;
225 case 'b':
226 boot_flag = 1;
227 break;
228 case 'e':
hasso4991f6c2004-04-06 11:36:17 +0000229 case 'c':
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000230 {
231 struct cmd_rec *cr;
232 cr = XMALLOC(0, sizeof(*cr));
233 cr->line = optarg;
234 cr->next = NULL;
235 if (tail)
236 tail->next = cr;
237 else
238 cmd = cr;
239 tail = cr;
240 }
241 break;
242 case 'd':
243 daemon_name = optarg;
244 break;
245 case 'E':
246 echo_command = 1;
paul718e3742002-12-13 20:15:29 +0000247 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000248 case 'C':
249 dryrun = 1;
250 break;
paul718e3742002-12-13 20:15:29 +0000251 case 'h':
252 usage (0);
253 break;
paul718e3742002-12-13 20:15:29 +0000254 default:
255 usage (1);
256 break;
257 }
258 }
259
260 /* Initialize user input buffer. */
261 line_read = NULL;
262
263 /* Signal and others. */
hassoe42f5a32004-08-28 17:04:33 +0000264 vtysh_signal_init ();
paul718e3742002-12-13 20:15:29 +0000265
266 /* Make vty structure and register commands. */
267 vtysh_init_vty ();
268 vtysh_init_cmd ();
269 vtysh_user_init ();
270 vtysh_config_init ();
271
272 vty_init_vtysh ();
273
274 sort_node ();
275
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000276 /* Read vtysh configuration file before connecting to daemons. */
hassoe7168df2004-10-03 20:11:32 +0000277 vtysh_read_config (config_default);
paul718e3742002-12-13 20:15:29 +0000278
Paul Jakma876b8be2006-10-15 23:35:57 +0000279 /* Start execution only if not in dry-run mode */
280 if(dryrun)
281 return(0);
282
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000283 /* Make sure we pass authentication before proceeding. */
284 vtysh_auth ();
285
286 /* Do not connect until we have passed authentication. */
287 if (vtysh_connect_all (daemon_name) <= 0)
paul718e3742002-12-13 20:15:29 +0000288 {
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000289 fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
290 exit(1);
291 }
292
293 /* If eval mode. */
294 if (cmd)
295 {
296 /* Enter into enable node. */
297 vtysh_execute ("enable");
298
299 while (cmd != NULL)
300 {
301 char *eol;
302
303 while ((eol = strchr(cmd->line, '\n')) != NULL)
304 {
305 *eol = '\0';
306 if (echo_command)
307 printf("%s%s\n", vtysh_prompt(), cmd->line);
308 vtysh_execute_no_pager(cmd->line);
309 cmd->line = eol+1;
310 }
311 if (echo_command)
312 printf("%s%s\n", vtysh_prompt(), cmd->line);
313 vtysh_execute_no_pager (cmd->line);
314
315 {
316 struct cmd_rec *cr;
317 cr = cmd;
318 cmd = cmd->next;
319 XFREE(0, cr);
320 }
321 }
paul718e3742002-12-13 20:15:29 +0000322 exit (0);
323 }
324
325 /* Boot startup configuration file. */
326 if (boot_flag)
327 {
hassoe7168df2004-10-03 20:11:32 +0000328 if (vtysh_read_config (integrate_default))
329 {
330 fprintf (stderr, "Can't open configuration file [%s]\n",
331 integrate_default);
332 exit (1);
333 }
334 else
335 exit (0);
paul718e3742002-12-13 20:15:29 +0000336 }
337
338 vtysh_pager_init ();
339
340 vtysh_readline_init ();
341
342 vty_hello (vty);
343
hassoe7168df2004-10-03 20:11:32 +0000344 /* Enter into enable node. */
345 vtysh_execute ("enable");
346
paul718e3742002-12-13 20:15:29 +0000347 /* Preparation for longjmp() in sigtstp(). */
348 sigsetjmp (jmpbuf, 1);
349 jmpflag = 1;
350
351 /* Main command loop. */
352 while (vtysh_rl_gets ())
353 vtysh_execute (line_read);
354
355 printf ("\n");
356
357 /* Rest in peace. */
358 exit (0);
359}