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