Add linux-next specific files for 20110831
[linux-2.6/next.git] / tools / kvm / builtin-list.c
blobfcf9bb0ec29d2b3dee311e644cdd3feec8ba8360
1 #include <kvm/util.h>
2 #include <kvm/kvm-cmd.h>
3 #include <kvm/builtin-list.h>
4 #include <kvm/kvm.h>
5 #include <kvm/parse-options.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <signal.h>
10 #include <fcntl.h>
12 #define PROCESS_NAME "kvm"
14 static const char * const list_usage[] = {
15 "kvm list",
16 NULL
19 static const struct option list_options[] = {
20 OPT_END()
23 void kvm_list_help(void)
25 usage_with_options(list_usage, list_options);
28 static int print_guest(const char *name, int pid)
30 char proc_name[PATH_MAX];
31 char *comm = NULL;
32 FILE *fd;
34 sprintf(proc_name, "/proc/%d/stat", pid);
35 fd = fopen(proc_name, "r");
36 if (fd == NULL)
37 goto cleanup;
38 if (fscanf(fd, "%*u (%as)", &comm) == 0)
39 goto cleanup;
40 if (strncmp(comm, PROCESS_NAME, strlen(PROCESS_NAME)))
41 goto cleanup;
43 printf("%5d %s\n", pid, name);
45 free(comm);
47 fclose(fd);
49 return 0;
51 cleanup:
52 if (fd)
53 fclose(fd);
54 if (comm)
55 free(comm);
57 kvm__remove_pidfile(name);
58 return 0;
61 int kvm_cmd_list(int argc, const char **argv, const char *prefix)
63 printf(" PID GUEST\n");
65 return kvm__enumerate_instances(print_guest);