Merge pull request #34675 from poettering/dupfd-query
fd-util: use F_DUPFD_QUERY for same_fd()
This commit is contained in:
+35
-6
@@ -40,9 +40,9 @@ TEST(close_many) {
|
||||
|
||||
close_many(fds, 2);
|
||||
|
||||
assert_se(fcntl(fds[0], F_GETFD) == -1);
|
||||
assert_se(fcntl(fds[1], F_GETFD) == -1);
|
||||
assert_se(fcntl(fds[2], F_GETFD) >= 0);
|
||||
assert_se(fd_validate(fds[0]) == -EBADF);
|
||||
assert_se(fd_validate(fds[1]) == -EBADF);
|
||||
assert_se(fd_validate(fds[2]) >= 0);
|
||||
|
||||
safe_close(fds[2]);
|
||||
}
|
||||
@@ -57,14 +57,29 @@ TEST(close_nointr) {
|
||||
assert_se(close_nointr(fd) < 0);
|
||||
}
|
||||
|
||||
TEST(fd_validate) {
|
||||
assert_se(fd_validate(-EINVAL) == -EBADF);
|
||||
assert_se(fd_validate(-EBADF) == -EBADF);
|
||||
|
||||
_cleanup_close_ int b = -EBADF;
|
||||
assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
|
||||
|
||||
assert_se(fd_validate(b) == 0);
|
||||
safe_close(b);
|
||||
assert_se(fd_validate(b) == -EBADF);
|
||||
TAKE_FD(b);
|
||||
}
|
||||
|
||||
TEST(same_fd) {
|
||||
_cleanup_close_pair_ int p[2];
|
||||
_cleanup_close_ int a, b, c;
|
||||
_cleanup_close_ int a, b, c, d, e;
|
||||
|
||||
assert_se(pipe2(p, O_CLOEXEC) >= 0);
|
||||
assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0);
|
||||
assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
|
||||
assert_se((c = fcntl(a, F_DUPFD, 3)) >= 0);
|
||||
assert_se((d = open("/dev/null", O_RDONLY|O_CLOEXEC|O_PATH)) >= 0); /* O_PATH changes error returns in F_DUPFD_QUERY, let's test explicitly */
|
||||
assert_se((e = fcntl(d, F_DUPFD, 3)) >= 0);
|
||||
|
||||
assert_se(same_fd(p[0], p[0]) > 0);
|
||||
assert_se(same_fd(p[1], p[1]) > 0);
|
||||
@@ -89,6 +104,20 @@ TEST(same_fd) {
|
||||
|
||||
assert_se(same_fd(a, b) == 0);
|
||||
assert_se(same_fd(b, a) == 0);
|
||||
|
||||
assert_se(same_fd(a, d) == 0);
|
||||
assert_se(same_fd(d, a) == 0);
|
||||
assert_se(same_fd(d, d) > 0);
|
||||
assert_se(same_fd(d, e) > 0);
|
||||
assert_se(same_fd(e, d) > 0);
|
||||
|
||||
/* Let's now compare with a valid fd nr, that is definitely closed, and verify it returns the right error code */
|
||||
safe_close(d);
|
||||
assert_se(same_fd(d, d) == -EBADF);
|
||||
assert_se(same_fd(e, d) == -EBADF);
|
||||
assert_se(same_fd(d, e) == -EBADF);
|
||||
assert_se(same_fd(e, e) > 0);
|
||||
TAKE_FD(d);
|
||||
}
|
||||
|
||||
TEST(open_serialization_fd) {
|
||||
@@ -222,9 +251,9 @@ static size_t validate_fds(
|
||||
continue;
|
||||
|
||||
if (opened)
|
||||
assert_se(fcntl(fds[i], F_GETFD) >= 0);
|
||||
assert_se(fd_validate(fds[i]) >= 0);
|
||||
else
|
||||
assert_se(fcntl(fds[i], F_GETFD) < 0 && errno == EBADF);
|
||||
assert_se(fd_validate(fds[i]) == -EBADF);
|
||||
|
||||
c++;
|
||||
}
|
||||
|
||||
+9
-16
@@ -23,8 +23,7 @@ TEST(fdset_new_fill) {
|
||||
assert_se(fdset_new_fill(/* filter_cloexec= */ -1, &fdset) >= 0);
|
||||
assert_se(fdset_contains(fdset, fd));
|
||||
fdset = fdset_free(fdset);
|
||||
assert_se(fcntl(fd, F_GETFD) < 0);
|
||||
assert_se(errno == EBADF);
|
||||
assert_se(fd_validate(fd) == -EBADF);
|
||||
|
||||
fd = open("/dev/null", O_CLOEXEC|O_RDONLY);
|
||||
assert_se(fd >= 0);
|
||||
@@ -32,13 +31,12 @@ TEST(fdset_new_fill) {
|
||||
assert_se(fdset_new_fill(/* filter_cloexec= */ 0, &fdset) >= 0);
|
||||
assert_se(!fdset_contains(fdset, fd));
|
||||
fdset = fdset_free(fdset);
|
||||
assert_se(fcntl(fd, F_GETFD) >= 0);
|
||||
assert_se(fd_validate(fd) >= 0);
|
||||
|
||||
assert_se(fdset_new_fill(/* filter_cloexec= */ 1, &fdset) >= 0);
|
||||
assert_se(fdset_contains(fdset, fd));
|
||||
fdset = fdset_free(fdset);
|
||||
assert_se(fcntl(fd, F_GETFD) < 0);
|
||||
assert_se(errno == EBADF);
|
||||
assert_se(fd_validate(fd) == -EBADF);
|
||||
|
||||
fd = open("/dev/null", O_RDONLY);
|
||||
assert_se(fd >= 0);
|
||||
@@ -46,7 +44,7 @@ TEST(fdset_new_fill) {
|
||||
assert_se(fdset_new_fill(/* filter_cloexec= */ 1, &fdset) >= 0);
|
||||
assert_se(!fdset_contains(fdset, fd));
|
||||
fdset = fdset_free(fdset);
|
||||
assert_se(fcntl(fd, F_GETFD) >= 0);
|
||||
assert_se(fd_validate(fd) >= 0);
|
||||
|
||||
assert_se(fdset_new_fill(/* filter_cloexec= */ 0, &fdset) >= 0);
|
||||
assert_se(fdset_contains(fdset, fd));
|
||||
@@ -54,8 +52,7 @@ TEST(fdset_new_fill) {
|
||||
assert_se(flags >= 0);
|
||||
assert_se(FLAGS_SET(flags, FD_CLOEXEC));
|
||||
fdset = fdset_free(fdset);
|
||||
assert_se(fcntl(fd, F_GETFD) < 0);
|
||||
assert_se(errno == EBADF);
|
||||
assert_se(fd_validate(fd) == -EBADF);
|
||||
|
||||
log_open();
|
||||
}
|
||||
@@ -102,10 +99,8 @@ TEST(fdset_cloexec) {
|
||||
}
|
||||
|
||||
TEST(fdset_close_others) {
|
||||
int fd = -EBADF;
|
||||
int copyfd = -EBADF;
|
||||
int fd = -EBADF, copyfd = -EBADF;
|
||||
_cleanup_fdset_free_ FDSet *fdset = NULL;
|
||||
int flags = -1;
|
||||
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_close_others.XXXXXX";
|
||||
|
||||
fd = mkostemp_safe(name);
|
||||
@@ -121,15 +116,13 @@ TEST(fdset_close_others) {
|
||||
log_close();
|
||||
assert_se(fdset_close_others(fdset) >= 0);
|
||||
|
||||
flags = fcntl(fd, F_GETFD);
|
||||
assert_se(flags < 0);
|
||||
assert_se(fd_validate(fd) == -EBADF);
|
||||
|
||||
/* Open log again after checking that fd is invalid, since reopening the log might make fd a valid
|
||||
* file descriptor again. */
|
||||
(void) log_open();
|
||||
|
||||
flags = fcntl(copyfd, F_GETFD);
|
||||
assert_se(flags >= 0);
|
||||
assert_se(fd_validate(copyfd) >= 0);
|
||||
}
|
||||
|
||||
TEST(fdset_remove) {
|
||||
@@ -146,7 +139,7 @@ TEST(fdset_remove) {
|
||||
assert_se(fdset_remove(fdset, fd) >= 0);
|
||||
assert_se(!fdset_contains(fdset, fd));
|
||||
|
||||
assert_se(fcntl(fd, F_GETFD) >= 0);
|
||||
assert_se(fd_validate(fd) >= 0);
|
||||
}
|
||||
|
||||
TEST(fdset_iterate) {
|
||||
|
||||
Reference in New Issue
Block a user