forked from ventanamicro/kvmtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin-list.c
155 lines (119 loc) · 2.67 KB
/
builtin-list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <kvm/util.h>
#include <kvm/kvm-cmd.h>
#include <kvm/builtin-list.h>
#include <kvm/kvm.h>
#include <kvm/parse-options.h>
#include <kvm/kvm-ipc.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
static bool run;
static bool rootfs;
static const char * const list_usage[] = {
"lkvm list",
NULL
};
static const struct option list_options[] = {
OPT_GROUP("General options:"),
OPT_BOOLEAN('i', "run", &run, "List running instances"),
OPT_BOOLEAN('r', "rootfs", &rootfs, "List rootfs instances"),
OPT_END()
};
#define KVM_INSTANCE_RUNNING "running"
#define KVM_INSTANCE_PAUSED "paused"
#define KVM_INSTANCE_SHUTOFF "shut off"
void kvm_list_help(void)
{
usage_with_options(list_usage, list_options);
}
static pid_t get_pid(int sock)
{
pid_t pid;
int r;
r = kvm_ipc__send(sock, KVM_IPC_PID);
if (r < 0)
return r;
r = read(sock, &pid, sizeof(pid));
if (r < 0)
return r;
return pid;
}
int get_vmstate(int sock)
{
int vmstate;
int r;
r = kvm_ipc__send(sock, KVM_IPC_VMSTATE);
if (r < 0)
return r;
r = read(sock, &vmstate, sizeof(vmstate));
if (r < 0)
return r;
return vmstate;
}
static int print_guest(const char *name, int sock)
{
pid_t pid;
int vmstate;
pid = get_pid(sock);
vmstate = get_vmstate(sock);
if ((int)pid < 0 || vmstate < 0)
return -1;
if (vmstate == KVM_VMSTATE_PAUSED)
printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_PAUSED);
else
printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_RUNNING);
return 0;
}
static int kvm_list_running_instances(void)
{
return kvm__enumerate_instances(print_guest);
}
static int kvm_list_rootfs(void)
{
DIR *dir;
struct dirent *dirent;
dir = opendir(kvm__get_dir());
if (dir == NULL)
return -1;
while ((dirent = readdir(dir))) {
if (dirent->d_type == DT_DIR &&
strcmp(dirent->d_name, ".") &&
strcmp(dirent->d_name, ".."))
printf("%5s %-20s %s\n", "", dirent->d_name, KVM_INSTANCE_SHUTOFF);
}
return 0;
}
static void parse_setup_options(int argc, const char **argv)
{
while (argc != 0) {
argc = parse_options(argc, argv, list_options, list_usage,
PARSE_OPT_STOP_AT_NON_OPTION);
if (argc != 0)
kvm_list_help();
}
}
int kvm_cmd_list(int argc, const char **argv, const char *prefix)
{
int status, r;
parse_setup_options(argc, argv);
if (!run && !rootfs)
run = rootfs = true;
printf("%6s %-20s %s\n", "PID", "NAME", "STATE");
printf("------------------------------------\n");
status = 0;
if (run) {
r = kvm_list_running_instances();
if (r < 0)
perror("Error listing instances");
status |= r;
}
if (rootfs) {
r = kvm_list_rootfs();
if (r < 0)
perror("Error listing rootfs");
status |= r;
}
return status;
}