1*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
2*bbb1b6f9SApple OSS Distributions #include <errno.h>
3*bbb1b6f9SApple OSS Distributions #include <fcntl.h>
4*bbb1b6f9SApple OSS Distributions #include <signal.h>
5*bbb1b6f9SApple OSS Distributions #include <stdio.h>
6*bbb1b6f9SApple OSS Distributions #include <sys/resource.h>
7*bbb1b6f9SApple OSS Distributions #include <unistd.h>
8*bbb1b6f9SApple OSS Distributions
9*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.vfs"));
10*bbb1b6f9SApple OSS Distributions
11*bbb1b6f9SApple OSS Distributions #define FSIZE_CUR (10*1024)
12*bbb1b6f9SApple OSS Distributions #define TMP_FILE_PATH "/tmp/ftruncate_test"
13*bbb1b6f9SApple OSS Distributions
14*bbb1b6f9SApple OSS Distributions static int sigcount = 0;
15*bbb1b6f9SApple OSS Distributions
16*bbb1b6f9SApple OSS Distributions static void
xfsz_signal_handler(__unused int signo)17*bbb1b6f9SApple OSS Distributions xfsz_signal_handler(__unused int signo)
18*bbb1b6f9SApple OSS Distributions {
19*bbb1b6f9SApple OSS Distributions sigcount++;
20*bbb1b6f9SApple OSS Distributions }
21*bbb1b6f9SApple OSS Distributions
22*bbb1b6f9SApple OSS Distributions static void
fsize_test(bool use_fd)23*bbb1b6f9SApple OSS Distributions fsize_test(bool use_fd)
24*bbb1b6f9SApple OSS Distributions {
25*bbb1b6f9SApple OSS Distributions struct rlimit rlim;
26*bbb1b6f9SApple OSS Distributions int fd, ret;
27*bbb1b6f9SApple OSS Distributions
28*bbb1b6f9SApple OSS Distributions T_SETUPBEGIN;
29*bbb1b6f9SApple OSS Distributions
30*bbb1b6f9SApple OSS Distributions signal(SIGXFSZ, xfsz_signal_handler);
31*bbb1b6f9SApple OSS Distributions
32*bbb1b6f9SApple OSS Distributions rlim.rlim_cur = FSIZE_CUR;
33*bbb1b6f9SApple OSS Distributions rlim.rlim_max = RLIM_INFINITY;
34*bbb1b6f9SApple OSS Distributions ret = setrlimit(RLIMIT_FSIZE, &rlim);
35*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "set soft RLIMIT_FSIZE to %d", FSIZE_CUR);
36*bbb1b6f9SApple OSS Distributions
37*bbb1b6f9SApple OSS Distributions fd = open(TMP_FILE_PATH, O_RDWR | O_CREAT, 0777);
38*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "create temp file: %s", TMP_FILE_PATH);
39*bbb1b6f9SApple OSS Distributions
40*bbb1b6f9SApple OSS Distributions T_SETUPEND;
41*bbb1b6f9SApple OSS Distributions
42*bbb1b6f9SApple OSS Distributions if (use_fd) {
43*bbb1b6f9SApple OSS Distributions ret = ftruncate(fd, FSIZE_CUR);
44*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(ret, "ftruncate() with length RLIMIT_FSIZE");
45*bbb1b6f9SApple OSS Distributions } else {
46*bbb1b6f9SApple OSS Distributions ret = truncate(TMP_FILE_PATH, FSIZE_CUR);
47*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(ret, "truncate() with length RLIMIT_FSIZE");
48*bbb1b6f9SApple OSS Distributions }
49*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(sigcount, 0, "no signal received");
50*bbb1b6f9SApple OSS Distributions
51*bbb1b6f9SApple OSS Distributions if (use_fd) {
52*bbb1b6f9SApple OSS Distributions ret = ftruncate(fd, FSIZE_CUR + 1);
53*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ret, EFBIG, "ftruncate() with length RLIMIT_FSIZE + 1");
54*bbb1b6f9SApple OSS Distributions } else {
55*bbb1b6f9SApple OSS Distributions ret = truncate(TMP_FILE_PATH, FSIZE_CUR + 1);
56*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ret, EFBIG, "truncate() with length RLIMIT_FSIZE + 1");
57*bbb1b6f9SApple OSS Distributions }
58*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(sigcount, 1, "SIGXFSZ signal received");
59*bbb1b6f9SApple OSS Distributions
60*bbb1b6f9SApple OSS Distributions ret = close(fd);
61*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "close temp file");
62*bbb1b6f9SApple OSS Distributions
63*bbb1b6f9SApple OSS Distributions ret = unlink(TMP_FILE_PATH);
64*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "unlink temp file");
65*bbb1b6f9SApple OSS Distributions }
66*bbb1b6f9SApple OSS Distributions
67*bbb1b6f9SApple OSS Distributions T_DECL(ftruncate_fsize,
68*bbb1b6f9SApple OSS Distributions "ftruncate() should fail with EFBIG and send SIGXFSZ signal when length > RLIMIT_FSIZE", T_META_TAG_VM_PREFERRED)
69*bbb1b6f9SApple OSS Distributions {
70*bbb1b6f9SApple OSS Distributions fsize_test(true);
71*bbb1b6f9SApple OSS Distributions }
72*bbb1b6f9SApple OSS Distributions
73*bbb1b6f9SApple OSS Distributions T_DECL(truncate_fsize,
74*bbb1b6f9SApple OSS Distributions "truncate() should fail with EFBIG and send SIGXFSZ signal when length > RLIMIT_FSIZE", T_META_TAG_VM_PREFERRED)
75*bbb1b6f9SApple OSS Distributions {
76*bbb1b6f9SApple OSS Distributions fsize_test(false);
77*bbb1b6f9SApple OSS Distributions }
78