paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 32 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | #include "getopt.h" |
| 34 | #include "command.h" |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 35 | #include "memory.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | |
| 37 | #include "vtysh/vtysh.h" |
| 38 | #include "vtysh/vtysh_user.h" |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 39 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | /* VTY shell program name. */ |
| 41 | char *progname; |
| 42 | |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 43 | /* Configuration file name and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG; |
| 45 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 46 | /* Flag for indicate executing child command. */ |
| 47 | int execute_flag = 0; |
| 48 | |
| 49 | /* For sigsetjmp() & siglongjmp(). */ |
| 50 | static sigjmp_buf jmpbuf; |
| 51 | |
| 52 | /* Flag for avoid recursive siglongjmp() call. */ |
| 53 | static int jmpflag = 0; |
| 54 | |
| 55 | /* A static variable for holding the line. */ |
| 56 | static char *line_read; |
| 57 | |
| 58 | /* Master of threads. */ |
| 59 | struct thread_master *master; |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 60 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 61 | /* SIGTSTP handler. This function care user's ^Z input. */ |
| 62 | void |
| 63 | sigtstp (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. */ |
| 83 | void |
| 84 | sigint (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 | |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 95 | /* Signale wrapper for vtysh. We don't use sigevent because |
| 96 | * vtysh doesn't use threads. TODO */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 97 | RETSIGTYPE * |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 98 | vtysh_signal_set (int signo, void (*func)(int)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | { |
| 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. */ |
| 120 | void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 121 | vtysh_signal_init () |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 122 | { |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 123 | vtysh_signal_set (SIGINT, sigint); |
| 124 | vtysh_signal_set (SIGTSTP, sigtstp); |
| 125 | vtysh_signal_set (SIGPIPE, SIG_IGN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 126 | } |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 127 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | /* Help information display. */ |
| 129 | static void |
| 130 | usage (int status) |
| 131 | { |
| 132 | if (status != 0) |
| 133 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 134 | else |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 135 | printf ("Usage : %s [OPTION...]\n\n" \ |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 136 | "Integrated shell for Quagga routing software suite. \n\n" \ |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 137 | "-b, --boot Execute boot startup configuration\n" \ |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 138 | "-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 Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame^] | 141 | "-C, --dryrun Check configuration for validity and exit\n" \ |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 142 | "-h, --help Display this help and exit\n\n" \ |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 143 | "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" \ |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 146 | "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 147 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | exit (status); |
| 149 | } |
| 150 | |
| 151 | /* VTY shell options, we use GNU getopt library. */ |
| 152 | struct option longopts[] = |
| 153 | { |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 154 | { "boot", no_argument, NULL, 'b'}, |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 155 | /* For compatibility with older zebra/quagga versions */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | { "eval", required_argument, NULL, 'e'}, |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 157 | { "command", required_argument, NULL, 'c'}, |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 158 | { "daemon", required_argument, NULL, 'd'}, |
| 159 | { "echo", no_argument, NULL, 'E'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame^] | 160 | { "dryrun", no_argument, NULL, 'C'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | { "help", no_argument, NULL, 'h'}, |
| 162 | { 0 } |
| 163 | }; |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 164 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ |
| 166 | char * |
| 167 | vtysh_rl_gets () |
| 168 | { |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 169 | HIST_ENTRY *last; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 170 | /* If the buffer has already been allocated, return the memory |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 171 | * to the free pool. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | 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 | |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 181 | /* If the line has any text in it, save it on the history. But only if |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 182 | * last command in history isn't the same one. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 183 | if (line_read && *line_read) |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 184 | { |
| 185 | using_history(); |
| 186 | last = previous_history(); |
| 187 | if (!last || strcmp (last->line, line_read) != 0) |
| 188 | add_history (line_read); |
| 189 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | |
| 191 | return (line_read); |
| 192 | } |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 193 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | /* VTY shell main routine. */ |
| 195 | int |
| 196 | main (int argc, char **argv, char **env) |
| 197 | { |
| 198 | char *p; |
| 199 | int opt; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame^] | 200 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 201 | int boot_flag = 0; |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 202 | 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; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | |
| 210 | /* Preserve name of myself. */ |
| 211 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 212 | |
| 213 | /* Option handling. */ |
| 214 | while (1) |
| 215 | { |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame^] | 216 | opt = getopt_long (argc, argv, "be:c:d:EhC", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | |
| 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': |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 229 | case 'c': |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 230 | { |
| 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; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 247 | break; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame^] | 248 | case 'C': |
| 249 | dryrun = 1; |
| 250 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | case 'h': |
| 252 | usage (0); |
| 253 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | default: |
| 255 | usage (1); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /* Initialize user input buffer. */ |
| 261 | line_read = NULL; |
| 262 | |
| 263 | /* Signal and others. */ |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 264 | vtysh_signal_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 265 | |
| 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. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 276 | /* Read vtysh configuration file before connecting to daemons. */ |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 277 | vtysh_read_config (config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame^] | 279 | /* Start execution only if not in dry-run mode */ |
| 280 | if(dryrun) |
| 281 | return(0); |
| 282 | |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 283 | /* 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) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | { |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 289 | 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 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | exit (0); |
| 323 | } |
| 324 | |
| 325 | /* Boot startup configuration file. */ |
| 326 | if (boot_flag) |
| 327 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 328 | 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); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | vtysh_pager_init (); |
| 339 | |
| 340 | vtysh_readline_init (); |
| 341 | |
| 342 | vty_hello (vty); |
| 343 | |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 344 | /* Enter into enable node. */ |
| 345 | vtysh_execute ("enable"); |
| 346 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 347 | /* 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 | } |