/* main program */ int main(int argc, char **argv) { char *dir; setlocale(LC_ALL, ""); if((dir = argv_parse(argc, argv)) == NULL) dir = "."; calc_init(dir, NULL); initscr(); cbreak(); noecho(); curs_set(0); keypad(stdscr, TRUE); if(ncresize(min_rows, min_cols)) min_rows = min_cols = 0; while(1) { if(pstate == ST_CALC && calc_process()) break; else if(pstate == ST_DEL) delete_process(); else if(input_handle(0)) break; } erase(); refresh(); endwin(); exclude_clear(); return 0; }
/* * check_wait_status: This waits on child processes, reporting terminations * as needed, etc */ int check_wait_status(int wanted) { Process *proc; int status; int pid, i; if ((pid = waitpid(wanted, &status, WNOHANG)) > 0) { if (wanted != -1 && pid == wanted) { if (WIFEXITED(status)) return WEXITSTATUS(status); if (WIFSTOPPED(status)) return -(WSTOPSIG(status)); if (WIFSIGNALED(status)) return -(WTERMSIG(status)); } errno = 0; /* reset errno, cause wait3 changes it */ for (i = 0; i < process_list_size; i++) { if ((proc = process_list[i]) && proc->pid == pid) { proc->exited = 1; proc->termsig = WTERMSIG(status); proc->retcode = WEXITSTATUS(status); if ((proc->p_stderr == -1) && (proc->p_stdout == -1)) delete_process(i); return 0; } } } return -1; }
void *tarefa_monitora(){ if(__DEBUG__){ printf("\e[36m[ DEBUG ]\e[0m Estamos na tarefa_monitora %d\n", (int) pthread_self() ); } int status; while(1){ /* Esperar que existam filhos em execucao */ sem_wait(&filhos_em_execucao); pthread_mutex_lock(&children_mutex); if(numChildren > 0) { pthread_mutex_unlock(&children_mutex); // aguarda pela terminacao dos processos filhos pid_t ret = wait(&status); /*Assinalar que existe menos um filho em execucao*/ sem_post(&lim_processos); if(__DEBUG__) printf("\e[36m[ DEBUG ]\e[0m Process %d finished\n", (int) ret ); // regista o pid do processo acabado de terminar e o respectivo return status na lista if(WIFEXITED(status)){ //atulizacao do tempo de fim do processo pthread_mutex_lock(&lista_mutex); update_terminated_process(lista_processos, ret, time(NULL), WEXITSTATUS(status)); pthread_mutex_unlock(&lista_mutex); }else{ //Eliminacao da lista de um processo no qual ocorreu um erro (ex. seg fault) pthread_mutex_lock(&lista_mutex); delete_process(lista_processos, ret); pthread_mutex_unlock(&lista_mutex); printf("\e[31m[ ERROR ]\e[0m Process %d terminated Abruptly\n", ret ); } pthread_mutex_lock(&children_mutex); numChildren--; pthread_mutex_unlock(&children_mutex); }else{ if(_exit_ctrl){
/* * check_process_limits: checks each running process to see if it's reached * the user selected maximum number of output lines. If so, the processes is * effectively killed */ void check_process_limits(void) { int limit; int i; Process *proc; if ((limit = get_int_var(SHELL_LIMIT_VAR)) && process_list) { for (i = 0; i < process_list_size; i++) { if ((proc = process_list[i]) != NULL) { if (proc->counter >= limit) { proc->p_stdin = exec_close(proc->p_stdin); proc->p_stdout = exec_close(proc->p_stdout); proc->p_stderr = exec_close(proc->p_stderr); if (proc->exited) delete_process(i); } } } } }
/* * do_processes: given a set of read-descriptors (as returned by a call to * select()), this will determine which of the process has produced output * and will hadle it appropriately */ void do_processes(fd_set * rd) { int i, flag; char exec_buffer[INPUT_BUFFER_SIZE + 1]; char *ptr; Process *proc; int old_timeout; if (process_list == NULL) return; old_timeout = dgets_timeout(1); for (i = 0; i < process_list_size; i++) { if ((proc = process_list[i]) && proc->p_stdout != -1) { if (FD_ISSET(proc->p_stdout, rd)) { /* * This switch case indented back to allow for 80 columns, * phone, jan 1993. */ switch (dgets(exec_buffer, INPUT_BUFFER_SIZE, proc->p_stdout, NULL)) { case 0: if (proc->p_stderr == -1) { proc->p_stdin = exec_close(proc->p_stdin); proc->p_stdout = exec_close(proc->p_stdout); if (proc->exited) delete_process(i); } else proc->p_stdout = exec_close(proc->p_stdout); break; case -1: if (proc->logical) flag = do_hook(EXEC_PROMPT_LIST, "%s %s", proc->logical, exec_buffer); else flag = do_hook(EXEC_PROMPT_LIST, "%d %s", i, exec_buffer); set_prompt_by_refnum(proc->refnum, exec_buffer); update_input(UPDATE_ALL); /* if (flag == 0) */ break; default: message_to(proc->refnum); proc->counter++; ptr = exec_buffer + strlen(exec_buffer) - 1; if ((*ptr == '\n') || (*ptr == '\r')) { *ptr = (char) 0; ptr = exec_buffer + strlen(exec_buffer) - 1; if ((*ptr == '\n') || (*ptr == '\r')) *ptr = (char) 0; } if (proc->logical) flag = do_hook(EXEC_LIST, "%s %s", proc->logical, exec_buffer); else flag = do_hook(EXEC_LIST, "%d %s", i, exec_buffer); if (flag) { if (proc->redirect) { int server; server = from_server; from_server = proc->server; send_text(proc->who, stripansicodes(exec_buffer), proc->redirect, 1, 1); from_server = server; } else put_it("%s", stripansicodes(exec_buffer)); } message_to(0); break; } /* end of special intendation */ } } if (process_list && i < process_list_size && (proc = process_list[i]) && proc->p_stderr != -1) { if (FD_ISSET(proc->p_stderr, rd)) { /* Do the intendation on this switch as well */ switch (dgets(exec_buffer, INPUT_BUFFER_SIZE, proc->p_stderr, NULL)) { case 0: if (proc->p_stdout == -1) { proc->p_stderr = exec_close(proc->p_stderr); proc->p_stdin = exec_close(proc->p_stdin); if (proc->exited) delete_process(i); } else proc->p_stderr = exec_close(proc->p_stderr); break; case -1: if (proc->logical) flag = do_hook(EXEC_PROMPT_LIST, "%s %s", proc->logical, exec_buffer); else flag = do_hook(EXEC_PROMPT_LIST, "%d %s", i, exec_buffer); set_prompt_by_refnum(proc->refnum, exec_buffer); update_input(UPDATE_ALL); if (flag == 0) break; default: message_to(proc->refnum); (proc->counter)++; ptr = exec_buffer + strlen(exec_buffer) - 1; if ((*ptr == '\n') || (*ptr == '\r')) { *ptr = (char) 0; ptr = exec_buffer + strlen(exec_buffer) - 1; if ((*ptr == '\n') || (*ptr == '\r')) *ptr = (char) 0; } if (proc->logical) flag = do_hook(EXEC_ERRORS_LIST, "%s %s", proc->logical, exec_buffer); else flag = do_hook(EXEC_ERRORS_LIST, "%d %s", i, exec_buffer); if (flag) { if (proc->redirect) { int server; server = from_server; from_server = proc->server; send_text(proc->who, stripansicodes(exec_buffer), process_list[i]->redirect, 1, 1); from_server = server; } else put_it("%s", stripansicodes(exec_buffer)); } message_to(0); break; } /* End of indentation for 80 columns */ } } } check_process_limits(); (void) dgets_timeout(old_timeout); }