From c2c03f41e078481921bad82487eded0fc51ebb59 Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Tue, 2 Dec 2014 01:02:49 +0100 Subject: further work on fork and friends --- userspace/foolshell.c | 13 ++++++++++++- userspace/init.c | 15 +++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) (limited to 'userspace') diff --git a/userspace/foolshell.c b/userspace/foolshell.c index 78ba327..7d2291e 100644 --- a/userspace/foolshell.c +++ b/userspace/foolshell.c @@ -109,7 +109,7 @@ int process(char *buf) if(!strcmp(command,"help")) { - puts("foolshell: supported built-in commands: 'help', 'echo [string]', 'malloc [bytes]', 'free [address]', 'getenv [var]', 'putenv/setenv [var] [val]', 'env' 'cd [dir]'"); + puts("foolshell: supported built-in commands: 'help', 'echo [string]', 'malloc [bytes]', 'free [address]', 'getenv [var]', 'putenv/setenv [var] [val]', 'env' 'cd [dir]', 'exit'"); } else if(!strcmp(command,"cd")) { @@ -172,6 +172,11 @@ int process(char *buf) setenv("PWD",buf,1); + } + else if(!strcmp(command,"exit")) + { + exit(1); + } else if(!strcmp(command,"echo")) { @@ -215,12 +220,18 @@ int process(char *buf) if(pid!=0) { printf("new task pid: %i \n",pid); + } + if(pid==0) + { char buf[256]; sprintf(buf,"%s/%s",getenv("PATH"),token[0]); + sprintf(buf,"%s/%s","/bin",token[0]); execve(buf,token,environ); puts("foolshell: command not found"); exit(1); } + int status; + wait(&status); } return 0; diff --git a/userspace/init.c b/userspace/init.c index fd562a9..8c3bcd7 100644 --- a/userspace/init.c +++ b/userspace/init.c @@ -1,21 +1,20 @@ + int main(int argc, char **argv) { int pid=fork(); - if(pid!=0) + if(pid==0) { - printf("fool-init: forked child (pid: %i) spawning a Fools Shell\n",pid); execve("/bin/foolshell",0,0); - } else { - // TODO: wait for child and respawn shell if needed! - volatile int i=0; - while(1) - { + printf("fool-init: forked child (pid: %i) spawning a Fools Shell\n",pid); + int status; + int pid_ret=wait(&status); + puts("child state has changed!"); + execve("/bin/init",0,0); - } } return 0; } -- cgit v1.2.3