xref: /kvmtool/builtin-list.c (revision ebc49f06b0078f3a0e09a06bc23a83ce0e532455)
1 #include <kvm/util.h>
2 #include <kvm/kvm-cmd.h>
3 #include <kvm/builtin-list.h>
4 #include <kvm/kvm.h>
5 
6 #include <stdio.h>
7 #include <string.h>
8 #include <signal.h>
9 #include <fcntl.h>
10 
11 #define PROCESS_NAME "kvm"
12 
13 static void print_guest(const char *name, int pid)
14 {
15 	char proc_name[PATH_MAX];
16 	char *comm = NULL;
17 	FILE *fd;
18 
19 	sprintf(proc_name, "/proc/%d/stat", pid);
20 	fd = fopen(proc_name, "r");
21 	if (fd == NULL)
22 		goto cleanup;
23 	if (fscanf(fd, "%*u (%as)", &comm) == 0)
24 		goto cleanup;
25 	if (strncmp(comm, PROCESS_NAME, strlen(PROCESS_NAME)))
26 		goto cleanup;
27 
28 	printf("%s (PID: %d)\n", name, pid);
29 
30 	free(comm);
31 
32 	fclose(fd);
33 
34 	return;
35 
36 cleanup:
37 	if (fd)
38 		fclose(fd);
39 	if (comm)
40 		free(comm);
41 
42 	kvm__remove_pidfile(name);
43 }
44 
45 int kvm_cmd_list(int argc, const char **argv, const char *prefix)
46 {
47 	kvm__enumerate_instances(print_guest);
48 
49 	return 0;
50 }
51