xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/read/read02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  * Ported to LTP: Wayne Boyer
5  * Copyright (c) 2017 Fujitsu Ltd.
6  *	04/2017 Modified by Jinhui Huang
7  */
8 /*
9  * DESCRIPTION
10  *	test 1:
11  *	Read with an invalid file descriptor, and expect an EBADF.
12  *
13  *	test 2:
14  *	The parameter passed to read is a directory, check if the errno is
15  *	set to EISDIR.
16  *
17  *	test 3:
18  *	Buf is outside the accessible address space, expect an EFAULT.
19  *
20  *	test 4:
21  *	The file was opened with the O_DIRECT flag, and transfer sizes was not
22  *	multiples of the logical block size of the file system, expect an
23  *	EINVAL.
24  *
25  *	test 5:
26  *	The file was opened with the O_DIRECT flag, and the alignment of the
27  *	user buffer was not multiples of the logical block size of the file
28  *	system, expect an EINVAL.
29  */
30 
31 #define _GNU_SOURCE
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include "tst_test.h"
37 
38 static int badfd = -1;
39 static int fd2, fd3, fd4 = -1;
40 static char buf[BUFSIZ];
41 static void *bufaddr = buf;
42 static void *outside_buf = (void *)-1;
43 static void *addr4;
44 static void *addr5;
45 static long fs_type;
46 
47 static struct tcase {
48 	int *fd;
49 	void **buf;
50 	size_t count;
51 	int exp_error;
52 } tcases[] = {
53 	{&badfd, &bufaddr, 1, EBADF},
54 	{&fd2, &bufaddr, 1, EISDIR},
55 	{&fd3, &outside_buf, 1, EFAULT},
56 	{&fd4, &addr4, 1, EINVAL},
57 	{&fd4, &addr5, 4096, EINVAL},
58 };
59 
verify_read(unsigned int n)60 static void verify_read(unsigned int n)
61 {
62 	struct tcase *tc = &tcases[n];
63 
64 	if (tc->fd == &fd4 && *tc->fd == -1) {
65 		tst_res(TCONF, "O_DIRECT not supported on %s filesystem",
66 			tst_fs_type_name(fs_type));
67 		return;
68 	}
69 
70 	TEST(read(*tc->fd, *tc->buf, tc->count));
71 
72 	if (*tc->fd == fd4 && TST_RET >= 0) {
73 		tst_res(TPASS,
74 			"O_DIRECT unaligned reads fallbacks to buffered I/O");
75 		return;
76 	}
77 
78 	if (TST_RET != -1) {
79 		tst_res(TFAIL, "read() succeeded unexpectedly");
80 		return;
81 	}
82 
83 	if (TST_ERR == tc->exp_error) {
84 		tst_res(TPASS | TTERRNO, "read() failed as expected");
85 	} else {
86 		tst_res(TFAIL | TTERRNO, "read() failed unexpectedly, "
87 			"expected %s", tst_strerrno(tc->exp_error));
88 	}
89 }
90 
setup(void)91 static void setup(void)
92 {
93 	fd2 = SAFE_OPEN(".", O_DIRECTORY);
94 
95 	SAFE_FILE_PRINTF("test_file", "A");
96 
97 	fd3 = SAFE_OPEN("test_file", O_RDWR);
98 
99 	outside_buf = SAFE_MMAP(0, 1, PROT_NONE,
100 				MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
101 
102 	addr4 = SAFE_MEMALIGN(getpagesize(), (4096 * 10));
103 	addr5 = addr4 + 1;
104 
105 	fs_type = tst_fs_type(".");
106 	if (fs_type != TST_TMPFS_MAGIC)
107 		fd4 = SAFE_OPEN("test_file", O_RDWR | O_DIRECT);
108 }
109 
cleanup(void)110 static void cleanup(void)
111 {
112 	free(addr4);
113 
114 	if (fd4 > 0)
115 		SAFE_CLOSE(fd4);
116 
117 	if (fd3 > 0)
118 		SAFE_CLOSE(fd3);
119 
120 	if (fd2 > 0)
121 		SAFE_CLOSE(fd2);
122 }
123 
124 static struct tst_test test = {
125 	.tcnt = ARRAY_SIZE(tcases),
126 	.test = verify_read,
127 	.setup = setup,
128 	.cleanup = cleanup,
129 	.needs_tmpdir = 1,
130 };
131