diff --git a/kernel-src/fs/file.c b/kernel-src/fs/file.c index 1b20f2e3..37799360 100644 --- a/kernel-src/fs/file.c +++ b/kernel-src/fs/file.c @@ -131,3 +131,29 @@ int fd_close(int fd) { return 0; } + +int fd_clone(proc_t *targproc) { + proc_t *proc = _cpu()->thread->proc; + int error = 0; + spinlock_acquire(&proc->fdlock); + + targproc->fdcount = proc->fdcount; + targproc->fdfirst = proc->fdfirst; + targproc->fd = alloc(proc->fdcount * sizeof(fd_t)); + if (targproc->fd == NULL) { + error = ENOMEM; + goto cleanup; + } + + for (int i = 0; i < targproc->fdcount; ++i) { + if (proc->fd[i].file == NULL) + continue; + + targproc->fd[i] = proc->fd[i]; + FILE_HOLD(targproc->fd[i].file); + } + + cleanup: + spinlock_release(&proc->fdlock); + return error; +} diff --git a/kernel-src/include/kernel/file.h b/kernel-src/include/kernel/file.h index 1e05e357..4e032e64 100644 --- a/kernel-src/include/kernel/file.h +++ b/kernel-src/include/kernel/file.h @@ -24,6 +24,7 @@ void fd_release(file_t *file); int fd_new(int flags, file_t **file, int *fd); int fd_close(int fd); int fd_dup(int oldfd, int newfd, bool exact, int flags, int *ret); +int fd_clone(proc_t *proc); #define FILE_READ 1 #define FILE_WRITE 2