#ifndef PROCESS_H #define PROCESS_H #include #include #include #include #include #include #include #include class process { public: pid_t pid; int pipefds[2]; process(const char* cmd){ // Run the process right here // Make a pipe, since we want the output // fork and exec // Return, if we're in the parent pipe(pipefds); pid_t child_pid = fork(); if(!child_pid){ // Child dup2(pipefds[1], 1); execl("/bin/sh", "sh", "-c", cmd, 0); perror("exec"); exit(1); } // Parent close(pipefds[1]); pid = child_pid; } bool output_ready(){ struct pollfd pfd = {pipefds[0], POLLIN, 0}; return poll(&pfd, 1, 0); } /* This might return less than quantity bytes if less is available */ /* We'd like this to return if nothing is available to read! */ std::string retrieve_output(size_t quantity){ if(!output_ready()) return ""; char output[quantity + 1];// For space for the null terminator ssize_t readlen = read(pipefds[0], output, quantity); output[readlen] = 0; printf("readlen: %ld\n", readlen); return std::string(output); } int stop(){ // Stop the process we're managing kill(pid, SIGKILL); int exit_status; waitpid(pid, &exit_status, 0); return exit_status; } bool is_running(){ // We'll test without this yet return true; } }; #endif