1 #include <kvm/util.h> 2 #include <kvm/kvm-cmd.h> 3 #include <kvm/builtin-stop.h> 4 #include <kvm/kvm.h> 5 #include <kvm/parse-options.h> 6 #include <kvm/kvm-ipc.h> 7 8 #include <stdio.h> 9 #include <string.h> 10 #include <signal.h> 11 12 struct stop_cmd { 13 u32 type; 14 u32 len; 15 }; 16 17 static bool all; 18 static const char *instance_name; 19 20 static const char * const stop_usage[] = { 21 "lkvm stop [--all] [-n name]", 22 NULL 23 }; 24 25 static const struct option stop_options[] = { 26 OPT_GROUP("General options:"), 27 OPT_BOOLEAN('a', "all", &all, "Stop all instances"), 28 OPT_STRING('n', "name", &instance_name, "name", "Instance name"), 29 OPT_END() 30 }; 31 32 static void parse_stop_options(int argc, const char **argv) 33 { 34 while (argc != 0) { 35 argc = parse_options(argc, argv, stop_options, stop_usage, 36 PARSE_OPT_STOP_AT_NON_OPTION); 37 if (argc != 0) 38 kvm_stop_help(); 39 } 40 } 41 42 void kvm_stop_help(void) 43 { 44 usage_with_options(stop_usage, stop_options); 45 } 46 47 static int do_stop(const char *name, int sock) 48 { 49 struct stop_cmd cmd = {KVM_IPC_STOP, 0}; 50 int r; 51 52 r = write(sock, &cmd, sizeof(cmd)); 53 if (r < 0) 54 return r; 55 56 return 0; 57 } 58 59 int kvm_cmd_stop(int argc, const char **argv, const char *prefix) 60 { 61 int instance; 62 int r; 63 64 parse_stop_options(argc, argv); 65 66 if (all) 67 return kvm__enumerate_instances(do_stop); 68 69 if (instance_name == NULL) 70 kvm_stop_help(); 71 72 instance = kvm__get_sock_by_instance(instance_name); 73 74 if (instance <= 0) 75 die("Failed locating instance"); 76 77 r = do_stop(instance_name, instance); 78 79 close(instance); 80 81 return r; 82 } 83