All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls/write06: Add new test
@ 2021-12-06 21:17 Dai Shili
  2021-12-07 13:51 ` Cyril Hrubis
  0 siblings, 1 reply; 9+ messages in thread
From: Dai Shili @ 2021-12-06 21:17 UTC (permalink / raw)
  To: ltp

Like pwrite04.c, test the write() system call with O_APPEND.

Signed-off-by: Dai Shili <daisl.fnst@fujitsu.com>
---
 runtest/syscalls                           |  1 +
 testcases/kernel/syscalls/write/.gitignore |  1 +
 testcases/kernel/syscalls/write/write06.c  | 94 ++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+)
 create mode 100644 testcases/kernel/syscalls/write/write06.c

diff --git a/runtest/syscalls b/runtest/syscalls
index bcf3d56..32fcda4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1699,6 +1699,7 @@ write02 write02
 write03 write03
 write04 write04
 write05 write05
+write06 write06
 
 writev01 writev01
 writev02 writev02
diff --git a/testcases/kernel/syscalls/write/.gitignore b/testcases/kernel/syscalls/write/.gitignore
index 7f36194..8529aae 100644
--- a/testcases/kernel/syscalls/write/.gitignore
+++ b/testcases/kernel/syscalls/write/.gitignore
@@ -3,3 +3,4 @@
 /write03
 /write04
 /write05
+/write06
diff --git a/testcases/kernel/syscalls/write/write06.c b/testcases/kernel/syscalls/write/write06.c
new file mode 100644
index 0000000..c62a266
--- /dev/null
+++ b/testcases/kernel/syscalls/write/write06.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Dai Shili <daisl.fnst@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test the write() system call with O_APPEND.
+ *
+ * Writing 2k data to the file, close it and reopen it with O_APPEND.
+ * Verify that the file size is 3k and offset is moved to the end of the file.
+ */
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include "tst_test.h"
+#include "tst_safe_prw.h"
+
+#define K1              1024
+#define K2              (K1 * 2)
+#define K3              (K1 * 3)
+#define DATA_FILE       "write06_file"
+
+static int fd = -1;
+static char *write_buf[2];
+
+static void l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
+{
+	off_t offloc;
+
+	offloc = SAFE_LSEEK(fdesc, offset, whence);
+	if (offloc != checkoff) {
+		tst_res(TFAIL, "%" PRId64 " = lseek(%d, %" PRId64 ", %d) != %" PRId64,
+			(int64_t)offloc, fdesc, (int64_t)offset, whence, (int64_t)checkoff);
+	}
+}
+
+static void verify_write(void)
+{
+	int fail = 0;
+	struct stat statbuf;
+
+	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);
+	SAFE_WRITE(1, fd, write_buf[0], K2);
+	SAFE_CLOSE(fd);
+
+	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND, 0666);
+	SAFE_FSTAT(fd, &statbuf);
+	if (statbuf.st_size != K2) {
+		fail++;
+		tst_res(TFAIL, "file size is %ld != K2", statbuf.st_size);
+	}
+
+	l_seek(fd, K1, SEEK_SET, K1);
+	SAFE_WRITE(1, fd, write_buf[1], K1);
+	l_seek(fd, 0, SEEK_CUR, K3);
+	SAFE_FSTAT(fd, &statbuf);
+	if (statbuf.st_size != K3) {
+		fail++;
+		tst_res(TFAIL, "file size is %ld != K3", statbuf.st_size);
+	}
+
+	if (!fail)
+		tst_res(TPASS, "O_APPEND test passed.");
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	write_buf[0] = SAFE_MALLOC(K2);
+	memset(write_buf[0], 0, K2);
+	write_buf[1] = SAFE_MALLOC(K1);
+	memset(write_buf[0], 1, K1);
+}
+
+static void cleanup(void)
+{
+	free(write_buf[0]);
+	free(write_buf[1]);
+
+	if (fd > -1)
+		SAFE_CLOSE(fd);
+
+	SAFE_UNLINK(DATA_FILE);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_write,
+};
-- 
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH] syscalls/write06: Add new test
  2021-12-06 21:17 [LTP] [PATCH] syscalls/write06: Add new test Dai Shili
@ 2021-12-07 13:51 ` Cyril Hrubis
  2021-12-08  8:38   ` daisl.fnst
  0 siblings, 1 reply; 9+ messages in thread
From: Cyril Hrubis @ 2021-12-07 13:51 UTC (permalink / raw)
  To: Dai Shili; +Cc: ltp

Hi!
> Signed-off-by: Dai Shili <daisl.fnst@fujitsu.com>
> ---
>  runtest/syscalls                           |  1 +
>  testcases/kernel/syscalls/write/.gitignore |  1 +
>  testcases/kernel/syscalls/write/write06.c  | 94 ++++++++++++++++++++++++++++++
>  3 files changed, 96 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/write/write06.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index bcf3d56..32fcda4 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -1699,6 +1699,7 @@ write02 write02
>  write03 write03
>  write04 write04
>  write05 write05
> +write06 write06
>  
>  writev01 writev01
>  writev02 writev02
> diff --git a/testcases/kernel/syscalls/write/.gitignore b/testcases/kernel/syscalls/write/.gitignore
> index 7f36194..8529aae 100644
> --- a/testcases/kernel/syscalls/write/.gitignore
> +++ b/testcases/kernel/syscalls/write/.gitignore
> @@ -3,3 +3,4 @@
>  /write03
>  /write04
>  /write05
> +/write06
> diff --git a/testcases/kernel/syscalls/write/write06.c b/testcases/kernel/syscalls/write/write06.c
> new file mode 100644
> index 0000000..c62a266
> --- /dev/null
> +++ b/testcases/kernel/syscalls/write/write06.c
> @@ -0,0 +1,94 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
> + * Author: Dai Shili <daisl.fnst@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Test the write() system call with O_APPEND.
> + *
> + * Writing 2k data to the file, close it and reopen it with O_APPEND.
> + * Verify that the file size is 3k and offset is moved to the end of the file.
> + */
> +
> +#include <stdlib.h>
> +#include <inttypes.h>
> +#include "tst_test.h"
> +#include "tst_safe_prw.h"
> +
> +#define K1              1024
> +#define K2              (K1 * 2)
> +#define K3              (K1 * 3)
> +#define DATA_FILE       "write06_file"
> +
> +static int fd = -1;
> +static char *write_buf[2];
> +
> +static void l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
> +{
> +	off_t offloc;
> +
> +	offloc = SAFE_LSEEK(fdesc, offset, whence);
> +	if (offloc != checkoff) {
> +		tst_res(TFAIL, "%" PRId64 " = lseek(%d, %" PRId64 ", %d) != %" PRId64,
> +			(int64_t)offloc, fdesc, (int64_t)offset, whence, (int64_t)checkoff);
> +	}
> +}
> +
> +static void verify_write(void)
> +{
> +	int fail = 0;
> +	struct stat statbuf;
> +
> +	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +	SAFE_WRITE(1, fd, write_buf[0], K2);
> +	SAFE_CLOSE(fd);
> +
> +	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND, 0666);
                                                     ^
						     No need to pass
						     mode without
						     O_CREAT
> +	SAFE_FSTAT(fd, &statbuf);
> +	if (statbuf.st_size != K2) {
> +		fail++;
> +		tst_res(TFAIL, "file size is %ld != K2", statbuf.st_size);
> +	}
> +
> +	l_seek(fd, K1, SEEK_SET, K1);

Why do we do the seek here?

Wouldn't it make much more sense just to write the write_buf[1] then
check that the st_size is K3?

That way we would check that O_APPEND opened file has correct position
associated with the file descriptor.

> +	SAFE_WRITE(1, fd, write_buf[1], K1);
> +	l_seek(fd, 0, SEEK_CUR, K3);

And here as well, why the seek? This is actually the place that
increases the size not the write() as it should have been.

> +	SAFE_FSTAT(fd, &statbuf);
> +	if (statbuf.st_size != K3) {
> +		fail++;
> +		tst_res(TFAIL, "file size is %ld != K3", statbuf.st_size);
> +	}

Really the whole test should do:

	open(FILE, O_RDWR | O_CREAT | O_TRUNC, 0666)
	write()
	close()

	open(FILE, O_RDWR | O_APPEND)
	check size
	write()
	check size
	close()

	open(FILE, O_RDONLY);
	read()
	verify data
	close()


> +	if (!fail)
> +		tst_res(TPASS, "O_APPEND test passed.");
> +	SAFE_CLOSE(fd);
> +}
> +
> +static void setup(void)
> +{
> +	write_buf[0] = SAFE_MALLOC(K2);
> +	memset(write_buf[0], 0, K2);
> +	write_buf[1] = SAFE_MALLOC(K1);
> +	memset(write_buf[0], 1, K1);
                         ^
			 1

Also can you please switch these two buffers to a guarded buffers?

See:

https://github.com/linux-test-project/ltp/wiki/C-Test-API#131-guarded-buffers

> +}
> +
> +static void cleanup(void)
> +{
> +	free(write_buf[0]);
> +	free(write_buf[1]);
> +
> +	if (fd > -1)

Probably fd != -1 would be a bit clearer.

> +		SAFE_CLOSE(fd);
> +
> +	SAFE_UNLINK(DATA_FILE);
> +}
> +
> +static struct tst_test test = {
> +	.needs_tmpdir = 1,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = verify_write,
> +};
> -- 
> 1.8.3.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH] syscalls/write06: Add new test
  2021-12-07 13:51 ` Cyril Hrubis
