Subject: openat(2) and friends
To: None <>
From: None <>
List: tech-kern
Date: 03/01/2005 21:35:39
Solaris (since 9?), has some useful calls I'd like to have
available on NetBSD too:


All interfaces take both a file descriptor and a pathname as arguments,
and have a "flag" argument:

int fchownat(int fildes, const char *path, uid_t owner, gid_t group, int flag);
int futimesat(int fildes, const char *path, const struct timeval times[2]);
int fstatat(int fildes, const char *path, struct stat *buf, int flag);
int openat(int fildes, const char *path, int oflag, /*mode_t mode*/ ...);
int renameat(int fromfd, const char *old, int tofd, const char *new);
int unlinkat(int dirfd, const char *path, int flag);

The basic point is that a relative path is traversed from a file
descriptor (refering to a open directory), instead of from the current
directory.  This is very useful when traversing a untrusted directory

If fildes is some special value AT_FDCWD, fstatat degenerates to stat,
and if path is NULL, it degenerates to fstat. filedes=AT_FDCWD and
flag&AT_SYMLNK_NOFOLLOW makes lstat.

Also, these calls will be needed for Solaris emulation.

fts(3) can take great advantage from the combination of openat(2) and
fdopendir(3), and will no longer need to use chdir(2).

fdopendir(3) is simply a opendir-from-fd:

DIR *fdopendir(int fildes);

I tried implementing the exact same ting as fdopendir a few years ago,
but ran into problems with NFS (and union mounts?). I beleve an openat
that supports path=NULL would make this much easier. (It seems Solaris
does not support path=NULL for openat.)

So, how does this sound? Are anyone eager to do this, or import it if
I manage to make some patches?