1 #include <stdio.h> 2 #include <string.h> 3 #include <errno.h> 4 5 #include <assert.h> 6 7 /* user defined header files */ 8 #include "kvm/builtin-debug.h" 9 #include "kvm/builtin-pause.h" 10 #include "kvm/builtin-resume.h" 11 #include "kvm/builtin-balloon.h" 12 #include "kvm/builtin-list.h" 13 #include "kvm/builtin-version.h" 14 #include "kvm/builtin-setup.h" 15 #include "kvm/builtin-stop.h" 16 #include "kvm/builtin-stat.h" 17 #include "kvm/builtin-help.h" 18 #include "kvm/builtin-sandbox.h" 19 #include "kvm/kvm-cmd.h" 20 #include "kvm/builtin-run.h" 21 #include "kvm/util.h" 22 23 struct cmd_struct kvm_commands[] = { 24 { "pause", kvm_cmd_pause, kvm_pause_help, 0 }, 25 { "resume", kvm_cmd_resume, kvm_resume_help, 0 }, 26 { "debug", kvm_cmd_debug, kvm_debug_help, 0 }, 27 { "balloon", kvm_cmd_balloon, kvm_balloon_help, 0 }, 28 { "list", kvm_cmd_list, kvm_list_help, 0 }, 29 { "version", kvm_cmd_version, NULL, 0 }, 30 { "--version", kvm_cmd_version, NULL, 0 }, 31 { "stop", kvm_cmd_stop, kvm_stop_help, 0 }, 32 { "stat", kvm_cmd_stat, kvm_stat_help, 0 }, 33 { "help", kvm_cmd_help, NULL, 0 }, 34 { "setup", kvm_cmd_setup, kvm_setup_help, 0 }, 35 { "run", kvm_cmd_run, kvm_run_help, 0 }, 36 { "sandbox", kvm_cmd_sandbox, kvm_run_help, 0 }, 37 { NULL, NULL, NULL, 0 }, 38 }; 39 40 /* 41 * kvm_get_command: Searches the command in an array of the commands and 42 * returns a pointer to cmd_struct if a match is found. 43 * 44 * Input parameters: 45 * command: Array of possible commands. The last entry in the array must be 46 * NULL. 47 * cmd: A string command to search in the array 48 * 49 * Return Value: 50 * NULL: If the cmd is not matched with any of the command in the command array 51 * p: Pointer to cmd_struct of the matching command 52 */ 53 struct cmd_struct *kvm_get_command(struct cmd_struct *command, 54 const char *cmd) 55 { 56 struct cmd_struct *p = command; 57 58 while (p->cmd) { 59 if (!strcmp(p->cmd, cmd)) 60 return p; 61 p++; 62 } 63 return NULL; 64 } 65 66 int handle_command(struct cmd_struct *command, int argc, const char **argv) 67 { 68 struct cmd_struct *p; 69 const char *prefix = NULL; 70 int ret = 0; 71 72 if (!argv || !*argv) { 73 p = kvm_get_command(command, "help"); 74 assert(p); 75 return p->fn(argc, argv, prefix); 76 } 77 78 p = kvm_get_command(command, argv[0]); 79 if (!p) { 80 p = kvm_get_command(command, "help"); 81 assert(p); 82 p->fn(0, NULL, prefix); 83 return EINVAL; 84 } 85 86 ret = p->fn(argc - 1, &argv[1], prefix); 87 if (ret < 0) { 88 if (errno == EPERM) 89 die("Permission error - are you root?"); 90 } 91 92 return ret; 93 } 94