END_TEST /* test auto complete init */ START_TEST (test_libvmi_init1) { vmi_instance_t vmi = NULL; status_t ret = vmi_init_complete(&vmi, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_GLOBAL_FILE_ENTRY, NULL, NULL); fail_unless(ret == VMI_SUCCESS, "vmi_init failed with VMI_INIT_DOMAINNAME and global config"); fail_unless(vmi != NULL, "vmi_init failed to initialize vmi instance struct"); vmi_destroy(vmi); }
END_TEST /* test partial init and init_complete function */ START_TEST (test_libvmi_init2) { vmi_instance_t vmi = NULL; status_t ret = vmi_init(&vmi, VMI_AUTO | VMI_INIT_PARTIAL, get_testvm()); fail_unless(ret == VMI_SUCCESS, "vmi_init failed with AUTO | PARTIAL"); fail_unless(vmi != NULL, "vmi_init failed to initialize vmi instance struct"); ret = vmi_init_complete(&vmi, NULL); fail_unless(ret == VMI_SUCCESS, "vmi_init_complete failed"); fail_unless(vmi != NULL, "vmi_init_complete failed to initialize vmi instance struct"); vmi_destroy(vmi); }
int main( int argc, char **argv) { if ( argc != 3 ) return 1; vmi_instance_t vmi; unsigned char *memory = malloc(PAGE_SIZE); /* this is the VM or file that we are looking at */ char *name = argv[1]; /* this is the address to map */ char *addr_str = argv[2]; addr_t addr = (addr_t) strtoul(addr_str, NULL, 16); /* initialize the libvmi library */ if (VMI_FAILURE == vmi_init_complete(&vmi, name, VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_GLOBAL_FILE_ENTRY, NULL, NULL)) { printf("Failed to init LibVMI library.\n"); goto error_exit; } /* get the symbol's memory page */ if (VMI_FAILURE == vmi_read_va(vmi, addr, 0, PAGE_SIZE, memory, NULL)) { printf("failed to map memory.\n"); goto error_exit; } vmi_print_hex(memory, PAGE_SIZE); error_exit: if (memory) free(memory); /* cleanup any memory associated with the libvmi instance */ vmi_destroy(vmi); return 0; }
END_TEST START_TEST (test_vmi_get_memsize_max_phys_addr) { vmi_instance_t vmi = NULL; uint64_t memsize = 0; addr_t max_physical_addr = 0; vmi_init_complete(&vmi, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_GLOBAL_FILE_ENTRY, NULL, NULL); memsize = vmi_get_memsize(vmi); max_physical_addr = vmi_get_max_physical_address(vmi); fail_unless(memsize > 0, "guest ram size is 0"); fail_unless(max_physical_addr > 0, "max physical address is 0"); fail_unless(max_physical_addr >= memsize, "max physical address is less than memsize"); vmi_destroy(vmi); }
END_TEST /* test init_complete with passed config */ START_TEST (test_libvmi_init3) { FILE *f = NULL; const char *ptr = NULL; char location[100]; const char *sudo_user = NULL; struct passwd *pw_entry = NULL; vmi_instance_t vmi = NULL; /* read the config entry from the config file */ /* first check home directory of sudo user */ if ((sudo_user = getenv("SUDO_USER")) != NULL) { if ((pw_entry = getpwnam(sudo_user)) != NULL) { snprintf(location, 100, "%s/etc/libvmi.conf\0", pw_entry->pw_dir); if ((f = fopen(location, "r")) != NULL) { goto success; } } } /* next check home directory for current user */ snprintf(location, 100, "%s/etc/libvmi.conf\0", getenv("HOME")); if ((f = fopen(location, "r")) != NULL) { goto success; } /* finally check in /etc */ snprintf(location, 100, "/etc/libvmi.conf\0"); if ((f = fopen(location, "r")) != NULL) { goto success; } fail_unless(0, "failed to find config file"); success: /* strip path for memory image files */ if ((ptr = strrchr(get_testvm(), '/')) == NULL) { ptr = get_testvm(); } else { ptr++; } /* check file size */ fseek(f, 0L, SEEK_END); long sz = ftell(f); fseek(f, 0L, SEEK_SET); /* read entry in from file */ char *buf = malloc(sz); fread(buf, sz, 1, f); long pos = 0; size_t max_len = strnlen(ptr, 100); int found = 0; for (pos = 0; pos < sz; ++pos) { if (strncmp(buf + pos, ptr, max_len) == 0) { found = 1; break; } } if (!found) { fail_unless(0, "failed to find config entry"); } long start = pos + max_len; found = 0; for ( ; pos < sz; ++pos) { if (buf[pos] == '}') { found = 1; break; } } if (!found) { fail_unless(0, "failed to find end of config entry"); } long end = pos + 1; long entry_length = end - start; char *config = malloc(entry_length); memcpy(config, buf + start, entry_length); free(buf); status_t ret = vmi_init_complete(&vmi, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_STRING, (void*)config, NULL); free(config); fail_unless(ret == VMI_SUCCESS, "vmi_init_complete failed"); fail_unless(vmi != NULL, "vmi_init_complete failed to initialize vmi instance struct"); vmi_destroy(vmi); }
int main (int argc, char **argv) { /* this is the VM or file that we are looking at */ if (argc != 2) { printf("Usage: %s <vmname>\n", argv[0]); return 1; } #if ENABLE_SHM_SNAPSHOT == 1 vmi_instance_t vmi; addr_t list_head = 0, current_list_entry = 0, next_list_entry = 0; addr_t current_process = 0; char *procname = NULL; vmi_pid_t pid = 0; unsigned long tasks_offset, pid_offset, name_offset; char *name = argv[1]; /* initialize the libvmi library */ if (VMI_FAILURE == vmi_init_complete(&vmi, name, VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_GLOBAL_FILE_ENTRY, NULL, NULL)) { printf("Failed to init LibVMI library.\n"); return 1; } /* init the offset values */ if (VMI_OS_LINUX == vmi_get_ostype(vmi)) { if ( VMI_FAILURE == vmi_get_offset(vmi, "linux_tasks", &tasks_offset) ) goto error_exit; if ( VMI_FAILURE == vmi_get_offset(vmi, "linux_name", &name_offset) ) goto error_exit; if ( VMI_FAILURE == vmi_get_offset(vmi, "linux_pid", &pid_offset) ) goto error_exit; /* NOTE: * name_offset is no longer hard-coded. Rather, it is now set * via libvmi.conf. */ } else if (VMI_OS_WINDOWS == vmi_get_ostype(vmi)) { if ( VMI_FAILURE == vmi_get_offset(vmi, "win_tasks", &tasks_offset) ) goto error_exit; if ( VMI_FAILURE == vmi_get_offset(vmi, "win_pname", &name_offset) ) goto error_exit; if ( VMI_FAILURE == vmi_get_offset(vmi, "win_pid", &pid_offset) ) goto error_exit; } /* create a shm-snapshot */ if (vmi_shm_snapshot_create(vmi) != VMI_SUCCESS) { printf("Failed to shm-snapshot VM\n"); goto error_exit; } /* demonstrate name and id accessors */ list_processes(vmi, current_process, list_head, tasks_offset, current_list_entry, next_list_entry, pid_offset, pid, procname, name_offset); error_exit: if (procname) free(procname); /* destroy the shm-snapshot, and return live mode */ vmi_shm_snapshot_destroy(vmi); /* cleanup any memory associated with the LibVMI instance */ vmi_destroy(vmi); return 0; #else printf("Error : this example should only run after ./configure --enable-shm-snapshot.\n"); return 1; // error #endif }