Index: lib/libutil/Makefile =================================================================== --- lib/libutil/Makefile +++ lib/libutil/Makefile @@ -35,6 +35,7 @@ property.3 pty.3 quotafile.3 realhostname.3 realhostname_sa.3 \ _secure_path.3 trimdomain.3 uucplock.3 pw_util.3 MAN+= login.conf.5 +MLINKS+=flopen.3 flopenat.3 MLINKS+=kld.3 kld_isloaded.3 kld.3 kld_load.3 MLINKS+=login_auth.3 auth_cat.3 login_auth.3 auth_checknologin.3 MLINKS+=login_cap.3 login_close.3 login_cap.3 login_getcapbool.3 \ Index: lib/libutil/flopen.3 =================================================================== --- lib/libutil/flopen.3 +++ lib/libutil/flopen.3 @@ -25,11 +25,12 @@ .\" .\" $FreeBSD$ .\" -.Dd June 6, 2009 +.Dd July 25, 2017 .Dt FLOPEN 3 .Os .Sh NAME -.Nm flopen +.Nm flopen , +.Nm flopenat .Nd "Reliably open and lock a file" .Sh LIBRARY .Lb libutil @@ -40,6 +41,10 @@ .Fn flopen "const char *path" "int flags" .Ft int .Fn flopen "const char *path" "int flags" "mode_t mode" +.Ft int +.Fn flopenat "int fd" "const char *path" "int flags" +.Ft int +.Fn flopenat "int fd" "const char *path" "int flags" "mode_t mode" .Sh DESCRIPTION The .Fn flopen @@ -79,6 +84,18 @@ .Va flags includes .Dv O_CREAT . +.Pp +The +.Fn flopenat +function is equivalent to the +.Fn flopen +function except in the case where the +.Fa path +specifies a relative path. +In this case the file to be opened is determined relative to the directory +associated with the file descriptor +.Fa fd +instead of the current working directory. .Sh RETURN VALUES If successful, .Fn flopen Index: lib/libutil/flopen.c =================================================================== --- lib/libutil/flopen.c +++ lib/libutil/flopen.c @@ -45,26 +45,16 @@ * code's apparent simplicity; there would be no need for this function if it * was easy to get right. */ -int -flopen(const char *path, int flags, ...) +static int +flopenatm(int dirfd, const char *path, int flags, mode_t mode) { int fd, operation, serrno, trunc; struct stat sb, fsb; - mode_t mode; #ifdef O_EXLOCK flags &= ~O_EXLOCK; #endif - mode = 0; - if (flags & O_CREAT) { - va_list ap; - - va_start(ap, flags); - mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */ - va_end(ap); - } - operation = LOCK_EX; if (flags & O_NONBLOCK) operation |= LOCK_NB; @@ -73,7 +63,7 @@ flags &= ~O_TRUNC; for (;;) { - if ((fd = open(path, flags, mode)) == -1) + if ((fd = openat(dirfd, path, flags, mode)) == -1) /* non-existent or no access */ return (-1); if (flock(fd, operation) == -1) { @@ -83,7 +73,7 @@ errno = serrno; return (-1); } - if (stat(path, &sb) == -1) { + if (fstatat(dirfd, path, &sb, 0) == -1) { /* disappeared from under our feet */ (void)close(fd); continue; @@ -123,3 +113,37 @@ return (fd); } } + +int +flopen(const char *path, int flags, ...) +{ + mode_t mode; + + mode = 0; + if (flags & O_CREAT) { + va_list ap; + + va_start(ap, flags); + mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */ + va_end(ap); + } + + return (flopenatm(AT_FDCWD, path, flags, mode)); +} + +int +flopenat(int dirfd, const char *path, int flags, ...) +{ + mode_t mode; + + mode = 0; + if (flags & O_CREAT) { + va_list ap; + + va_start(ap, flags); + mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */ + va_end(ap); + } + + return (flopenatm(dirfd, path, flags, mode)); +} Index: lib/libutil/libutil.h =================================================================== --- lib/libutil/libutil.h +++ lib/libutil/libutil.h @@ -93,6 +93,7 @@ int extattr_namespace_to_string(int _attrnamespace, char **_string); int extattr_string_to_namespace(const char *_string, int *_attrnamespace); int flopen(const char *_path, int _flags, ...); +int flopenat(int _dirfd, const char *_path, int _flags, ...); int forkpty(int *_amaster, char *_name, struct termios *_termp, struct winsize *_winp); void hexdump(const void *_ptr, int _length, const char *_hdr, int _flags);