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