From 4ec7ff5aaa0ff5e4b98d7268b4bc2591c53d0d6a Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 12 Feb 2025 20:01:11 -0500 Subject: [PATCH] ipc: add fifo --- fs/vfs.c | 76 ++++++++++++++++++++++++++++++++++++++++ include/fs/vfs.h | 3 ++ include/kernel/common.h | 2 ++ include/kernel/ringbuf.h | 12 +++---- include/kernel/syscall.h | 1 + lib/ringbuf.c | 10 +++--- sched/syscall.c | 14 ++++++++ usr.bin/shtest/shtest.c | 33 ++++++++++++----- usr.bin/shtest/syscall.S | 1 + 9 files changed, 133 insertions(+), 19 deletions(-) diff --git a/fs/vfs.c b/fs/vfs.c index 3481ae4..af4a648 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -189,6 +190,10 @@ ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count) if ((file->vnode->flags & VFS_VTYPE_MASK) == VFS_VNO_DIR) return -EISDIR; + if (file->vnode->flags & VFS_VNO_FIFO) { + return ringbuf_write(file->vnode->priv_data, buf, count); + } + return file->vnode->fs->ops->write(file->vnode, buf, off, count); } @@ -200,9 +205,15 @@ ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count) if (off >= file->vnode->size && file->type == VFS_VNO_REG) return 0; + /* read from fifo */ + if (file->vnode->flags & VFS_VNO_FIFO) { + return ringbuf_read(file->vnode->priv_data, buf, count); + } + if (off + count > file->vnode->size) count = file->vnode->size - off; + return file->vnode->fs->ops->read(file->vnode, buf, off, count); } @@ -255,6 +266,71 @@ inline void *vfs_file_dev(struct file *file) return ((struct devfs_dev_info *)file->vnode->priv_data)->dev; } +struct vnode *vfs_mknod(const char *pathname, mode_t mode) +{ + char *pathcpy = kzalloc(strlen(pathname) + 1, ALLOC_KERN); + strcpy(pathcpy, pathname); + + char *name = basename(pathcpy); + char *dir = dirname(pathcpy); + + struct file *dir_file = vfs_open(dir, NULL); + if (!dir_file) + return NULL; + + struct fs *fs = dir_file->vnode->fs; + struct vnode *dir_vnode = dir_file->vnode; + + struct vnode *vnode = vfs_create_vno(); + if (!vnode) { + vfs_close(dir_file); + return NULL; + } + + vnode->refcount = 1; + vnode->fs = dir_vnode->fs; + vnode->flags = mode; + vnode->priv_data = ringbuf_create(0x4000); + + /* create directory entry */ + struct dirent *dirents = dir_vnode->dirents; + int num_dirents = dir_vnode->num_dirents; + + /* search for existing entry */ + for (int i = 0; i < num_dirents; i++) { + if (strcmp(dirents[i].name, name) == 0) { + vfs_close(dir_file); + slab_free(vnode_slab, vnode); + return NULL; + } + } + + spinlock_acquire(&dir_vnode->lock); + + dirents = krealloc(dirents, (num_dirents + 1) * sizeof(struct dirent), ALLOC_KERN); + if (!dirents) { + vfs_close(dir_file); + slab_free(vnode_slab, vnode); + return NULL; + } + + dir_vnode->dirents = dirents; + + struct dirent *dirent = &dirents[num_dirents]; + memset(dirent, 0, sizeof(struct dirent)); + dirent->vnode = vnode; + dirent->reclen = sizeof(struct dirent); + dirent->inode = 0; + strcpy(dirent->name, name); + + dir_vnode->num_dirents++; + spinlock_release(&dir_vnode->lock); + + vfs_close(dir_file); + + return vnode; +} + struct vnode *vfs_create_vno() { struct vnode *vnode = slab_alloc(vnode_slab); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 37e7bf0..8f7171d 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -46,6 +46,8 @@ #define O_TRUNC 0x0010 #define O_APPEND 0x0020 +#define S_IFIFO 0x1000 + #define FS_TYPE_EXT2 0x0001 #define FS_TYPE_PFS 0x0002 /* pseudo fs */ @@ -139,6 +141,7 @@ ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count); ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count); int vfs_statf(struct file *file, struct statbuf *statbuf); struct file *vfs_open(const char *pathname, int *err); +struct vnode *vfs_mknod(const char *pathname, mode_t mode); int vfs_close(struct file *file); int unlink(const char *pathname); int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf); diff --git a/include/kernel/common.h b/include/kernel/common.h index 58e0018..fbb5582 100644 --- a/include/kernel/common.h +++ b/include/kernel/common.h @@ -42,5 +42,7 @@ void load_stack_and_jump(uintptr_t rsp, uintptr_t rbp, void *func, void *arg); typedef uintptr_t paddr_t; typedef uint32_t paddr32_t; typedef int64_t ssize_t; +typedef int mode_t; +typedef long dev_t; #endif /* _COMMON_H_ */ diff --git a/include/kernel/ringbuf.h b/include/kernel/ringbuf.h index f7b4567..0795241 100644 --- a/include/kernel/ringbuf.h +++ b/include/kernel/ringbuf.h @@ -4,17 +4,17 @@ #include -struct ringbuf { +typedef struct _ringbuf { char *buf; size_t size; size_t head; size_t tail; -}; +} ringbuf_t; -struct ringbuf *ringbuf_create(size_t size); -void ringbuf_destroy(struct ringbuf *rb); +ringbuf_t *ringbuf_create(size_t size); +void ringbuf_destroy(ringbuf_t *rb); -size_t ringbuf_write(struct ringbuf *rb, const char *buf, size_t count); -size_t ringbuf_read(struct ringbuf *rb, char *buf, size_t count); +size_t ringbuf_write(ringbuf_t *rb, const char *buf, size_t count); +size_t ringbuf_read(ringbuf_t *rb, char *buf, size_t count); #endif /* _RINGBUF_H */ diff --git a/include/kernel/syscall.h b/include/kernel/syscall.h index 97492f9..e648cf5 100644 --- a/include/kernel/syscall.h +++ b/include/kernel/syscall.h @@ -7,6 +7,7 @@ #define SYS_OPEN 2 #define SYS_FORK 57 #define SYS_EXIT 60 +#define SYS_MKNOD 133 #include #include diff --git a/lib/ringbuf.c b/lib/ringbuf.c index 10e97ae..f999977 100644 --- a/lib/ringbuf.c +++ b/lib/ringbuf.c @@ -3,9 +3,9 @@ #include #include -struct ringbuf *ringbuf_create(size_t size) +ringbuf_t *ringbuf_create(size_t size) { - struct ringbuf *rb = kmalloc(sizeof(struct ringbuf), ALLOC_KERN); + ringbuf_t *rb = kzalloc(sizeof(ringbuf_t), ALLOC_KERN); rb->buf = kmalloc(size, ALLOC_KERN); rb->size = size; rb->head = 0; @@ -13,13 +13,13 @@ struct ringbuf *ringbuf_create(size_t size) return rb; } -void ringbuf_destroy(struct ringbuf *rb) +void ringbuf_destroy(ringbuf_t *rb) { ATTEMPT_FREE(rb->buf); ATTEMPT_FREE(rb); } -size_t ringbuf_write(struct ringbuf *rb, const char *buf, size_t count) +size_t ringbuf_write(ringbuf_t *rb, const char *buf, size_t count) { size_t written = 0; while (count--) { @@ -32,7 +32,7 @@ size_t ringbuf_write(struct ringbuf *rb, const char *buf, size_t count) return written; } -size_t ringbuf_read(struct ringbuf *rb, char *buf, size_t count) +size_t ringbuf_read(ringbuf_t *rb, char *buf, size_t count) { size_t read = 0; while (count--) { diff --git a/sched/syscall.c b/sched/syscall.c index f2fd017..556b535 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -190,6 +190,19 @@ pid_t sys_fork() return proc->pid; } +int sys_mknod(const char *pathname, mode_t mode, dev_t dev) +{ + if (mode != S_IFIFO) + return -ENOSYS; + + struct vnode *vnode = vfs_mknod(pathname, mode); + + if (vnode == NULL) + return -1; + + return 0; +} + static void syscall_insert(uint64_t syscall_no, syscall_t syscall) { syscall_table[syscall_no] = syscall; @@ -205,6 +218,7 @@ void syscall_init() syscall_insert(SYS_OPEN, (syscall_t)sys_open); syscall_insert(SYS_FORK, (syscall_t)sys_fork); syscall_insert(SYS_EXIT, (syscall_t)sys_exit); + syscall_insert(SYS_MKNOD, (syscall_t)sys_mknod); /* init syscall instruction */ uint64_t star = rdmsr(MSR_IA32_STAR); diff --git a/usr.bin/shtest/shtest.c b/usr.bin/shtest/shtest.c index 3d6f57a..08aa2c6 100644 --- a/usr.bin/shtest/shtest.c +++ b/usr.bin/shtest/shtest.c @@ -2,6 +2,8 @@ /* shtest: simple userland shell test */ #include +#define S_IFIFO 0x1000 + typedef int64_t ssize_t; typedef int64_t pid_t; @@ -11,6 +13,7 @@ ssize_t write(int fd, void *buf, int count); int close(int fd); pid_t fork(); int exit(int status); +int mknod(const char *pathname, int mode, int dev); void promptfd(int fd) { @@ -19,32 +22,46 @@ void promptfd(int fd) void _start() { - int fd = open("/dev/tty0", 0); - if (fd < 0) + int term = open("/dev/tty0", 0); + if (term < 0) exit(1); - promptfd(fd); char buf[256]; int n; + /* FIFOs!!!!!!!!! */ + int r = mknod("/dev/pts", S_IFIFO, 0); + int fifofd = open("/dev/pts", 0); + if (fifofd < 0) + write(term, "open failed\n", 12); + + /* write hello to fifo */ + int i = write(fifofd, "hello\n", 6); + /* read back from fifo */ + i = read(fifofd, buf, 6); + + /* print output to terminal */ + write(term, buf, i); + promptfd(term); + /* fork test */ pid_t pid = fork(); if (pid < 0) { - write(fd, "fork failed\n", 12); + write(term, "fork failed\n", 12); exit(1); } if (pid == 0) { - write(fd, "child\n", 6); + write(term, "child\n", 6); exit(1); } else { - write(fd, "parent\n", 7); + write(term, "parent\n", 7); } /* rw test */ - while ((n = read(fd, buf, 256)) > 0) { - write(fd, buf, n); + while ((n = read(term, buf, 256)) > 0) { + write(term, buf, n); if(buf[0] == 'q') break; diff --git a/usr.bin/shtest/syscall.S b/usr.bin/shtest/syscall.S index 949f5b5..09893ca 100644 --- a/usr.bin/shtest/syscall.S +++ b/usr.bin/shtest/syscall.S @@ -18,3 +18,4 @@ _sc_define open, 2 _sc_define close, 3 _sc_define fork, 57 _sc_define exit, 60 +_sc_define mknod, 133