Advertisement
droidvoider

fork_u.c

Mar 20th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include<string.h>
  7.  
  8. //===>] FUNCTION DECLARATOINS [<===
  9. int run_forked(int argc, char **argv);
  10. int parse_cmdline(int argc, char*  args[]);
  11.  
  12. //--> global variables <--
  13. int children_active; //0 = kill -- global kill switch for all child processes. Re: while loop in run_forked function
  14. int sys_exec;        //0 = system()
  15. pid_t targetProcess; //pid we will execute on our desired task, all passed in by command line
  16. //last work i did was adding these variables so i could control my loops from command line <TODO>
  17.  
  18.  
  19. //===>] CHILD PROCESS USER PROMPT [<===
  20. int user_prompt() {
  21.  char str[1024];
  22.  
  23.    printf( "Enter a value :");
  24.    fgets(str, 1020, stdin );
  25.  
  26.    printf( "\nYou entered: ");
  27.    puts( str );
  28.  
  29.    return 0;
  30. }
  31.  
  32.  
  33. //===>] COMMAND LINE PROCESSOR [<===
  34. int parse_cmdline(int argc, char*  args[]) {
  35. int i = 1;
  36. while (i < argc) {
  37.     char *arg = args[i++];
  38.     //attempt to execute a binary from our child
  39.     if (!strcmp(arg, "-e")) {
  40.         printf("Have -e\n");
  41.     } else if (!strcmp(arg, "-w")) {
  42.         if (i >= argc)
  43.             printf("error processing command line i:%i >= argc:%i\n",i,argc);
  44.         char *param = args[i++];
  45.         printf("Have -w %s\n", param);
  46.     } else {
  47.         printf("error processing command line\n");
  48.     }
  49. }
  50.  
  51. return 0;
  52. }
  53.  
  54. int main( int argc, char *argv[] )  {
  55.  
  56.    if( argc == 2 ) {
  57.       printf("The argument supplied is %s\n", argv[1]);
  58.    }
  59.    else if( argc > 2 ) {
  60.       if(parse_cmdline(argc, argv)) {printf("error calling function\n");}
  61.    }
  62.    else {
  63.       printf("One argument expected.\n");
  64.    }
  65.  
  66.     children_active=1; //This is used to kill the child loop process. 1 = run 0 = kill
  67.    
  68.     int i;
  69.     pid_t cpid;
  70.     pid_t child_pid;
  71.  
  72.  
  73.     //attempts to fork the current process returning the results as pid_t, which is basically int
  74.     cpid = fork();
  75.     //let's grab our child process and route it away to do our bidding
  76.     switch (cpid) {
  77.         case -1: printf("Fork failed; cpid == -1\n");
  78.                  break;
  79.     //means we successfully forked, let's retrieve the process id
  80.         case 0: child_pid = getpid();
  81.         //run away to do child work
  82.         if (run_forked(argc, argv)){
  83.            printf("there was an error returned from function run_forked()");
  84.         }
  85.                 exit(0);
  86.    
  87.     //probably best to hold this instance of toolbox open until we are done forking around
  88.         default: printf("Toolbox is waiting for pid: %d\n", cpid);
  89.                  waitpid(cpid, NULL, 0);
  90.                  printf("Child(s) finished....\n\n\nToolbox finished.\n");
  91.     }
  92.     return 0;
  93. }
  94.  
  95. int run_forked(int argc, char*  args[]) {
  96.     //<TODO>  this doesn't have a default assignment when ported to toolbox.c
  97.     char *currcon="test_shell";
  98.     //<TODO> this needs to be uncommented when ported to toolbox.c
  99.     //<TODO> getcon(&currcon);
  100.    
  101.     //Each child will have their own process id
  102.     pid_t child_pid = getpid();
  103.     printf("New child process id: %i\n", child_pid);
  104.  
  105.         int iLoop=0;     //<TODO> <DEBUG> run away code protection
  106.     int iLoopMax=10;     //<TODO> <DEBUG> run away code protection
  107.  
  108.     char promptLine[1024];
  109.  
  110.     printf("[+] droidvoider's toolbox running as uid: %d, gid: %d, context %s\n", getuid(), getgid(), currcon);
  111.  
  112.     //control loop
  113.     while(children_active) {
  114.         //<TODO> -- add way to exit from cmd line
  115.         if (iLoop > iLoopMax){
  116.            printf("run away program protection, killing child\n");
  117.            children_active=0;
  118.         }
  119.  
  120.         printf("[+] running as uid: %d, gid: %d\n", getuid(), getgid());
  121.  
  122. //===>] CHILD PROMPT [<===
  123.    printf( "usage: -e or-s <COMMAND to EXECUTE>\n");
  124.    printf( "example: -s pm disable com.ws.dm\n(would execute system(pm disable com.ws.dm)\n\n\n\n\n");
  125.  
  126.  
  127.    printf("Enter a command:");
  128.    fgets(promptLine, 1020, stdin );
  129.  
  130.    printf( "\nChild %i received the following command: ", child_pid);
  131.    puts( promptLine );
  132.     if (strcmp(promptLine, "x")) {children_active=0;}
  133.  
  134.         sleep(6);
  135.     iLoop++;
  136. }
  137. printf("while loop exited on loop: %i\n",iLoop);
  138.     //<TODO> this needs to be uncommented when ported to toolbox.c
  139.     //free(currcon);
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement