]> git.ozlabs.org Git - ccan/blob - ccan/failtest/test/run-with-fdlimit.c
Merge branch 'ccantool'
[ccan] / ccan / failtest / test / run-with-fdlimit.c
1 /* Include the C files directly. */
2 #include <ccan/failtest/failtest.c>
3 #include <stdlib.h>
4 #include <err.h>
5 #include <ccan/tap/tap.h>
6
7 int main(void)
8 {
9         int fd, pfd[2], ecode;
10         struct rlimit lim;
11
12         if (getrlimit(RLIMIT_NOFILE, &lim) != 0)
13                 err(1, "getrlimit RLIMIT_NOFILE fail?");
14
15         printf("rlimit = %lu/%lu (inf=%lu)\n",
16                (long)lim.rlim_cur, (long)lim.rlim_max,
17                (long)RLIM_INFINITY);
18         lim.rlim_cur /= 2;
19         if (lim.rlim_cur < 8)
20                 errx(1, "getrlimit limit %li too low", (long)lim.rlim_cur);
21         if (setrlimit(RLIMIT_NOFILE, &lim) != 0)
22                 err(1, "setrlimit RLIMIT_NOFILE (%li/%li)",
23                     (long)lim.rlim_cur, (long)lim.rlim_max);
24
25         plan_tests(2);
26         failtest_init(0, NULL);
27
28         if (pipe(pfd))
29                 abort();
30
31         fd = failtest_open("run-with-fdlimit-scratch", "run-with_fdlimit.c", 1,
32                            O_RDWR|O_CREAT, 0600);
33         if (fd == -1) {
34                 /* We are the child: write error code for parent to check. */
35                 ecode = errno;
36                 if (write(pfd[1], &ecode, sizeof(ecode)) != sizeof(ecode))
37                         abort();
38                 failtest_exit(0);
39         }
40
41         /* Check child got correct errno. */
42         ok1(read(pfd[0], &ecode, sizeof(ecode)) == sizeof(ecode));
43         ok1(ecode == EACCES);
44
45         /* Clean up. */
46         failtest_close(fd, "run-open.c", 1);
47         close(pfd[0]);
48         close(pfd[1]);
49
50         return exit_status();
51 }