Add pipes to shell, add wc
This commit is contained in:
+2
-2
@@ -79,10 +79,10 @@ void kernel_main() {
|
||||
fs_read(shell_node, 0, shell_node->size, shell_code);
|
||||
fs_close(shell_node);
|
||||
|
||||
process_t *terminal = process_create(kernel, terminal_code, terminal_node->size);
|
||||
process_t *terminal = process_create(kernel, terminal_code, terminal_node->size, 0, 1);
|
||||
memory_free(terminal_code);
|
||||
|
||||
process_t *shell = process_create(kernel, shell_code, shell_node->size);
|
||||
process_t *shell = process_create(kernel, shell_code, shell_node->size, 0, 1);
|
||||
memory_free(shell_code);
|
||||
|
||||
uint64_t *terminal_stack = (uint64_t *)terminal->user_stack;
|
||||
|
||||
@@ -31,6 +31,9 @@ path_t *path_open(const path_t *base, const char *path, uint64_t flags) {
|
||||
}
|
||||
|
||||
if (string_empty(path_components[i]) || string_equal(path_components[i], ".")) {
|
||||
if (i == path_length - 1 && result->depth) {
|
||||
// TODO Apply flags to current top of stack.
|
||||
}
|
||||
continue;
|
||||
} else if (string_equal(path_components[i], "..")) {
|
||||
if (result->depth > 1) {
|
||||
|
||||
@@ -14,7 +14,7 @@ static uint64_t free_pid = 1;
|
||||
|
||||
process_t *current_process;
|
||||
|
||||
process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t size) {
|
||||
process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t size, uint64_t stdin, uint64_t stdout) {
|
||||
if (size >= USER_VIRTUAL_HEAP - USER_VIRTUAL_CODE) {
|
||||
return NUL;
|
||||
}
|
||||
@@ -29,6 +29,10 @@ process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t
|
||||
return NUL;
|
||||
}
|
||||
|
||||
if (stdin >= MAX_FDS || !parent->fds[stdin] || stdout > MAX_FDS || !parent->fds[stdout]) {
|
||||
return NUL;
|
||||
}
|
||||
|
||||
process_t *proc = processes[free_pi] = memory_allocate(sizeof(process_t));
|
||||
|
||||
proc->parent = parent;
|
||||
@@ -71,8 +75,8 @@ process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t
|
||||
proc->pid = free_pid++;
|
||||
proc->state = PROCESS_RUNNING;
|
||||
proc->cwd = path_open_again(parent->cwd);
|
||||
proc->fds[0] = parent->fds[0];
|
||||
proc->fds[1] = parent->fds[1];
|
||||
proc->fds[0] = parent->fds[stdin];
|
||||
proc->fds[1] = parent->fds[stdout];
|
||||
proc->free_fd = 2;
|
||||
proc->code = EXIT_CODE_OK;
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ extern process_t *current_process;
|
||||
|
||||
extern void process_trampoline();
|
||||
|
||||
process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t size);
|
||||
process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t size, uint64_t stdin, uint64_t stdout);
|
||||
|
||||
process_t *process_get(uint64_t pid);
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ syscall_entry:
|
||||
push r14
|
||||
push r15
|
||||
|
||||
mov r9, r8 ; arg5
|
||||
mov r8, r10 ; arg4
|
||||
mov rcx, rdx ; arg3
|
||||
mov rdx, rsi ; arg2
|
||||
|
||||
+17
-4
@@ -2,6 +2,7 @@
|
||||
#include "src/kernel/fs.h"
|
||||
#include "src/kernel/gdt.h"
|
||||
#include "src/kernel/path.h"
|
||||
#include "src/kernel/pipe.h"
|
||||
#include "src/kernel/process.h"
|
||||
#include "src/kernel/stream.h"
|
||||
#include "src/lib/layout.h"
|
||||
@@ -89,7 +90,7 @@ static uint64_t chdir(const char *path) {
|
||||
return current_process->cwd->depth;
|
||||
}
|
||||
|
||||
static uint64_t spawn(const char *path, uint64_t argc, const char **argv) {
|
||||
static uint64_t spawn(const char *path, uint64_t argc, const char **argv, uint64_t stdin, uint64_t stdout) {
|
||||
fs_node_t *node = path_open_node(current_process->cwd, path, OPEN_FILE);
|
||||
if (!node) {
|
||||
return EXIT_CODE_NOT_FOUND;
|
||||
@@ -109,7 +110,7 @@ static uint64_t spawn(const char *path, uint64_t argc, const char **argv) {
|
||||
memory_copy(argv[i], sizes[i], blob + offsets[i]);
|
||||
}
|
||||
|
||||
process_t *child = process_create(current_process, bin, node->size);
|
||||
process_t *child = process_create(current_process, bin, node->size, stdin, stdout);
|
||||
|
||||
memory_free(bin);
|
||||
fs_close(node);
|
||||
@@ -192,6 +193,16 @@ static uint64_t remove(const char *path) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint64_t pipe(uint64_t *write_fd, uint64_t *read_fd) {
|
||||
if (current_process->free_fd + 2 >= MAX_FDS) {
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
*write_fd = current_process->free_fd++;
|
||||
*read_fd = current_process->free_fd++;
|
||||
pipe_init(¤t_process->fds[*write_fd], ¤t_process->fds[*read_fd]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void exit(exit_code_t code) {
|
||||
current_process->state = PROCESS_ZOMBIE;
|
||||
current_process->code = code;
|
||||
@@ -206,7 +217,7 @@ static void exit(exit_code_t code) {
|
||||
process_next();
|
||||
}
|
||||
|
||||
uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, __attribute__((unused)) uint64_t arg4) {
|
||||
uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) {
|
||||
switch (func) {
|
||||
case SYSCALL_READ:
|
||||
return read(arg1, arg2, (char *)arg3);
|
||||
@@ -217,7 +228,7 @@ uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t
|
||||
case SYSCALL_CHDIR:
|
||||
return chdir((const char *)arg1);
|
||||
case SYSCALL_SPAWN:
|
||||
return spawn((const char *)arg1, arg2, (const char **)arg3);
|
||||
return spawn((const char *)arg1, arg2, (const char **)arg3, arg4, arg5);
|
||||
case SYSCALL_WAIT:
|
||||
return wait(arg1);
|
||||
case SYSCALL_OPEN:
|
||||
@@ -228,6 +239,8 @@ uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t
|
||||
return truncate(arg1, arg2);
|
||||
case SYSCALL_REMOVE:
|
||||
return remove((char *)arg1);
|
||||
case SYSCALL_PIPE:
|
||||
return pipe((uint64_t *)arg1, (uint64_t *)arg2);
|
||||
case SYSCALL_EXIT:
|
||||
exit(arg1);
|
||||
return 0;
|
||||
|
||||
@@ -4,4 +4,4 @@
|
||||
|
||||
void syscall_init();
|
||||
|
||||
uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4);
|
||||
uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
|
||||
|
||||
Reference in New Issue
Block a user