fd-util: Add fd_is_writable() to check if FD is opened for writing
This checks whether a file descriptor is valid and opened in a mode that allows writing (O_WRONLY or O_RDWR). This is useful when we want to verify that inherited FDs can actually be used for output operations before dup'ing them. The helper explicitly handles O_PATH file descriptors, which cannot be used for I/O operations and thus are never writable.
This commit is contained in:
@@ -998,6 +998,21 @@ int fd_vet_accmode(int fd, int mode) {
|
||||
return -EPROTOTYPE;
|
||||
}
|
||||
|
||||
int fd_is_writable(int fd) {
|
||||
int r;
|
||||
|
||||
assert(fd >= 0);
|
||||
|
||||
r = fd_vet_accmode(fd, O_WRONLY);
|
||||
if (r >= 0)
|
||||
return true;
|
||||
|
||||
if (IN_SET(r, -EPROTOTYPE, -EBADFD, -EISDIR))
|
||||
return false;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int fd_verify_safe_flags_full(int fd, int extra_flags) {
|
||||
int flags, unexpected_flags;
|
||||
|
||||
|
||||
@@ -152,6 +152,7 @@ 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_is_writable(int fd);
|
||||
|
||||
int fd_verify_safe_flags_full(int fd, int extra_flags);
|
||||
static inline int fd_verify_safe_flags(int fd) {
|
||||
|
||||
Reference in New Issue
Block a user