From e1b124fb2e43193463418d287b67da41c2b67185 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Mon, 27 Aug 2012 19:55:01 -0400 Subject: [PATCH] new helpers: fdget()/fdput() Signed-off-bs: Al Viro Issue: SEC-2677 [Backport: move up declaration of fget_raw_light to before it's used.] Change-Id: I52cd7555021d13f9c73481ea9a4f2562190524ec (cherry picked from commit a5b470ba06aa3f96999ede5feba178df6bdb134a) --- include/linux/file.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/linux/file.h b/include/linux/file.h index 58bf158c53d..4341818e5e7 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -26,10 +26,36 @@ static inline void fput_light(struct file *file, int fput_needed) fput(file); } +struct fd { + struct file *file; + int need_put; +}; + +static inline void fdput(struct fd fd) +{ + if (fd.need_put) + fput(fd.file); +} + extern struct file *fget(unsigned int fd); extern struct file *fget_light(unsigned int fd, int *fput_needed); extern struct file *fget_raw(unsigned int fd); extern struct file *fget_raw_light(unsigned int fd, int *fput_needed); + +static inline struct fd fdget(unsigned int fd) +{ + int b; + struct file *f = fget_light(fd, &b); + return (struct fd){f,b}; +} + +static inline struct fd fdget_raw(unsigned int fd) +{ + int b; + struct file *f = fget_raw_light(fd, &b); + return (struct fd){f,b}; +} + extern void set_close_on_exec(unsigned int fd, int flag); extern void put_filp(struct file *); extern int alloc_fd(unsigned start, unsigned flags);