diff --git a/libmaxsi/process.cpp b/libmaxsi/process.cpp index 7687f3a0..a7f3954b 100644 --- a/libmaxsi/process.cpp +++ b/libmaxsi/process.cpp @@ -75,12 +75,7 @@ namespace Maxsi DUAL_FUNCTION(void, exit, Exit, (int status)) { - // TODO: Once wait() works and is used in the shell, call _exit! - //_exit(status); - const char* sh = "sh"; - const char* argv[] = { sh }; - Execute("sh", 1, argv); - while(true); + _exit(status); } DUAL_FUNCTION(pid_t, fork, Fork, ()) diff --git a/utils/init.cpp b/utils/init.cpp index f0481d7f..4d012f53 100644 --- a/utils/init.cpp +++ b/utils/init.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -7,8 +8,9 @@ using namespace Maxsi; int parent(pid_t childid) { - // TODO: wait for child to finish. - while ( true ) { Thread::Sleep(100000); } + int status; + waitpid(childid, &status, 0); + return status; } int child() @@ -18,7 +20,7 @@ int child() Process::Execute(programname, 1, newargv); - return 1; + return 2; } int main(int argc, char* argv[]) @@ -31,7 +33,7 @@ int main(int argc, char* argv[]) if ( childpid < 0 ) { printf("init: unable to fork a child\n"); - return 1; + return 2; } return ( childpid == 0 ) ? child() : parent(childpid); diff --git a/utils/mxsh.cpp b/utils/mxsh.cpp index 9d9020b6..e6775902 100644 --- a/utils/mxsh.cpp +++ b/utils/mxsh.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include #include #include #include @@ -43,12 +46,24 @@ void command() if ( String::Compare(command, "$$") == 0 ) { printf("%u\n", Process::GetPID()); return; } if ( String::Compare(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); return; } + if ( String::Compare(command, "exit") == 0 ) { exit(0); return; } + + pid_t child = fork(); + if ( child < 0 ) { printf("fork failed\n"); return; } + if ( child != 0 ) + { + int status; + pid_t childpid = wait(&status); + return; + } // Replace the current process with another process image. Process::Execute(command, 0, NULL); // This is clever. This only happens if the program didn't change. printf("%s: command not found\n", command); + + exit(127); } int main(int argc, char* argv[])