void btpd_connect(void) { if ((errno = ipc_open(btpd_dir, &ipc)) != 0) diemsg("cannot open connection to btpd in %s (%s).\n", btpd_dir, strerror(errno)); }
void init() { int i, j, k, pid, aux_semid; int string_length; int to_exec_length; char * to_exec; int ct_length; char * ct_path = get_ipc_path(); ct_length = strlen(ct_path); ipc_create(server_params); ipc_open(server_params, O_RDONLY); aux_semid = init_sem(); for(i = 0; i < CANT_INSTRUCTIONS; i++){ switch(pid = fork()){ case -1: perror("Fork exception"); exit(1); break; case 0: /* Hijo */ (*(process_list[i]))->pid = getpid(); to_exec_length = strlen(process_name_list[i]) + ct_length + 1; to_exec = calloc(1, to_exec_length); strcpy(to_exec, ct_path); strcat(to_exec, process_name_list[i]); to_exec[to_exec_length - 1] = 0; execvp(to_exec, NULL); /* No deberia llegar aca */ perror("Process error"); exit(1); break; } (*(process_list[i]))->params->unique_id = pid; (*process_list[i])->params->socklistener = TRUE; ipc_create((*process_list[i])->params); ipc_open((*(process_list[i]))->params, O_WRONLY); } free(ct_path); }
void init() { int i, j, k, pid; int string_length; int to_exec_length; char * to_exec; char * ct_bin = "bin/"; ipc_destroy(server_params); ipc_create(server_params); ipc_open(server_params, O_RDONLY); ipc_destroy(server_receive_params); ipc_create(server_receive_params); ipc_open(server_receive_params, O_RDONLY|O_NONBLOCK); //sem_init(&sem,0,0); for(i = 0; i < CANT_INSTRUCTIONS; i++) { switch(pid = fork()) { case -1: perror("Fork exception"); exit(1); break; case 0: /* Hijo */ (*(process_list[i]))->pid = getpid(); to_exec_length = strlen(process_name_list[i]) + BIN_LENGTH + 1; to_exec = calloc(1, to_exec_length); strcpy(to_exec, ct_bin); strcat(to_exec, process_name_list[i]); to_exec[to_exec_length - 1] = 0; execvp(to_exec, NULL); /* No deberia llegar aca */ perror("Process error"); exit(1); break; } (*(process_list[i]))->params->unique_id = pid; ipc_destroy((*process_list[i])->params); ipc_create((*process_list[i])->params); ipc_open((*(process_list[i]))->params, O_WRONLY); } }
int main(int argc, char ** argv){ char * program_name; struct status cl_program; int i; ipc_params_t client_params; init_processes(); if(argc > 1){ program_name = argv[1]; }else{ printf("Entrada incorrecta\n"); return 0; } client_params = get_params_from_pid(getpid(), PROGRAM_STATUS, sizeof(struct status)); client_header_t header = calloc(1, sizeof(struct client_header)); header->program_size = strlen(program_name); header->client_id = getpid(); ipc_open(server_params, O_WRONLY); server_params->msg_type = PRE_HEADER; ipc_send(server_params, header, sizeof(struct client_header)); server_params->msg_type = PROGRAM_STRING; ipc_send(server_params, program_name, header->program_size); ipc_close(server_params); ipc_create(client_params); ipc_open(client_params, O_RDONLY); while (!ipc_receive(client_params, &cl_program, sizeof(struct status))); ipc_close(client_params); ipc_destroy(client_params); for (i = 0; i < MEM_SIZE; i++){ printf("%d ", cl_program.mem[i]); } printf("\n"); return 0; }
int main(void) { status c_program; init_processes(); ipc_open(endif_process->params, O_RDONLY); while(1) if (ipc_receive(endif_process->params, &c_program, sizeof(struct status)) > 0) run_process(&c_program, &execute_endif); return 0; }
void * run_server_receive(void * params){ ipc_params_t client_params; status client_final; ipc_create(server_receive_params); ipc_open(server_receive_params, O_RDONLY|O_NONBLOCK); while(1){ if(ipc_receive(server_receive_params, &client_final, sizeof(struct status))){ client_params = get_params_from_pid(client_final.client_id, PROGRAM_STATUS, sizeof(struct status), server_params->semid); client_params->socklistener = TRUE; printf("Answering to client: %d\n", client_final.client_id); ipc_open(client_params, O_WRONLY); ipc_send(client_params, &client_final, sizeof(struct status)); ipc_close(client_params); free(client_params->file); free(client_params); } sleep(1); } }
int main(void) { status c_program; init_processes(FALSE); signal(SIGINT, end_process); ipc_open(if_process->params, O_RDONLY); while(1) if (ipc_receive(if_process->params, &c_program, sizeof(struct status)) > 0) run_process(&c_program, &execute_if); return 0; }
void answer_error_to_client(int err_code, int client_id){ ipc_params_t client_params; status client_program; client_params = get_params_from_pid(client_id, PROGRAM_STATUS, sizeof(struct status), server_params->semid); client_params->socklistener = TRUE; client_program.flag = err_code; printf("Answering to client: %d\n", client_id); ipc_open(client_params, O_WRONLY); ipc_send(client_params, &client_program, sizeof(struct status)); ipc_close(client_params); free(client_params->file); free(client_params); }
void * run_program(void * program_stat) { ipc_params_t client_params; stack_t c_stack; graph_t c_graph; int memkey, i; status client_program; process_t process_type; thread_status_t thread_info = (thread_status_t)program_stat; c_stack = parse_file((char*)thread_info->file); c_graph = build_graph(c_stack); if (c_graph != NULL) { create_sh_graph(c_graph, get_graph_size(c_graph), &memkey, &client_program.g_header); client_program.cursor = 0; client_program.flag = FALSE; for(i = 0; i < MEM_SIZE; i++) { client_program.mem[i] = 0; } process_type = c_graph->first->instruction_process->instruction_type; client_program.mtype = process_type->params->unique_mq_id; ipc_send(process_type->params, &client_program, sizeof(struct status)); } else { printf("Entrada incorrecta\n"); } while(!ipc_receive(server_receive_params, &client_program, sizeof(struct status))) sleep(1); client_params = get_params_from_pid(thread_info->client_id, PROGRAM_STATUS, sizeof(struct status)); ipc_open(client_params, O_WRONLY); ipc_send(client_params, &client_program, sizeof(struct status)); ipc_close(client_params); }
int main(void) { int i; // char *ppp; /************************ initializations ****************************/ /* set up file mapped shared memory for inter process communication */ ipc_sem_init(); // setup semaphores semid = ipc_sem_id(skey); // set semaphor id /* set up shared memory */ ipc_sem_lock(semid, &sb); // wait for a lock on shared memory fd = ipc_open(ipc_file, ipc_size()); // create/open ipc file data = ipc_map(fd, ipc_size()); // map file to memory ipc_ptr = data; // overlay data with _IPC_DAT data structure ipc_sem_free(semid, &sb); // free lock on shared memory /* setup control block pointers */ cmd_fsm_cb.ipc_ptr = ipc_ptr; // set pointer to shared memory cmd_fsm_cb.sys_ptr = &(ipc_ptr->sys_data); // set pointer to system data in shared memory cmd_fsm_cb.ssch_ptr = &ipc_ptr->sys_data.sys_sch; // set pointer to active shecule in shared memory cmd_fsm_cb.wsch_ptr = &cmd_fsm_cb.w_sch; // set pointer to working schedule /* check system versions */ if (sys_comp(&(ipc_ptr->sys_data.config))) { printf("*** the system configuration in shared memory and in the application are different\n update shared memory? (y)|(n) > "); if (getchar() == 'y') { ipc_ptr->sys_data.config.major_version = _MAJOR_VERSION_system; ipc_ptr->sys_data.config.minor_version = _MINOR_VERSION_system; ipc_ptr->sys_data.config.minor_revision = _MINOR_REVISION_system; ipc_ptr->sys_data.config.channels = _NUMBER_OF_CHANNELS; ipc_ptr->sys_data.config.sensors = _NUMBER_OF_SENSORS; ipc_ptr->sys_data.config.commands = _CMD_TOKENS; ipc_ptr->sys_data.config.states = _CMD_STATES; c = fgetc(stdin); // get rid of trailing CR } else { c = fgetc(stdin); // get rid of trailing CR printf("*** application terminated\n"); exit(1); } } /* load working schedule from system schedule */ ipc_sem_lock(semid, &sb); // wait for a lock on shared memory cmd_fsm_cb.w_sch = ipc_ptr->sys_data.sys_sch; ipc_sem_free(semid, &sb); // free lock on shared memory /* initialize working sensor name and description */ cmd_fsm_cb.w_sen_dat.group[0] = '\0'; cmd_fsm_cb.w_sen_dat.description[0] = '\0'; cmd_fsm_cb.w_sen_dat.description[1] = '\0'; /* initialize state machines */ cmd_fsm_reset(&cmd_fsm_cb); // initialize the command processor fsm char_fsm_reset(); // initialize the character fsm char_state = 0; /* initialize input buffer */ memset(work_buffer, '\0', sizeof(work_buffer)); work_buffer_ptr = work_buffer; start_buff = work_buffer; input_ptr = work_buffer; end_buff = (char *)((int)start_buff + _INPUT_BUFFER_SIZE); escape = false; /* initialize ring buffer & indexs*/ for (i = 0; i < _CMD_BUFFER_DEPTH; i++) memset(&ring_buffer[i][0], '\0', _INPUT_BUFFER_SIZE); rb_in_idx = 0; rb_out_idx = 0; /* set up unbuffered io */ fflush(stdout); system("stty -echo"); //turn off terminal echo system("/bin/stty raw"); // use system call to make terminal send all keystrokes directly to stdin int flags = fcntl(STDOUT_FILENO, F_GETFL); fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); escape = false; /* initialize user interface */ printf("Pcon %d.%d.%d starting\n\r", _MAJOR_VERSION_Pcon, _MINOR_VERSION_Pcon, _MINOR_REVISION_Pcon); printf("\nSystem configuration\r\n"); printf(" Git tag - %s\r\n", _TAG); printf(" Inter Process Commucination support %d.%d.%d\n\r", _MAJOR_VERSION_ipc, _MINOR_VERSION_ipc, _MINOR_REVISION_ipc); printf(" Dcon version %d.%d.%d\n\r", _MAJOR_VERSION_Dcon, _MINOR_VERSION_Dcon, _MINOR_REVISION_Dcon); printf(" Scon version %d.%d.%d\n\r", _MAJOR_VERSION_Scon, _MINOR_VERSION_Scon, _MINOR_REVISION_Scon); printf(" Pcon version %d.%d.%d\n\r", _MAJOR_VERSION_Pcon, _MINOR_VERSION_Pcon, _MINOR_REVISION_Pcon); printf(" char_fsm version %d.%d.%d\n\r", _MAJOR_VERSION_char_fsm, _MINOR_VERSION_char_fsm, _MINOR_REVISION_char_fsm); printf(" cmd_fsm version %d.%d.%d\n\n\r", _MAJOR_VERSION_cmd_fsm, _MINOR_VERSION_cmd_fsm, _MINOR_REVISION_cmd_fsm); printf("initializations complete\r\n\nenter ? for a list of commands\r\n\n"); /* set initial prompt */ strcpy(cmd_fsm_cb.prompt_buffer, "Pcon enter a command"); /************************************************************/ /**************** start main processing loop ****************/ /************************************************************/ while (1) { while (pop_cmd_q(cmd_fsm_cb.token)) // check the token queue { cmd_fsm(&cmd_fsm_cb); // cycle cmd fsm until queue is empty prompted = false; } if (prompted == false) // display prompt if necessary { prompted = true; prompt(cmd_fsm_cb.state); } c = fgetc(stdin); // read the keyboard switch (c) { /* NOCR */ case _NO_CHAR: break; /* ESC */ case _ESC: c = fgetc(stdin); // skip to next character c = fgetc(stdin); // skip to next character switch (c) { /* up arrow */ case 'A': if (rb_out_idx > 0) rb_out_idx--; else rb_out_idx = rb_in_idx - 1; if (rb_out_idx >= rb_in_idx) rb_out_idx = 0; arrow_reprompt(); continue; break; /* down arrow */case 'B': rb_out_idx++; if (rb_out_idx >= rb_in_idx) rb_out_idx = 0; arrow_reprompt(); continue; break; /* right arrow */case 'C': if (input_ptr < work_buffer_ptr) { input_ptr++; printf("\033[1C"); // move cursor right } continue; break; /* left arrow */case 'D': if (input_ptr > start_buff) { input_ptr--; printf("\033[1D"); // move cursor left } continue; break; /* ESC */ default: escape = true; while (pop_cmd_q(cmd_fsm_cb.token)); // empty command queue memset(work_buffer, '\0', sizeof(work_buffer)); // clean out work buffer memset(previous_work_buffer, '\0', sizeof(work_buffer)); // clean out previous command buffer work_buffer_ptr = work_buffer; // set pointer to start of buffer input_ptr = work_buffer; // set pointer to start of buffer char_fsm_reset(); // initialize the character fsm cmd_fsm_reset(&cmd_fsm_cb); // initialize the command processor fsm char_state = 0; prompted = false; // force a prompt printf("\n\rcommand processor reset\n\r"); strcpy(cmd_fsm_cb.prompt_buffer, "enter a command"); continue; break; } /* CR */ case _CR: if (work_buffer_ptr != start_buff) // skip null input lines { if (strcmp(work_buffer, previous_work_buffer) != 0) // remove duplicates { strcpy(&ring_buffer[rb_in_idx++][0], work_buffer); if (rb_in_idx > _CMD_BUFFER_DEPTH - 1) rb_in_idx = 0; rb_out_idx = rb_in_idx; memset(previous_work_buffer, '\0', sizeof(work_buffer)); strcpy(previous_work_buffer, work_buffer); } } printf("\n\r"); // move cursor to next line *work_buffer_ptr++ = _CR; // load the CR into the work buffer *work_buffer_ptr++ = '\0'; // load the NULL into the work buffer work_buffer_ptr = work_buffer; // reset pointer char_fsm_reset(); // reset char_fsm while (*work_buffer_ptr != '\0') // send characters to char_fsm { char_fsm(char_type(*work_buffer_ptr), &char_state, work_buffer_ptr); work_buffer_ptr++; } work_buffer_ptr = work_buffer; // reset pointer input_ptr = work_buffer_ptr; // reset pointer memset(work_buffer, '\0', sizeof(work_buffer)); memset(screen_buf, '\0', sizeof(screen_buf)); memset(&ring_buffer[rb_in_idx][0], '\0', _INPUT_BUFFER_SIZE); break; /* DEL */ case _DEL: if (input_ptr == start_buff) break; if (input_ptr == work_buffer_ptr) { // no arrow keys in play *work_buffer_ptr-- = '\0'; *work_buffer_ptr = '\0'; input_ptr = work_buffer_ptr; printf("\r"); del_prompt(cmd_fsm_cb.state); // display user prompt printf("%s", work_buffer); // print work_buffer printf("\033[K"); // Erase to end of line prompted = true; } else { mv = work_buffer_ptr - input_ptr; input_ptr--; hptr = input_ptr; while (input_ptr < work_buffer_ptr) // shift buffer left { *input_ptr = *(input_ptr + 1); input_ptr++; } input_ptr = hptr; *work_buffer_ptr-- = '\0'; *work_buffer_ptr = '\0'; printf("\r"); printf("\033[K"); // Erase to end of line del_prompt(cmd_fsm_cb.state); printf("%s", work_buffer); while (mv > 0) { printf("\033[1D"); // move cursor left mv--; } } break; /* OTHER */ default: if (work_buffer_ptr <= end_buff) // room to add character ? { if (input_ptr == work_buffer_ptr) // cursor is at the end of the input buffer { *work_buffer_ptr++ = c; input_ptr = work_buffer_ptr; printf("%c", c); } else // cursor is not at the end of the input buffer { move_ptr = work_buffer_ptr++; move_ptr++; *move_ptr-- = '\0'; while (move_ptr > input_ptr) { *move_ptr = *(move_ptr - 1); move_ptr--; } *input_ptr++ = c; mv = work_buffer_ptr - input_ptr; printf("\r"); printf("\033[K"); // Erase to end of line prompt(cmd_fsm_cb.state); printf("%s", work_buffer); while (mv > 0) { printf("\033[1D"); // move cursor left mv--; } } } } /* do suff while waiting or the keyboard */ } system("/bin/stty cooked"); //switch to buffered iput system("/bin/stty echo"); //turn on terminal echo printf("\f\n***normal termination - but should not happen\n\n"); return 0; }
int main(int argc, char **argv) { int ch, longindex, nr; int is_daemon = 1, is_debug = 1; pid_t pid; struct pollfd *poll_array; while ((ch = getopt_long(argc, argv, "fd:vh", long_options, &longindex)) >= 0) { switch (ch) { case 'f': is_daemon = 0; break; case 'd': is_debug = atoi(optarg); break; case 'v': exit(0); break; case 'h': usage(0); break; default: usage(1); break; } } init(is_daemon, is_debug); if (is_daemon) { pid = fork(); if (pid < 0) exit(-1); else if (pid) exit(0); chdir("/"); close(0); open("/dev/null", O_RDWR); dup2(0, 1); dup2(0, 2); setsid(); } nl_fd = nl_open(); if (nl_fd < 0) exit(nl_fd); ipc_fd = ipc_open(); if (ipc_fd < 0) exit(ipc_fd); dl_init(); nr = MAX_DL_HANDLES; poll_array = poll_init(nr); dl_config_load(); event_loop(nr, poll_array); return 0; }