/* * This function checks if the state of a process is correct and if * the process is on ready queue or not. * This function works properly only when the double link lists * are created correcty, i.e. a traversal that start with the first process * will eventurally return to the first process. */ void check_process(char* name, int state, BOOL should_be_on_ready_queue) { //find the PCB entry for the process PROCESS this_process = find_process_by_name(name); //if the process is not in the pcb array, it is incorrect. if (this_process == NULL) { test_result = 10; return; } //check the process's state if (this_process->state != state) { test_result = 13; return; } BOOL on = is_on_ready_queue(this_process); //If should_be_on_ready_queue, the process should be on ready queue and //state should be STATE_READY. //Otherwise, the process should not be on the ready queue. (state can //be STATE_READY or other states.) if ( should_be_on_ready_queue == 0 && on == TRUE) { test_result = 14; } else if ( should_be_on_ready_queue == 1 && on == FALSE) { test_result = 15; } else if ( on == TRUE && state != STATE_READY) { test_result = 16; } }
addr_t windows_find_eprocess( vmi_instance_t vmi, char *name) { addr_t start_address = 0; check_magic_func check = get_check_magic_func(vmi); if (vmi->os.windows_instance.pname_offset == 0) { vmi->os.windows_instance.pname_offset = find_pname_offset(vmi, check); if (vmi->os.windows_instance.pname_offset == 0) { dbprint("--failed to find pname_offset\n"); return 0; } else { dbprint("**set os.windows_instance.pname_offset (0x%x)\n", vmi->os.windows_instance.pname_offset); } } if (vmi->init_task) { start_address = vmi->init_task - vmi->os.windows_instance.tasks_offset; } return find_process_by_name(vmi, check, start_address, name); }
/* * Checks if a port is correct. * Parameters: * the_port -- the port to be check * owner_name -- the name of the the owner of the port * should_be_open -- whether the port is open */ void check_port(PORT the_port, char* owner_name, BOOL should_be_open) { //find the owner process PROCESS owner = find_process_by_name(owner_name); //check the port entry if (the_port -> magic != MAGIC_PORT || the_port -> used != TRUE || the_port -> owner != owner ) { test_result = 31; return; } if (the_port -> open == TRUE && should_be_open == FALSE) { test_result = 34; return; } if (the_port -> open == FALSE && should_be_open == TRUE) { test_result = 33; return; } //check if the port is in the owner's port list BOOL found = FALSE; PORT temp = owner -> first_port; while (temp != NULL) { if (temp == the_port) { found = TRUE; break; } temp = temp -> next; } if (!found ) test_result = 32; }
/* * This function is used to check if a process is created correctly. * It checks for the PCB entry and process stack */ void check_create_process(char* name, int priority, void (*entry_point) (PROCESS, PARAM), PARAM param) { //find the PCB entry for the process PROCESS this_process = find_process_by_name(name); //if the process is not in the pcb array, it is incorrect. if (this_process == NULL) { test_result = 10; return; } //check if the PCB entry is initialized correctly if (this_process-> magic != MAGIC_PCB || this_process -> used != TRUE || this_process -> state != STATE_READY || this_process-> priority != priority ) { test_result = 11; return; } //check new process's stack if(string_compare(name, boot_name)) { //no need to check boot process's stack return; } unsigned stack_pointer = this_process->esp; if (peek_l(stack_pointer + 28 ) != (LONG) entry_point ) { test_result = 12; return; } }
addr_t windows_find_eprocess( vmi_instance_t vmi, const char *name) { addr_t start_address = 0; windows_instance_t windows = vmi->os_data; check_magic_func check = get_check_magic_func(vmi); if (windows == NULL) { return 0; } if (!windows->pname_offset) { if(windows->rekall_profile) { if ( VMI_FAILURE == rekall_profile_symbol_to_rva(windows->rekall_profile, "_EPROCESS", "ImageFileName", &windows->pname_offset) ) return 0; } else { windows->pname_offset = find_pname_offset(vmi, check); } if (!windows->pname_offset) { dbprint(VMI_DEBUG_MISC, "--failed to find pname_offset\n"); return 0; } else { dbprint(VMI_DEBUG_MISC, "**set os.windows_instance.pname_offset (0x%"PRIx64")\n", windows->pname_offset); } } if (vmi->init_task) { start_address = vmi->init_task; } return find_process_by_name(vmi, check, start_address, name); }
void test_isr_3 () { test_reset(); check_sum = 0; int check_2 = 0; kprintf("=== test_isr_3 === \n"); kprintf("This test will take a while.\n\n"); init_interrupts(); create_process(isr_process, 5, 0, "ISR process"); resign(); int i; int j = 0; unsigned char* screen_base; screen_base = (unsigned char*) 0xb8000 + 7 * 80 * 2; kprintf("\n\nBoot process:\n"); kprintf("ABCDEF"); PROCESS isr_pro = find_process_by_name("ISR process"); for (i = 0; i < 600000; i++) { if (isr_pro->state == STATE_INTR_BLOCKED) check_2++; *(screen_base + j * 2) = *(screen_base + j * 2) + 1; j++; if (j == 6) j = 0; } if (check_2 == 0) test_failed(72); if (check_sum <= 1) test_failed(73); }
int main(int argc, char **argv) { //argument variables const char *exe = NULL; int perclimit = 0; int exe_ok = 0; int pid_ok = 0; int limit_ok = 0; pid_t pid = 0; int include_children = 0; //get program name char *p = (char*)memrchr(argv[0], (unsigned int)'/', strlen(argv[0])); program_name = p==NULL ? argv[0] : (p+1); //get current pid cpulimit_pid = getpid(); //get cpu count NCPU = get_ncpu(); //parse arguments int next_option; int option_index = 0; //A string listing valid short options letters const char* short_options = "+p:e:l:vzih"; //An array describing valid long options const struct option long_options[] = { { "pid", required_argument, NULL, 'p' }, { "exe", required_argument, NULL, 'e' }, { "limit", required_argument, NULL, 'l' }, { "verbose", no_argument, NULL, 'v' }, { "lazy", no_argument, NULL, 'z' }, { "include-children", no_argument, NULL, 'i' }, { "help", no_argument, NULL, 'h' }, { 0, 0, 0, 0 } }; do { next_option = getopt_long(argc, argv, short_options,long_options, &option_index); switch(next_option) { case 'p': pid = atoi(optarg); pid_ok = 1; break; case 'e': exe = optarg; exe_ok = 1; break; case 'l': perclimit = atoi(optarg); limit_ok = 1; break; case 'v': verbose = 1; break; case 'z': lazy = 1; break; case 'i': include_children = 1; break; case 'h': print_usage(stdout, 1); break; case '?': print_usage(stderr, 1); break; case -1: break; default: abort(); } } while(next_option != -1); if (pid_ok && (pid <= 1 || pid >= get_pid_max())) { fprintf(stderr,"Error: Invalid value for argument PID\n"); print_usage(stderr, 1); exit(1); } if (pid != 0) { lazy = 1; } if (!limit_ok) { fprintf(stderr,"Error: You must specify a cpu limit percentage\n"); print_usage(stderr, 1); exit(1); } double limit = perclimit / 100.0; if (limit<0 || limit >NCPU) { fprintf(stderr,"Error: limit must be in the range 0-%d00\n", NCPU); print_usage(stderr, 1); exit(1); } int command_mode = optind < argc; if (exe_ok + pid_ok + command_mode == 0) { fprintf(stderr,"Error: You must specify one target process, either by name, pid, or command line\n"); print_usage(stderr, 1); exit(1); } if (exe_ok + pid_ok + command_mode > 1) { fprintf(stderr,"Error: You must specify exactly one target process, either by name, pid, or command line\n"); print_usage(stderr, 1); exit(1); } //all arguments are ok! signal(SIGINT, quit); signal(SIGTERM, quit); //print the number of available cpu if (verbose) printf("%d cpu detected\n", NCPU); if (command_mode) { int i; //executable file const char *cmd = argv[optind]; //command line arguments char **cmd_args = (char**)malloc((argc-optind + 1) * sizeof(char*)); if (cmd_args==NULL) exit(2); for (i=0; i<argc-optind; i++) { cmd_args[i] = argv[i+optind]; } cmd_args[i] = NULL; if (verbose) { printf("Running command: '%s", cmd); for (i=1; i<argc-optind; i++) { printf(" %s", cmd_args[i]); } printf("'\n"); } int child = fork(); if (child < 0) { exit(EXIT_FAILURE); } else if (child == 0) { //target process code int ret = execvp(cmd, cmd_args); //if we are here there was an error, show it perror("Error"); exit(ret); } else { //parent code free(cmd_args); int limiter = fork(); if (limiter < 0) { exit(EXIT_FAILURE); } else if (limiter > 0) { //parent int status_process; int status_limiter; waitpid(child, &status_process, 0); waitpid(limiter, &status_limiter, 0); if (WIFEXITED(status_process)) { if (verbose) printf("Process %d terminated with exit status %d\n", child, (int)WEXITSTATUS(status_process)); exit(WEXITSTATUS(status_process)); } printf("Process %d terminated abnormally\n", child); exit(status_process); } else { //limiter code if (verbose) printf("Limiting process %d\n",child); limit_process(child, limit, include_children); exit(0); } } } while(1) { //look for the target process..or wait for it pid_t ret = 0; if (pid_ok) { //search by pid ret = find_process_by_pid(pid); if (ret == 0) { printf("No process found\n"); } else if (ret < 0) { printf("Process found but you aren't allowed to control it\n"); } } else { //search by file or path name ret = find_process_by_name(exe); if (ret == 0) { printf("No process found\n"); } else if (ret < 0) { printf("Process found but you aren't allowed to control it\n"); } else { pid = ret; } } if (ret > 0) { if (ret == cpulimit_pid) { printf("Target process %d is cpulimit itself! Aborting because it makes no sense\n", ret); exit(1); } printf("Process %d found\n", pid); //control limit_process(pid, limit, include_children); } if (lazy) break; sleep(2); }; exit(0); }