file: fd_clone

This commit is contained in:
Matheus
2023-05-16 19:23:23 -03:00
parent 69b1ff7a70
commit 6f1eae6db1
2 changed files with 27 additions and 0 deletions
+26
View File
@@ -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;
}
+1
View File
@@ -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