Advertisement
Combreal

PipedForks04.cpp

Aug 16th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <iostream>
  7. #include <sys/wait.h>
  8.  
  9. #define PIPE_READ 0
  10. #define PIPE_WRITE 1
  11.  
  12. std::string createChild(const char* bashCmd)
  13. {
  14.     int aStdinPipe[2];
  15.     int aStdoutPipe[2];
  16.     int nChild;
  17.     int nResult;
  18.     char buffer[4096];
  19.     char ch;
  20.     std::string commandResult, cmdRec;
  21.     if(pipe(aStdinPipe) < 0)
  22.     {
  23.         exit(1);
  24.     }
  25.     if(pipe(aStdoutPipe) < 0)
  26.     {
  27.         close(aStdinPipe[PIPE_READ]);
  28.         close(aStdinPipe[PIPE_WRITE]);
  29.         exit(1);
  30.     }
  31.     nChild = fork();
  32.     if(0 == nChild)
  33.     {
  34.         if(dup2(aStdinPipe[PIPE_READ], STDIN_FILENO) == -1)//redirect stdin
  35.         {
  36.             exit(1);
  37.         }
  38.         if(dup2(aStdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1)
  39.         {
  40.             exit(1);
  41.         }
  42.         close(aStdinPipe[PIPE_READ]);
  43.         close(aStdinPipe[PIPE_WRITE]);
  44.         close(aStdoutPipe[PIPE_READ]);
  45.         close(aStdoutPipe[PIPE_WRITE]);
  46.         nResult = execl("/bin/bash", "bash", "-c", bashCmd, (char*)0);
  47.         exit(nResult);
  48.     }
  49.     else if(nChild > 0)
  50.     {
  51.         close(aStdinPipe[PIPE_READ]);
  52.         close(aStdoutPipe[PIPE_WRITE]);
  53.         memset(buffer, 0, sizeof(buffer));
  54.     //read(aStdoutPipe[PIPE_READ], buffer, sizeof(buffer));
  55.     while(read(aStdoutPipe[PIPE_READ], &ch, 1) > 0)
  56.       {
  57.         if(ch != 0)
  58.           cmdRec.push_back(ch);
  59.             else
  60.           {
  61.         cmdRec.append(" ");
  62.           }
  63.       }
  64.         //commandResult = buffer;
  65.         commandResult = cmdRec;
  66.         wait(NULL);//wait for child proc
  67.         close(aStdinPipe[PIPE_WRITE]);
  68.         close(aStdoutPipe[PIPE_READ]);
  69.     } else
  70.     {
  71.         close(aStdinPipe[PIPE_READ]);
  72.         close(aStdinPipe[PIPE_WRITE]);
  73.         close(aStdoutPipe[PIPE_READ]);
  74.         close(aStdoutPipe[PIPE_WRITE]);
  75.     std::cout<<"ERROR - couldn't read from pipe"<<std::endl;
  76.     }
  77.     return commandResult;
  78. }
  79.  
  80. int main(void)
  81. {
  82.   std::cout<<"CommandBReturns : "<<createChild("echo \"test\"")<<std::endl;
  83.   //std::cout<<"CommandBReturns : "<<createChild("/usr/local/bin/brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb")<<std::endl;
  84.     return 0;
  85. }
  86.  
  87. //g++ -g -o testProc main.cpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement