]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | ||
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 | ||
5e4fa164 | 32 | #include <lib/version.h> |
718e3744 | 33 | #include "getopt.h" |
34 | #include "command.h" | |
f366ad31 | 35 | #include "memory.h" |
718e3744 | 36 | |
37 | #include "vtysh/vtysh.h" | |
38 | #include "vtysh/vtysh_user.h" | |
b094d260 | 39 | |
718e3744 | 40 | /* VTY shell program name. */ |
41 | char *progname; | |
42 | ||
67e29abc | 43 | /* Configuration file name and directory. */ |
718e3744 | 44 | char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG; |
3f4ab7f9 | 45 | char history_file[MAXPATHLEN]; |
718e3744 | 46 | |
718e3744 | 47 | /* Flag for indicate executing child command. */ |
48 | int execute_flag = 0; | |
49 | ||
50 | /* For sigsetjmp() & siglongjmp(). */ | |
51 | static sigjmp_buf jmpbuf; | |
52 | ||
53 | /* Flag for avoid recursive siglongjmp() call. */ | |
54 | static int jmpflag = 0; | |
55 | ||
56 | /* A static variable for holding the line. */ | |
57 | static char *line_read; | |
58 | ||
59 | /* Master of threads. */ | |
60 | struct thread_master *master; | |
b094d260 | 61 | |
57fb9748 SH |
62 | /* Command logging */ |
63 | FILE *logfile; | |
64 | ||
718e3744 | 65 | /* SIGTSTP handler. This function care user's ^Z input. */ |
c0e8c16f | 66 | static void |
718e3744 | 67 | sigtstp (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. */ | |
c0e8c16f | 87 | static void |
718e3744 | 88 | sigint (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 | ||
e42f5a37 | 99 | /* Signale wrapper for vtysh. We don't use sigevent because |
100 | * vtysh doesn't use threads. TODO */ | |
c0e8c16f | 101 | static RETSIGTYPE * |
e42f5a37 | 102 | vtysh_signal_set (int signo, void (*func)(int)) |
718e3744 | 103 | { |
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. */ | |
c0e8c16f DS |
124 | static void |
125 | vtysh_signal_init (void) | |
718e3744 | 126 | { |
e42f5a37 | 127 | vtysh_signal_set (SIGINT, sigint); |
128 | vtysh_signal_set (SIGTSTP, sigtstp); | |
129 | vtysh_signal_set (SIGPIPE, SIG_IGN); | |
718e3744 | 130 | } |
b094d260 | 131 | |
718e3744 | 132 | /* Help information display. */ |
133 | static void | |
134 | usage (int status) | |
135 | { | |
136 | if (status != 0) | |
137 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); | |
138 | else | |
95e735b5 | 139 | printf ("Usage : %s [OPTION...]\n\n" \ |
f366ad31 | 140 | "Integrated shell for Quagga routing software suite. \n\n" \ |
95e735b5 | 141 | "-b, --boot Execute boot startup configuration\n" \ |
f366ad31 AS |
142 | "-c, --command Execute argument as command\n" \ |
143 | "-d, --daemon Connect only to the specified daemon\n" \ | |
0846286b | 144 | "-f, --inputfile Execute commands from specific file and exit\n" \ |
f366ad31 | 145 | "-E, --echo Echo prompt and command in -c mode\n" \ |
876b8be0 | 146 | "-C, --dryrun Check configuration for validity and exit\n" \ |
0846286b | 147 | "-m, --markfile Mark input file with context end\n" |
95e735b5 | 148 | "-h, --help Display this help and exit\n\n" \ |
f366ad31 AS |
149 | "Note that multiple commands may be executed from the command\n" \ |
150 | "line by passing multiple -c args, or by embedding linefeed\n" \ | |
151 | "characters in one or more of the commands.\n\n" \ | |
95e735b5 | 152 | "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
153 | ||
718e3744 | 154 | exit (status); |
155 | } | |
156 | ||
157 | /* VTY shell options, we use GNU getopt library. */ | |
158 | struct option longopts[] = | |
159 | { | |
b094d260 | 160 | { "boot", no_argument, NULL, 'b'}, |
4991f6ca | 161 | /* For compatibility with older zebra/quagga versions */ |
718e3744 | 162 | { "eval", required_argument, NULL, 'e'}, |
4991f6ca | 163 | { "command", required_argument, NULL, 'c'}, |
f366ad31 | 164 | { "daemon", required_argument, NULL, 'd'}, |
0846286b | 165 | { "inputfile", required_argument, NULL, 'f'}, |
f366ad31 | 166 | { "echo", no_argument, NULL, 'E'}, |
876b8be0 | 167 | { "dryrun", no_argument, NULL, 'C'}, |
718e3744 | 168 | { "help", no_argument, NULL, 'h'}, |
914131f8 | 169 | { "noerror", no_argument, NULL, 'n'}, |
0846286b | 170 | { "mark", no_argument, NULL, 'm'}, |
718e3744 | 171 | { 0 } |
172 | }; | |
b094d260 | 173 | |
718e3744 | 174 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ |
c0e8c16f DS |
175 | static char * |
176 | vtysh_rl_gets (void) | |
718e3744 | 177 | { |
4991f6ca | 178 | HIST_ENTRY *last; |
718e3744 | 179 | /* If the buffer has already been allocated, return the memory |
95e735b5 | 180 | * to the free pool. */ |
718e3744 | 181 | if (line_read) |
182 | { | |
183 | free (line_read); | |
184 | line_read = NULL; | |
185 | } | |
186 | ||
187 | /* Get a line from the user. Change prompt according to node. XXX. */ | |
188 | line_read = readline (vtysh_prompt ()); | |
189 | ||
4991f6ca | 190 | /* If the line has any text in it, save it on the history. But only if |
95e735b5 | 191 | * last command in history isn't the same one. */ |
718e3744 | 192 | if (line_read && *line_read) |
4991f6ca | 193 | { |
194 | using_history(); | |
195 | last = previous_history(); | |
3f4ab7f9 | 196 | if (!last || strcmp (last->line, line_read) != 0) { |
4991f6ca | 197 | add_history (line_read); |
3f4ab7f9 TP |
198 | append_history(1,history_file); |
199 | } | |
4991f6ca | 200 | } |
718e3744 | 201 | |
202 | return (line_read); | |
203 | } | |
b094d260 | 204 | |
57fb9748 SH |
205 | static void log_it(const char *line) |
206 | { | |
207 | time_t t = time(NULL); | |
208 | struct tm *tmp = localtime(&t); | |
c0e8c16f | 209 | const char *user = getenv("USER") ? : "boot"; |
57fb9748 SH |
210 | char tod[64]; |
211 | ||
212 | strftime(tod, sizeof tod, "%Y%m%d-%H:%M.%S", tmp); | |
213 | ||
214 | fprintf(logfile, "%s:%s %s\n", tod, user, line); | |
215 | } | |
216 | ||
718e3744 | 217 | /* VTY shell main routine. */ |
218 | int | |
219 | main (int argc, char **argv, char **env) | |
220 | { | |
221 | char *p; | |
222 | int opt; | |
876b8be0 | 223 | int dryrun = 0; |
718e3744 | 224 | int boot_flag = 0; |
f366ad31 | 225 | const char *daemon_name = NULL; |
0846286b | 226 | const char *inputfile = NULL; |
f366ad31 AS |
227 | struct cmd_rec { |
228 | const char *line; | |
229 | struct cmd_rec *next; | |
230 | } *cmd = NULL; | |
231 | struct cmd_rec *tail = NULL; | |
232 | int echo_command = 0; | |
914131f8 | 233 | int no_error = 0; |
0846286b | 234 | int markfile = 0; |
fba55c8a | 235 | char *homedir = NULL; |
718e3744 | 236 | |
237 | /* Preserve name of myself. */ | |
238 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); | |
239 | ||
57fb9748 SH |
240 | /* if logging open now */ |
241 | if ((p = getenv("VTYSH_LOG")) != NULL) | |
242 | logfile = fopen(p, "a"); | |
243 | ||
718e3744 | 244 | /* Option handling. */ |
245 | while (1) | |
246 | { | |
0846286b | 247 | opt = getopt_long (argc, argv, "be:c:d:nf:mEhC", longopts, 0); |
718e3744 | 248 | |
249 | if (opt == EOF) | |
250 | break; | |
251 | ||
252 | switch (opt) | |
253 | { | |
254 | case 0: | |
255 | break; | |
256 | case 'b': | |
257 | boot_flag = 1; | |
258 | break; | |
259 | case 'e': | |
4991f6ca | 260 | case 'c': |
f366ad31 AS |
261 | { |
262 | struct cmd_rec *cr; | |
263 | cr = XMALLOC(0, sizeof(*cr)); | |
264 | cr->line = optarg; | |
265 | cr->next = NULL; | |
266 | if (tail) | |
267 | tail->next = cr; | |
268 | else | |
269 | cmd = cr; | |
270 | tail = cr; | |
271 | } | |
272 | break; | |
273 | case 'd': | |
274 | daemon_name = optarg; | |
275 | break; | |
0846286b DS |
276 | case 'f': |
277 | inputfile = optarg; | |
278 | break; | |
279 | case 'm': | |
280 | markfile = 1; | |
281 | break; | |
914131f8 SH |
282 | case 'n': |
283 | no_error = 1; | |
284 | break; | |
f366ad31 AS |
285 | case 'E': |
286 | echo_command = 1; | |
718e3744 | 287 | break; |
876b8be0 PJ |
288 | case 'C': |
289 | dryrun = 1; | |
290 | break; | |
718e3744 | 291 | case 'h': |
292 | usage (0); | |
293 | break; | |
718e3744 | 294 | default: |
295 | usage (1); | |
296 | break; | |
297 | } | |
298 | } | |
299 | ||
300 | /* Initialize user input buffer. */ | |
301 | line_read = NULL; | |
0fbd62a1 | 302 | setlinebuf(stdout); |
718e3744 | 303 | |
304 | /* Signal and others. */ | |
e42f5a37 | 305 | vtysh_signal_init (); |
718e3744 | 306 | |
307 | /* Make vty structure and register commands. */ | |
308 | vtysh_init_vty (); | |
309 | vtysh_init_cmd (); | |
310 | vtysh_user_init (); | |
311 | vtysh_config_init (); | |
312 | ||
313 | vty_init_vtysh (); | |
314 | ||
f366ad31 | 315 | /* Read vtysh configuration file before connecting to daemons. */ |
0846286b DS |
316 | vtysh_read_config(config_default); |
317 | ||
318 | if (markfile) | |
319 | { | |
320 | if (!inputfile) | |
321 | { | |
322 | fprintf(stderr, "-f option MUST be specified with -m option\n"); | |
323 | return(1); | |
324 | } | |
325 | return(vtysh_mark_file(inputfile)); | |
326 | } | |
718e3744 | 327 | |
876b8be0 PJ |
328 | /* Start execution only if not in dry-run mode */ |
329 | if(dryrun) | |
0846286b DS |
330 | { |
331 | if (inputfile) | |
332 | { | |
333 | vtysh_read_config(inputfile); | |
334 | } | |
335 | return(0); | |
336 | } | |
337 | ||
914131f8 SH |
338 | /* Ignore error messages */ |
339 | if (no_error) | |
7a49a5b5 DS |
340 | { |
341 | if (freopen("/dev/null", "w", stdout) == NULL) | |
342 | { | |
343 | fprintf(stderr, "Exiting: Failed to duplicate stdout with -n option"); | |
344 | exit(1); | |
345 | } | |
346 | } | |
914131f8 | 347 | |
f366ad31 AS |
348 | /* Make sure we pass authentication before proceeding. */ |
349 | vtysh_auth (); | |
350 | ||
351 | /* Do not connect until we have passed authentication. */ | |
352 | if (vtysh_connect_all (daemon_name) <= 0) | |
353 | { | |
354 | fprintf(stderr, "Exiting: failed to connect to any daemons.\n"); | |
355 | exit(1); | |
356 | } | |
357 | ||
0846286b DS |
358 | if (inputfile) |
359 | { | |
360 | vtysh_read_config(inputfile); | |
361 | exit(0); | |
362 | } | |
363 | ||
fba55c8a DS |
364 | /* |
365 | * Setup history file for use by both -c and regular input | |
366 | * If we can't find the home directory, then don't store | |
367 | * the history information | |
368 | */ | |
369 | homedir = vtysh_get_home (); | |
370 | if (homedir) | |
371 | { | |
372 | snprintf(history_file, sizeof(history_file), "%s/.history_quagga", homedir); | |
373 | if (read_history (history_file) != 0) | |
374 | { | |
375 | int fp; | |
376 | ||
377 | fp = open (history_file, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); | |
378 | if (fp) | |
379 | close (fp); | |
380 | ||
381 | read_history (history_file); | |
382 | } | |
383 | } | |
384 | ||
95e735b5 | 385 | /* If eval mode. */ |
f366ad31 | 386 | if (cmd) |
718e3744 | 387 | { |
f366ad31 AS |
388 | /* Enter into enable node. */ |
389 | vtysh_execute ("enable"); | |
390 | ||
391 | while (cmd != NULL) | |
392 | { | |
57fb9748 | 393 | int ret; |
f366ad31 AS |
394 | char *eol; |
395 | ||
396 | while ((eol = strchr(cmd->line, '\n')) != NULL) | |
397 | { | |
398 | *eol = '\0'; | |
57fb9748 | 399 | |
fba55c8a DS |
400 | add_history (cmd->line); |
401 | append_history (1, history_file); | |
402 | ||
f366ad31 | 403 | if (echo_command) |
57fb9748 SH |
404 | printf("%s%s\n", vtysh_prompt(), cmd->line); |
405 | ||
406 | if (logfile) | |
407 | log_it(cmd->line); | |
408 | ||
409 | ret = vtysh_execute_no_pager(cmd->line); | |
914131f8 SH |
410 | if (!no_error && |
411 | ! (ret == CMD_SUCCESS || | |
412 | ret == CMD_SUCCESS_DAEMON || | |
413 | ret == CMD_WARNING)) | |
57fb9748 SH |
414 | exit(1); |
415 | ||
f366ad31 AS |
416 | cmd->line = eol+1; |
417 | } | |
57fb9748 | 418 | |
fba55c8a DS |
419 | add_history (cmd->line); |
420 | append_history (1, history_file); | |
421 | ||
f366ad31 AS |
422 | if (echo_command) |
423 | printf("%s%s\n", vtysh_prompt(), cmd->line); | |
57fb9748 SH |
424 | |
425 | if (logfile) | |
426 | log_it(cmd->line); | |
427 | ||
428 | ret = vtysh_execute_no_pager(cmd->line); | |
914131f8 SH |
429 | if (!no_error && |
430 | ! (ret == CMD_SUCCESS || | |
431 | ret == CMD_SUCCESS_DAEMON || | |
432 | ret == CMD_WARNING)) | |
57fb9748 | 433 | exit(1); |
f366ad31 AS |
434 | |
435 | { | |
436 | struct cmd_rec *cr; | |
437 | cr = cmd; | |
438 | cmd = cmd->next; | |
439 | XFREE(0, cr); | |
440 | } | |
441 | } | |
fba55c8a DS |
442 | |
443 | history_truncate_file(history_file,1000); | |
718e3744 | 444 | exit (0); |
445 | } | |
446 | ||
447 | /* Boot startup configuration file. */ | |
448 | if (boot_flag) | |
449 | { | |
e7168df4 | 450 | if (vtysh_read_config (integrate_default)) |
451 | { | |
452 | fprintf (stderr, "Can't open configuration file [%s]\n", | |
453 | integrate_default); | |
454 | exit (1); | |
455 | } | |
456 | else | |
457 | exit (0); | |
718e3744 | 458 | } |
459 | ||
460 | vtysh_pager_init (); | |
461 | ||
462 | vtysh_readline_init (); | |
463 | ||
464 | vty_hello (vty); | |
465 | ||
e7168df4 | 466 | /* Enter into enable node. */ |
467 | vtysh_execute ("enable"); | |
468 | ||
718e3744 | 469 | /* Preparation for longjmp() in sigtstp(). */ |
470 | sigsetjmp (jmpbuf, 1); | |
471 | jmpflag = 1; | |
472 | ||
473 | /* Main command loop. */ | |
474 | while (vtysh_rl_gets ()) | |
475 | vtysh_execute (line_read); | |
476 | ||
3f4ab7f9 | 477 | history_truncate_file(history_file,1000); |
718e3744 | 478 | printf ("\n"); |
479 | ||
480 | /* Rest in peace. */ | |
481 | exit (0); | |
482 | } |