Compare commits

...

3 Commits

Author SHA1 Message Date
a815beacfb sh: add builtins 2022-10-19 20:34:10 +02:00
20db8eaba6 Init: exit if the primary child process exits 2022-10-19 20:33:59 +02:00
51665a04b7 Kernel: Restart if init exits 2022-10-19 20:33:41 +02:00
3 changed files with 43 additions and 3 deletions

View File

@ -67,11 +67,14 @@ int main()
return 1;
}
pid_t result;
for (;;)
{
while (wait(NULL) == 0) // No child has exited yet
while ((result = wait(NULL)) == 0) // No child has exited yet
{
msleep(100);
}
if (result == child) { return 0; }
}
}

View File

@ -1,6 +1,7 @@
#include <luna.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
@ -9,8 +10,8 @@ static int status;
typedef struct
{
char* buffer;
long size;
long capacity;
size_t size;
size_t capacity;
} command;
void show_prompt()
@ -18,6 +19,33 @@ void show_prompt()
printf("[%ld]> ", getpid());
}
int command_matches(command* cmd, const char* string)
{
if (cmd->size <= strlen(string)) // cmd->size includes null terminator
return 0;
return strncmp(cmd->buffer, string, strlen(string)) == 0;
}
int command_matches_exactly(command* cmd, const char* string)
{
if (cmd->size <= strlen(string)) // cmd->size includes null terminator
return 0;
if (cmd->size > (strlen(string) + 1)) return 0;
return strncmp(cmd->buffer, string, strlen(string)) == 0;
}
int command_match_builtins(command* cmd)
{
if (command_matches(cmd, "exit ")) { exit(atoi(cmd->buffer + 5)); }
if (command_matches_exactly(cmd, "exit")) { exit(0); }
if (command_matches_exactly(cmd, "pid"))
{
printf("pid %ld, ppid %ld\n", getpid(), getppid());
return 1;
}
return 0;
}
void command_expand(command* cmd, long new_capacity)
{
char* buffer = realloc(cmd->buffer, new_capacity);
@ -58,6 +86,12 @@ void command_clear(command* cmd)
void command_execute(command* cmd)
{
command_push(cmd, '\0');
if (command_match_builtins(cmd))
{
command_clear(cmd);
show_prompt();
return;
}
pid_t child = fork();
if (child < 0)
{

View File

@ -7,6 +7,7 @@
#include "memory/PMM.h"
#include "memory/VMM.h"
#include "misc/hang.h"
#include "misc/reboot.h"
#include "misc/utils.h"
#include "panic/Panic.h"
#include "std/assert.h"
@ -280,6 +281,7 @@ void Scheduler::task_exit(Context* context, int64_t status)
return true;
});
}
else { reboot(); }
task_yield(context);
}
@ -298,6 +300,7 @@ void Scheduler::task_misbehave(Context* context, int64_t status)
return true;
});
}
else { reboot(); }
task_yield(context);
}