summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/dup.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test/syscalls/linux/dup.cc')
-rw-r--r--test/syscalls/linux/dup.cc29
1 files changed, 16 insertions, 13 deletions
diff --git a/test/syscalls/linux/dup.cc b/test/syscalls/linux/dup.cc
index fca0880a6..8f0974f45 100644
--- a/test/syscalls/linux/dup.cc
+++ b/test/syscalls/linux/dup.cc
@@ -101,18 +101,16 @@ TEST(DupTest, Dup2) {
}
TEST(DupTest, Rlimit) {
- constexpr int kFDLimit = 101;
auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(f.path(), O_RDONLY));
struct rlimit rl = {};
EXPECT_THAT(getrlimit(RLIMIT_NOFILE, &rl), SyscallSucceeds());
- // Lower the rlimit first, as it may be equal to /proc/sys/fs/nr_open, in
- // which case even users with CAP_SYS_RESOURCE can't raise it.
- rl.rlim_cur = kFDLimit * 2;
ASSERT_THAT(setrlimit(RLIMIT_NOFILE, &rl), SyscallSucceeds());
+ constexpr int kFDLimit = 101;
+ // Create a file descriptor that will be above the limit.
FileDescriptor aboveLimitFD =
ASSERT_NO_ERRNO_AND_VALUE(Dup2(fd, kFDLimit * 2 - 1));
@@ -121,19 +119,24 @@ TEST(DupTest, Rlimit) {
ASSERT_THAT(dup3(fd.get(), kFDLimit, 0), SyscallFails());
std::vector<std::unique_ptr<FileDescriptor>> fds;
- int prev = fd.get();
- for (int i = 0; i < kFDLimit; i++) {
- int d = dup(fd.get());
- if (d == -1) {
+ int prev_fd = fd.get();
+ int used_fds = 0;
+ for (int i = 0; i < kFDLimit; ++i) {
+ int new_fd = dup(fd.get());
+ if (new_fd == -1) {
break;
}
- std::unique_ptr<FileDescriptor> f = absl::make_unique<FileDescriptor>(d);
- EXPECT_LT(d, kFDLimit);
- EXPECT_GT(d, prev);
- prev = d;
+ auto f = absl::make_unique<FileDescriptor>(new_fd);
+ EXPECT_LT(new_fd, kFDLimit);
+ EXPECT_GT(new_fd, prev_fd);
+ // Check that all fds in (prev_fd, new_fd) are used.
+ for (int j = prev_fd + 1; j < new_fd; ++j) {
+ if (fcntl(j, F_GETFD) != -1) used_fds++;
+ }
+ prev_fd = new_fd;
fds.push_back(std::move(f));
}
- EXPECT_EQ(fds.size(), kFDLimit - fd.get() - 1);
+ EXPECT_EQ(fds.size() + used_fds, kFDLimit - fd.get() - 1);
}
TEST(DupTest, Dup2SameFD) {