@ 2021-12-08  8:38   ` daisl.fnst
  2021-12-10 10:57     ` Cyril Hrubis
  0 siblings, 1 reply; 9+ messages in thread
From: daisl.fnst @ 2021-12-08  8:38 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

Hi Cyril

在 2021/12/7 21:51, Cyril Hrubis 写道:
> Hi!
>> Signed-off-by: Dai Shili <daisl.fnst@fujitsu.com>
>> ---
>>   runtest/syscalls                           |  1 +
>>   testcases/kernel/syscalls/write/.gitignore |  1 +
>>   testcases/kernel/syscalls/write/write06.c  | 94 ++++++++++++++++++++++++++++++
>>   3 files changed, 96 insertions(+)
>>   create mode 100644 testcases/kernel/syscalls/write/write06.c
>>
>> diff --git a/runtest/syscalls b/runtest/syscalls
>> index bcf3d56..32fcda4 100644
>> --- a/runtest/syscalls
>> +++ b/runtest/syscalls
>> @@ -1699,6 +1699,7 @@ write02 write02
>>   write03 write03
>>   write04 write04
>>   write05 write05
>> +write06 write06
>>   
>>   writev01 writev01
>>   writev02 writev02
>> diff --git a/testcases/kernel/syscalls/write/.gitignore b/testcases/kernel/syscalls/write/.gitignore
>> index 7f36194..8529aae 100644
>> --- a/testcases/kernel/syscalls/write/.gitignore
>> +++ b/testcases/kernel/syscalls/write/.gitignore
>> @@ -3,3 +3,4 @@
>>   /write03
>>   /write04
>>   /write05
>> +/write06
>> diff --git a/testcases/kernel/syscalls/write/write06.c b/testcases/kernel/syscalls/write/write06.c
>> new file mode 100644
>> index 0000000..c62a266
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/write/write06.c
>> @@ -0,0 +1,94 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
>> + * Author: Dai Shili <daisl.fnst@fujitsu.com>
>> + */
>> +
>> +/*\
>> + * [Description]
>> + *
>> + * Test the write() system call with O_APPEND.
>> + *
>> + * Writing 2k data to the file, close it and reopen it with O_APPEND.
>> + * Verify that the file size is 3k and offset is moved to the end of the file.
>> + */
>> +
>> +#include <stdlib.h>
>> +#include <inttypes.h>
>> +#include "tst_test.h"
>> +#include "tst_safe_prw.h"
>> +
>> +#define K1              1024
>> +#define K2              (K1 * 2)
>> +#define K3              (K1 * 3)
>> +#define DATA_FILE       "write06_file"
>> +
>> +static int fd = -1;
>> +static char *write_buf[2];
>> +
>> +static void l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
>> +{
>> +	off_t offloc;
>> +
>> +	offloc = SAFE_LSEEK(fdesc, offset, whence);
>> +	if (offloc != checkoff) {
>> +		tst_res(TFAIL, "%" PRId64 " = lseek(%d, %" PRId64 ", %d) != %" PRId64,
>> +			(int64_t)offloc, fdesc, (int64_t)offset, whence, (int64_t)checkoff);
>> +	}
>> +}
>> +
>> +static void verify_write(void)
>> +{
>> +	int fail = 0;
>> +	struct stat statbuf;
>> +
>> +	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);
>> +	SAFE_WRITE(1, fd, write_buf[0], K2);
>> +	SAFE_CLOSE(fd);
>> +
>> +	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND, 0666);
>                                                       ^
> 						     No need to pass
> 						     mode without
> 						     O_CREAT
Got it.
>> +	SAFE_FSTAT(fd, &statbuf);
>> +	if (statbuf.st_size != K2) {
>> +		fail++;
>> +		tst_res(TFAIL, "file size is %ld != K2", statbuf.st_size);
>> +	}
>> +
>> +	l_seek(fd, K1, SEEK_SET, K1);
> Why do we do the seek here?
>
> Wouldn't it make much more sense just to write the write_buf[1] then
> check that the st_size is K3?
>
> That way we would check that O_APPEND opened file has correct position
> associated with the file descriptor.

