From f7dc760dd7a2dd8ba5a133e396e717b7886059f4 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 4 Nov 2024 01:41:14 -0500 Subject: kernel: descrip: Add support for fd duplication Sometimes we may need to duplicate a file descriptor and create a new fdno that refers to the same vnode. The fd_dup() function enables us to do this when needed. Signed-off-by: Ian Moffett --- sys/kern/kern_descrip.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'sys/kern') diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index c3d9b5c..da10f89 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -226,3 +226,26 @@ fd_open(const char *pathname, int flags) filedes->vp = nd.vp; return filedes->fdno; } + +/* + * Duplicate a file descriptor. New file descriptor + * points to the same vnode. + */ +int +fd_dup(int fd) +{ + int error; + struct filedesc *new_desc, *tmp; + + tmp = fd_get(fd); + if (tmp == NULL) + return -EBADF; + + if ((error = fd_alloc(&new_desc)) != 0) + return error; + + /* Ref that vnode before we point to it */ + vfs_vref(tmp->vp); + new_desc->vp = tmp->vp; + return new_desc->fdno; +} -- cgit v1.2.3