summaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
authordzwdz2022-07-11 22:21:57 +0200
committerdzwdz2022-07-11 22:21:57 +0200
commit93af95b3419edfc127c6ea4245753540190c520e (patch)
treeafacc32ba93239f2cde81efd35aacb015647cf99 /src/user
parent2e79a2e5af3affa7a6a3becdffb1c91d89af90af (diff)
user: add shorthand close() and fork() wrappers for those syscalls
Diffstat (limited to 'src/user')
-rw-r--r--src/user/app/main.c6
-rw-r--r--src/user/app/shell.c6
-rw-r--r--src/user/fs/misc.c4
-rw-r--r--src/user/lib/esemaphore.c18
-rw-r--r--src/user/lib/stdlib.c13
-rw-r--r--src/user/lib/stdlib.h6
-rw-r--r--src/user/tests/main.c28
-rw-r--r--src/user/tests/pipe.c34
-rw-r--r--src/user/tests/semaphore.c8
-rw-r--r--src/user/tests/stress.c4
10 files changed, 70 insertions, 57 deletions
diff --git a/src/user/app/main.c b/src/user/app/main.c
index e59b5a6..f87133e 100644
--- a/src/user/app/main.c
+++ b/src/user/app/main.c
@@ -37,7 +37,7 @@ int main(void) {
MOUNT("/bind/", fs_passthru(NULL));
- if (_syscall_fork(0, NULL)) {
+ if (fork()) {
/* (used to) expose a bug in the kernel
* the program will flow like this:
* 1. we launch the forked init
@@ -54,7 +54,7 @@ int main(void) {
_syscall_exit(1);
}
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
if (!file_reopen(stdout, "/kdev/com1", 0)) {
printf("couldn't open /kdev/com1\n"); // TODO borked
_syscall_exit(1);
@@ -69,7 +69,7 @@ int main(void) {
}
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
if (!file_reopen(stdout, "/vga_tty", 0)) {
printf("couldn't open /vga_tty\n"); // TODO borked
_syscall_exit(1);
diff --git a/src/user/app/shell.c b/src/user/app/shell.c
index 0a8c44f..5bab2ab 100644
--- a/src/user/app/shell.c
+++ b/src/user/app/shell.c
@@ -111,7 +111,7 @@ static void cmd_hexdump(const char *args) {
printf("|\n");
}
- _syscall_close(fd);
+ close(fd);
}
static void cmd_touch(const char *args) {
@@ -120,7 +120,7 @@ static void cmd_touch(const char *args) {
printf("couldn't create file.\n");
return;
}
- _syscall_close(fd);
+ close(fd);
}
void shell_loop(void) {
@@ -157,7 +157,7 @@ void shell_loop(void) {
} else if (!strcmp(cmd, "exit")) {
_syscall_exit(0);
} else if (!strcmp(cmd, "fork")) {
- if (_syscall_fork(0, NULL))
+ if (fork())
_syscall_await();
else level++;
} else if (!strcmp(cmd, "run_tests")) {
diff --git a/src/user/fs/misc.c b/src/user/fs/misc.c
index bf09718..b3e1a2c 100644
--- a/src/user/fs/misc.c
+++ b/src/user/fs/misc.c
@@ -9,7 +9,7 @@ bool fork2_n_mount(const char *path) {
handle_t h;
if (_syscall_fork(FORK_NEWFS, &h) > 0) { /* parent */
_syscall_mount(h, path, strlen(path));
- _syscall_close(h);
+ close(h);
return true;
}
return false;
@@ -129,7 +129,7 @@ void fs_dir_inject(const char *path) {
case VFSOP_CLOSE:
if (data->delegate >= 0)
- _syscall_close(data->delegate);
+ close(data->delegate);
_syscall_fs_respond(NULL, 0, 0);
break;
diff --git a/src/user/lib/esemaphore.c b/src/user/lib/esemaphore.c
index ed42ee9..2fdd659 100644
--- a/src/user/lib/esemaphore.c
+++ b/src/user/lib/esemaphore.c
@@ -21,7 +21,7 @@ struct evil_sem *esem_new(int value) {
if (!(sem = malloc(sizeof *sem))) goto fail_malloc;
if (!_syscall_fork(FORK_NOREAP, NULL)) {
- _syscall_close(ends_signal[1]);
+ close(ends_signal[1]);
while (_syscall_read(ends_signal[0], NULL, 0, 0) >= 0) {
if (!_syscall_fork(FORK_NOREAP, NULL)) {
_syscall_write(ends_wait[1], NULL, 0, 0);
@@ -30,8 +30,8 @@ struct evil_sem *esem_new(int value) {
}
_syscall_exit(0);
}
- _syscall_close(ends_signal[0]);
- _syscall_close(ends_wait[1]);
+ close(ends_signal[0]);
+ close(ends_wait[1]);
sem->wait = ends_wait[0];
sem->signal = ends_signal[1];
@@ -40,16 +40,16 @@ struct evil_sem *esem_new(int value) {
return sem;
fail_malloc:
- _syscall_close(ends_signal[0]);
- _syscall_close(ends_signal[1]);
+ close(ends_signal[0]);
+ close(ends_signal[1]);
fail_signal:
- _syscall_close(ends_wait[0]);
- _syscall_close(ends_wait[1]);
+ close(ends_wait[0]);
+ close(ends_wait[1]);
return NULL;
}
void esem_free(struct evil_sem *sem) {
- _syscall_close(sem->wait);
- _syscall_close(sem->signal);
+ close(sem->wait);
+ close(sem->signal);
free(sem);
}
diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c
index c055d04..32b4f06 100644
--- a/src/user/lib/stdlib.c
+++ b/src/user/lib/stdlib.c
@@ -62,7 +62,7 @@ libc_file *file_open(const char *path, int flags) {
f = malloc(sizeof *f);
if (!f) {
- _syscall_close(h);
+ close(h);
return NULL;
}
f->pos = 0;
@@ -114,7 +114,16 @@ int file_write(libc_file *f, const char *buf, size_t len) {
}
void file_close(libc_file *f) {
- if (f->fd > 0) _syscall_close(f->fd);
+ if (f->fd > 0) close(f->fd);
if (f != &_stdin_null && f != &_stdout_null)
free(f);
}
+
+
+int fork(void) {
+ return _syscall_fork(0, NULL);
+}
+
+int close(handle_t h) {
+ return _syscall_close(h);
+}
diff --git a/src/user/lib/stdlib.h b/src/user/lib/stdlib.h
index 9bbcc5f..22227e3 100644
--- a/src/user/lib/stdlib.h
+++ b/src/user/lib/stdlib.h
@@ -1,8 +1,9 @@
#pragma once
-#include <user/lib/malloc.h>
#include <shared/mem.h>
+#include <shared/types.h>
#include <stdbool.h>
#include <stddef.h>
+#include <user/lib/malloc.h>
int printf(const char *fmt, ...);
int snprintf(char *str, size_t len, const char *fmt, ...);
@@ -21,3 +22,6 @@ int file_write(libc_file*, const char *buf, size_t len);
void file_close(libc_file*);
extern libc_file *stdin, *stdout;
+
+int fork(void);
+int close(handle_t h);
diff --git a/src/user/tests/main.c b/src/user/tests/main.c
index fd5ffca..5bfd3aa 100644
--- a/src/user/tests/main.c
+++ b/src/user/tests/main.c
@@ -5,7 +5,7 @@
#include <shared/syscalls.h>
static void run_forked(void (*fn)()) {
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
fn();
_syscall_exit(0);
} else {
@@ -24,7 +24,7 @@ static void test_await(void) {
int counts[16] = {0};
for (int i = 0; i < 16; i++)
- if (!_syscall_fork(0, NULL))
+ if (!fork())
_syscall_exit(i);
while ((ret = _syscall_await()) != ~0) {
@@ -42,12 +42,12 @@ static void test_faults(void) {
* reap all its children */
int await_cnt = 0;
- if (!_syscall_fork(0, NULL)) { // invalid memory access
+ if (!fork()) { // invalid memory access
asm volatile("movb $69, 0" ::: "memory");
printf("this shouldn't happen");
_syscall_exit(-1);
}
- if (!_syscall_fork(0, NULL)) { // #GP
+ if (!fork()) { // #GP
asm volatile("hlt" ::: "memory");
printf("this shouldn't happen");
_syscall_exit(-1);
@@ -90,7 +90,7 @@ static void test_memflag(void) {
memset(page, 77, 4096); // write to it
_syscall_memflag(page, 4096, 0); // free it
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
memset(page, 11, 4096); // should segfault
_syscall_exit(0);
} else {
@@ -118,8 +118,8 @@ static void test_dup(void) {
handle_t h1, h2;
assert(_syscall_pipe(pipe, 0) >= 0);
- if (!_syscall_fork(0, NULL)) {
- _syscall_close(pipe[0]);
+ if (!fork()) {
+ close(pipe[0]);
h1 = _syscall_dup(pipe[1], -1, 0);
assert(h1 >= 0);
@@ -132,7 +132,7 @@ static void test_dup(void) {
_syscall_write(h1, "h1", 2, 0);
_syscall_write(h2, "h2", 2, 0);
- _syscall_close(pipe[1]);
+ close(pipe[1]);
_syscall_write(h1, "h1", 2, 0);
_syscall_write(h2, "h2", 2, 0);
@@ -140,22 +140,22 @@ static void test_dup(void) {
assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]);
assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]);
assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]);
- _syscall_close(h1);
- _syscall_close(h2);
+ close(h1);
+ close(h2);
assert(_syscall_dup(pipe[1], h2, 0) == h2);
_syscall_write(h2, "h2", 2, 0);
- _syscall_close(h2);
+ close(h2);
assert(_syscall_dup(pipe[1], h1, 0) == h1);
_syscall_write(h1, "h1", 2, 0);
- _syscall_close(h1);
+ close(h1);
_syscall_exit(0);
} else {
char buf[16];
size_t count = 0;
- _syscall_close(pipe[1]);
+ close(pipe[1]);
while (_syscall_read(pipe[0], buf, sizeof buf, 0) >= 0)
count++;
assert(count == 7);
@@ -163,7 +163,7 @@ static void test_dup(void) {
}
- _syscall_close(pipe[0]);
+ close(pipe[0]);
}
static void test_malloc(void) {
diff --git a/src/user/tests/pipe.c b/src/user/tests/pipe.c
index e973993..2e964f9 100644
--- a/src/user/tests/pipe.c
+++ b/src/user/tests/pipe.c
@@ -13,7 +13,7 @@ void test_pipe(void) {
/* test regular reads / writes */
assert(_syscall_pipe(ends, 0) >= 0);
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
/* those repeated asserts ensure that you can't read/write to the wrong ends */
assert(_syscall_read(ends[1], buf, 16, 0) < 0);
assert(_syscall_write(ends[0], buf, 16, 0) < 0);
@@ -45,70 +45,70 @@ void test_pipe(void) {
_syscall_await();
}
- _syscall_close(ends[0]);
- _syscall_close(ends[1]);
+ close(ends[0]);
+ close(ends[1]);
/* writing to pipes with one end closed */
assert(_syscall_pipe(ends, 0) >= 0);
for (int i = 0; i < 16; i++) {
- if (!_syscall_fork(0, NULL)) {
- _syscall_close(ends[1]);
+ if (!fork()) {
+ close(ends[1]);
assert(_syscall_read(ends[0], buf, 16, 0) < 0);
_syscall_exit(0);
}
}
- _syscall_close(ends[1]);
+ close(ends[1]);
for (int i = 0; i < 16; i++)
_syscall_await();
- _syscall_close(ends[0]);
+ close(ends[0]);
assert(_syscall_pipe(ends, 0) >= 0);
for (int i = 0; i < 16; i++) {
- if (!_syscall_fork(0, NULL)) {
- _syscall_close(ends[0]);
+ if (!fork()) {
+ close(ends[0]);
assert(_syscall_write(ends[1], buf, 16, 0) < 0);
_syscall_exit(0);
}
}
- _syscall_close(ends[0]);
+ close(ends[0]);
for (int i = 0; i < 16; i++)
_syscall_await();
- _syscall_close(ends[1]);
+ close(ends[1]);
/* queueing */
assert(_syscall_pipe(ends, 0) >= 0);
for (int i = 0; i < 16; i++) {
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
assert(_syscall_write(ends[1], pipe_msgs[0], 5, -1) == 5);
_syscall_exit(0);
}
}
- _syscall_close(ends[1]);
+ close(ends[1]);
for (int i = 0; i < 16; i++) {
assert(_syscall_read(ends[0], buf, sizeof buf, 0) == 5);
_syscall_await();
}
assert(_syscall_read(ends[0], buf, sizeof buf, 0) < 0);
- _syscall_close(ends[0]);
+ close(ends[0]);
assert(_syscall_pipe(ends, 0) >= 0);
for (int i = 0; i < 16; i++) {
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
memset(buf, 0, sizeof buf);
assert(_syscall_read(ends[0], buf, 5, -1) == 5);
assert(!memcmp(buf, pipe_msgs[1], 5));
_syscall_exit(0);
}
}
- _syscall_close(ends[0]);
+ close(ends[0]);
for (int i = 0; i < 16; i++) {
assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) == 5);
_syscall_await();
}
assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) < 0);
- _syscall_close(ends[1]);
+ close(ends[1]);
// not a to.do detect when all processes that can read are stuck on writing to the pipe and vice versa
diff --git a/src/user/tests/semaphore.c b/src/user/tests/semaphore.c
index e5aacaa..836edf5 100644
--- a/src/user/tests/semaphore.c
+++ b/src/user/tests/semaphore.c
@@ -37,11 +37,11 @@ void test_semaphore(void) {
handle_t pipe[2];
assert(_syscall_pipe(pipe, 0) >= 0);
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
sem1 = esem_new(0);
sem2 = esem_new(0);
assert(sem1 && sem2);
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
odd(pipe[1], sem1, sem2);
_syscall_exit(69);
} else {
@@ -56,7 +56,7 @@ void test_semaphore(void) {
sem1 = esem_new(0);
sem2 = esem_new(0);
assert(sem1 && sem2);
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
even(pipe[1], sem1, sem2);
_syscall_exit(69);
} else {
@@ -69,7 +69,7 @@ void test_semaphore(void) {
_syscall_exit(0);
} else {
- _syscall_close(pipe[1]);
+ close(pipe[1]);
char buf[16];
size_t pos = 0;
diff --git a/src/user/tests/stress.c b/src/user/tests/stress.c
index 51b9c4e..5d29e87 100644
--- a/src/user/tests/stress.c
+++ b/src/user/tests/stress.c
@@ -5,7 +5,7 @@
#include <shared/syscalls.h>
static void run_forked(void (*fn)()) {
- if (!_syscall_fork(0, NULL)) {
+ if (!fork()) {
fn();
_syscall_exit(0);
} else {
@@ -18,7 +18,7 @@ static void run_forked(void (*fn)()) {
static void stress_fork(void) {
for (size_t i = 0; i < 2048; i++) {
- if (!_syscall_fork(0, NULL)) _syscall_exit(0);
+ if (!fork()) _syscall_exit(0);
_syscall_await();
}
}