I want to verify that the offset will move to the end of the file.

So I put the offset in the middle of the file before write.

>> +	SAFE_WRITE(1, fd, write_buf[1], K1);
>> +	l_seek(fd, 0, SEEK_CUR, K3);
> And here as well, why the seek? This is actually the place that
> increases the size not the write() as it should have been.
Do seek to check the offset is moved to the end of the file.
>> +	SAFE_FSTAT(fd, &statbuf);
>> +	if (statbuf.st_size != K3) {
>> +		fail++;
>> +		tst_res(TFAIL, "file size is %ld != K3", statbuf.st_size);
>> +	}
> Really the whole test should do:
>
> 	open(FILE, O_RDWR | O_CREAT | O_TRUNC, 0666)
> 	write()
> 	close()
>
> 	open(FILE, O_RDWR | O_APPEND)
> 	check size
> 	write()
> 	check size
> 	close()
>
> 	open(FILE, O_RDONLY);
> 	read()
> 	verify data
> 	close()
>
I'm so sorry, my commit msg is not clear.

What I want to test is the following description of open(2) man-pages:

O_APPEND
       The file is opened in append mode.  Before each write(2), the 
file offset is positioned at the end of the file, as if with lseek(2).  
The modification of the file offset and the write operation are 
performed as a single atomic step.
>> +	if (!fail)
>> +		tst_res(TPASS, "O_APPEND test passed.");
>> +	SAFE_CLOSE(fd);
>> +}
>> +
>> +static void setup(void)
>> +{
>> +	write_buf[0] = SAFE_MALLOC(K2);
>> +	memset(write_buf[0], 0, K2);
>> +	write_buf[1] = SAFE_MALLOC(K1);
>> +	memset(write_buf[0], 1, K1);
>                           ^
> 			 1
>
> Also can you please switch these two buffers to a guarded buffers?
>
> See:
>
> https://github.com/linux-test-project/ltp/wiki/C-Test-API#131-guarded-buffers
OK. I will switch to guarded buffers.
>> +}
>> +
>> +static void cleanup(void)
>> +{
>> +	free(write_buf[0]);
>> +	free(write_buf[1]);
>> +
>> +	if (fd > -1)
> Probably fd != -1 would be a bit clearer.
Got it.
>> +		SAFE_CLOSE(fd);
>> +
>> +	SAFE_UNLINK(DATA_FILE);
>> +}
>> +
>> +static struct tst_test test = {
>> +	.needs_tmpdir = 1,
>> +	.setup = setup,
>> +	.cleanup = cleanup,
>> +	.test_all = verify_write,
>> +};
>> -- 
>> 1.8.3.1
>>
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH] syscalls/write06: Add new test
  2021-12-08  8:38   ` daisl.fnst
@ 2021-12-10 10:57     ` Cyril Hrubis
  2021-12-20 19:23       ` [LTP] [PATCH v2] " Dai Shili
  0 siblings, 1 reply; 9+ messages in thread
From: Cyril Hrubis @ 2021-12-10 10:57 UTC (permalink / raw)
  To: daisl.fnst; +Cc: ltp

Hi!
> What I want to test is the following description of open(2) man-pages:
> 
> O_APPEND
>  ?????? ?? The file is opened in append mode.?? Before each write(2), the 
> file offset is positioned at the end of the file, as if with lseek(2).?? 
> The modification of the file offset and the write operation are 
> performed as a single atomic step.

Ah, now it makes much more sense.

I think part of the problem is that the code is actually confusing. I do
not like the l_seek() function at all, it's confusing at best because it
does several different things at the same time. So it would be much
better to write the offset check as:

	off = SAFE_LSEEK(fd, 1K, SEEK_SET)
	if (off != 1K)
		tst_brk(TBROK, "Failed to seek to 1K");

	SAFE_WRITE(1, fd, write_buf[1], K1);

	off = SAFE_LSEEK(fd, 0, SEEK_CUR);
	if (off != K3)
		tst_res(TFAIL, "Wrong offset after write %zu expected %u", off, K3)
	else
		tst_res(TPASS, "Offset is correct after write %zu", off);

	SAFE_FSTAT(fd, &statbuf);
	if (statbuf.st_size != K3) {
		tst_res(TFAIL, "Wrong file size after append %zu expected %u",
			statubuf.st_size, K3);
	} else {
		tst_res(TPASS, "Correct file size after append %u", K3);
	}


This makes it much clearer what happens here and the TPASS message
actually says what has passed...

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v2] syscalls/write06: Add new test
  2021-12-20 19:23       ` [LTP] [PATCH v2] " Dai Shili
