Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include<string.h>
- //===>] FUNCTION DECLARATOINS [<===
- int run_forked(int argc, char **argv);
- int parse_cmdline(int argc, char* args[]);
- //--> global variables <--
- int children_active; //0 = kill -- global kill switch for all child processes. Re: while loop in run_forked function
- int sys_exec; //0 = system()
- pid_t targetProcess; //pid we will execute on our desired task, all passed in by command line
- //last work i did was adding these variables so i could control my loops from command line <TODO>
- //===>] CHILD PROCESS USER PROMPT [<===
- int user_prompt() {
- char str[1024];
- printf( "Enter a value :");
- fgets(str, 1020, stdin );
- printf( "\nYou entered: ");
- puts( str );
- return 0;
- }
- //===>] COMMAND LINE PROCESSOR [<===
- int parse_cmdline(int argc, char* args[]) {
- int i = 1;
- while (i < argc) {
- char *arg = args[i++];
- //attempt to execute a binary from our child
- if (!strcmp(arg, "-e")) {
- printf("Have -e\n");
- } else if (!strcmp(arg, "-w")) {
- if (i >= argc)
- printf("error processing command line i:%i >= argc:%i\n",i,argc);
- char *param = args[i++];
- printf("Have -w %s\n", param);
- } else {
- printf("error processing command line\n");
- }
- }
- return 0;
- }
- int main( int argc, char *argv[] ) {
- if( argc == 2 ) {
- printf("The argument supplied is %s\n", argv[1]);
- }
- else if( argc > 2 ) {
- if(parse_cmdline(argc, argv)) {printf("error calling function\n");}
- }
- else {
- printf("One argument expected.\n");
- }
- children_active=1; //This is used to kill the child loop process. 1 = run 0 = kill
- int i;
- pid_t cpid;
- pid_t child_pid;
- //attempts to fork the current process returning the results as pid_t, which is basically int
- cpid = fork();
- //let's grab our child process and route it away to do our bidding
- switch (cpid) {
- case -1: printf("Fork failed; cpid == -1\n");
- break;
- //means we successfully forked, let's retrieve the process id
- case 0: child_pid = getpid();
- //run away to do child work
- if (run_forked(argc, argv)){
- printf("there was an error returned from function run_forked()");
- }
- exit(0);
- //probably best to hold this instance of toolbox open until we are done forking around
- default: printf("Toolbox is waiting for pid: %d\n", cpid);
- waitpid(cpid, NULL, 0);
- printf("Child(s) finished....\n\n\nToolbox finished.\n");
- }
- return 0;
- }
- int run_forked(int argc, char* args[]) {
- //<TODO> this doesn't have a default assignment when ported to toolbox.c
- char *currcon="test_shell";
- //<TODO> this needs to be uncommented when ported to toolbox.c
- //<TODO> getcon(&currcon);
- //Each child will have their own process id
- pid_t child_pid = getpid();
- printf("New child process id: %i\n", child_pid);
- int iLoop=0; //<TODO> <DEBUG> run away code protection
- int iLoopMax=10; //<TODO> <DEBUG> run away code protection
- char promptLine[1024];
- printf("[+] droidvoider's toolbox running as uid: %d, gid: %d, context %s\n", getuid(), getgid(), currcon);
- //control loop
- while(children_active) {
- //<TODO> -- add way to exit from cmd line
- if (iLoop > iLoopMax){
- printf("run away program protection, killing child\n");
- children_active=0;
- }
- printf("[+] running as uid: %d, gid: %d\n", getuid(), getgid());
- //===>] CHILD PROMPT [<===
- printf( "usage: -e or-s <COMMAND to EXECUTE>\n");
- printf( "example: -s pm disable com.ws.dm\n(would execute system(pm disable com.ws.dm)\n\n\n\n\n");
- printf("Enter a command:");
- fgets(promptLine, 1020, stdin );
- printf( "\nChild %i received the following command: ", child_pid);
- puts( promptLine );
- if (strcmp(promptLine, "x")) {children_active=0;}
- sleep(6);
- iLoop++;
- }
- printf("while loop exited on loop: %i\n",iLoop);
- //<TODO> this needs to be uncommented when ported to toolbox.c
- //free(currcon);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement