From bd7230debadfd090e7ccba20376e80611d239bd5 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Tue, 9 Apr 2019 19:20:23 +0200 Subject: Stuff --- init/init.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 16 deletions(-) (limited to 'init') diff --git a/init/init.c b/init/init.c index 090dfe2..a87930b 100644 --- a/init/init.c +++ b/init/init.c @@ -1,27 +1,47 @@ #include +#include +#include #include #include +#include #include #include -int run_start(char* _prog) +#include + + +//Needs NULL as last arg!! +int run_start(char* _prog, char* _args[]) { pid_t pid = fork(); if( pid < 0 ) return 1; else if (pid == 0) + { + //wait(0); return 0; + } + + setsid(); + if(fork()) + exit(0); + + close(STDOUT_FILENO); + close(STDIN_FILENO); + close(STDERR_FILENO); - execl(_prog, _prog, NULL); + execv(_prog, _args); + + exit(1); } -int run_stdio(char* _prog) -{ - +//Need NULL as last arg!! +int run_stdio(char* _prog, const char* _args[]) +{ int fork_pipe_out[2]; int fork_pipe_in [2]; - pid_t pid = 0; + pid_t pid; if(pipe(fork_pipe_out) == -1) return 1; @@ -34,18 +54,22 @@ int run_stdio(char* _prog) if (pid == 0) { + //replace STDIO dup2(fork_pipe_out[1], STDOUT_FILENO); dup2(fork_pipe_in [0], STDIN_FILENO ); close (fork_pipe_out[0]); close (fork_pipe_in [1]); - - execl(_prog, _prog, NULL); - return 3; + execv(_prog, _args); + + //Error handling + int err = errno; + printf("Fuckup %i: %s\n", err, strerror(err)); + + exit(1); } else { - printf("asdf\n"); close(fork_pipe_out[1]); close(fork_pipe_in [0]); @@ -57,7 +81,7 @@ int run_stdio(char* _prog) fds[1].fd = fork_pipe_out[0]; fds[1].events = POLLIN; - while(1) + while(1)//Check for data while pipe is open { int timeout = 1; //Timeout msecs ret = poll(fds, 2, timeout); @@ -73,28 +97,51 @@ int run_stdio(char* _prog) if(fds[0].revents & POLLIN) //STDIN_FILENO { - printf("a\n"); nbytes = read(STDIN_FILENO, buffer, sizeof(buffer)); write(fork_pipe_in[1], buffer, nbytes); - write(fork_pipe_in[1], "\n", 1); + //write(fork_pipe_in[1], "\n", 1); } if(fds[1].revents & POLLIN) //PIPE { - printf("b\”"); nbytes = read(fork_pipe_out[0], buffer, sizeof(buffer)); write(STDOUT_FILENO, buffer, nbytes); - write(STDOUT_FILENO, "\n", 1); + //write(STDOUT_FILENO, "\n", 1); } } wait(0); } } +int exists(const char *fname) +{ + FILE *file; + if ((file = fopen(fname, "r"))) + { + fclose(file); + return 1; + } + return 0; +} + int main(int argc, char* argv[]) { printf("MegumInit explosion!\n"); - run_stdio("yes"); + + //char* arg[] = {"/bin/bash" ,(char*)0}; + + //run_start(arg[0], arg); + + char* arg2[] = {"/bin/toybox", "echo", "/test.asdf", NULL}; + + run_start(arg2[0], arg2); + + sleep(2); + + printf("%i\n", exists("/test.asdf")); + printf("\nEND\n"); + + sleep(99999); return 0; } -- cgit v1.2.3