@ 2021-12-20  9:53         ` Petr Vorel
  2021-12-21 20:03           ` [LTP] [PATCH v3] " Dai Shili
  0 siblings, 1 reply; 9+ messages in thread
From: Petr Vorel @ 2021-12-20  9:53 UTC (permalink / raw)
  To: Dai Shili; +Cc: ltp

Hi Dai,

> Like pwrite04.c, test the write() system call with O_APPEND.

...
> +++ b/testcases/kernel/syscalls/write/write06.c
> @@ -0,0 +1,95 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
> + * Author: Dai Shili <daisl.fnst@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Test the write() system call with O_APPEND.
> + *
> + * The full description of O_APPEND is in open(2) man-pages:
> + * O_APPEND
> + *        The file is opened in append mode.  Before each write(2), the
nit: FYI this formatting will be lost in our metadata.
(To see the output, you can run make metadata/ and check docparse/metadata.html
or docparse/metadata.pdf.)

...
> +static void verify_write(void)
> +{
> +	off_t off;
> +	struct stat statbuf;
> +
> +	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC);
> +	SAFE_WRITE(1, fd, write_buf[0], K2);
> +	SAFE_CLOSE(fd);
> +
> +	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND);

Quite often I get EACCES when running the test. It's strange that not always.

$ ./write06
tst_buffers.c:55: TINFO: Test is using guarded buffers
tst_test.c:1423: TINFO: Timeout per run is 0h 05m 00s
write06.c:45: TBROK: open(write06_file,1026,4162714000) failed: EACCES (13)

