humble beginnings for subprocess- needs reworked
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "Task.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include "../sproc/Sproc.h"
|
||||
|
||||
/// Task_InvalidDataStructure - Exception thrown when a Task is defined with invalid JSON.
|
||||
class Task_InvalidDataStructure: public std::runtime_error { public:
|
||||
@@ -96,75 +97,15 @@ void Task::execute( bool verbose )
|
||||
std::cout << "\t Executing target \"" << this->definition.get_target() << "\"." << std::endl;
|
||||
}
|
||||
|
||||
this->do_raw_command();
|
||||
}
|
||||
ExecutionInput execIn;
|
||||
Execution Result;
|
||||
|
||||
int Task::do_raw_command( std::string command, std::string & STDOUT, std::string & STDIN );
|
||||
{
|
||||
execIn.executionString = this->definition.get_target();
|
||||
|
||||
/*
|
||||
* since pipes are unidirectional, we need two pipes. one for data to flow from parent's stdout to child's stdin and
|
||||
* the other for child's stdout to flow to parent's stdin
|
||||
*/
|
||||
int execution_status = Sproc::execute( execIn, Result );
|
||||
|
||||
#define NUM_PIPES 2
|
||||
|
||||
#define PARENT_WRITE_PIPE 0
|
||||
#define PARENT_READ_PIPE 1
|
||||
|
||||
int pipes[NUM_PIPES][2];
|
||||
|
||||
/*
|
||||
* always in a pipe[], pipe[0] is for read and pipe[1] is for write
|
||||
*/
|
||||
|
||||
#define READ_FD 0
|
||||
#define WRITE_FD 1
|
||||
#define PARENT_READ_FD ( pipes[PARENT_READ_PIPE][READ_FD] )
|
||||
#define PARENT_WRITE_FD ( pipes[PARENT_WRITE_PIPE][WRITE_FD] )
|
||||
|
||||
#define CHILD_READ_FD ( pipes[PARENT_WRITE_PIPE][READ_FD] )
|
||||
#define CHILD_WRITE_FD ( pipes[PARENT_READ_PIPE][WRITE_FD] )
|
||||
|
||||
int outfd[2];
|
||||
int infd[2];
|
||||
|
||||
// pipes for parent to write and read
|
||||
pipe(pipes[PARENT_READ_PIPE]);
|
||||
pipe(pipes[PARENT_WRITE_PIPE]);
|
||||
|
||||
if(!fork()) {
|
||||
char *argv[]={ "/usr/bin/bc", "-q", 0};
|
||||
|
||||
dup2(CHILD_READ_FD, STDIN_FILENO);
|
||||
dup2(CHILD_WRITE_FD, STDOUT_FILENO);
|
||||
|
||||
/* Close fds not required by child. Also, we don't
|
||||
want the exec'ed program to know these existed */
|
||||
close(CHILD_READ_FD);
|
||||
close(CHILD_WRITE_FD);
|
||||
close(PARENT_READ_FD);
|
||||
close(PARENT_WRITE_FD);
|
||||
|
||||
execv(argv[0], argv);
|
||||
} else {
|
||||
char buffer[100];
|
||||
int count;
|
||||
|
||||
/* close fds not required by parent */
|
||||
close(CHILD_READ_FD);
|
||||
close(CHILD_WRITE_FD);
|
||||
|
||||
// Write to child’s stdin
|
||||
write(PARENT_WRITE_FD, "2^32\n", 5);
|
||||
|
||||
// Read from child’s stdout
|
||||
count = read(PARENT_READ_FD, buffer, sizeof(buffer)-1);
|
||||
if (count >= 0) {
|
||||
buffer[count] = 0;
|
||||
printf("%s", buffer);
|
||||
} else {
|
||||
printf("IO Error\n");
|
||||
}
|
||||
if ( execution_status )
|
||||
{
|
||||
std::cout << std::endl << "STDOUT:" << std::endl<< Result.STDOUT << std::endl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user