Advertisement
Guest User

Untitled

a guest
Nov 26th, 2011
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <netdb.h>
  5. #include <string.h>
  6.  
  7. int ForkPS(int *PsIn, int *PsOut, char *Program, char **Argv);
  8.  
  9. int main(void)
  10. {
  11.     int PsIn, PsOut;    /* Child's STDIN and STDOUT. */
  12.     fd_set FDs, FDCopy;
  13.     char Output[20], Input[5000];
  14.  
  15.     ForkPS(&PsIn, &PsOut, "/bin/bash", NULL);
  16.  
  17.     /* Setup select(). */
  18.     FD_ZERO(&FDs);
  19.     FD_SET(0, &FDs);    /* Add STDIN to the list. */
  20.     FD_SET(PsOut, &FDs);    /* Add child's STDOUT to the list. */
  21.  
  22.     for(;;)
  23.     {   /* This loop continually looks for input on the descriptors. */
  24.         FDCopy = FDs;
  25.  
  26.         select(PsOut+1, &FDCopy, NULL, NULL, NULL);
  27.  
  28.         /* Check for output from the child. */
  29.         if(FD_ISSET(PsOut, &FDCopy))
  30.         {
  31.             memset(Output, 0, sizeof(Output));
  32.             read(PsOut, Output, sizeof(Output));
  33.             printf("%s", Output);
  34.         }
  35.  
  36.         /* Check if the user has entered commands. */
  37.         if(FD_ISSET(0, &FDCopy))
  38.         {
  39.             memset(Input, 0, sizeof(Input));
  40.             fgets(Input, sizeof(Input), stdin); /* Read from keyboard. */
  41.             /* Perform an "exit check" to end the program and child. */
  42.             write(PsIn, Input, strlen(Input));
  43.         }
  44.  
  45.     }
  46. }
  47.  
  48. int ForkPS(int *PsIn, int *PsOut, char *Program, char **Argv)
  49. {
  50.     /* This function forks a process and set's up its
  51.      * STDIN and STDOUT as PsIn and PsOut.
  52.      */
  53.  
  54.     /* Here's how it works:
  55.      *
  56.      * A child process inherits the parent's
  57.      * file descriptors. We are going to set the
  58.      * parent and child's STDIN and STDOUT descriptors
  59.      * to OutFDs[0] and InFDs[1] respectively. Our
  60.      * parent program will write to OutFDs[1] and
  61.      * read from InFDs[0]. These descriptors will
  62.      * be copied to PsIn and PsOut.
  63.      *
  64.      * After the fork(), the parent's default
  65.      * STDIN and STDOUT is copied back.
  66.      *
  67.      * All in all, after the function returns the
  68.      * only noticable difference is the values of
  69.      * PsIn and PsOut which will be used to control
  70.      * the child.
  71.      */
  72.  
  73.     int InFDs[2];
  74.     int OutFDs[2];     
  75.  
  76.     int OldStdin, OldStdout;    /* Backups of initial streams. */
  77.  
  78.     pipe(OutFDs);
  79.     pipe(InFDs);
  80.  
  81.     OldStdin = dup(0);  /* Save old STDIN. */
  82.     OldStdout = dup(1); /* Save old STDOUT. */
  83.  
  84.     close(0);
  85.     close(1);   /* Close our streams. */
  86.  
  87.     /* These next steps prepare our soon to be child process. */
  88.     dup2(OutFDs[0], 0); /* Set STDIN to OutFDs[0]. */
  89.     dup2(InFDs[1], 1);  /* Set STDOUT to InFDs[1]. */
  90.  
  91.     if(!fork()) /* Spawn our child process. */
  92.     {
  93.         /* Clean-up these descriptors, no longer needed. */
  94.         close(OutFDs[0]);
  95.         close(OutFDs[1]);
  96.         close(InFDs[0]);
  97.         close(InFDs[1]);
  98.  
  99.         /* Execute program. */
  100.         execv(Program, Argv);
  101.  
  102.         /* End child. */
  103.         exit(0);
  104.     }
  105.  
  106.     else    /* Parent. */
  107.     {
  108.         /* Let's reset our streams. */
  109.         close(0);
  110.         close(1);
  111.  
  112.         dup2(OldStdin, 0);  /* Copy them back... */
  113.         dup2(OldStdout, 1);
  114.  
  115.         close(OutFDs[0]);   /* Being used by the child. */
  116.         close(InFDs[1]);
  117.     }
  118.  
  119.     /* Set our function arguments. */
  120.     (*PsIn) = OutFDs[1];
  121.     (*PsOut) = InFDs[0];
  122.  
  123.     return 0;
  124. }
  125.  
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement