1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *   07/2001 Ported by Wayne Boyer
5  */
6 /*
7  * Verify that lseek64() call succeeds to set the file pointer position to an
8  * offset larger than file size limit (RLIMIT_FSIZE). Also, verify that any
9  * attempt to write to this location fails.
10  */
11 
12 #ifndef _GNU_SOURCE
13 #define _GNU_SOURCE
14 #endif
15 
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 
20 #include "tst_test.h"
21 
22 #define TEMP_FILE	"tmp_file"
23 #define FILE_MODE	0644
24 
25 static char write_buff[BUFSIZ];
26 static int fildes;
27 
verify_llseek(void)28 static void verify_llseek(void)
29 {
30 	TEST(lseek64(fildes, (loff_t) (80 * BUFSIZ), SEEK_SET));
31 	if (TST_RET == (80 * BUFSIZ))
32 		tst_res(TPASS, "lseek64() can set file pointer position larger than file size limit");
33 	else
34 		tst_res(TFAIL, "lseek64() returned wrong value %ld when write past file size", TST_RET);
35 
36 	if (write(fildes, write_buff, BUFSIZ) == -1)
37 		tst_res(TPASS,"write failed after file size limit");
38 	else
39 		tst_brk(TFAIL, "write successful after file size limit");
40 
41 	TEST(lseek64(fildes, (loff_t) BUFSIZ, SEEK_SET));
42 	if (TST_RET == BUFSIZ)
43 		tst_res(TPASS,"lseek64() can set file pointer position under filer size limit");
44 	else
45 		tst_brk(TFAIL,"lseek64() returns wrong value %ld when write under file size", TST_RET);
46 
47 	if (write(fildes, write_buff, BUFSIZ) != -1)
48 		tst_res(TPASS, "write succcessfully under file size limit");
49 	else
50 		tst_brk(TFAIL, "write failed under file size limit");
51 
52 	if (write(fildes, write_buff, BUFSIZ) == -1)
53 		tst_res(TPASS, "write failed after file size limit");
54 	else
55 		tst_brk(TFAIL, "write successfully after file size limit");
56 }
57 
setup(void)58 static void setup(void)
59 {
60 	struct sigaction act;
61 	struct rlimit rlp;
62 
63 	act.sa_handler = SIG_IGN;
64 	act.sa_flags = 0;
65 	sigemptyset(&act.sa_mask);
66 	SAFE_SIGACTION(SIGXFSZ, &act, NULL);
67 
68 	rlp.rlim_cur = rlp.rlim_max = 2 * BUFSIZ;
69 	SAFE_SETRLIMIT(RLIMIT_FSIZE, &rlp);
70 
71 	fildes = SAFE_OPEN(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE);
72 
73 	SAFE_WRITE(1, fildes, write_buff, BUFSIZ);
74 }
75 
76 static struct tst_test test = {
77 	.setup = setup,
78 	.needs_tmpdir = 1,
79 	.test_all = verify_llseek,
80 };
81