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