xref: /kvmtool/builtin-pause.c (revision 08ffc82ad59c994da383e71368e17a5833e4ba15)
1 #include <kvm/util.h>
2 #include <kvm/kvm-cmd.h>
3 #include <kvm/builtin-pause.h>
4 #include <kvm/builtin-list.h>
5 #include <kvm/kvm.h>
6 #include <kvm/parse-options.h>
7 #include <kvm/kvm-ipc.h>
8 
9 #include <stdio.h>
10 #include <string.h>
11 #include <signal.h>
12 
13 static bool all;
14 static const char *instance_name;
15 
16 static const char * const pause_usage[] = {
17 	"lkvm pause [--all] [-n name]",
18 	NULL
19 };
20 
21 static const struct option pause_options[] = {
22 	OPT_GROUP("General options:"),
23 	OPT_BOOLEAN('a', "all", &all, "Pause all instances"),
24 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
25 	OPT_END()
26 };
27 
parse_pause_options(int argc,const char ** argv)28 static void parse_pause_options(int argc, const char **argv)
29 {
30 	while (argc != 0) {
31 		argc = parse_options(argc, argv, pause_options, pause_usage,
32 				PARSE_OPT_STOP_AT_NON_OPTION);
33 		if (argc != 0)
34 			kvm_pause_help();
35 	}
36 }
37 
kvm_pause_help(void)38 void kvm_pause_help(void)
39 {
40 	usage_with_options(pause_usage, pause_options);
41 }
42 
do_pause(const char * name,int sock)43 static int do_pause(const char *name, int sock)
44 {
45 	int r;
46 	int vmstate;
47 
48 	vmstate = get_vmstate(sock);
49 	if (vmstate < 0)
50 		return vmstate;
51 	if (vmstate == KVM_VMSTATE_PAUSED) {
52 		printf("Guest %s is already paused.\n", name);
53 		return 0;
54 	}
55 
56 	r = kvm_ipc__send(sock, KVM_IPC_PAUSE);
57 	if (r)
58 		return r;
59 
60 	printf("Guest %s paused\n", name);
61 
62 	return 0;
63 }
64 
kvm_cmd_pause(int argc,const char ** argv,const char * prefix)65 int kvm_cmd_pause(int argc, const char **argv, const char *prefix)
66 {
67 	int instance;
68 	int r;
69 
70 	parse_pause_options(argc, argv);
71 
72 	if (all)
73 		return kvm__enumerate_instances(do_pause);
74 
75 	if (instance_name == NULL)
76 		kvm_pause_help();
77 
78 	instance = kvm__get_sock_by_instance(instance_name);
79 
80 	if (instance <= 0)
81 		die("Failed locating instance");
82 
83 	r = do_pause(instance_name, instance);
84 
85 	close(instance);
86 
87 	return r;
88 }
89