summaryrefslogtreecommitdiff
path: root/src/user/app/tests/kernel/fdlimit.c
blob: d22af1399198bb56d33316de9f2e302f2a7bc1f7 (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
#include "../tests.h"
#include <camellia/flags.h>
#include <errno.h>
#include <unistd.h>

static void test_fdlimit_pipe(void) {
	handle_t ends[2];
	handle_t h[2] = {-1, -1};
	for (;;) {
		handle_t cur = _syscall_open("/", 1, OPEN_READ);
		if (cur == -EMFILE) break;
		test(cur >= 0);
		h[0] = h[1];
		h[1] = cur;
	}
	test(h[0] >= 0); /* we need at least two handles */

	test(_syscall_pipe(ends, 0) == -EMFILE);
	test(_syscall_open("/", 1, OPEN_READ) == -EMFILE);

	close(h[1]);
	test(_syscall_pipe(ends, 0) == -EMFILE);
	test(_syscall_open("/", 1, OPEN_READ) == h[1]);
	test(_syscall_open("/", 1, OPEN_READ) == -EMFILE);

	close(h[0]);
	close(h[1]);
	test(_syscall_pipe(ends, 0) == 0);
}

static void test_fdlimit_fork(void) {
	for (;;) {
		handle_t cur = _syscall_open("/", 1, OPEN_READ);
		if (cur == -EMFILE) break;
		test(cur >= 0);
	}

	if (!_syscall_fork(0, NULL)) _syscall_exit(123);

	test(_syscall_fork(FORK_NEWFS, NULL) == -EMFILE);
	test(_syscall_await() == 123);
	test(_syscall_await() == ~0);
}

void r_k_fdlimit(void) {
	/* all these tests implicitly test if the vfs returns -EMFILE */
	run_test(test_fdlimit_pipe);
	run_test(test_fdlimit_fork);
}