Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <netdb.h>
- #include <string.h>
- int ForkPS(int *PsIn, int *PsOut, char *Program, char **Argv);
- int main(void)
- {
- int PsIn, PsOut; /* Child's STDIN and STDOUT. */
- fd_set FDs, FDCopy;
- char Output[20], Input[5000];
- ForkPS(&PsIn, &PsOut, "/bin/bash", NULL);
- /* Setup select(). */
- FD_ZERO(&FDs);
- FD_SET(0, &FDs); /* Add STDIN to the list. */
- FD_SET(PsOut, &FDs); /* Add child's STDOUT to the list. */
- for(;;)
- { /* This loop continually looks for input on the descriptors. */
- FDCopy = FDs;
- select(PsOut+1, &FDCopy, NULL, NULL, NULL);
- /* Check for output from the child. */
- if(FD_ISSET(PsOut, &FDCopy))
- {
- memset(Output, 0, sizeof(Output));
- read(PsOut, Output, sizeof(Output));
- printf("%s", Output);
- }
- /* Check if the user has entered commands. */
- if(FD_ISSET(0, &FDCopy))
- {
- memset(Input, 0, sizeof(Input));
- fgets(Input, sizeof(Input), stdin); /* Read from keyboard. */
- /* Perform an "exit check" to end the program and child. */
- write(PsIn, Input, strlen(Input));
- }
- }
- }
- int ForkPS(int *PsIn, int *PsOut, char *Program, char **Argv)
- {
- /* This function forks a process and set's up its
- * STDIN and STDOUT as PsIn and PsOut.
- */
- /* Here's how it works:
- *
- * A child process inherits the parent's
- * file descriptors. We are going to set the
- * parent and child's STDIN and STDOUT descriptors
- * to OutFDs[0] and InFDs[1] respectively. Our
- * parent program will write to OutFDs[1] and
- * read from InFDs[0]. These descriptors will
- * be copied to PsIn and PsOut.
- *
- * After the fork(), the parent's default
- * STDIN and STDOUT is copied back.
- *
- * All in all, after the function returns the
- * only noticable difference is the values of
- * PsIn and PsOut which will be used to control
- * the child.
- */
- int InFDs[2];
- int OutFDs[2];
- int OldStdin, OldStdout; /* Backups of initial streams. */
- pipe(OutFDs);
- pipe(InFDs);
- OldStdin = dup(0); /* Save old STDIN. */
- OldStdout = dup(1); /* Save old STDOUT. */
- close(0);
- close(1); /* Close our streams. */
- /* These next steps prepare our soon to be child process. */
- dup2(OutFDs[0], 0); /* Set STDIN to OutFDs[0]. */
- dup2(InFDs[1], 1); /* Set STDOUT to InFDs[1]. */
- if(!fork()) /* Spawn our child process. */
- {
- /* Clean-up these descriptors, no longer needed. */
- close(OutFDs[0]);
- close(OutFDs[1]);
- close(InFDs[0]);
- close(InFDs[1]);
- /* Execute program. */
- execv(Program, Argv);
- /* End child. */
- exit(0);
- }
- else /* Parent. */
- {
- /* Let's reset our streams. */
- close(0);
- close(1);
- dup2(OldStdin, 0); /* Copy them back... */
- dup2(OldStdout, 1);
- close(OutFDs[0]); /* Being used by the child. */
- close(InFDs[1]);
- }
- /* Set our function arguments. */
- (*PsIn) = OutFDs[1];
- (*PsOut) = InFDs[0];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement