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