init: run /etc/rc on boot

This commit is contained in:
Matheus
2023-07-26 00:21:07 -03:00
parent dcaa20ae9c
commit ab129f6596
2 changed files with 37 additions and 6 deletions
+6 -1
View File
@@ -1 +1,6 @@
mkdir /tmp #!/usr/bin/bash
mkdir -p /dev
mount /dev devfs
mkdir -p /tmp
mount /tmp tmpfs
+31 -5
View File
@@ -7,6 +7,7 @@
int main() { int main() {
printf("init: Welcome to Astral!\n"); printf("init: Welcome to Astral!\n");
// set up initial environmental variables
struct passwd* pw = getpwuid(getuid()); struct passwd* pw = getpwuid(getuid());
if (pw == NULL) { if (pw == NULL) {
perror("init: failed to open /etc/passwd"); perror("init: failed to open /etc/passwd");
@@ -23,24 +24,49 @@ int main() {
setenv("PATH", "/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin", 1); setenv("PATH", "/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin", 1);
setenv("TERM", "linux", 1); setenv("TERM", "linux", 1);
printf("init: running /usr/bin/bash\n"); // run rc to initialize environment (mount filesystems, etc)
printf("init: running /etc/rc\n");
pid_t pid = fork(); pid_t pid = fork();
if (pid == -1) { if (pid == -1) {
perror("init: fork failed:"); perror("init: fork failed");
return EXIT_FAILURE;
}
if (pid == 0) {
execl("/usr/bin/bash", "/usr/bin/bash", "/etc/rc", NULL);
perror("init: exec /usr/bin/bash failed");
return EXIT_FAILURE;
}
int status = 0;
if (waitpid(pid, &status, 0) == -1) {
perror("init: waitpid failed");
return EXIT_FAILURE;
}
if (WEXITSTATUS(status)) {
printf("/etc/rc returned failure status %d\n", WEXITSTATUS(status));
return EXIT_FAILURE;
}
// start user shell
printf("init: running /usr/bin/bash\n");
pid = fork();
if (pid == -1) {
perror("init: fork failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (pid == 0) { if (pid == 0) {
execl("/usr/bin/bash", "/usr/bin/bash", "-l", NULL); execl("/usr/bin/bash", "/usr/bin/bash", "-l", NULL);
perror("init: execl /usr/bin/bash failed:"); perror("init: execl /usr/bin/bash failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
for (;;) { for (;;) {
int status; int status;
if (waitpid(-1, &status, 0) == -1) { if (waitpid(-1, &status, 0) == -1) {
perror("init: waitpid failed:"); perror("init: waitpid failed");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }