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> 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <signal.h> 10 #include <fcntl.h> 11 12 #define PROCESS_NAME "kvm" 13 14 static const char * const list_usage[] = { 15 "kvm list", 16 NULL 17 }; 18 19 static const struct option list_options[] = { 20 OPT_END() 21 }; 22 23 void kvm_list_help(void) 24 { 25 usage_with_options(list_usage, list_options); 26 } 27 28 static int print_guest(const char *name, int pid) 29 { 30 char proc_name[PATH_MAX]; 31 char *comm = NULL; 32 FILE *fd; 33 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; 42 43 printf("%5d %s\n", pid, name); 44 45 free(comm); 46 47 fclose(fd); 48 49 return 0; 50 51 cleanup: 52 if (fd) 53 fclose(fd); 54 if (comm) 55 free(comm); 56 57 kvm__remove_pidfile(name); 58 return 0; 59 } 60 61 int kvm_cmd_list(int argc, const char **argv, const char *prefix) 62 { 63 printf(" PID GUEST\n"); 64 65 return kvm__enumerate_instances(print_guest); 66 } 67