1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <sys/resource.h>
20
21#if defined(__GLIBC__)
22/* The host glibc we're currently building with doesn't have prlimit64 yet. */
23static int prlimit64(pid_t, int resource, const struct rlimit64* new_limit, struct rlimit64* old_limit) {
24  if (new_limit != NULL) {
25    return setrlimit64(resource, new_limit);
26  } else {
27    return getrlimit64(resource, old_limit);
28  }
29}
30#endif
31
32TEST(sys_resource, smoke) {
33#if defined(__LP64__) || defined(__GLIBC__)
34  ASSERT_EQ(sizeof(rlimit), sizeof(rlimit64));
35  ASSERT_EQ(8U, sizeof(rlim_t));
36#else
37  ASSERT_NE(sizeof(rlimit), sizeof(rlimit64));
38  ASSERT_EQ(4U, sizeof(rlim_t));
39#endif
40
41  // Read with getrlimit, getrlimit64, and prlimit64.
42  // (prlimit is prlimit64 on LP64 and unimplemented on 32-bit.)
43  rlimit l32;
44  rlimit64 l64;
45  rlimit64 pr_l64;
46  ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32));
47  ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64));
48  ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64));
49  ASSERT_EQ(l64.rlim_cur, l32.rlim_cur);
50  ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur);
51  ASSERT_EQ(l64.rlim_max, pr_l64.rlim_max);
52  if (l64.rlim_max == RLIM64_INFINITY) {
53    ASSERT_EQ(RLIM_INFINITY, l32.rlim_max);
54  } else {
55    ASSERT_EQ(l64.rlim_max, l32.rlim_max);
56  }
57
58  // Write with setrlimit and read back with everything.
59  l32.rlim_cur = 123;
60  ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &l32));
61  ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32));
62  ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64));
63  ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64));
64  ASSERT_EQ(123U, l32.rlim_cur);
65  ASSERT_EQ(l64.rlim_cur, l32.rlim_cur);
66  ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur);
67
68  // Write with setrlimit64 and read back with everything.
69  l64.rlim_cur = 456;
70  ASSERT_EQ(0, setrlimit64(RLIMIT_CORE, &l64));
71  ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32));
72  ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64));
73  ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64));
74  ASSERT_EQ(456U, l32.rlim_cur);
75  ASSERT_EQ(l64.rlim_cur, l32.rlim_cur);
76  ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur);
77
78  // Write with prlimit64 and read back with everything.
79  l64.rlim_cur = 789;
80  ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, &l64, NULL));
81  ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32));
82  ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64));
83  ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64));
84  ASSERT_EQ(789U, l32.rlim_cur);
85  ASSERT_EQ(l64.rlim_cur, l32.rlim_cur);
86  ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur);
87}
88