xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pread/pread02.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 /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Tests basic error handling of the pread syscall.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - ESPIPE when attempted to read from an unnamed pipe
13*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL if the specified offset position was invalid
14*49cdfc7eSAndroid Build Coastguard Worker  * - EISDIR when fd refers to a directory
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #define PREAD_TEMPFILE  "pread_file"
22*49cdfc7eSAndroid Build Coastguard Worker #define PREAD_TEMPDIR	"pread_dir"
23*49cdfc7eSAndroid Build Coastguard Worker #define K1              1024
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static int pipe_fd[2], fd, dir_fd;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
28*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
29*49cdfc7eSAndroid Build Coastguard Worker 	size_t nb;
30*49cdfc7eSAndroid Build Coastguard Worker 	off_t offst;
31*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
32*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
33*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
34*49cdfc7eSAndroid Build Coastguard Worker 	{&pipe_fd[0], K1, 0, "file descriptor is a PIPE or FIFO", ESPIPE},
35*49cdfc7eSAndroid Build Coastguard Worker 	{&fd, K1, -1, "specified offset is negative", EINVAL},
36*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, K1, 0, "file descriptor is a directory", EISDIR}
37*49cdfc7eSAndroid Build Coastguard Worker };
38*49cdfc7eSAndroid Build Coastguard Worker 
verify_pread(unsigned int n)39*49cdfc7eSAndroid Build Coastguard Worker static void verify_pread(unsigned int n)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case_t *tc = &tcases[n];
42*49cdfc7eSAndroid Build Coastguard Worker 	char buf[K1];
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(pread(*tc->fd, &buf, tc->nb, tc->offst), tc->exp_errno,
45*49cdfc7eSAndroid Build Coastguard Worker 		"pread(%d, %zu, %lld) %s", *tc->fd, tc->nb, (long long)tc->offst, tc->desc);
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)48*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(pipe_fd);
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, pipe_fd[1], "x", 1);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(PREAD_TEMPFILE, O_RDWR | O_CREAT, 0666);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(PREAD_TEMPDIR, 0777);
56*49cdfc7eSAndroid Build Coastguard Worker 	dir_fd = SAFE_OPEN(PREAD_TEMPDIR, O_RDONLY);
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)59*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	int i;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 2; i++) {
64*49cdfc7eSAndroid Build Coastguard Worker 		if (pipe_fd[i] > 0)
65*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CLOSE(pipe_fd[i]);
66*49cdfc7eSAndroid Build Coastguard Worker 	}
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
69*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
70*49cdfc7eSAndroid Build Coastguard Worker 	if (dir_fd > 0)
71*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dir_fd);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
75*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
76*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
77*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
78*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
79*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_pread,
80*49cdfc7eSAndroid Build Coastguard Worker };
81