void grok_program_add_input_process(grok_program_t *gprog, grok_input_t *ginput) { struct bufferevent *bev; grok_input_process_t *gipt = &(ginput->source.process); int childin[2], childout[2], childerr[2]; int pid; struct timeval now = { 0, 0 }; safe_pipe(childin); safe_pipe(childout); safe_pipe(childerr); gipt->p_stdin = childin[1]; gipt->p_stdout = childout[0]; gipt->p_stderr = childerr[0]; gipt->c_stdin = childin[0]; gipt->c_stdout = childout[1]; gipt->c_stderr = childerr[1]; bev = bufferevent_new(gipt->p_stdout, _program_process_stdout_read, NULL, _program_process_buferror, ginput); bufferevent_enable(bev, EV_READ); ginput->bev = bev; if (gipt->read_stderr) { /* store this somewhere */ bev = bufferevent_new(gipt->p_stderr, _program_process_stdout_read, NULL, _program_process_buferror, ginput); bufferevent_enable(bev, EV_READ); } grok_log(ginput, LOG_PROGRAMINPUT, "Scheduling start of: %s", gipt->cmd); event_once(-1, EV_TIMEOUT, _program_process_start, ginput, &now); }
void open_console() { safe_pipe(&control_read,&control_write); safe_pipe(&size_read,&size_write); safe_pipe(&stdin_read,&stdin_write); start_thread(stdin_loop,NULL); }
void factor_vm::open_console() { FACTOR_ASSERT(!stdin_thread_initialized_p); safe_pipe(&control_read, &control_write); safe_pipe(&size_read, &size_write); safe_pipe(&stdin_read, &stdin_write); stdin_thread = start_thread(stdin_loop, NULL); stdin_thread_initialized_p = true; pthread_mutex_init(&stdin_mutex, NULL); }
static void init_signal_pipe(factor_vm* vm) { safe_pipe(&vm->signal_pipe_input, &vm->signal_pipe_output); if (fcntl(vm->signal_pipe_output, F_SETFL, O_NONBLOCK) < 0) fatal_error("Error with fcntl", errno); vm->special_objects[OBJ_SIGNAL_PIPE] = tag_fixnum(vm->signal_pipe_input); }
void grok_program_add_input_file(grok_program_t *gprog, grok_input_t *ginput) { struct bufferevent *bev; struct stat st; int ret; int pipefd[2]; grok_input_file_t *gift = &(ginput->source.file); grok_log(ginput, LOG_PROGRAMINPUT, "Adding file input: %s", gift->filename); ret = stat(gift->filename, &st); if (ret == -1) { grok_log(gprog, LOG_PROGRAMINPUT , "Failure stat(2)'ing file: %s", gift->filename); grok_log(gprog, LOG_PROGRAMINPUT , "strerror(%d): %s", strerror(errno)); return; } gift->fd = open(gift->filename, O_RDONLY); if (gift->fd < 0) { grok_log(gprog, LOG_PROGRAM, "Failure open(2)'ing file for read '%s': %s", gift->filename, strerror(errno)); return; } safe_pipe(pipefd); gift->offset = 0; gift->reader = pipefd[0]; gift->writer = pipefd[1]; memcpy(&(gift->st), &st, sizeof(st)); gift->waittime.tv_sec = 0; gift->waittime.tv_usec = 0; gift->readbuffer = malloc(st.st_blksize); grok_log(ginput, LOG_PROGRAMINPUT, "dup2(%d, %d)", gift->fd, gift->writer); /* Tie our open file read fd to the writer of our pipe */ // this doesn't work bev = bufferevent_new(gift->reader, _program_file_read_buffer, NULL, _program_file_buferror, ginput); bufferevent_enable(bev, EV_READ); ginput->bev = bev; event_once(-1, EV_TIMEOUT, _program_file_read_real, ginput, &(gift->waittime)); }
void grok_matchconfig_start_shell(grok_program_t *gprog, grok_matchconf_t *gmc) { int pipefd[2]; if (!strcmp(gmc->shell, "stdout")) { /* Special case: Use the stdout FILE */ grok_log(gprog, LOG_PROGRAM, "matchconfig subshell set to 'stdout', directing reaction " \ "output to stdout instead of a process."); gmc->shellinput = stdout; return; } safe_pipe(pipefd); grok_log(gprog, LOG_PROGRAM, "Starting matchconfig subshell: %s", (gmc->shell == NULL) ? "/bin/sh" : gmc->shell); gmc->pid = fork(); if (gmc->pid == 0) { close(pipefd[1]); /* close the stdin input from the parent */ /* child */ dup2(pipefd[0], 0); if (gmc->shell == NULL) { /* default to just sh if there's no shell set */ execlp("sh", "sh", NULL); } else { execlp("sh", "sh", "-c", gmc->shell, NULL); } fprintf(stderr, "!!! Shouldn't have gotten here. execlp failed"); perror("errno says"); exit(-1);; } gmc->shellinput = fdopen(pipefd[1], "w"); if (gmc->shellinput == NULL) { grok_log(gprog, LOG_PROGRAM, "Fatal: Unable to fdopen(%d) subshell pipe: %s", pipefd[1], strerror(errno)); exit(1); /* XXX: We shouldn't exit here, but what else should we do? */ } }