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" \ |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 141 | "-h, --help Display this help and exit\n\n" \ |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 142 | "Note that multiple commands may be executed from the command\n" \ |
| 143 | "line by passing multiple -c args, or by embedding linefeed\n" \ |
| 144 | "characters in one or more of the commands.\n\n" \ |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 145 | "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 146 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | exit (status); |
| 148 | } |
| 149 | |
| 150 | /* VTY shell options, we use GNU getopt library. */ |
| 151 | struct option longopts[] = |
| 152 | { |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 153 | { "boot", no_argument, NULL, 'b'}, |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 154 | /* For compatibility with older zebra/quagga versions */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | { "eval", required_argument, NULL, 'e'}, |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 156 | { "command", required_argument, NULL, 'c'}, |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 157 | { "daemon", required_argument, NULL, 'd'}, |
| 158 | { "echo", no_argument, NULL, 'E'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 159 | { "help", no_argument, NULL, 'h'}, |
| 160 | { 0 } |
| 161 | }; |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 162 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ |
| 164 | char * |
| 165 | vtysh_rl_gets () |
| 166 | { |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 167 | HIST_ENTRY *last; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 168 | /* If the buffer has already been allocated, return the memory |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 169 | * to the free pool. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 170 | if (line_read) |
| 171 | { |
| 172 | free (line_read); |
| 173 | line_read = NULL; |
| 174 | } |
| 175 | |
| 176 | /* Get a line from the user. Change prompt according to node. XXX. */ |
| 177 | line_read = readline (vtysh_prompt ()); |
| 178 | |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 179 | /* 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] | 180 | * last command in history isn't the same one. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | if (line_read && *line_read) |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 182 | { |
| 183 | using_history(); |
| 184 | last = previous_history(); |
| 185 | if (!last || strcmp (last->line, line_read) != 0) |
| 186 | add_history (line_read); |
| 187 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 188 | |
| 189 | return (line_read); |
| 190 | } |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 191 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 192 | /* VTY shell main routine. */ |
| 193 | int |
| 194 | main (int argc, char **argv, char **env) |
| 195 | { |
| 196 | char *p; |
| 197 | int opt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | int boot_flag = 0; |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 199 | const char *daemon_name = NULL; |
| 200 | struct cmd_rec { |
| 201 | const char *line; |
| 202 | struct cmd_rec *next; |
| 203 | } *cmd = NULL; |
| 204 | struct cmd_rec *tail = NULL; |
| 205 | int echo_command = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | |
| 207 | /* Preserve name of myself. */ |
| 208 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 209 | |
| 210 | /* Option handling. */ |
| 211 | while (1) |
| 212 | { |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 213 | opt = getopt_long (argc, argv, "be:c:d:Eh", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 214 | |
| 215 | if (opt == EOF) |
| 216 | break; |
| 217 | |
| 218 | switch (opt) |
| 219 | { |
| 220 | case 0: |
| 221 | break; |
| 222 | case 'b': |
| 223 | boot_flag = 1; |
| 224 | break; |
| 225 | case 'e': |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 226 | case 'c': |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 227 | { |
| 228 | struct cmd_rec *cr; |
| 229 | cr = XMALLOC(0, sizeof(*cr)); |
| 230 | cr->line = optarg; |
| 231 | cr->next = NULL; |
| 232 | if (tail) |
| 233 | tail->next = cr; |
| 234 | else |
| 235 | cmd = cr; |
| 236 | tail = cr; |
| 237 | } |
| 238 | break; |
| 239 | case 'd': |
| 240 | daemon_name = optarg; |
| 241 | break; |
| 242 | case 'E': |
| 243 | echo_command = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 244 | break; |
| 245 | case 'h': |
| 246 | usage (0); |
| 247 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | default: |
| 249 | usage (1); |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* Initialize user input buffer. */ |
| 255 | line_read = NULL; |
| 256 | |
| 257 | /* Signal and others. */ |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 258 | vtysh_signal_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | |
| 260 | /* Make vty structure and register commands. */ |
| 261 | vtysh_init_vty (); |
| 262 | vtysh_init_cmd (); |
| 263 | vtysh_user_init (); |
| 264 | vtysh_config_init (); |
| 265 | |
| 266 | vty_init_vtysh (); |
| 267 | |
| 268 | sort_node (); |
| 269 | |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 270 | /* Read vtysh configuration file before connecting to daemons. */ |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 271 | vtysh_read_config (config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 272 | |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 273 | /* Make sure we pass authentication before proceeding. */ |
| 274 | vtysh_auth (); |
| 275 | |
| 276 | /* Do not connect until we have passed authentication. */ |
| 277 | if (vtysh_connect_all (daemon_name) <= 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | { |
Andrew J. Schorr | f366ad3 | 2006-07-27 18:01:41 +0000 | [diff] [blame] | 279 | fprintf(stderr, "Exiting: failed to connect to any daemons.\n"); |
| 280 | exit(1); |
| 281 | } |
| 282 | |
| 283 | /* If eval mode. */ |
| 284 | if (cmd) |
| 285 | { |
| 286 | /* Enter into enable node. */ |
| 287 | vtysh_execute ("enable"); |
| 288 | |
| 289 | while (cmd != NULL) |
| 290 | { |
| 291 | char *eol; |
| 292 | |
| 293 | while ((eol = strchr(cmd->line, '\n')) != NULL) |
| 294 | { |
| 295 | *eol = '\0'; |
| 296 | if (echo_command) |
| 297 | printf("%s%s\n", vtysh_prompt(), cmd->line); |
| 298 | vtysh_execute_no_pager(cmd->line); |
| 299 | cmd->line = eol+1; |
| 300 | } |
| 301 | if (echo_command) |
| 302 | printf("%s%s\n", vtysh_prompt(), cmd->line); |
| 303 | vtysh_execute_no_pager (cmd->line); |
| 304 | |
| 305 | { |
| 306 | struct cmd_rec *cr; |
| 307 | cr = cmd; |
| 308 | cmd = cmd->next; |
| 309 | XFREE(0, cr); |
| 310 | } |
| 311 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 312 | exit (0); |
| 313 | } |
| 314 | |
| 315 | /* Boot startup configuration file. */ |
| 316 | if (boot_flag) |
| 317 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 318 | if (vtysh_read_config (integrate_default)) |
| 319 | { |
| 320 | fprintf (stderr, "Can't open configuration file [%s]\n", |
| 321 | integrate_default); |
| 322 | exit (1); |
| 323 | } |
| 324 | else |
| 325 | exit (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | vtysh_pager_init (); |
| 329 | |
| 330 | vtysh_readline_init (); |
| 331 | |
| 332 | vty_hello (vty); |
| 333 | |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 334 | /* Enter into enable node. */ |
| 335 | vtysh_execute ("enable"); |
| 336 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 337 | /* Preparation for longjmp() in sigtstp(). */ |
| 338 | sigsetjmp (jmpbuf, 1); |
| 339 | jmpflag = 1; |
| 340 | |
| 341 | /* Main command loop. */ |
| 342 | while (vtysh_rl_gets ()) |
| 343 | vtysh_execute (line_read); |
| 344 | |
| 345 | printf ("\n"); |
| 346 | |
| 347 | /* Rest in peace. */ |
| 348 | exit (0); |
| 349 | } |