IMHO this depends on umask (0022) in my case.
Obviously specifying permissions as it's in pwrite04.c fixes the problem:


@@ -38,7 +38,7 @@ static void verify_write(void)
 	off_t off;
 	struct stat statbuf;
 
-	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC);
+	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);
 	SAFE_WRITE(1, fd, write_buf[0], K2);
 	SAFE_CLOSE(fd);

(pwrite04.c specifies permission in both SAFE_OPEN(), but it's obviously enough
to put it just in the first one, which uses O_CREAT).

Also Cyril noted using permissions in review of v1 patch:
https://lore.kernel.org/ltp/Ya9my0FdHXLqvkJr@yuki/

Otherwise LGTM.
Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [LTP] [PATCH v2] syscalls/write06: Add new test
  2021-12-10 10:57     ` Cyril Hrubis
@ 2021-12-20 19:23       ` Dai Shili
  2021-12-20  9:53         ` Petr Vorel
  0 siblings, 1 reply; 9+ messages in thread
From: Dai Shili @ 2021-12-20 19:23 UTC (permalink / raw)
  To: chrubis; +Cc: ltp

Like pwrite04.c, test the write() system call with O_APPEND.

Signed-off-by: Dai Shili <daisl.fnst@fujitsu.com>
---
 runtest/syscalls                           |  1 +
 testcases/kernel/syscalls/write/.gitignore |  1 +
 testcases/kernel/syscalls/write/write06.c  | 95 ++++++++++++++++++++++++++++++
 3 files changed, 97 insertions(+)
 create mode 100644 testcases/kernel/syscalls/write/write06.c

diff --git a/runtest/syscalls b/runtest/syscalls
index bcf3d56..32fcda4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1699,6 +1699,7 @@ write02 write02
 write03 write03
 write04 write04
 write05 write05
+write06 write06
 
 writev01 writev01
 writev02 writev02
diff --git a/testcases/kernel/syscalls/write/.gitignore b/testcases/kernel/syscalls/write/.gitignore
index 7f36194..8529aae 100644
--- a/testcases/kernel/syscalls/write/.gitignore
+++ b/testcases/kernel/syscalls/write/.gitignore
@@ -3,3 +3,4 @@
 /write03
 /write04
 /write05
+/write06
diff --git a/testcases/kernel/syscalls/write/write06.c b/testcases/kernel/syscalls/write/write06.c
new file mode 100644
index 0000000..ea30d73
--- /dev/null
+++ b/testcases/kernel/syscalls/write/write06.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Dai Shili <daisl.fnst@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test the write() system call with O_APPEND.
+ *
+ * The full description of O_APPEND is in open(2) man-pages:
+ * O_APPEND
+ *        The file is opened in append mode.  Before each write(2), the
+ * file offset is positioned at the end of the file, as if with lseek(2).
+ * The modification of the file offset and the write operation are
+ * performed as a single atomic step.
+ *
+ * Writing 2k data to the file, close it and reopen it with O_APPEND.
+ * Verify that the file size is 3k and offset is moved to the end of the file.
+ */
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include "tst_test.h"
+#include "tst_safe_prw.h"
+
+#define K1              1024
+#define K2              (K1 * 2)
+#define K3              (K1 * 3)
+#define DATA_FILE       "write06_file"
+
+static int fd = -1;
+static char *write_buf[2];
+
+static void verify_write(void)
+{
+	off_t off;
+	struct stat statbuf;
+
+	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC);
+	SAFE_WRITE(1, fd, write_buf[0], K2);
+	SAFE_CLOSE(fd);
+
+	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND);
+	SAFE_FSTAT(fd, &statbuf);
+	if (statbuf.st_size != K2)
+		tst_res(TFAIL, "file size is %ld != K2", statbuf.st_size);
+
+	off = SAFE_LSEEK(fd, K1, SEEK_SET);
+	if (off != K1)
+		tst_brk(TBROK, "Failed to seek to K1");
+
+	SAFE_WRITE(1, fd, write_buf[1], K1);
+
+	off = SAFE_LSEEK(fd, 0, SEEK_CUR);
+	if (off != K3)
+		tst_res(TFAIL, "Wrong offset after write %zu expected %u", off, K3);
+	else
+		tst_res(TPASS, "Offset is correct after write %zu", off);
+
+	SAFE_FSTAT(fd, &statbuf);
+	if (statbuf.st_size != K3)
+		tst_res(TFAIL, "Wrong file size after append %zu expected %u", statbuf.st_size, K3);
+	else
+		tst_res(TPASS, "Correct file size after append %u", K3);
+
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	memset(write_buf[0], 0, K2);
+	memset(write_buf[1], 1, K1);
+}
+
+static void cleanup(void)
+{
+	if (fd != -1)
+		SAFE_CLOSE(fd);
+
+	SAFE_UNLINK(DATA_FILE);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_write,
+	.bufs = (struct tst_buffers[]) {
+		{&write_buf[0], .size = K2},
+		{&write_buf[1], .size = K1},
+		{}
+	}
+};
-- 
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v3] syscalls/write06: Add new test
  2021-12-21 20:03           ` [LTP] [PATCH v3] " Dai Shili
