xref: /kvmtool/builtin-pause.c (revision 5aa502e4b0d83393b72961715886f899f3894023)
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 	return kvm_ipc__send(sock, KVM_IPC_PAUSE);
45 }
46 
47 int kvm_cmd_pause(int argc, const char **argv, const char *prefix)
48 {
49 	int instance;
50 	int r;
51 
52 	parse_pause_options(argc, argv);
53 
54 	if (all)
55 		return kvm__enumerate_instances(do_pause);
56 
57 	if (instance_name == NULL)
58 		kvm_pause_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_pause(instance_name, instance);
66 
67 	close(instance);
68 
69 	return r;
70 }
71