58 lines
1.1 KiB
C
58 lines
1.1 KiB
C
|
// init: The initial user-level program
|
||
|
|
||
|
#include "kernel/types.h"
|
||
|
#include "kernel/stat.h"
|
||
|
#include "kernel/sched.h"
|
||
|
#include "kernel/fs.h"
|
||
|
#include "kernel/file.h"
|
||
|
#include "user/user.h"
|
||
|
#include "kernel/fcntl.h"
|
||
|
|
||
|
char *argv[2];// = { "sh", 0 };
|
||
|
|
||
|
int
|
||
|
main(void)
|
||
|
{
|
||
|
int pid, wpid;
|
||
|
|
||
|
argv[0] = "sh";
|
||
|
argv[1] = (void*) 0UL;
|
||
|
|
||
|
if(open("console", O_RDWR) < 0){
|
||
|
mknod("console", CONSOLE, 0);
|
||
|
open("console", O_RDWR);
|
||
|
}
|
||
|
dup(0); // stdout
|
||
|
dup(0); // stderr
|
||
|
|
||
|
for(;;){
|
||
|
printf("init: starting sh\n");
|
||
|
pid = fork();
|
||
|
if(pid < 0){
|
||
|
printf("init: fork failed\n");
|
||
|
exit(1);
|
||
|
}
|
||
|
if(pid == 0){
|
||
|
prio(getpid(), 4, 0);
|
||
|
exec("sh", argv);
|
||
|
printf("init: exec sh failed\n");
|
||
|
exit(1);
|
||
|
}
|
||
|
|
||
|
for(;;){
|
||
|
// this call to wait() returns if the shell exits,
|
||
|
// or if a parentless process exits.
|
||
|
wpid = wait((int *) 0);
|
||
|
if(wpid == pid){
|
||
|
// the shell exited; restart it.
|
||
|
break;
|
||
|
} else if(wpid < 0){
|
||
|
printf("init: wait returned an error\n");
|
||
|
exit(1);
|
||
|
} else {
|
||
|
// it was a parentless process; do nothing.
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|