From d54dcb2efc4be344900a7721ac4b65b47840c5d2 Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Tue, 26 Jul 2022 22:21:52 +0200
Subject: user/libc: exit()

What an interesting commit.
---
 src/user/app/init/driver/ansiterm.c |  3 ++-
 src/user/app/init/driver/ps2.c      |  5 +++--
 src/user/app/init/driver/termcook.c |  2 +-
 src/user/app/init/driver/tmpfs.c    |  3 ++-
 src/user/app/init/main.c            | 16 ++++++++--------
 src/user/app/init/shell.c           |  8 ++++----
 src/user/app/init/tests/main.c      | 20 ++++++++++----------
 src/user/app/init/tests/pipe.c      | 10 +++++-----
 src/user/app/init/tests/semaphore.c |  6 +++---
 src/user/app/init/tests/stress.c    |  4 ++--
 src/user/app/init/tests/tests.h     |  2 +-
 src/user/app/testelf/main.c         |  3 ++-
 12 files changed, 43 insertions(+), 39 deletions(-)

(limited to 'src/user/app')

diff --git a/src/user/app/init/driver/ansiterm.c b/src/user/app/init/driver/ansiterm.c
index 4b66d6f..b5d3772 100644
--- a/src/user/app/init/driver/ansiterm.c
+++ b/src/user/app/init/driver/ansiterm.c
@@ -1,6 +1,7 @@
 #include "driver.h"
 #include <camellia/syscalls.h>
 #include <stdbool.h>
+#include <unistd.h>
 
 struct vga_cell {
 	unsigned char c;
@@ -95,5 +96,5 @@ void ansiterm_drv(void) {
 		}
 	}
 
-	_syscall_exit(1);
+	exit(1);
 }
diff --git a/src/user/app/init/driver/ps2.c b/src/user/app/init/driver/ps2.c
index 6aed2fb..532695f 100644
--- a/src/user/app/init/driver/ps2.c
+++ b/src/user/app/init/driver/ps2.c
@@ -2,6 +2,7 @@
 #include <camellia/syscalls.h>
 #include <shared/container/ring.h>
 #include <stdbool.h>
+#include <unistd.h>
 
 
 static const char keymap_lower[] = {
@@ -79,8 +80,8 @@ static void main_loop(void) {
 
 void ps2_drv(void) {
 	fd = _syscall_open("/kdev/ps2", 9, 0);
-	if (fd < 0) _syscall_exit(1);
+	if (fd < 0) exit(1);
 
 	main_loop();
-	_syscall_exit(0);
+	exit(0);
 }
diff --git a/src/user/app/init/driver/termcook.c b/src/user/app/init/driver/termcook.c
index 59cef77..3dc6f32 100644
--- a/src/user/app/init/driver/termcook.c
+++ b/src/user/app/init/driver/termcook.c
@@ -63,7 +63,7 @@ void termcook(void) {
 	if (!fork()) {
 		close(stdin_pipe[0]);
 		line_editor(0, stdin_pipe[1]);
-		_syscall_exit(0);
+		exit(0);
 	}
 	/* 0 is stdin, like in unix */
 	_syscall_dup(stdin_pipe[0], 0, 0);
diff --git a/src/user/app/init/driver/tmpfs.c b/src/user/app/init/driver/tmpfs.c
index 83c42c7..d9f73ab 100644
--- a/src/user/app/init/driver/tmpfs.c
+++ b/src/user/app/init/driver/tmpfs.c
@@ -2,6 +2,7 @@
 #include <shared/mem.h>
 #include <stddef.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 struct node {
 	const char *name;
@@ -128,5 +129,5 @@ void tmpfs_drv(void) {
 		}
 	}
 
-	_syscall_exit(1);
+	exit(1);
 }
diff --git a/src/user/app/init/main.c b/src/user/app/init/main.c
index 71220af..426933f 100644
--- a/src/user/app/init/main.c
+++ b/src/user/app/init/main.c
@@ -38,40 +38,40 @@ int main(void) {
 		 * of a dead process, which is invalid state
 		 */
 		_syscall_await();
-		_syscall_exit(1);
+		exit(1);
 	}
 
 	if (!fork()) {
 		if (!freopen("/kdev/com1", "a+", stdout)) {
 			printf("couldn't open /kdev/com1\n"); // TODO borked
-			_syscall_exit(1);
+			exit(1);
 		}
 		if (!freopen("/kdev/com1", "r", stdin)) {
 			printf("couldn't open /kdev/com1\n");
-			_syscall_exit(1);
+			exit(1);
 		}
 		termcook();
 
 		shell_loop();
-		_syscall_exit(1);
+		exit(1);
 	}
 
 	if (!fork()) {
 		if (!freopen("/vga_tty", "a+", stdout)) {
 			printf("couldn't open /vga_tty\n"); // TODO borked
-			_syscall_exit(1);
+			exit(1);
 		}
 		if (!freopen("/keyboard", "r", stdin)) {
 			printf("couldn't open /keyboard\n");
-			_syscall_exit(1);
+			exit(1);
 		}
 		termcook();
 
 		shell_loop();
-		_syscall_exit(1);
+		exit(1);
 	}
 
 	_syscall_await();
 	printf("init: quitting\n");
-	_syscall_exit(0);
+	exit(0);
 }