@ 2021-12-21 19:44             ` Petr Vorel
  2021-12-23  1:42             ` xuyang2018.jy
  1 sibling, 0 replies; 9+ messages in thread
From: Petr Vorel @ 2021-12-21 19:44 UTC (permalink / raw)
  To: Dai Shili; +Cc: ltp

Hi Dai,

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Thanks!

Waiting to our CI be fixed before merge.

Kind regards,
Petr

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [LTP] [PATCH v3] syscalls/write06: Add new test
  2021-12-20  9:53         ` Petr Vorel
@ 2021-12-21 20:03           ` Dai Shili
  2021-12-21 19:44             ` Petr Vorel
  2021-12-23  1:42             ` xuyang2018.jy
  0 siblings, 2 replies; 9+ messages in thread
From: Dai Shili @ 2021-12-21 20:03 UTC (permalink / raw)
  To: pvorel; +Cc: ltp

Like pwrite04.c, test the write() system call with O_APPEND.

Signed-off-by: Dai Shili <daisl.fnst@fujitsu.com>
---
 runtest/syscalls                           |  1 +
 testcases/kernel/syscalls/write/.gitignore |  1 +
 testcases/kernel/syscalls/write/write06.c  | 94 ++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+)
 create mode 100644 testcases/kernel/syscalls/write/write06.c

