summaryrefslogtreecommitdiff
path: root/lib/ccan/failtest/test/run-with-fdlimit.c
blob: 6b4483f07eea7a41a79423963318c7e6489ce73d (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
/* Include the C files directly. */
#include <ccan/failtest/failtest.c>
#include <stdlib.h>
#include <err.h>
#include <ccan/tap/tap.h>

int main(void)
{
	int fd, pfd[2], ecode;
	struct rlimit lim;

	if (getrlimit(RLIMIT_NOFILE, &lim) != 0)
		err(1, "getrlimit RLIMIT_NOFILE fail?");

	printf("rlimit = %lu/%lu (inf=%lu)\n",
	       (long)lim.rlim_cur, (long)lim.rlim_max,
	       (long)RLIM_INFINITY);
	lim.rlim_cur /= 2;
	if (lim.rlim_cur < 8)
		errx(1, "getrlimit limit %li too low", (long)lim.rlim_cur);
	if (setrlimit(RLIMIT_NOFILE, &lim) != 0)
		err(1, "setrlimit RLIMIT_NOFILE (%li/%li)",
		    (long)lim.rlim_cur, (long)lim.rlim_max);

	plan_tests(2);
	failtest_init(0, NULL);

	if (pipe(pfd))
		abort();

	fd = failtest_open("run-with-fdlimit-scratch", "run-with_fdlimit.c", 1,
			   O_RDWR|O_CREAT, 0600);
	if (fd == -1) {
		/* We are the child: write error code for parent to check. */
		ecode = errno;
		if (write(pfd[1], &ecode, sizeof(ecode)) != sizeof(ecode))
			abort();
		failtest_exit(0);
	}

	/* Check child got correct errno. */
	ok1(read(pfd[0], &ecode, sizeof(ecode)) == sizeof(ecode));
	ok1(ecode == EACCES);

	/* Clean up. */
	failtest_close(fd, "run-open.c", 1);
	close(pfd[0]);
	close(pfd[1]);

	return exit_status();
}