diff --git a/src/user/app/init/shell.c b/src/user/app/init/shell.c
index e53d7d9..85e56e1 100644
--- a/src/user/app/init/shell.c
+++ b/src/user/app/init/shell.c
@@ -136,7 +136,7 @@ void shell_loop(void) {
 
 		readline(buf, 256);
 		if (feof(stdin))
-			_syscall_exit(0);
+			exit(0);
 		redir = strtrim(strsplit(buf, '>'));
 		cmd = strtrim(buf);
 		args = strtrim(strsplit(cmd, 0));
@@ -146,7 +146,7 @@ void shell_loop(void) {
 			_syscall_mount(-1, args, strlen(args));
 			continue;
 		} else if (!strcmp(cmd, "exit")) {
-			_syscall_exit(0);
+			exit(0);
 			continue;
 		} else if (!strcmp(cmd, "fork")) {
 			if (!fork()) level++;
@@ -157,7 +157,7 @@ void shell_loop(void) {
 		if (!fork()) {
 			if (redir && !freopen(redir, "w", stdout)) {
 				// TODO stderr
-				_syscall_exit(0);
+				exit(0);
 			}
 
 			if (!strcmp(cmd, "echo")) {
@@ -196,7 +196,7 @@ void shell_loop(void) {
 			} else {
 				printf("unknown command :(\n");
 			}
-			_syscall_exit(0);
+			exit(0);
 		} else {
 			_syscall_await();
 		}
diff --git a/src/user/app/init/tests/main.c b/src/user/app/init/tests/main.c
index cf59925..7036965 100644
--- a/src/user/app/init/tests/main.c
+++ b/src/user/app/init/tests/main.c
@@ -11,7 +11,7 @@
 static void run_forked(void (*fn)()) {
 	if (!fork()) {
 		fn();
-		_syscall_exit(0);
+		exit(0);
 	} else {
 		/* successful tests must return 0
 		 * TODO add a better fail msg */
@@ -30,7 +30,7 @@ static void test_await(void) {
 
 	for (int i = 0; i < 16; i++)
 		if (!fork())
-			_syscall_exit(i);
+			exit(i);
 
 	while ((ret = _syscall_await()) != ~0) {
 		assert(0 <= ret && ret < 16);
@@ -50,12 +50,12 @@ static void test_faults(void) {
 	if (!fork()) { // invalid memory access
 		asm volatile("movb $69, 0" ::: "memory");
 		printf("this shouldn't happen");
-		_syscall_exit(-1);
+		exit(-1);
 	}
 	if (!fork()) { // #GP
 		asm volatile("hlt" ::: "memory");
 		printf("this shouldn't happen");
-		_syscall_exit(-1);
+		exit(-1);
 	}
 
 	while (_syscall_await() != ~0) await_cnt++;
@@ -68,24 +68,24 @@ static void test_interrupted_fs(void) {
 		// TODO make a similar test with all 0s passed to fs_wait
 		struct fs_wait_response res;
 		_syscall_fs_wait(NULL, 0, &res);
-		_syscall_exit(0);
+		exit(0);
 	} else { /* parent */
 		_syscall_mount(h, "/", 1);
 		int ret = _syscall_open("/", 1, 0);
 		// the handler quits while handling that call - but this syscall should return anyways
-		_syscall_exit(ret < 0 ? 0 : -1);
+		exit(ret < 0 ? 0 : -1);
 	}
 }
 
 static void test_orphaned_fs(void) {
 	handle_t h;
 	if (_syscall_fork(FORK_NEWFS, &h)) { /* child */
-		_syscall_exit(0);
+		exit(0);
 	} else { /* parent */
 		_syscall_mount(h, "/", 1);
 		int ret = _syscall_open("/", 1, 0);
 		// no handler will ever be available to handle this call - the syscall should instantly return
-		_syscall_exit(ret < 0 ? 0 : -1);
+		exit(ret < 0 ? 0 : -1);
 	}
 }
 
@@ -97,7 +97,7 @@ static void test_memflag(void) {
 
 	if (!fork()) {
 		memset(page, 11, 4096); // should segfault
-		_syscall_exit(0);
+		exit(0);
 	} else {
 		assert(_syscall_await() != 0); // test if the process crashed
 	}
@@ -156,7 +156,7 @@ static void test_dup(void) {
 		_syscall_write(h1, "h1", 2, 0);
 		close(h1);
 
-		_syscall_exit(0);
+		exit(0);
 	} else {
 		char buf[16];
 		size_t count = 0;
diff --git a/src/user/app/init/tests/pipe.c b/src/user/app/init/tests/pipe.c
index f7005f8..d6e954a 100644
--- a/src/user/app/init/tests/pipe.c
+++ b/src/user/app/init/tests/pipe.c
@@ -28,7 +28,7 @@ void test_pipe(void) {
 		ret = _syscall_write(ends[1], pipe_msgs[1], 5, -1);
 		assert(ret == 5);
 
-		_syscall_exit(0);
+		exit(0);
 	} else {
 		assert(_syscall_read(ends[1], buf, 16, 0) < 0);
 		assert(_syscall_write(ends[0], buf, 16, 0) < 0);
@@ -56,7 +56,7 @@ void test_pipe(void) {
 		if (!fork()) {
 			close(ends[1]);
 			assert(_syscall_read(ends[0], buf, 16, 0) < 0);
-			_syscall_exit(0);
+			exit(0);
 		}
 	}
 	close(ends[1]);
@@ -69,7 +69,7 @@ void test_pipe(void) {
 		if (!fork()) {
 			close(ends[0]);
 			assert(_syscall_write(ends[1], buf, 16, 0) < 0);
-			_syscall_exit(0);
+			exit(0);
 		}
 	}
 	close(ends[0]);
@@ -83,7 +83,7 @@ void test_pipe(void) {
 	for (int i = 0; i < 16; i++) {
 		if (!fork()) {
 			assert(_syscall_write(ends[1], pipe_msgs[0], 5, -1) == 5);
-			_syscall_exit(0);
+			exit(0);
 		}
 	}
 	close(ends[1]);
@@ -100,7 +100,7 @@ void test_pipe(void) {
 			memset(buf, 0, sizeof buf);
 			assert(_syscall_read(ends[0], buf, 5, -1) == 5);
 			assert(!memcmp(buf, pipe_msgs[1], 5));
-			_syscall_exit(0);
+			exit(0);
 		}
 	}
 	close(ends[0]);
diff --git a/src/user/app/init/tests/semaphore.c b/src/user/app/init/tests/semaphore.c
index fc0cc10..e05f2f9 100644
--- a/src/user/app/init/tests/semaphore.c
+++ b/src/user/app/init/tests/semaphore.c
@@ -45,7 +45,7 @@ void test_semaphore(void) {
 		assert(sem1 && sem2);
 		if (!fork()) {
 			odd(pipe[1], sem1, sem2);
-			_syscall_exit(69);
+			exit(69);
 		} else {
 			even(pipe[1], sem1, sem2);
 			assert(_syscall_await() == 69);
@@ -60,7 +60,7 @@ void test_semaphore(void) {
 		assert(sem1 && sem2);
 		if (!fork()) {
 			even(pipe[1], sem1, sem2);
-			_syscall_exit(69);
+			exit(69);
 		} else {
 			odd(pipe[1], sem1, sem2);
 			assert(_syscall_await() == 69);
@@ -69,7 +69,7 @@ void test_semaphore(void) {
 		esem_free(sem1);
 		esem_free(sem2);
 
-		_syscall_exit(0);
+		exit(0);
 	} else {
 		close(pipe[1]);
 
diff --git a/src/user/app/init/tests/stress.c b/src/user/app/init/tests/stress.c
index e620f0a..9be88d7 100644
--- a/src/user/app/init/tests/stress.c
+++ b/src/user/app/init/tests/stress.c
@@ -7,7 +7,7 @@
 static void run_forked(void (*fn)()) {
 	if (!fork()) {
 		fn();
-		_syscall_exit(0);
+		exit(0);
 	} else {
 		/* successful tests must return 0
 		 * TODO add a better fail msg */
@@ -18,7 +18,7 @@ static void run_forked(void (*fn)()) {
 
 static void stress_fork(void) {
 	for (size_t i = 0; i < 2048; i++) {
-		if (!fork()) _syscall_exit(0);
+		if (!fork()) exit(0);
 		_syscall_await();
 	}
 }
diff --git a/src/user/app/init/tests/tests.h b/src/user/app/init/tests/tests.h
index 23ed1e3..39294eb 100644
--- a/src/user/app/init/tests/tests.h
+++ b/src/user/app/init/tests/tests.h
@@ -13,7 +13,7 @@ void test_semaphore(void);
 #define argify(str) str, sizeof(str) - 1
 #define test_fail() do { \
 	printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \
-	_syscall_exit(0); \
+	exit(0); \
 } while (0)
 #define assert(cond) if (!(cond)) test_fail();
 
diff --git a/src/user/app/testelf/main.c b/src/user/app/testelf/main.c
index de7b965..93a5e11 100644
--- a/src/user/app/testelf/main.c
+++ b/src/user/app/testelf/main.c
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <user/lib/elf.h>
 #include <user/lib/elfload.h>
+#include <unistd.h>
 
 const char *str = "Hello!\n", *str2 = "World.\n";
 
@@ -13,5 +14,5 @@ int main(void) {
 	printf("loaded at %x\n", &_image_base);
 	printf(str);
 	printf(str2);
-	_syscall_exit(0);
+	exit(0);
 }
-- 
cgit v1.2.3