summaryrefslogtreecommitdiff
path: root/init
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2019-04-09 19:20:23 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2019-04-09 19:20:23 +0200
commitbd7230debadfd090e7ccba20376e80611d239bd5 (patch)
tree7be66bf8df8180facf9db848c44ac6fcbeb0c1ec /init
parent13b5e77a8be5f41f333e150d25f228d4a35ccbef (diff)
downloadminilinux-bd7230debadfd090e7ccba20376e80611d239bd5.tar.gz
Stuff
Diffstat (limited to 'init')
-rw-r--r--init/init.c79
1 files changed, 63 insertions, 16 deletions
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 <stdio.h>
+#include <errno.h>
+#include <string.h>
#include <unistd.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <sys/types.h>
#include <poll.h>
-int run_start(char* _prog)
+#include <dirent.h>
+
+
+//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;
}