diff --git a/runtest/syscalls b/runtest/syscalls
index bcf3d56..32fcda4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1699,6 +1699,7 @@ write02 write02
 write03 write03
 write04 write04
 write05 write05
+write06 write06
 
 writev01 writev01
 writev02 writev02
diff --git a/testcases/kernel/syscalls/write/.gitignore b/testcases/kernel/syscalls/write/.gitignore
index 7f36194..8529aae 100644
--- a/testcases/kernel/syscalls/write/.gitignore
+++ b/testcases/kernel/syscalls/write/.gitignore
@@ -3,3 +3,4 @@
 /write03
 /write04
 /write05
+/write06
diff --git a/testcases/kernel/syscalls/write/write06.c b/testcases/kernel/syscalls/write/write06.c
new file mode 100644
index 0000000..c175548
--- /dev/null
+++ b/testcases/kernel/syscalls/write/write06.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Dai Shili <daisl.fnst@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test the write() system call with O_APPEND.
+ *
+ * The full description of O_APPEND is in open(2) man-pages:
+ * The file is opened in append mode.  Before each write(2), the
+ * file offset is positioned at the end of the file, as if with lseek(2).
+ * The modification of the file offset and the write operation are
+ * performed as a single atomic step.
+ *
+ * Writing 2k data to the file, close it and reopen it with O_APPEND.
+ * Verify that the file size is 3k and offset is moved to the end of the file.
+ */
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include "tst_test.h"
+#include "tst_safe_prw.h"
+
+#define K1              1024
+#define K2              (K1 * 2)
+#define K3              (K1 * 3)
+#define DATA_FILE       "write06_file"
+
+static int fd = -1;
+static char *write_buf[2];
+
+static void verify_write(void)
+{
+	off_t off;
+	struct stat statbuf;
+
+	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);
+	SAFE_WRITE(1, fd, write_buf[0], K2);
+	SAFE_CLOSE(fd);
+
+	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND);
+	SAFE_FSTAT(fd, &statbuf);
+	if (statbuf.st_size != K2)
+		tst_res(TFAIL, "file size is %ld != K2", statbuf.st_size);
+
+	off = SAFE_LSEEK(fd, K1, SEEK_SET);
+	if (off != K1)
+		tst_brk(TBROK, "Failed to seek to K1");
+
+	SAFE_WRITE(1, fd, write_buf[1], K1);
+
+	off = SAFE_LSEEK(fd, 0, SEEK_CUR);
+	if (off != K3)
+		tst_res(TFAIL, "Wrong offset after write %zu expected %u", off, K3);
+	else
+		tst_res(TPASS, "Offset is correct after write %zu", off);
+
+	SAFE_FSTAT(fd, &statbuf);
+	if (statbuf.st_size != K3)
+		tst_res(TFAIL, "Wrong file size after append %zu expected %u", statbuf.st_size, K3);
+	else
+		tst_res(TPASS, "Correct file size after append %u", K3);
+
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	memset(write_buf[0], 0, K2);
+	memset(write_buf[1], 1, K1);
+}
+
+static void cleanup(void)
+{
+	if (fd != -1)
+		SAFE_CLOSE(fd);
+
+	SAFE_UNLINK(DATA_FILE);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_write,
+	.bufs = (struct tst_buffers[]) {
+		{&write_buf[0], .size = K2},
+		{&write_buf[1], .size = K1},
+		{}
+	}
+};
-- 
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v3] syscalls/write06: Add new test
  2021-12-21 20:03           ` [LTP] [PATCH v3] " Dai Shili
  2021-12-21 19:44             ` Petr Vorel
