init(1) now restarts the shell upon crash.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-04-13 21:47:47 +02:00
parent 10291fcb38
commit a24e86e751
1 changed files with 22 additions and 11 deletions

View File

@ -28,13 +28,6 @@
#include <fcntl.h>
#include <unistd.h>
int parent(pid_t childid)
{
int status;
waitpid(childid, &status, 0);
return status;
}
int child()
{
const char* programname = "sh";
@ -46,6 +39,27 @@ int child()
return 2;
}
int runsystem()
{
pid_t childpid = fork();
if ( childpid < 0 ) { perror("fork"); return 2; }
if ( childpid )
{
int status;
waitpid(childpid, &status, 0);
// TODO: Use the proper macro!
if ( 128 <= status )
{
printf("Looks like the system crashed, trying to bring it back up.\n");
return runsystem();
}
return status;
}
return child();
}
int main(int argc, char* argv[])
{
if ( open("/dev/tty", O_RDONLY) != 0 ) { return 2; }
@ -56,9 +70,6 @@ int main(int argc, char* argv[])
printf("\r\e[m\e[J");
fflush(stdout);
pid_t childpid = fork();
if ( childpid < 0 ) { perror("fork"); return 2; }
return ( childpid == 0 ) ? child() : parent(childpid);
return runsystem();
}