Advertisement
Combreal

forkedChildLinux02.cpp

Jun 10th, 2018
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 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.  
  8. #define PIPE_READ 0
  9. #define PIPE_WRITE 1
  10.  
  11. std::string createChild(const char* bashCmd)//int createChild(const char* szCommand, char* const aArguments[], char* const aEnvironment[], const char* szMessage)
  12. {
  13.     int aStdinPipe[2];
  14.     int aStdoutPipe[2];
  15.     int nChild;
  16.     //char nChar;
  17.     int nResult;
  18.     char buffer[4096];
  19.     std::string commandResult;
  20.  
  21.     if(pipe(aStdinPipe) < 0)
  22.     {
  23.         perror("allocating pipe for child input redirect");
  24.         return "Error";
  25.     }
  26.     if(pipe(aStdoutPipe) < 0)
  27.     {
  28.         close(aStdinPipe[PIPE_READ]);
  29.         close(aStdinPipe[PIPE_WRITE]);
  30.         perror("allocating pipe for child output redirect");
  31.         return "Error";
  32.     }
  33.  
  34.     nChild = fork();
  35.     if(0 == nChild)
  36.     {
  37.         // child continues here
  38.  
  39.         // redirect stdin
  40.         if(dup2(aStdinPipe[PIPE_READ], STDIN_FILENO) == -1)
  41.         {
  42.             exit(errno);
  43.         }
  44.  
  45.         // redirect stdout
  46.         if(dup2(aStdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1)
  47.         {
  48.             exit(errno);
  49.         }
  50.  
  51.         // redirect stderr
  52.         if(dup2(aStdoutPipe[PIPE_WRITE], STDERR_FILENO) == -1)
  53.         {
  54.             exit(errno);
  55.         }
  56.  
  57.         // all these are for use by parent only
  58.         close(aStdinPipe[PIPE_READ]);
  59.         close(aStdinPipe[PIPE_WRITE]);
  60.         close(aStdoutPipe[PIPE_READ]);
  61.         close(aStdoutPipe[PIPE_WRITE]);
  62.  
  63.         // run child process image
  64.         // replace this with any exec* function find easier to use ("man exec")
  65.         //nResult = execve(szCommand, aArguments, aEnvironment);
  66.         nResult = execl("/bin/bash", "bash", "-c", bashCmd, (char*)0);
  67.  
  68.         // if we get here at all, an error occurred, but we are in the child
  69.         // process, so just exit
  70.         exit(nResult);
  71.     }
  72.     else if(nChild > 0)
  73.     {
  74.         // parent continues here
  75.  
  76.         // close unused file descriptors, these are for child only
  77.         close(aStdinPipe[PIPE_READ]);
  78.         close(aStdoutPipe[PIPE_WRITE]);
  79.  
  80.         // Include error check here
  81.         /*if(NULL != szMessage)
  82.         {
  83.             write(aStdinPipe[PIPE_WRITE], szMessage, strlen(szMessage));
  84.         }*/
  85.  
  86.         // Just a char by char read here, you can change it accordingly //////////////
  87.         /*while(read(aStdoutPipe[PIPE_READ], &nChar, 1) == 1)
  88.         {
  89.             write(STDOUT_FILENO, &nChar, 1);
  90.         }*/
  91.         read(aStdoutPipe[PIPE_READ], buffer, sizeof(buffer));
  92.         commandResult = buffer;
  93.  
  94.         // done with these in this example program, you would normally keep these
  95.         // open of course as long as you want to talk to the child
  96.         close(aStdinPipe[PIPE_WRITE]);
  97.         close(aStdoutPipe[PIPE_READ]);
  98.     } else
  99.     {
  100.         // failed to create child
  101.         close(aStdinPipe[PIPE_READ]);
  102.         close(aStdinPipe[PIPE_WRITE]);
  103.         close(aStdoutPipe[PIPE_READ]);
  104.         close(aStdoutPipe[PIPE_WRITE]);
  105.     }
  106.     return commandResult; //buffer;//nChild;
  107. }
  108.  
  109. int main(void)
  110. {
  111.     std::cout<<"CommandReturns : "<<createChild("ls")<<std::endl;//"echo \"test\""
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement