xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pwritev/pwritev01.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) 2015 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2016-2023
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Testcase to check the basic functionality of the pwritev(2).
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * pwritev(2) should succeed to write the expected content of data
14*49cdfc7eSAndroid Build Coastguard Worker  * and after writing the file, the file offset is not changed.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
18*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "pwritev.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_prw.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define	CHUNK		64
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static char buf[CHUNK];
27*49cdfc7eSAndroid Build Coastguard Worker static char initbuf[CHUNK * 2];
28*49cdfc7eSAndroid Build Coastguard Worker static char preadbuf[CHUNK];
29*49cdfc7eSAndroid Build Coastguard Worker static int fd;
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static struct iovec wr_iovec[] = {
32*49cdfc7eSAndroid Build Coastguard Worker 	{buf, CHUNK},
33*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, 0},
34*49cdfc7eSAndroid Build Coastguard Worker };
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
37*49cdfc7eSAndroid Build Coastguard Worker 	int count;
38*49cdfc7eSAndroid Build Coastguard Worker 	off_t offset;
39*49cdfc7eSAndroid Build Coastguard Worker 	ssize_t size;
40*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
41*49cdfc7eSAndroid Build Coastguard Worker 	{1, 0, CHUNK},
42*49cdfc7eSAndroid Build Coastguard Worker 	{2, 0, CHUNK},
43*49cdfc7eSAndroid Build Coastguard Worker 	{1, CHUNK/2, CHUNK},
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker 
verify_pwritev(unsigned int n)46*49cdfc7eSAndroid Build Coastguard Worker static void verify_pwritev(unsigned int n)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	int i;
49*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PWRITE(1, fd, initbuf, sizeof(initbuf), 0);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(fd, 0, SEEK_SET);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	TEST(pwritev(fd, wr_iovec, tc->count, tc->offset));
56*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0) {
57*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "pwritev() failed");
58*49cdfc7eSAndroid Build Coastguard Worker 		return;
59*49cdfc7eSAndroid Build Coastguard Worker 	}
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != tc->size) {
62*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "pwritev() wrote %li bytes, expected %zi",
63*49cdfc7eSAndroid Build Coastguard Worker 			 TST_RET, tc->size);
64*49cdfc7eSAndroid Build Coastguard Worker 		return;
65*49cdfc7eSAndroid Build Coastguard Worker 	}
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	if (SAFE_LSEEK(fd, 0, SEEK_CUR) != 0) {
68*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "pwritev() had changed file offset");
69*49cdfc7eSAndroid Build Coastguard Worker 		return;
70*49cdfc7eSAndroid Build Coastguard Worker 	}
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PREAD(1, fd, preadbuf, tc->size, tc->offset);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < tc->size; i++) {
75*49cdfc7eSAndroid Build Coastguard Worker 		if (preadbuf[i] != 0x61)
76*49cdfc7eSAndroid Build Coastguard Worker 			break;
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	if (i != tc->size) {
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "buffer wrong at %i have %02x expected 61",
81*49cdfc7eSAndroid Build Coastguard Worker 			 i, preadbuf[i]);
82*49cdfc7eSAndroid Build Coastguard Worker 		return;
83*49cdfc7eSAndroid Build Coastguard Worker 	}
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "writev() wrote %zi bytes successfully "
86*49cdfc7eSAndroid Build Coastguard Worker 		 "with content 'a' expectedly ", tc->size);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)89*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker 	memset(&buf, 0x61, CHUNK);
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("file", O_RDWR | O_CREAT, 0644);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)96*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
99*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
103*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
104*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
105*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
106*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_pwritev,
107*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
108*49cdfc7eSAndroid Build Coastguard Worker };
109