summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-10-06 21:59:20 -0400
committerIan Moffett <ian@osmora.org>2025-10-06 21:59:29 -0400
commit5adf5344bfa4388a70c054308447c64c06b98265 (patch)
tree4fc93d7a352a359a53d8dd464d385a43efccb52c /src
parent0b4270f30b7746d3400513e2c16ff833e6fbeb90 (diff)
parent473afa3b7e826063b66e579cfbebaa99ee4602ab (diff)
Fix conflicts
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/include/unistd.h10
-rw-r--r--src/lib/libc/src/unistd/open.c46
-rw-r--r--src/sys/include/compat/unix/syscall.h5
-rw-r--r--src/sys/include/os/filedesc.h11
-rw-r--r--src/sys/include/sys/syscall.h1
-rw-r--r--src/sys/os/os_filedes.c66
6 files changed, 138 insertions, 1 deletions
diff --git a/src/lib/libc/include/unistd.h b/src/lib/libc/include/unistd.h
index b50fc12..fa7f951 100644
--- a/src/lib/libc/include/unistd.h
+++ b/src/lib/libc/include/unistd.h
@@ -38,6 +38,16 @@
#define STDERR_FILENO 2
/*
+ * POSIX open system call
+ *
+ * @path: Path to open
+ * @flags: O_ flags
+ *
+ * Returns the file descriptor on success
+ */
+int open(const char *path, int flags);
+
+/*
* POSIX write system call
*
* @fd: File descriptor to write at
diff --git a/src/lib/libc/src/unistd/open.c b/src/lib/libc/src/unistd/open.c
new file mode 100644
index 0000000..b8eca60
--- /dev/null
+++ b/src/lib/libc/src/unistd/open.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2025 Ian Marco Moffett and L5 engineers
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/syscall.h>
+#include <sys/errno.h>
+#include <unistd.h>
+
+int
+open(const char *path, int flags)
+{
+ if (path == NULL) {
+ return -EINVAL;
+ }
+
+ return syscall(
+ SYS_open,
+ (uintptr_t)path,
+ flags
+ );
+}
diff --git a/src/sys/include/compat/unix/syscall.h b/src/sys/include/compat/unix/syscall.h
index 5339d47..14088f4 100644
--- a/src/sys/include/compat/unix/syscall.h
+++ b/src/sys/include/compat/unix/syscall.h
@@ -55,6 +55,11 @@ scret_t sys_cross(struct syscall_args *scargs);
*/
scret_t sys_query(struct syscall_args *scargs);
+/*
+ * Open a file
+ */
+scret_t sys_open(struct syscall_args *scargs);
+
#ifdef _NEED_UNIX_SCTAB
scret_t(*g_unix_sctab[])(struct syscall_args *) = {
[SYS_none] = NULL,
diff --git a/src/sys/include/os/filedesc.h b/src/sys/include/os/filedesc.h
index 87bf242..c339ed6 100644
--- a/src/sys/include/os/filedesc.h
+++ b/src/sys/include/os/filedesc.h
@@ -60,6 +60,17 @@ struct filedesc {
int fdtab_init(struct proc *procp);
/*
+ * Open a file
+ *
+ * @path: Path to file in which we wish to open
+ * @mode: Mode of our desired file
+ *
+ * Returns the file descriptor on success,
+ * otherwise a less than zero value on failure.
+ */
+int fd_open(const char *path, mode_t mode);
+
+/*
* Write to a file descriptor
*
* @fd: File descriptor to write to
diff --git a/src/sys/include/sys/syscall.h b/src/sys/include/sys/syscall.h
index 4121d54..181f734 100644
--- a/src/sys/include/sys/syscall.h
+++ b/src/sys/include/sys/syscall.h
@@ -50,6 +50,7 @@
#define SYS_query 0x05 /* query a border (mandatory) */
#define SYS_spawn 0x06 /* spawn a process */
#define SYS_mount 0x07 /* mount a filesystem */
+#define SYS_open 0x08 /* open a file */
typedef __ssize_t scret_t;
typedef __ssize_t scarg_t;
diff --git a/src/sys/os/os_filedes.c b/src/sys/os/os_filedes.c
index e7cc83a..3353a5f 100644
--- a/src/sys/os/os_filedes.c
+++ b/src/sys/os/os_filedes.c
@@ -31,10 +31,13 @@
#include <sys/syslog.h>
#include <sys/errno.h>
#include <sys/limits.h>
+#include <sys/proc.h>
+#include <sys/namei.h>
#include <os/filedesc.h>
#include <os/kalloc.h>
+#include <os/systm.h>
#include <io/cons/cons.h>
-#include <sys/proc.h>
+#include <compat/unix/syscall.h>
#include <string.h>
#define STDOUT_FILENO 1
@@ -86,6 +89,47 @@ fd_alloc(struct proc *procp, struct filedesc **fd_res)
return -EMFILE;
}
+int
+fd_open(const char *path, mode_t mode)
+{
+ struct filedesc *fd;
+ struct nameidata nd;
+ struct proc *self = proc_self();
+ struct vnode *vp;
+ int error;
+
+ /* We need the current proc */
+ if (self == NULL) {
+ return -ESRCH;
+ }
+
+ if (path == NULL) {
+ return -ESRCH;
+ }
+
+ /* Allocate a new file descriptor */
+ error = fd_alloc(self, &fd);
+ if (error < 0) {
+ return error;
+ }
+
+ /*
+ * Now we try to do the lookup, we'll need
+ * the vnode for file references to be
+ * useful
+ */
+ nd.path = path;
+ nd.flags = 0;
+ nd.vp_res = &vp;
+ error = namei(&nd);
+ if (error < 0) {
+ return error;
+ }
+
+ fd->vp = vp;
+ return fd->fdno;
+}
+
/*
* Initialize file descriptor table
*/
@@ -138,3 +182,23 @@ write(int fd, const void *buf, size_t count)
return count;
}
+
+/*
+ * ARG0: Path
+ * ARG1: Mode
+ */
+scret_t
+sys_open(struct syscall_args *scargs)
+{
+ const char *u_path = SCARG(scargs, const char *, 0);
+ mode_t mode = SCARG(scargs, mode_t, 1);
+ char pathbuf[PATH_MAX];
+ int error;
+
+ error = copyinstr(u_path, pathbuf, sizeof(PATH_MAX));
+ if (error < 0) {
+ return error;
+ }
+
+ return fd_open(pathbuf, mode);
+}