On Sat, Oct 31, 2015 at 07:21:18PM +0000, Sami Kerola wrote: > Check if a file path and descriptor are indeed the same file. This function > is needed for TACTOU avoidance. > > Signed-off-by: Sami Kerola <kerolasa@xxxxxx> > --- > include/fileutils.h | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/include/fileutils.h b/include/fileutils.h > index be6d1f7..55df1a7 100644 > --- a/include/fileutils.h > +++ b/include/fileutils.h > @@ -4,6 +4,7 @@ > #include <stdio.h> > #include <fcntl.h> > #include <unistd.h> > +#include <sys/stat.h> > > #include "c.h" > > @@ -25,6 +26,19 @@ static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefi > return ret; > } > > +static inline int is_same_inode(const int fd, const char *path) > +{ > + struct stat f, p; > + > + if (fstat(fd, &f) < 0) > + return 0; > + if (stat(path, &p) < 0) > + return 0; > + if (f.st_dev != p.st_dev || f.st_ino != p.st_ino) > + return 0; > + return 1; > +} Maybe don't call fstat() here and use static inline int is_same_inode(struct stat st, const char *path) than you reduce stat() calls on many places... Karel -- Karel Zak <kzak@xxxxxxxxxx> http://karelzak.blogspot.com -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html