xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/write/write06.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) 2021 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Dai Shili <[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  * Test the write() system call with O_APPEND.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * The full description of O_APPEND is in open(2) man-pages:
13*49cdfc7eSAndroid Build Coastguard Worker  * The file is opened in append mode.  Before each write(2), the
14*49cdfc7eSAndroid Build Coastguard Worker  * file offset is positioned at the end of the file, as if with lseek(2).
15*49cdfc7eSAndroid Build Coastguard Worker  * The modification of the file offset and the write operation are
16*49cdfc7eSAndroid Build Coastguard Worker  * performed as a single atomic step.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * Writing 2k data to the file, close it and reopen it with O_APPEND.
19*49cdfc7eSAndroid Build Coastguard Worker  * Verify that the file size is 3k and offset is moved to the end of the file.
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <inttypes.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_prw.h"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #define K1              1024
28*49cdfc7eSAndroid Build Coastguard Worker #define K2              (K1 * 2)
29*49cdfc7eSAndroid Build Coastguard Worker #define K3              (K1 * 3)
30*49cdfc7eSAndroid Build Coastguard Worker #define DATA_FILE       "write06_file"
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
33*49cdfc7eSAndroid Build Coastguard Worker static char *write_buf[2];
34*49cdfc7eSAndroid Build Coastguard Worker 
verify_write(void)35*49cdfc7eSAndroid Build Coastguard Worker static void verify_write(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	off_t off;
38*49cdfc7eSAndroid Build Coastguard Worker 	struct stat statbuf;
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd, write_buf[0], K2);
42*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND);
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FSTAT(fd, &statbuf);
46*49cdfc7eSAndroid Build Coastguard Worker 	if (statbuf.st_size != K2)
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "file size is %ld != K2", statbuf.st_size);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	off = SAFE_LSEEK(fd, K1, SEEK_SET);
50*49cdfc7eSAndroid Build Coastguard Worker 	if (off != K1)
51*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Failed to seek to K1");
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd, write_buf[1], K1);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	off = SAFE_LSEEK(fd, 0, SEEK_CUR);
56*49cdfc7eSAndroid Build Coastguard Worker 	if (off != K3)
57*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Wrong offset after write %zu expected %u", off, K3);
58*49cdfc7eSAndroid Build Coastguard Worker 	else
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Offset is correct after write %zu", off);
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FSTAT(fd, &statbuf);
62*49cdfc7eSAndroid Build Coastguard Worker 	if (statbuf.st_size != K3)
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Wrong file size after append %zu expected %u", statbuf.st_size, K3);
64*49cdfc7eSAndroid Build Coastguard Worker 	else
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Correct file size after append %u", K3);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)70*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	memset(write_buf[0], 0, K2);
73*49cdfc7eSAndroid Build Coastguard Worker 	memset(write_buf[1], 1, K1);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)76*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker 	if (fd != -1)
79*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(DATA_FILE);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
85*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
86*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
87*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
88*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_write,
89*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers[]) {
90*49cdfc7eSAndroid Build Coastguard Worker 		{&write_buf[0], .size = K2},
91*49cdfc7eSAndroid Build Coastguard Worker 		{&write_buf[1], .size = K1},
92*49cdfc7eSAndroid Build Coastguard Worker 		{}
93*49cdfc7eSAndroid Build Coastguard Worker 	}
94*49cdfc7eSAndroid Build Coastguard Worker };
95