xref: /xnu-10063.101.15/tests/pwrite_avoid_sigxfsz_28581610.c (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions /*
2*94d3b452SApple OSS Distributions  * testname: pwrite_avoid_sigxfsz_28581610
3*94d3b452SApple OSS Distributions  */
4*94d3b452SApple OSS Distributions 
5*94d3b452SApple OSS Distributions #include <darwintest.h>
6*94d3b452SApple OSS Distributions #include <fcntl.h>
7*94d3b452SApple OSS Distributions #include <limits.h>
8*94d3b452SApple OSS Distributions #include <setjmp.h>
9*94d3b452SApple OSS Distributions #include <signal.h>
10*94d3b452SApple OSS Distributions #include <sys/resource.h>
11*94d3b452SApple OSS Distributions #include <sys/stat.h>
12*94d3b452SApple OSS Distributions #include <unistd.h>
13*94d3b452SApple OSS Distributions 
14*94d3b452SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
15*94d3b452SApple OSS Distributions 
16*94d3b452SApple OSS Distributions #define TMP_FILE_PATH "/tmp/test_pwrite_28581610"
17*94d3b452SApple OSS Distributions 
18*94d3b452SApple OSS Distributions static sigjmp_buf xfsz_jmpbuf;
19*94d3b452SApple OSS Distributions 
20*94d3b452SApple OSS Distributions void xfsz_signal(int);
21*94d3b452SApple OSS Distributions 
22*94d3b452SApple OSS Distributions void
xfsz_signal(__unused int signo)23*94d3b452SApple OSS Distributions xfsz_signal(__unused int signo)
24*94d3b452SApple OSS Distributions {
25*94d3b452SApple OSS Distributions 	siglongjmp(xfsz_jmpbuf, 1);
26*94d3b452SApple OSS Distributions }
27*94d3b452SApple OSS Distributions 
28*94d3b452SApple OSS Distributions T_DECL(pwrite, "Tests avoiding SIGXFSZ with pwrite and odd offsets",
29*94d3b452SApple OSS Distributions     T_META_ASROOT(true))
30*94d3b452SApple OSS Distributions {
31*94d3b452SApple OSS Distributions 	int fd, x;
32*94d3b452SApple OSS Distributions 	off_t ret;
33*94d3b452SApple OSS Distributions 	struct stat f_stat;
34*94d3b452SApple OSS Distributions 	struct rlimit crl;
35*94d3b452SApple OSS Distributions 	static const int offs[] = { -1, -1 * 1024, -1 * 1024 * 16, -1 * 1024 * 1024 * 16, 0 };
36*94d3b452SApple OSS Distributions 	static unsigned char buffer[1048576];
37*94d3b452SApple OSS Distributions 
38*94d3b452SApple OSS Distributions 	T_SETUPBEGIN;
39*94d3b452SApple OSS Distributions 	/* We expect zero SIGXFSZ signals because we have no file size limits */
40*94d3b452SApple OSS Distributions 	crl.rlim_cur = crl.rlim_max = RLIM_INFINITY;
41*94d3b452SApple OSS Distributions 	ret = setrlimit(RLIMIT_FSIZE, &crl);
42*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "setting infinite file size limit");
43*94d3b452SApple OSS Distributions 
44*94d3b452SApple OSS Distributions 	/* we just needed root to setup unlimited file size */
45*94d3b452SApple OSS Distributions 	remove(TMP_FILE_PATH);
46*94d3b452SApple OSS Distributions 	setuid(5000);
47*94d3b452SApple OSS Distributions 
48*94d3b452SApple OSS Distributions 	/* We just want an empty regular file to test with */
49*94d3b452SApple OSS Distributions 	fd = open(TMP_FILE_PATH, O_RDWR | O_CREAT | O_EXCL, 0777);
50*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "opening fd on temp file %s.", TMP_FILE_PATH);
51*94d3b452SApple OSS Distributions 
52*94d3b452SApple OSS Distributions 	/* sanity check that this new file is really zero bytes in size */
53*94d3b452SApple OSS Distributions 	ret = fstat(fd, &f_stat);
54*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "stat() fd on temp file.");
55*94d3b452SApple OSS Distributions 	T_ASSERT_TRUE(0 == f_stat.st_size, "ensure %s is empty", TMP_FILE_PATH);
56*94d3b452SApple OSS Distributions 
57*94d3b452SApple OSS Distributions 	/* sanity check that ftruncate() considers negative offsets an error */
58*94d3b452SApple OSS Distributions 	for (x = 0; offs[x] != 0; x++) {
59*94d3b452SApple OSS Distributions 		ret = ftruncate(fd, offs[x]);
60*94d3b452SApple OSS Distributions 		T_ASSERT_TRUE(((ret == -1) && (errno == EINVAL)),
61*94d3b452SApple OSS Distributions 		    "negative offset %d", offs[x]);
62*94d3b452SApple OSS Distributions 	}
63*94d3b452SApple OSS Distributions 
64*94d3b452SApple OSS Distributions 	T_SETUPEND;
65*94d3b452SApple OSS Distributions 
66*94d3b452SApple OSS Distributions 	/* we want to get the EFBIG errno but without a SIGXFSZ signal */
67*94d3b452SApple OSS Distributions 	if (!sigsetjmp(xfsz_jmpbuf, 1)) {
68*94d3b452SApple OSS Distributions 		signal(SIGXFSZ, xfsz_signal);
69*94d3b452SApple OSS Distributions 		ret = pwrite(fd, buffer, sizeof buffer, QUAD_MAX);
70*94d3b452SApple OSS Distributions 		T_ASSERT_TRUE(((ret == -1) && (errno == EFBIG)),
71*94d3b452SApple OSS Distributions 		    "large offset %d", 13);
72*94d3b452SApple OSS Distributions 	} else {
73*94d3b452SApple OSS Distributions 		signal(SIGXFSZ, SIG_DFL);
74*94d3b452SApple OSS Distributions 		T_FAIL("%s unexpected SIGXFSZ with offset %lX",
75*94d3b452SApple OSS Distributions 		    "<rdar://problem/28581610>", LONG_MAX);
76*94d3b452SApple OSS Distributions 	}
77*94d3b452SApple OSS Distributions 
78*94d3b452SApple OSS Distributions 	/* Negative offsets are invalid, no SIGXFSZ signals required */
79*94d3b452SApple OSS Distributions 	for (x = 0; offs[x] != 0; x++) {
80*94d3b452SApple OSS Distributions 		if (!sigsetjmp(xfsz_jmpbuf, 1)) {
81*94d3b452SApple OSS Distributions 			signal(SIGXFSZ, xfsz_signal);
82*94d3b452SApple OSS Distributions 			ret = pwrite(fd, buffer, sizeof buffer, offs[x]);
83*94d3b452SApple OSS Distributions 			T_ASSERT_TRUE(((ret == -1) && (errno == EINVAL)),
84*94d3b452SApple OSS Distributions 			    "negative offset %d", offs[x]);
85*94d3b452SApple OSS Distributions 		} else {
86*94d3b452SApple OSS Distributions 			signal(SIGXFSZ, SIG_DFL);
87*94d3b452SApple OSS Distributions 			T_FAIL("%s unexpected SIGXFSZ with negative offset %d",
88*94d3b452SApple OSS Distributions 			    "<rdar://problem/28581610>", offs[x]);
89*94d3b452SApple OSS Distributions 		}
90*94d3b452SApple OSS Distributions 	}
91*94d3b452SApple OSS Distributions 
92*94d3b452SApple OSS Distributions 	remove(TMP_FILE_PATH);
93*94d3b452SApple OSS Distributions }
94