fd-util: introduce fd_validate() helper

It just uses F_GETFD to validate an fd. it's a bit easier to read
though, and handles the < 0 case internally.
This commit is contained in:
Lennart Poettering
2024-10-08 13:13:44 +02:00
parent bf34d642bc
commit 6056663a14
5 changed files with 42 additions and 22 deletions
+10
View File
@@ -511,6 +511,16 @@ int pack_fds(int fds[], size_t n_fds) {
return 0;
}
int fd_validate(int fd) {
if (fd < 0)
return -EBADF;
if (fcntl(fd, F_GETFD) < 0)
return -errno;
return 0;
}
int same_fd(int a, int b) {
struct stat sta, stb;
pid_t pid;
+1
View File
@@ -80,6 +80,7 @@ int close_all_fds_without_malloc(const int except[], size_t n_except);
int pack_fds(int fds[], size_t n);
int fd_validate(int fd);
int same_fd(int a, int b);
void cmsg_close_all(struct msghdr *mh);