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