1 | //===-- Unittests for getrlimit and setrlimit -----------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/__support/CPP/string_view.h" |
10 | #include "src/errno/libc_errno.h" |
11 | #include "src/fcntl/open.h" |
12 | #include "src/sys/resource/getrlimit.h" |
13 | #include "src/sys/resource/setrlimit.h" |
14 | #include "src/unistd/close.h" |
15 | #include "src/unistd/unlink.h" |
16 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
17 | #include "test/UnitTest/Test.h" |
18 | |
19 | #include <sys/resource.h> |
20 | #include <sys/stat.h> |
21 | |
22 | TEST(LlvmLibcResourceLimitsTest, SetNoFileLimit) { |
23 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
24 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
25 | |
26 | // The test strategy is to first create initialize two file descriptors |
27 | // successfully. Next, close the files and set the file descriptor limit |
28 | // to 4. This will allow us to open one of those file but not the other. |
29 | |
30 | constexpr const char *TEST_FILE1 = "testdata/resource_limits1.test" ; |
31 | constexpr const char *TEST_FILE2 = "testdata/resource_limits2.test" ; |
32 | LIBC_NAMESPACE::libc_errno = 0; |
33 | |
34 | int fd1 = LIBC_NAMESPACE::open(path: TEST_FILE1, O_CREAT | O_WRONLY, S_IRWXU); |
35 | ASSERT_GT(fd1, 0); |
36 | ASSERT_ERRNO_SUCCESS(); |
37 | int fd2 = LIBC_NAMESPACE::open(path: TEST_FILE2, O_CREAT | O_WRONLY, S_IRWXU); |
38 | ASSERT_GT(fd2, 0); |
39 | ASSERT_ERRNO_SUCCESS(); |
40 | |
41 | ASSERT_THAT(LIBC_NAMESPACE::close(fd1), Succeeds(0)); |
42 | ASSERT_THAT(LIBC_NAMESPACE::close(fd2), Succeeds(0)); |
43 | |
44 | struct rlimit limits { |
45 | .rlim_cur: 4, .rlim_max: 4 |
46 | }; |
47 | ASSERT_THAT(LIBC_NAMESPACE::setrlimit(RLIMIT_NOFILE, &limits), Succeeds(0)); |
48 | |
49 | // One can now only open one of the files successfully. |
50 | fd1 = LIBC_NAMESPACE::open(path: TEST_FILE1, O_RDONLY); |
51 | ASSERT_GT(fd1, 0); |
52 | ASSERT_ERRNO_SUCCESS(); |
53 | fd2 = LIBC_NAMESPACE::open(path: TEST_FILE2, O_RDONLY); |
54 | ASSERT_LT(fd2, 0); |
55 | ASSERT_ERRNO_FAILURE(); |
56 | |
57 | LIBC_NAMESPACE::libc_errno = 0; |
58 | ASSERT_THAT(LIBC_NAMESPACE::close(fd1), Succeeds(0)); |
59 | |
60 | fd2 = LIBC_NAMESPACE::open(path: TEST_FILE2, O_RDONLY); |
61 | ASSERT_GT(fd2, 0); |
62 | ASSERT_ERRNO_SUCCESS(); |
63 | fd1 = LIBC_NAMESPACE::open(path: TEST_FILE1, O_RDONLY); |
64 | ASSERT_LT(fd1, 0); |
65 | ASSERT_ERRNO_FAILURE(); |
66 | |
67 | LIBC_NAMESPACE::libc_errno = 0; |
68 | ASSERT_THAT(LIBC_NAMESPACE::close(fd2), Succeeds(0)); |
69 | |
70 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE1), Succeeds(0)); |
71 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE2), Succeeds(0)); |
72 | |
73 | struct rlimit current_limits; |
74 | ASSERT_THAT(LIBC_NAMESPACE::getrlimit(RLIMIT_NOFILE, ¤t_limits), |
75 | Succeeds(0)); |
76 | ASSERT_EQ(current_limits.rlim_cur, rlim_t(4)); |
77 | ASSERT_EQ(current_limits.rlim_max, rlim_t(4)); |
78 | } |
79 | |