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