2007-08-28 19:04:36 +00:00
|
|
|
// init: The initial user-level program
|
|
|
|
|
2019-06-11 13:57:14 +00:00
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/stat.h"
|
2020-08-19 00:48:53 +00:00
|
|
|
#include "kernel/spinlock.h"
|
|
|
|
#include "kernel/sleeplock.h"
|
|
|
|
#include "kernel/fs.h"
|
|
|
|
#include "kernel/file.h"
|
2019-06-11 13:57:14 +00:00
|
|
|
#include "user/user.h"
|
|
|
|
#include "kernel/fcntl.h"
|
2006-08-11 13:55:18 +00:00
|
|
|
|
2009-05-31 05:12:21 +00:00
|
|
|
char *argv[] = { "sh", 0 };
|
2006-08-11 13:55:18 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2007-08-08 08:39:23 +00:00
|
|
|
int pid, wpid;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-08-29 19:06:37 +00:00
|
|
|
if(open("console", O_RDWR) < 0){
|
2020-08-19 00:48:53 +00:00
|
|
|
mknod("console", CONSOLE, 0);
|
2006-08-29 19:06:37 +00:00
|
|
|
open("console", O_RDWR);
|
2006-08-11 13:55:18 +00:00
|
|
|
}
|
2006-09-08 14:36:44 +00:00
|
|
|
dup(0); // stdout
|
|
|
|
dup(0); // stderr
|
2006-08-11 13:55:18 +00:00
|
|
|
|
2006-09-06 18:47:51 +00:00
|
|
|
for(;;){
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("init: starting sh\n");
|
2006-08-11 13:55:18 +00:00
|
|
|
pid = fork();
|
2006-08-29 19:06:37 +00:00
|
|
|
if(pid < 0){
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("init: fork failed\n");
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-08-29 19:06:37 +00:00
|
|
|
}
|
2006-08-11 13:55:18 +00:00
|
|
|
if(pid == 0){
|
2009-05-31 05:12:21 +00:00
|
|
|
exec("sh", argv);
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("init: exec sh failed\n");
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-08-29 19:06:37 +00:00
|
|
|
}
|
2020-08-15 09:46:32 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
}
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
2006-08-11 13:55:18 +00:00
|
|
|
}
|
|
|
|
}
|