]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/pwrite_avoid_sigxfsz_28581610.c
2 * testname: pwrite_avoid_sigxfsz_28581610
5 #include <darwintest.h>
10 #include <sys/resource.h>
14 #define TMP_FILE_PATH "/tmp/test_pwrite"
16 static sigjmp_buf xfsz_jmpbuf
;
18 void xfsz_signal(int);
21 xfsz_signal(__unused
int signo
)
23 siglongjmp(xfsz_jmpbuf
, 1);
26 T_DECL(pwrite
, "Tests avoiding SIGXFSZ with pwrite and odd offsets",
33 static const int offs
[] = { -1, -1 * 1024, -1 * 1024 * 16, -1 * 1024 * 1024 * 16, 0 };
34 static unsigned char buffer
[1048576];
37 /* We expect zero SIGXFSZ signals because we have no file size limits */
38 crl
.rlim_cur
= crl
.rlim_max
= RLIM_INFINITY
;
39 ret
= setrlimit(RLIMIT_FSIZE
, &crl
);
40 T_ASSERT_POSIX_SUCCESS(ret
, "setting infinite file size limit");
42 /* we just needed root to setup unlimited file size */
43 remove(TMP_FILE_PATH
);
46 /* We just want an empty regular file to test with */
47 fd
= open(TMP_FILE_PATH
, O_RDWR
| O_CREAT
| O_EXCL
, 0777);
48 T_ASSERT_POSIX_SUCCESS(fd
, "opening fd on temp file %s.", TMP_FILE_PATH
);
50 /* sanity check that this new file is really zero bytes in size */
51 ret
= fstat(fd
, &f_stat
);
52 T_ASSERT_POSIX_SUCCESS(ret
, "stat() fd on temp file.");
53 T_ASSERT_TRUE(0 == f_stat
.st_size
, "ensure %s is empty", TMP_FILE_PATH
);
55 /* sanity check that ftruncate() considers negative offsets an error */
56 for (x
= 0; offs
[x
] != 0; x
++) {
57 ret
= ftruncate(fd
, offs
[x
]);
58 T_ASSERT_TRUE(((ret
== -1) && (errno
== EINVAL
)),
59 "negative offset %d", offs
[x
]);
64 /* we want to get the EFBIG errno but without a SIGXFSZ signal */
66 if (!sigsetjmp(xfsz_jmpbuf
, 1)) {
67 signal(SIGXFSZ
, xfsz_signal
);
68 ret
= pwrite(fd
, buffer
, sizeof buffer
, LONG_MAX
);
69 T_ASSERT_TRUE(((ret
== -1) && (errno
== EFBIG
)),
70 "large offset %d", 13);
72 signal(SIGXFSZ
, SIG_DFL
);
73 T_FAIL("%s unexpected SIGXFSZ with offset %lX",
74 "<rdar://problem/28581610>", LONG_MAX
);
77 /* Negative offsets are invalid, no SIGXFSZ signals required */
78 for (x
= 0; offs
[x
] != 0; x
++) {
79 /* only -1 gives the correct result */
84 if (!sigsetjmp(xfsz_jmpbuf
, 1)) {
85 signal(SIGXFSZ
, xfsz_signal
);
86 ret
= pwrite(fd
, buffer
, sizeof buffer
, offs
[x
]);
87 T_ASSERT_TRUE(((ret
== -1) && (errno
== EINVAL
)),
88 "negative offset %d", offs
[x
]);
90 signal(SIGXFSZ
, SIG_DFL
);
91 T_FAIL("%s unexpected SIGXFSZ with negative offset %d",
92 "<rdar://problem/28581610>", offs
[x
]);
96 remove(TMP_FILE_PATH
);