summaryrefslogtreecommitdiff
path: root/test/misc/tst-rlimit.c
blob: 84aa465869da7c46d34f7de0a140cf6a5581c103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* 
 * setrlimit/getrlimit/prlimit and setrlimit64/getrlimit64/prlimit64 functions 
 * test for uClibc.
 *
 * The prlimit64 function is not called directly in this test, because it is a
 * new function for uclibc and can cause build problems with uclibc-ng <= 1.0.44.
 * With _FILE_OFFSET_BITS == 64 the prlimit call is redirected to the prlimit64
 * call.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/resource.h>

#define __ASSERT(x, f, l) \
	if (!(x)) { \
		fprintf(stderr, "%s: LINE %d: ASSERT: " #x "\n", f, l); \
		exit(1); \
	} 

#define ASSERT(x)  __ASSERT(x, __FILE__, __LINE__)

static int resources[] = {
	RLIMIT_CORE,
	RLIMIT_CPU,
	RLIMIT_DATA,
	RLIMIT_FSIZE,
	RLIMIT_NOFILE,
	RLIMIT_STACK,
	RLIMIT_AS
};

#define nresources	(sizeof (resources) / sizeof (resources[0]))
#define TEST_VAL	0x11223344


static void test_getrlimit(int rnum, rlim_t exp_cur, rlim_t exp_max)
{
	struct rlimit r;

	ASSERT(getrlimit(rnum, &r) == 0);
	ASSERT(r.rlim_cur == exp_cur);
	ASSERT(r.rlim_max == exp_max);
}

static void test_setrlimit(int rnum, rlim_t new_cur, rlim_t new_max)
{
	struct rlimit r = {.rlim_cur = new_cur, .rlim_max = new_max};

	ASSERT(setrlimit(rnum, &r) == 0);
}

static void test_prlimit_get(int rnum, rlim_t exp_cur, rlim_t exp_max)
{
	struct rlimit r;

	ASSERT(prlimit(0, rnum, NULL, &r) == 0);
	ASSERT(r.rlim_cur == exp_cur);
	ASSERT(r.rlim_max == exp_max);
}

static void test_prlimit_set(int rnum, rlim_t new_cur, rlim_t new_max)
{
	struct rlimit r = {.rlim_cur = new_cur, .rlim_max = new_max};
	ASSERT(prlimit(0, rnum, &r, NULL) == 0);
}

#if defined(__USE_LARGEFILE64)
static void test_getrlimit64(int rnum, rlim64_t exp_cur, rlim64_t exp_max)
{
	struct rlimit64 r;

	ASSERT(getrlimit64(rnum, &r) == 0);
	ASSERT(r.rlim_cur == exp_cur);
	ASSERT(r.rlim_max == exp_max);
}

static void test_setrlimit64(int rnum, rlim64_t new_cur, rlim64_t new_max)
{
	struct rlimit64 r = {.rlim_cur = new_cur, .rlim_max = new_max};

	ASSERT(setrlimit64(rnum, &r) == 0);
}
#endif

int main(void)
{
	int rnum = -1;
	struct rlimit rlim;
	int i, ret;

	/* Find a resource with hard limit set to infinity */
	for (i = 0; i < nresources; ++i) {
		ret = getrlimit(resources[i], &rlim);
		if ((!ret) && (rlim.rlim_max == RLIM_INFINITY)) {
			rnum = resources[i];
			break;
		}
	}

	/* Can't continue, return unsupported */
	if (rnum == -1)
		return 23;

	/* Test cases */
	test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY);
	test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY);
	test_setrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_prlimit_set(rnum, TEST_VAL, RLIM_INFINITY);
	test_prlimit_get(rnum, TEST_VAL, RLIM_INFINITY);
	test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY);
	test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY);

#if defined(__USE_LARGEFILE64)
	/* Check setrlim64 and getrlim64 in 32-bit offset LFS environment */
	test_setrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
	test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY);
	test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
	test_setrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
	test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);

	test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY);
	test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
	test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
#endif


#if defined(__USE_LARGEFILE64) && _FILE_OFFSET_BITS == 64
	/* Check setrlim64/getrlim64/prlimit64 in 64-bit offset environment */
	test_setrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
	test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
	test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY);
	test_setrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
	test_prlimit_get(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
	test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_prlimit_set(rnum, TEST_VAL, RLIM64_INFINITY);
	test_prlimit_get(rnum, TEST_VAL, RLIM64_INFINITY);
	test_prlimit_set(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
	test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
	test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);

	test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY);
	test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
	test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY);
	test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
#endif

	return 0;
}