@ 2021-12-23  1:42             ` xuyang2018.jy
  1 sibling, 0 replies; 9+ messages in thread
From: xuyang2018.jy @ 2021-12-23  1:42 UTC (permalink / raw)
  To: daisl.fnst; +Cc: ltp

Hi Dai
LGTM, merged!

Best Regards
Yang Xu
> Like pwrite04.c, test the write() system call with O_APPEND.
>
> Signed-off-by: Dai Shili<daisl.fnst@fujitsu.com>
> ---
>   runtest/syscalls                           |  1 +
>   testcases/kernel/syscalls/write/.gitignore |  1 +
>   testcases/kernel/syscalls/write/write06.c  | 94 ++++++++++++++++++++++++++++++
>   3 files changed, 96 insertions(+)
>   create mode 100644 testcases/kernel/syscalls/write/write06.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index bcf3d56..32fcda4 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -1699,6 +1699,7 @@ write02 write02
>   write03 write03
>   write04 write04
>   write05 write05
> +write06 write06
>
>   writev01 writev01
>   writev02 writev02
> diff --git a/testcases/kernel/syscalls/write/.gitignore b/testcases/kernel/syscalls/write/.gitignore
> index 7f36194..8529aae 100644
> --- a/testcases/kernel/syscalls/write/.gitignore
> +++ b/testcases/kernel/syscalls/write/.gitignore
> @@ -3,3 +3,4 @@
>   /write03
>   /write04
>   /write05
> +/write06
> diff --git a/testcases/kernel/syscalls/write/write06.c b/testcases/kernel/syscalls/write/write06.c
> new file mode 100644
> index 0000000..c175548
> --- /dev/null
> +++ b/testcases/kernel/syscalls/write/write06.c
> @@ -0,0 +1,94 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
> + * Author: Dai Shili<daisl.fnst@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Test the write() system call with O_APPEND.
> + *
> + * The full description of O_APPEND is in open(2) man-pages:
> + * The file is opened in append mode.  Before each write(2), the
> + * file offset is positioned at the end of the file, as if with lseek(2).
> + * The modification of the file offset and the write operation are
> + * performed as a single atomic step.
> + *
> + * Writing 2k data to the file, close it and reopen it with O_APPEND.
> + * Verify that the file size is 3k and offset is moved to the end of the file.
> + */
> +
> +#include<stdlib.h>
> +#include<inttypes.h>
> +#include "tst_test.h"
> +#include "tst_safe_prw.h"
> +
> +#define K1              1024
> +#define K2              (K1 * 2)
> +#define K3              (K1 * 3)
> +#define DATA_FILE       "write06_file"
> +
> +static int fd = -1;
> +static char *write_buf[2];
> +
> +static void verify_write(void)
> +{
> +	off_t off;
> +	struct stat statbuf;
> +
> +	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +	SAFE_WRITE(1, fd, write_buf[0], K2);
> +	SAFE_CLOSE(fd);
> +
> +	fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND);
> +	SAFE_FSTAT(fd,&statbuf);
> +	if (statbuf.st_size != K2)
> +		tst_res(TFAIL, "file size is %ld != K2", statbuf.st_size);
> +
> +	off = SAFE_LSEEK(fd, K1, SEEK_SET);
> +	if (off != K1)
> +		tst_brk(TBROK, "Failed to seek to K1");
> +
> +	SAFE_WRITE(1, fd, write_buf[1], K1);
> +
> +	off = SAFE_LSEEK(fd, 0, SEEK_CUR);
> +	if (off != K3)
> +		tst_res(TFAIL, "Wrong offset after write %zu expected %u", off, K3);
> +	else
> +		tst_res(TPASS, "Offset is correct after write %zu", off);
> +
> +	SAFE_FSTAT(fd,&statbuf);
> +	if (statbuf.st_size != K3)
> +		tst_res(TFAIL, "Wrong file size after append %zu expected %u", statbuf.st_size, K3);
> +	else
> +		tst_res(TPASS, "Correct file size after append %u", K3);
> +
> +	SAFE_CLOSE(fd);
> +}
> +
> +static void setup(void)
> +{
> +	memset(write_buf[0], 0, K2);
> +	memset(write_buf[1], 1, K1);
> +}
> +
> +static void cleanup(void)
> +{
> +	if (fd != -1)
> +		SAFE_CLOSE(fd);
> +
> +	SAFE_UNLINK(DATA_FILE);
> +}
> +
> +static struct tst_test test = {
> +	.needs_tmpdir = 1,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = verify_write,
> +	.bufs = (struct tst_buffers[]) {
> +		{&write_buf[0], .size = K2},
> +		{&write_buf[1], .size = K1},
> +		{}
> +	}
> +};

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-12-23  1:42 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-06 21:17 [LTP] [PATCH] syscalls/write06: Add new test Dai Shili
2021-12-07 13:51 ` Cyril Hrubis
2021-12-08  8:38   ` daisl.fnst
2021-12-10 10:57     ` Cyril Hrubis
2021-12-20 19:23       ` [LTP] [PATCH v2] " Dai Shili
2021-12-20  9:53         ` Petr Vorel
2021-12-21 20:03           ` [LTP] [PATCH v3] " Dai Shili
2021-12-21 19:44             ` Petr Vorel
2021-12-23  1:42             ` xuyang2018.jy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.