ipc: add fifo

This commit is contained in:
Michael Shaw 2025-02-12 20:01:11 -05:00
commit 4ec7ff5aaa
9 changed files with 133 additions and 19 deletions

View file

@ -2,6 +2,7 @@
#include <kernel/slab.h>
#include <kernel/block.h>
#include <kernel/rbtree.h>
#include <kernel/ringbuf.h>
#include <fs/ext2.h>
#include <fs/devfs.h>
@ -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);

View file

@ -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);

View file

@ -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_ */

View file

@ -4,17 +4,17 @@
#include <kernel/common.h>
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 */

View file

@ -7,6 +7,7 @@
#define SYS_OPEN 2
#define SYS_FORK 57
#define SYS_EXIT 60
#define SYS_MKNOD 133
#include <kernel/common.h>
#include <kernel/proc.h>

View file

@ -3,9 +3,9 @@
#include <kernel/common.h>
#include <kernel/ringbuf.h>
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--) {

View file

@ -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);

View file

@ -2,6 +2,8 @@
/* shtest: simple userland shell test */
#include <stdint.h>
#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;

View file

@ -18,3 +18,4 @@ _sc_define open, 2
_sc_define close, 3
_sc_define fork, 57
_sc_define exit, 60
_sc_define mknod, 133