From db7dd9d3d2c597c2f6740d3f11c4b4bb53d5cf65 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 15 Aug 2020 15:20:11 +0900 Subject: [PATCH] sys/io: Add proc_get_fd() function implementation --- sys/io/proc.c | 36 ++++++++++++++++++++++++++++++++++++ sys/io/proc.h | 1 + 2 files changed, 37 insertions(+) diff --git a/sys/io/proc.c b/sys/io/proc.c index ef49397..7812102 100644 --- a/sys/io/proc.c +++ b/sys/io/proc.c @@ -235,3 +235,39 @@ int proc_add_fd(struct proc *proc, struct filedesc *fd) return(ret_val); } + +/* + * proc_get_fd() - Look up a file descriptor using its number + * + * SYNOPSIS + * int proc_get_fd(struct proc *proc, const int fd, struct filedesc **dst); + * + * DESCRIPTION + * The proc_get_fd() function retrieves the file descriptor structure that is associated with the + * descriptor number `fd' in the process pointed to by `proc'. Upon success, the pointer pointed + * to by `dst' will be adjusted to point to the file descriptor. + * + * RETURN VALUE + * Upon success, zero is returned. Otherwise, a negative error number is returned and the value + * of `*dst' is undefined. + * + * ERRORS + * -EINVAL Invalid arguments have been passed to the function + * -ENOENT There is no file descriptor associated with the file descriptor number `fd' + */ +int proc_get_fd(struct proc *proc, const int fd, struct filedesc **dst) +{ + int ret_val; + + ret_val = -EINVAL; + + if(proc && fd >= 0 && dst) { + ret_val = -ENOENT; + + if(!cx_array_get(proc->p_fds, fd, (void**)dst)) { + ret_val = 0; + } + } + + return(ret_val); +} diff --git a/sys/io/proc.h b/sys/io/proc.h index 8588823..7836544 100644 --- a/sys/io/proc.h +++ b/sys/io/proc.h @@ -12,5 +12,6 @@ int proc_new(const pid_t, struct proc**); void proc_free(struct proc*); int proc_add_fd(struct proc*, struct filedesc*); +int proc_get_fd(struct proc*, const int, struct filedesc**); #endif /* PROC_H */ -- 2.47.3