xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/llseek/llseek02.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  * Description:
8*49cdfc7eSAndroid Build Coastguard Worker  * 1) lseek(2) fails and sets errno to EINVAL when whence is invalid.
9*49cdfc7eSAndroid Build Coastguard Worker  * 2) lseek(2) fails ans sets errno to EBADF when fd is not an open
10*49cdfc7eSAndroid Build Coastguard Worker  * file descriptor.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
14*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
15*49cdfc7eSAndroid Build Coastguard Worker #endif
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE1 "tmp_file1"
21*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE2 "tmp_file2"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
24*49cdfc7eSAndroid Build Coastguard Worker #define SEEK_TOP 10
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static int fd1;
27*49cdfc7eSAndroid Build Coastguard Worker static int fd2;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
30*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
31*49cdfc7eSAndroid Build Coastguard Worker 	int whence;
32*49cdfc7eSAndroid Build Coastguard Worker 	int exp_err;
33*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
34*49cdfc7eSAndroid Build Coastguard Worker 	{&fd1, SEEK_TOP, EINVAL},
35*49cdfc7eSAndroid Build Coastguard Worker 	{&fd2, SEEK_SET, EBADF},
36*49cdfc7eSAndroid Build Coastguard Worker };
37*49cdfc7eSAndroid Build Coastguard Worker 
verify_llseek(unsigned int n)38*49cdfc7eSAndroid Build Coastguard Worker static void verify_llseek(unsigned int n)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	TEST(lseek(*tc->fd, (loff_t) 1, tc->whence));
43*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != (off_t) -1) {
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "lseek(%d, 1, %d) succeeded unexpectedly (%ld)",
45*49cdfc7eSAndroid Build Coastguard Worker 			*tc->fd, tc->whence, TST_RET);
46*49cdfc7eSAndroid Build Coastguard Worker 		return;
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == tc->exp_err) {
49*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "lseek(%d, 1, %d) failed as expected",
50*49cdfc7eSAndroid Build Coastguard Worker 			*tc->fd, tc->whence);
51*49cdfc7eSAndroid Build Coastguard Worker 	} else {
52*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "lseek(%d, 1, %d) failed "
53*49cdfc7eSAndroid Build Coastguard Worker 		        "unexpectedly, expected %s", *tc->fd, tc->whence,
54*49cdfc7eSAndroid Build Coastguard Worker 		        tst_strerrno(tc->exp_err));
55*49cdfc7eSAndroid Build Coastguard Worker 	}
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 	fd1 = SAFE_OPEN(TEMP_FILE1, O_RDWR | O_CREAT, FILE_MODE);
61*49cdfc7eSAndroid Build Coastguard Worker 	fd2 = SAFE_OPEN(TEMP_FILE2, O_RDWR | O_CREAT, FILE_MODE);
62*49cdfc7eSAndroid Build Coastguard Worker 	close(fd2);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
66*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup ,
67*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1 ,
68*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_llseek,
69*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
70*49cdfc7eSAndroid Build Coastguard Worker };
71