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