xref: /kvmtool/builtin-pause.c (revision 0b235f6b5975c5a30ccbfaae30d6a49b4c2e05df)
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 const char *instance_name;
19 
20 static const char * const pause_usage[] = {
21 	"lkvm pause [--all] [-n name]",
22 	NULL
23 };
24 
25 static const struct option pause_options[] = {
26 	OPT_GROUP("General options:"),
27 	OPT_BOOLEAN('a', "all", &all, "Pause all instances"),
28 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
29 	OPT_END()
30 };
31 
32 static void parse_pause_options(int argc, const char **argv)
33 {
34 	while (argc != 0) {
35 		argc = parse_options(argc, argv, pause_options, pause_usage,
36 				PARSE_OPT_STOP_AT_NON_OPTION);
37 		if (argc != 0)
38 			kvm_pause_help();
39 	}
40 }
41 
42 void kvm_pause_help(void)
43 {
44 	usage_with_options(pause_usage, pause_options);
45 }
46 
47 static int do_pause(const char *name, int sock)
48 {
49 	struct pause_cmd cmd = {KVM_IPC_PAUSE, 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_pause(int argc, const char **argv, const char *prefix)
60 {
61 	int instance;
62 	int r;
63 
64 	parse_pause_options(argc, argv);
65 
66 	if (all)
67 		return kvm__enumerate_instances(do_pause);
68 
69 	if (instance_name == NULL)
70 		kvm_pause_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_pause(instance_name, instance);
78 
79 	close(instance);
80 
81 	return r;
82 }
83