xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/llseek/llseek03.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) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiaoguang Wang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker  * Description:
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * For each of SEEK_SET, SEEK_CUR and SEEK_END verify that,
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  *   1. llseek() succeeds to set file position in the middle of the data. The
12*49cdfc7eSAndroid Build Coastguard Worker  *      file offset is checked by reading from a file and comparing the data.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  *   2. llseek() succeeds to set file postion to the end of the data, reading
15*49cdfc7eSAndroid Build Coastguard Worker  *      this postion returns 0.
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  *   3. llseek() succeeds to set file position after the end of the data,
18*49cdfc7eSAndroid Build Coastguard Worker  *      reading from this postion returns 0 as well.
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "testfile"
25*49cdfc7eSAndroid Build Coastguard Worker #define STR "abcdefgh"
26*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)27*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT(TEST_FILE, 0644);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd, STR, sizeof(STR) - 1);
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
36*49cdfc7eSAndroid Build Coastguard Worker }
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
39*49cdfc7eSAndroid Build Coastguard Worker 	int whence;
40*49cdfc7eSAndroid Build Coastguard Worker 	int off;
41*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
42*49cdfc7eSAndroid Build Coastguard Worker 	int read;
43*49cdfc7eSAndroid Build Coastguard Worker 	const char *str;
44*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
45*49cdfc7eSAndroid Build Coastguard Worker 	/* Seek somewhere in the middle of data */
46*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_SET, 1, 1, 3, "bcd"},
47*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_CUR, 1, 5, 3, "fgh"},
48*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_END, -1, 7, 1, "h"},
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	/* Seek to the end of data */
51*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_SET, 8, 8, 0, NULL},
52*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_CUR, 4, 8, 0, NULL},
53*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_END, 0, 8, 0, NULL},
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	/* Seek after the end of data */
56*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_SET, 10, 10, 0, NULL},
57*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_CUR, 8, 12, 0, NULL},
58*49cdfc7eSAndroid Build Coastguard Worker 	{SEEK_END, 4, 12, 0, NULL},
59*49cdfc7eSAndroid Build Coastguard Worker };
60*49cdfc7eSAndroid Build Coastguard Worker 
str_whence(int whence)61*49cdfc7eSAndroid Build Coastguard Worker static const char *str_whence(int whence)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	switch (whence) {
64*49cdfc7eSAndroid Build Coastguard Worker 	case SEEK_SET:
65*49cdfc7eSAndroid Build Coastguard Worker 		return "SEEK_SET";
66*49cdfc7eSAndroid Build Coastguard Worker 	case SEEK_CUR:
67*49cdfc7eSAndroid Build Coastguard Worker 		return "SEEK_CUR";
68*49cdfc7eSAndroid Build Coastguard Worker 	case SEEK_END:
69*49cdfc7eSAndroid Build Coastguard Worker 		return "SEEK_END";
70*49cdfc7eSAndroid Build Coastguard Worker 	default:
71*49cdfc7eSAndroid Build Coastguard Worker 		return "INVALID";
72*49cdfc7eSAndroid Build Coastguard Worker 	}
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker 
verify_lseek(unsigned int n)75*49cdfc7eSAndroid Build Coastguard Worker static void verify_lseek(unsigned int n)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
78*49cdfc7eSAndroid Build Coastguard Worker 	char read_buf[128];
79*49cdfc7eSAndroid Build Coastguard Worker 	int fd, ret;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TEST_FILE, O_RDONLY);
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(1, fd, read_buf, 4);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	TEST(lseek(fd, tc->off, tc->whence));
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
88*49cdfc7eSAndroid Build Coastguard Worker                 tst_res(TFAIL | TTERRNO, "llseek failed on %s ", TEST_FILE);
89*49cdfc7eSAndroid Build Coastguard Worker                 goto exit;
90*49cdfc7eSAndroid Build Coastguard Worker         }
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != tc->ret) {
93*49cdfc7eSAndroid Build Coastguard Worker                 tst_res(TFAIL, "llseek returned %li expected %i", TST_RET, tc->ret);
94*49cdfc7eSAndroid Build Coastguard Worker                 goto exit;
95*49cdfc7eSAndroid Build Coastguard Worker         } else {
96*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "llseek returned %i", tc->ret);
97*49cdfc7eSAndroid Build Coastguard Worker 	}
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker         memset(read_buf, 0, sizeof(read_buf));
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker         ret = SAFE_READ(0, fd, read_buf, tc->read);
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	if (!tc->read) {
104*49cdfc7eSAndroid Build Coastguard Worker 		if (ret != 0)
105*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "Read bytes after llseek to end of file");
106*49cdfc7eSAndroid Build Coastguard Worker 		else
107*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS, "%s read returned 0", str_whence(tc->whence));
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 		goto exit;
110*49cdfc7eSAndroid Build Coastguard Worker 	}
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker         if (strcmp(read_buf, tc->str))
113*49cdfc7eSAndroid Build Coastguard Worker                 tst_res(TFAIL, "Read wrong bytes after llseek");
114*49cdfc7eSAndroid Build Coastguard Worker         else
115*49cdfc7eSAndroid Build Coastguard Worker                 tst_res(TPASS, "%s for llseek", str_whence(tc->whence));
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker exit:
118*49cdfc7eSAndroid Build Coastguard Worker         SAFE_CLOSE(fd);
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
122*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
123*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
124*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_lseek,
125*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
126*49cdfc7eSAndroid Build Coastguard Worker };
127