xref: /kvmtool/builtin-pause.c (revision 2fa65af3160d7e4ad4603f0064411fd8ee784e45)
1 #include <kvm/util.h>
2 #include <kvm/kvm-cmd.h>
3 #include <kvm/builtin-pause.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 pause_usage[] = {
16 	"lkvm pause [--all] [-n name]",
17 	NULL
18 };
19 
20 static const struct option pause_options[] = {
21 	OPT_GROUP("General options:"),
22 	OPT_BOOLEAN('a', "all", &all, "Pause all instances"),
23 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
24 	OPT_END()
25 };
26 
27 static void parse_pause_options(int argc, const char **argv)
28 {
29 	while (argc != 0) {
30 		argc = parse_options(argc, argv, pause_options, pause_usage,
31 				PARSE_OPT_STOP_AT_NON_OPTION);
32 		if (argc != 0)
33 			kvm_pause_help();
34 	}
35 }
36 
37 void kvm_pause_help(void)
38 {
39 	usage_with_options(pause_usage, pause_options);
40 }
41 
42 static int do_pause(const char *name, int sock)
43 {
44 	int r;
45 
46 	r = kvm_ipc__send(sock, KVM_IPC_PAUSE);
47 	if (r)
48 		return r;
49 
50 	printf("Guest %s paused\n", name);
51 
52 	return 0;
53 }
54 
55 int kvm_cmd_pause(int argc, const char **argv, const char *prefix)
56 {
57 	int instance;
58 	int r;
59 
60 	parse_pause_options(argc, argv);
61 
62 	if (all)
63 		return kvm__enumerate_instances(do_pause);
64 
65 	if (instance_name == NULL)
66 		kvm_pause_help();
67 
68 	instance = kvm__get_sock_by_instance(instance_name);
69 
70 	if (instance <= 0)
71 		die("Failed locating instance");
72 
73 	r = do_pause(instance_name, instance);
74 
75 	close(instance);
76 
77 	return r;
78 }
79