bool get(std::string *output, std::string *error) { if (m_finish) { return false; } is_running(); std::vector < char > buffer; buffer.reserve(BUFSIZ); int result; if ((result = input_timeout(m_pipeout[0], m_waitchildtimeout)) == -1) return false; if (result) { int size = read(m_pipeout[0], &buffer[0], BUFSIZ); if (size > 0 and output) { output->append(&buffer[0], size); } } if ((result = input_timeout(m_pipeerr[0], m_waitchildtimeout)) == -1) return false; if (result) { int size = read(m_pipeerr[0], &buffer[0], BUFSIZ); if (size > 0 and error) { error->append(&buffer[0], size); } } return true; }
int main (void) { fprintf (stderr, "select returned %d.\n", input_timeout (STDIN_FILENO, 5)); return 0; }
static int handle_output_buffer (int terminal_out, char *buffer) { int pid, terminal_buffer = open(buffer,O_WRONLY|O_CREAT,0777); if ( -1 == terminal_buffer ) /* failed */ exit_error("creating buffer <id>_out failed"); pid = fork(); if ( -1 == pid ) /* failed */ exit_error("fork failed"); /* are we parent ? */ if ( 0 != pid ) /* yes - return with child pid */ return pid; /* copy from pseudo terminal to write buffer. Exit when ternminal dies or when killed by parent */ while (-1 != input_timeout(terminal_out,0)) { char buf; if ( 1 == input_timeout(terminal_out,100) ) { int res = read(terminal_out,&buf,1); /* Reading from pseudo terminal in loop until closed */ if ( -1 == res) break; if ( 0 ==res ) waitms(100); else /* keep writing to pseudo terminal */ write(terminal_buffer,&buf,1); } } /* close file */ close(terminal_buffer); /* terminate process */ _exit(1); }
static void run(Options *options) { int socket = open_udp_socket(options->listen_port); if (options->verbose) fprintf(stderr, "Opened socket on port %d\n", options->listen_port); if (signal(SIGQUIT, handle_sigquit) == SIG_IGN) signal(SIGQUIT, SIG_IGN); reload_config = 1; while (1) { int sleep_time; if (reload_config) { if (options->config != NULL) destroy_config(options->config); options->config = load_config(options->config_filename); if (!options->config) { fprintf(stderr, "Warning: Config file cannot be read: %s\n", options->config_filename); options->config = create_config(); } initialise_config(options->config); if (options->verbose) { fprintf(stderr, "Loaded config from %s\n", options->config_filename); print_config(options->config, stderr); } reload_config = 0; } //TODO how long until the next request goes out? sleep_time = 1; input_timeout(socket, sleep_time); check_for_responses(options, socket); check_requests(options, socket); } close(socket); }
int main (int argc, char* argv[]) { if (argc < 5) { fprintf(stderr, "Usage: watchdogrun outfile errfile timeout cmd arg1 ... argn\n"); exit(EXIT_FAILURE); } char* outfilename = argv[1]; char* errfilename = argv[2]; double timeout = atof(argv[3]); char* cmd = argv[4]; char** cmdargv = &(argv[4]); // File descriptor numbers for pipe. int pipefd[2]; // pipfd[0] is number for read end descriptor // pipfd[1] is number for write end descriptor pid_t cpid; // Child process id char buf; if (pipe(pipefd) == -1) { perror("pipe creation failed"); exit(EXIT_FAILURE); } int pipe_input_fdnum = pipefd[1]; int pipe_output_fdnum = pipefd[0]; cpid = fork(); if (cpid == -1) { perror("fork failed"); exit(EXIT_FAILURE); } if (cpid == 0) { // CHILD PROCESS CODE // Child writes to pipe, so close unused read end TEMP_FAILURE_RETRY (close(pipe_output_fdnum)); int errfile_fdnum = TEMP_FAILURE_RETRY (open (errfilename, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR)); if (errfile_fdnum < 0) { fprintf(stderr, "Error on open of errfile %s: %s\n", errfilename, strerror(errno)); exit(EXIT_FAILURE); } // Duplicate file descriptor for errfile and use it as file descriptor // referenced by stderr. { int dup2_status = dup2 (errfile_fdnum, STDERR_FILENO); if (dup2_status < 0) { perror("Error on use of errfile fd for child stderr"); exit(EXIT_FAILURE); } } // Delete original file descriptor for errfile. TEMP_FAILURE_RETRY (close (errfile_fdnum)); // Duplicate file descriptor for pipe input and use it as file // descriptor referenced by stdout. { int dup2_status = dup2 (pipe_input_fdnum, STDOUT_FILENO); if (dup2_status < 0) { perror("Error on use of pip input fd for child stdout"); exit(EXIT_FAILURE); } } // Delete original file descriptor for pipe input TEMP_FAILURE_RETRY (close (pipe_input_fdnum)); // Run command with supplied arguments. int status = execvp (cmd, cmdargv); // Only reach here if something goes wrong with // execv. if (status == -1) { perror("execv of cmd failed"); } exit(EXIT_FAILURE); } else { // PARENT PROCESS CODE // Close unused input end of pipe. TEMP_FAILURE_RETRY (close(pipe_input_fdnum)); int outfile_fdnum = TEMP_FAILURE_RETRY (open (outfilename, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR)); if (outfile_fdnum < 0) { fprintf(stderr, "Error on open of outfile %s: %s\n", outfilename, strerror(errno)); exit(EXIT_FAILURE); } while (1) { int input_status = input_timeout (pipe_output_fdnum, timeout); if (input_status == -1) { fprintf (stderr, "Error on select input check: %s\n", strerror(errno)); exit(EXIT_FAILURE); } else if (input_status == 0) { // Timeout on read. // Does it matter which signal is used? // kill(cpid, SIGKILL); int kill_status = kill(cpid, SIGTERM); if (kill_status < 0) { perror("Error on kill of child"); exit(EXIT_FAILURE); } int term_status; int wait_status = wait(&term_status); // Wait is required to ensure child // isn't left as zombie. if (wait_status < 0) { perror("Error on wait after kill of child: "); exit(EXIT_FAILURE); } process_term_status(term_status); assert(0); } // input_status = 1, at least 1 character present int read_status = read(pipe_output_fdnum, &buf, 1); if (read_status == 0) { // EOF int term_status; // termination status int wait_status = wait(&term_status); // Wait for child to finish exiting. // Know then it has closed the errfile. if (wait_status < 0) { perror("Error on wait after child exit: "); exit(EXIT_FAILURE); } process_term_status(term_status); assert(0); // Should not reach here } else if (read_status == -1) { // Error on read perror("Error on read of pipe from child"); exit(EXIT_FAILURE); } write(outfile_fdnum, &buf, 1); } } }