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