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 static bool all;
13 static const char *instance_name;
14
15 static const char * const stop_usage[] = {
16 "lkvm stop [--all] [-n name]",
17 NULL
18 };
19
20 static const struct option stop_options[] = {
21 OPT_GROUP("General options:"),
22 OPT_BOOLEAN('a', "all", &all, "Stop all instances"),
23 OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
24 OPT_END()
25 };
26
parse_stop_options(int argc,const char ** argv)27 static void parse_stop_options(int argc, const char **argv)
28 {
29 while (argc != 0) {
30 argc = parse_options(argc, argv, stop_options, stop_usage,
31 PARSE_OPT_STOP_AT_NON_OPTION);
32 if (argc != 0)
33 kvm_stop_help();
34 }
35 }
36
kvm_stop_help(void)37 void kvm_stop_help(void)
38 {
39 usage_with_options(stop_usage, stop_options);
40 }
41
do_stop(const char * name,int sock)42 static int do_stop(const char *name, int sock)
43 {
44 return kvm_ipc__send(sock, KVM_IPC_STOP);
45 }
46
kvm_cmd_stop(int argc,const char ** argv,const char * prefix)47 int kvm_cmd_stop(int argc, const char **argv, const char *prefix)
48 {
49 int instance;
50 int r;
51
52 parse_stop_options(argc, argv);
53
54 if (all)
55 return kvm__enumerate_instances(do_stop);
56
57 if (instance_name == NULL)
58 kvm_stop_help();
59
60 instance = kvm__get_sock_by_instance(instance_name);
61
62 if (instance <= 0)
63 die("Failed locating instance");
64
65 r = do_stop(instance_name, instance);
66
67 close(instance);
68
69 return r;
70 }
71