fd-util: introduce fd_vet_accmode()

Inspired by #39674
This commit is contained in:
Mike Yuan
2025-11-11 19:04:38 +01:00
parent 3b3113b87c
commit 7cf4f07567
2 changed files with 36 additions and 0 deletions

View File

@@ -960,6 +960,41 @@ int fd_is_opath(int fd) {
return FLAGS_SET(r, O_PATH);
}
int fd_vet_accmode(int fd, int mode) {
int flags;
/* Check if fd is opened with desired access mode.
*
* Returns > 0 on strict match, == 0 if opened for both reading and writing (partial match),
* -EPROTOTYPE otherwise. O_PATH fds are always refused with -EBADFD.
*
* Note that while on O_DIRECTORY -EISDIR will be returned, this should not be relied upon as
* the flag might not have been specified when open() was called originally. */
assert(fd >= 0);
assert(IN_SET(mode, O_RDONLY, O_WRONLY, O_RDWR));
flags = fcntl(fd, F_GETFL);
if (flags < 0)
return -errno;
if (FLAGS_SET(flags, O_DIRECTORY))
return -EISDIR;
if (FLAGS_SET(flags, O_PATH))
return -EBADFD;
flags &= O_ACCMODE_STRICT;
if (flags == mode)
return 1;
if (flags == O_RDWR)
return 0;
return -EPROTOTYPE;
}
int fd_verify_safe_flags_full(int fd, int extra_flags) {
int flags, unexpected_flags;

View File

@@ -151,6 +151,7 @@ int fd_reopen_propagate_append_and_position(int fd, int flags);
int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd);
int fd_is_opath(int fd);
int fd_vet_accmode(int fd, int mode);
int fd_verify_safe_flags_full(int fd, int extra_flags);
static inline int fd_verify_safe_flags(int fd) {