diff options
-rw-r--r-- | src/kernel/proc.c | 8 | ||||
-rw-r--r-- | src/kernel/proc.h | 2 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 86 |
3 files changed, 50 insertions, 46 deletions
diff --git a/src/kernel/proc.c b/src/kernel/proc.c index b9eb48e..c69cb14 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -241,13 +241,9 @@ handle_t process_find_free_handle(struct process *proc, handle_t start_at) { return handle; } -struct handle* -process_handle_get(struct process *p, handle_t id, enum handle_type type) { - struct handle *h; +struct handle *process_handle_get(struct process *p, handle_t id) { if (id < 0 || id >= HANDLE_MAX) return NULL; - h = p->handles[id]; - if (h == NULL || h->type != type) return NULL; - return h; + return p->handles[id]; } void process_transition(struct process *p, enum process_state state) { diff --git a/src/kernel/proc.h b/src/kernel/proc.h index eb5bd6b..20585bc 100644 --- a/src/kernel/proc.h +++ b/src/kernel/proc.h @@ -82,6 +82,6 @@ struct process *process_next(struct process *); struct process *process_find(enum process_state); handle_t process_find_free_handle(struct process *proc, handle_t start_at); -struct handle *process_handle_get(struct process *, handle_t, enum handle_type); +struct handle *process_handle_get(struct process *, handle_t); void process_transition(struct process *, enum process_state); diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index cfd895e..c916e81 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -148,8 +148,9 @@ int _syscall_mount(handle_t hid, const char __user *path, int len) { } if (hid >= 0) { // mounting a real backend? - struct handle *handle = process_handle_get(process_current, hid, HANDLE_FS_FRONT); - if (!handle) goto fail; + struct handle *handle = process_handle_get(process_current, hid); + if (!handle || handle->type != HANDLE_FS_FRONT) + goto fail; backend = handle->backend; backend->refcount++; } // otherwise backend == NULL @@ -176,48 +177,55 @@ fail: } int _syscall_read(handle_t handle_num, void __user *buf, size_t len, int offset) { - struct handle *h; - // TODO get rid of the type argument in process_handle_get - if ((h = process_handle_get(process_current, handle_num, HANDLE_FILE))) { - vfsreq_create((struct vfs_request) { - .type = VFSOP_READ, - .output = { - .buf = (userptr_t) buf, - .len = len, - }, - .id = h->file_id, - .offset = offset, - .caller = process_current, - .backend = h->backend, - }); - } else if ((h = process_handle_get(process_current, handle_num, HANDLE_PIPE))) { - pipe_joinqueue(h, true, process_current, buf, len); - pipe_trytransfer(h); - } else { - SYSCALL_RETURN(-1); + struct handle *h = process_handle_get(process_current, handle_num); + if (!h) SYSCALL_RETURN(-1); + switch (h->type) { + case HANDLE_FILE: + vfsreq_create((struct vfs_request) { + .type = VFSOP_READ, + .output = { + .buf = (userptr_t) buf, + .len = len, + }, + .id = h->file_id, + .offset = offset, + .caller = process_current, + .backend = h->backend, + }); + break; + case HANDLE_PIPE: + pipe_joinqueue(h, true, process_current, buf, len); + pipe_trytransfer(h); + break; + default: + SYSCALL_RETURN(-1); } return -1; // dummy } int _syscall_write(handle_t handle_num, const void __user *buf, size_t len, int offset) { - struct handle *h; - if ((h = process_handle_get(process_current, handle_num, HANDLE_FILE))) { - vfsreq_create((struct vfs_request) { - .type = VFSOP_WRITE, - .input = { - .buf = (userptr_t) buf, - .len = len, - }, - .id = h->file_id, - .offset = offset, - .caller = process_current, - .backend = h->backend, - }); - } else if ((h = process_handle_get(process_current, handle_num, HANDLE_PIPE))) { - pipe_joinqueue(h, false, process_current, (void __user *)buf, len); - pipe_trytransfer(h); - } else { - SYSCALL_RETURN(-1); + struct handle *h = process_handle_get(process_current, handle_num); + if (!h) SYSCALL_RETURN(-1); + switch (h->type) { + case HANDLE_FILE: + vfsreq_create((struct vfs_request) { + .type = VFSOP_WRITE, + .input = { + .buf = (userptr_t) buf, + .len = len, + }, + .id = h->file_id, + .offset = offset, + .caller = process_current, + .backend = h->backend, + }); + break; + case HANDLE_PIPE: + pipe_joinqueue(h, false, process_current, (void __user *)buf, len); + pipe_trytransfer(h); + break; + default: + SYSCALL_RETURN(-1); } return -1; // dummy } |