summaryrefslogtreecommitdiff
path: root/user
diff options
context:
space:
mode:
authorIan McFarlane <i.mcfarlane2002@gmail.com>2025-05-06 15:14:11 -0400
committerIan McFarlane <i.mcfarlane2002@gmail.com>2025-05-06 15:14:11 -0400
commitda396afa8b612b8f8ff07d71c57761a627b158eb (patch)
treeb4935b29aca686c6ee17a583cffe149d7bb3c819 /user
parentupdate forkman with spinlock (diff)
parentstart docs (diff)
downloadcomus-da396afa8b612b8f8ff07d71c57761a627b158eb.tar.gz
comus-da396afa8b612b8f8ff07d71c57761a627b158eb.tar.bz2
comus-da396afa8b612b8f8ff07d71c57761a627b158eb.zip
merge main into forkmanforkman
Diffstat (limited to 'user')
-rw-r--r--user/include/stdio.h6
-rw-r--r--user/include/sys/types.h15
-rw-r--r--user/include/unistd.h14
-rw-r--r--user/init.c115
-rw-r--r--user/lib/entry.S5
-rw-r--r--user/lib/fclose.c10
-rw-r--r--user/lib/fseek.c16
-rw-r--r--user/lib/syscall.S3
8 files changed, 171 insertions, 13 deletions
diff --git a/user/include/stdio.h b/user/include/stdio.h
index fe29c9d..bb57c6d 100644
--- a/user/include/stdio.h
+++ b/user/include/stdio.h
@@ -252,7 +252,7 @@ extern size_t fwrite(const void *restrict ptr, size_t size, size_t n,
* @param stream - the stream to seek
* @param off - the offset from whence
* @param whence - where to seek from (SEEK_SET, SEEK_CUR, SEEK_END)
- * @returns 0 on success, -1 on error setting errno
+ * @returns new offset on success, -1 on error
*/
extern int fseek(FILE *stream, long int off, int whence);
@@ -260,9 +260,9 @@ extern int fseek(FILE *stream, long int off, int whence);
* return the current position of stream
*
* @param stream - the stream to tell
- * @return the position on success, -1 on error setting errno
+ * @returns new offset on success, -1 on error
*/
-extern long int ftell(FILE *stream);
+extern long ftell(FILE *stream);
/**
* rewing to the begining of a stream
diff --git a/user/include/sys/types.h b/user/include/sys/types.h
new file mode 100644
index 0000000..f1b3266
--- /dev/null
+++ b/user/include/sys/types.h
@@ -0,0 +1,15 @@
+/**
+ * @file types.h
+ *
+ * @author Freya Murphy <freya@freyacat.org>
+ *
+ * System types
+ */
+
+#ifndef _TYPES_H
+#define _TYPES_H
+
+typedef long int off_t;
+typedef unsigned short pid_t;
+
+#endif /* types.h */
diff --git a/user/include/unistd.h b/user/include/unistd.h
index e817c84..c54cd20 100644
--- a/user/include/unistd.h
+++ b/user/include/unistd.h
@@ -11,6 +11,7 @@
#include <stdint.h>
#include <stddef.h>
+#include <sys/types.h>
/* System Call Definitions */
@@ -33,7 +34,7 @@ enum {
O_RDONLY = 0x02,
O_WRONLY = 0x04,
O_APPEND = 0x08,
- O_RDWR = O_RDONLY | O_WRONLY,
+ O_RDWR = 0x010,
};
/**
@@ -73,11 +74,9 @@ extern int fork(void);
*
* @param prog - program table index of the program to exec
* @param args - the command-line argument vector
- *
- * Does not return if it succeeds; if it returns, something has
- * gone wrong.
+ * @returns error code on failure
*/
-extern void exec(const char *filename, char **args);
+extern int exec(const char *filename, const char **args);
/**
* open a stream with a given filename
@@ -91,8 +90,9 @@ extern int open(const char *filename, int flags);
* closes a stream with the given file descriptior
*
* @param fd - the file descriptior of the open stream
+ * @returns 0 on success, error code on invalid fd
*/
-extern void close(int fd);
+extern int close(int fd);
/**
* read into a buffer from a stream
@@ -122,7 +122,7 @@ extern int write(int fd, const void *buffer, size_t nbytes);
* @param whence - whence to seek
* @return 0 on success, or an error code
*/
-extern int seek(int fd, long int off, int whence);
+extern off_t seek(int fd, off_t off, int whence);
/**
* gets the pid of the calling process
diff --git a/user/init.c b/user/init.c
new file mode 100644
index 0000000..51d8f92
--- /dev/null
+++ b/user/init.c
@@ -0,0 +1,115 @@
+#include <stdio.h>
+#include <unistd.h>
+
+#define MAX_ARGS 4
+
+static long running = 0;
+
+struct proc {
+ pid_t pid;
+ const char *filename;
+ const char *args[MAX_ARGS];
+};
+
+static struct proc spawn_table[] = {
+ // apple
+ { 0, "bin/apple", { NULL } },
+ // end,
+ { 0, NULL, { NULL } },
+};
+
+static int spawn(const char *filename, const char **args)
+{
+ int ret;
+
+ // fork init
+ if ((ret = fork()) != 0)
+ return ret;
+
+ // call exec
+ if ((ret = exec(filename, args)))
+ exit(ret);
+
+ // should not happen!
+ exit(1);
+}
+
+static void spawn_proc(struct proc *proc)
+{
+ int ret;
+
+ // update running on respawn
+ if (proc->pid)
+ running--;
+
+ // attempt to fork / exec
+ ret = spawn(proc->filename, proc->args);
+
+ // handle result
+ if (ret < 0) {
+ printf("init: cannot exec '%s': %d\n", proc->filename, ret);
+ proc->pid = 0;
+ } else {
+ running++;
+ proc->pid = ret;
+ }
+}
+
+static void spawn_proc_loop(struct proc *proc)
+{
+ while (proc->pid == 0)
+ spawn_proc(proc);
+}
+
+static void spawn_all(void)
+{
+ struct proc *proc;
+ for (proc = spawn_table; proc->filename != NULL; proc++) {
+ spawn_proc_loop(proc);
+ }
+}
+
+static struct proc *get_proc(pid_t pid)
+{
+ struct proc *proc;
+ for (proc = spawn_table; proc->filename != NULL; proc++) {
+ if (proc->pid == pid)
+ return proc;
+ }
+ return NULL;
+}
+
+int main(void)
+{
+ // spawn our processes
+ spawn_all();
+
+ // clean up dead on restart ours
+ while (1) {
+ struct proc *proc;
+ int pid, status;
+
+ // dont wait if nothing running
+ if (running) {
+ pid = waitpid(0, &status);
+ // ???
+ if (pid < 0)
+ continue;
+ } else {
+ spawn_all();
+ continue;
+ }
+
+ printf("init: pid %d exited with %d\n", pid, status);
+
+ // figure out if this is one of ours
+ proc = get_proc(pid);
+ if (proc == NULL)
+ continue;
+
+ spawn_proc_loop(proc);
+ }
+
+ // failed to launch anythingvery very bad!
+ return 1;
+}
diff --git a/user/lib/entry.S b/user/lib/entry.S
index 40570b5..efaa652 100644
--- a/user/lib/entry.S
+++ b/user/lib/entry.S
@@ -5,7 +5,6 @@
.section .text
.code64
_start:
- call main
- subq $16, %rsp # ???
- pushq %rax
+ call main
+ movq %rax, %rdi
call exit
diff --git a/user/lib/fclose.c b/user/lib/fclose.c
new file mode 100644
index 0000000..be31421
--- /dev/null
+++ b/user/lib/fclose.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+#include <unistd.h>
+
+void fclose(FILE *stream)
+{
+ int fd;
+
+ fd = (uintptr_t)stream;
+ close(fd);
+}
diff --git a/user/lib/fseek.c b/user/lib/fseek.c
new file mode 100644
index 0000000..a7a3377
--- /dev/null
+++ b/user/lib/fseek.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <unistd.h>
+
+int fseek(FILE *stream, long off, int whence)
+{
+ int fd;
+ fd = (uintptr_t)stream;
+ return seek(fd, off, whence);
+}
+
+long ftell(FILE *stream)
+{
+ int fd;
+ fd = (uintptr_t)stream;
+ return seek(fd, 0, SEEK_CUR);
+}
diff --git a/user/lib/syscall.S b/user/lib/syscall.S
index 93b7daa..c45f641 100644
--- a/user/lib/syscall.S
+++ b/user/lib/syscall.S
@@ -13,6 +13,8 @@ SYSCALL exit SYS_exit
SYSCALL waitpid SYS_waitpid
SYSCALL fork SYS_fork
SYSCALL exec SYS_exec
+SYSCALL open SYS_open
+SYSCALL close SYS_close
SYSCALL read SYS_read
SYSCALL write SYS_write
SYSCALL getpid SYS_getpid
@@ -27,6 +29,7 @@ SYSCALL sbrk SYS_sbrk
SYSCALL poweroff SYS_poweroff
SYSCALL drm SYS_drm
SYSCALL ticks SYS_ticks
+SYSCALL seek SYS_seek
SYSCALL allocshared SYS_allocshared
SYSCALL popsharedmem SYS_popsharedmem
SYSCALL keypoll SYS_keypoll