xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/read/read01.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) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017 Fujitsu Ltd.
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker #define SIZE 512
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker static int fd;
13*49cdfc7eSAndroid Build Coastguard Worker static char buf[SIZE];
14*49cdfc7eSAndroid Build Coastguard Worker 
verify_read(void)15*49cdfc7eSAndroid Build Coastguard Worker static void verify_read(void)
16*49cdfc7eSAndroid Build Coastguard Worker {
17*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(fd, 0, SEEK_SET);
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker 	TEST(read(fd, buf, SIZE));
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
22*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "read(2) failed");
23*49cdfc7eSAndroid Build Coastguard Worker 	else
24*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "read(2) returned %ld", TST_RET);
25*49cdfc7eSAndroid Build Coastguard Worker }
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 	memset(buf, '*', SIZE);
30*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0700);
31*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, SIZE);
32*49cdfc7eSAndroid Build Coastguard Worker }
33*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)34*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
37*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
41*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_read,
42*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
43*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
44*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
45*49cdfc7eSAndroid Build Coastguard Worker };
46