2007-08-28 21:04:36 +02:00
|
|
|
// init: The initial user-level program
|
|
|
|
|
2019-06-11 15:57:14 +02:00
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/stat.h"
|
2020-08-19 02:48:53 +02:00
|
|
|
#include "kernel/spinlock.h"
|
|
|
|
#include "kernel/sleeplock.h"
|
|
|
|
#include "kernel/fs.h"
|
|
|
|
#include "kernel/file.h"
|
2019-06-11 15:57:14 +02:00
|
|
|
#include "user/user.h"
|
|
|
|
#include "kernel/fcntl.h"
|
2006-08-11 15:55:18 +02:00
|
|
|
|
2009-05-31 07:12:21 +02:00
|
|
|
char *argv[] = { "sh", 0 };
|
2006-08-11 15:55:18 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2007-08-08 10:39:23 +02:00
|
|
|
int pid, wpid;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2024-06-15 16:55:06 +02:00
|
|
|
if(open("console", O_RDWR) < 0) {
|
2020-08-19 02:48:53 +02:00
|
|
|
mknod("console", CONSOLE, 0);
|
2006-08-29 21:06:37 +02:00
|
|
|
open("console", O_RDWR);
|
2006-08-11 15:55:18 +02:00
|
|
|
}
|
2024-06-15 16:55:06 +02:00
|
|
|
dup(0); // stdout
|
|
|
|
dup(0); // stderr
|
2006-08-11 15:55:18 +02:00
|
|
|
|
2024-06-15 16:55:06 +02:00
|
|
|
for(;;) {
|
2019-08-27 19:13:03 +02:00
|
|
|
printf("init: starting sh\n");
|
2006-08-11 15:55:18 +02:00
|
|
|
pid = fork();
|
2024-06-15 16:55:06 +02:00
|
|
|
if(pid < 0) {
|
2019-08-27 19:13:03 +02:00
|
|
|
printf("init: fork failed\n");
|
2019-09-11 16:04:40 +02:00
|
|
|
exit(1);
|
2006-08-29 21:06:37 +02:00
|
|
|
}
|
2024-06-15 16:55:06 +02:00
|
|
|
if(pid == 0) {
|
2009-05-31 07:12:21 +02:00
|
|
|
exec("sh", argv);
|
2019-08-27 19:13:03 +02:00
|
|
|
printf("init: exec sh failed\n");
|
2019-09-11 16:04:40 +02:00
|
|
|
exit(1);
|
2006-08-29 21:06:37 +02:00
|
|
|
}
|
2020-08-15 11:46:32 +02:00
|
|
|
|
2024-06-15 16:55:06 +02:00
|
|
|
for(;;) {
|
2020-08-15 11:46:32 +02:00
|
|
|
// this call to wait() returns if the shell exits,
|
|
|
|
// or if a parentless process exits.
|
2024-06-15 16:55:06 +02:00
|
|
|
wpid = wait((int *)0);
|
|
|
|
if(wpid == pid) {
|
2020-08-15 11:46:32 +02:00
|
|
|
// the shell exited; restart it.
|
|
|
|
break;
|
2024-06-15 16:55:06 +02:00
|
|
|
} else if(wpid < 0) {
|
2020-08-15 11:46:32 +02:00
|
|
|
printf("init: wait returned an error\n");
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
// it was a parentless process; do nothing.
|
|
|
|
}
|
2019-07-05 18:33:26 +02:00
|
|
|
}
|
2006-08-11 15:55:18 +02:00
|
|
|
}
|
|
|
|
}
|