All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2
@ 2021-09-22  5:39 Yang Xu
  2021-09-22  5:39 ` [LTP] [PATCH v1 2/3] syscalls/dup206: Add a test when newfd equals oldfd Yang Xu
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Yang Xu @ 2021-09-22  5:39 UTC (permalink / raw)
  To: ltp

Since the two file descriptors refer to the same open file description, they should
share the file status. So change mode for file after creat, then check whether oldfd
mode equals newfd mode.

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 testcases/kernel/syscalls/dup2/dup202.c | 30 +++++++++++++++++++------
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/testcases/kernel/syscalls/dup2/dup202.c b/testcases/kernel/syscalls/dup2/dup202.c
index 94bedcf18..c50b56ba0 100644
--- a/testcases/kernel/syscalls/dup2/dup202.c
+++ b/testcases/kernel/syscalls/dup2/dup202.c
@@ -8,10 +8,14 @@
  * [Description]
  *
  * Test whether the access mode are the same for both file descriptors.
+ * Style: creat mode, dup2, [change mode], check mode
  *
- * - 0: read only ? "0444"
- * - 1: write only ? "0222"
- * - 2: read/write ? "0666"
+ * - 0: read only, dup2, null, read only ? "0444"
+ * - 1: write only, dup2, null, write only ? "0222"
+ * - 2: read/write, dup2, null, read/write ? "0666"
+ * - 3: read/write/execute, dup2, read only, read only ? "0444"
+ * - 4: read/write/execute, dup2, write only, write only ? "0222"
+ * - 5: read/write/execute, dup2, read/write, read/write ? "0666"
  */
 
 #include <errno.h>
@@ -29,10 +33,15 @@ static int duprdo = 10, dupwro = 20, duprdwr = 30;
 static struct tcase {
 	int *nfd;
 	mode_t mode;
+	/* 0 - set mode before dup2, 1 - change mode after dup2 */
+	int flag;
 } tcases[] = {
-	{&duprdo, 0444},
-	{&dupwro, 0222},
-	{&duprdwr, 0666},
+	{&duprdo, 0444, 0},
+	{&dupwro, 0222, 0},
+	{&duprdwr, 0666, 0},
+	{&duprdo, 0444, 1},
+	{&dupwro, 0222, 1},
+	{&duprdwr, 0666, 1},
 };
 
 static void setup(void)
@@ -52,7 +61,10 @@ static void run(unsigned int i)
 	struct stat oldbuf, newbuf;
 	struct tcase *tc = tcases + i;
 
-	ofd = SAFE_CREAT(testfile, tc->mode);
+	if (tc->flag)
+		ofd = SAFE_CREAT(testfile, 0777);
+	else
+		ofd = SAFE_CREAT(testfile, tc->mode);
 	nfd = *tc->nfd;
 
 	TEST(dup2(ofd, nfd));
@@ -60,6 +72,10 @@ static void run(unsigned int i)
 		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
 		goto free;
 	}
+	if (tc->flag) {
+		SAFE_CHMOD(testfile, tc->mode);
+		tst_res(TINFO, "original mode 0777, new mode 0%o after chmod", tc->mode);
+	}
 
 	SAFE_FSTAT(ofd, &oldbuf);
 
-- 
2.23.0


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

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

* [LTP] [PATCH v1 2/3] syscalls/dup206: Add a test when newfd equals oldfd
  2021-09-22  5:39 [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2 Yang Xu
@ 2021-09-22  5:39 ` Yang Xu
  2021-10-07 13:15   ` Cyril Hrubis
  2021-09-22  5:39 ` [LTP] [PATCH v1 3/3] syscalls/dup207: Add file offset check test Yang Xu
  2021-10-07 12:53 ` [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2 Cyril Hrubis
  2 siblings, 1 reply; 6+ messages in thread
From: Yang Xu @ 2021-09-22  5:39 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/dup2/.gitignore |  1 +
 testcases/kernel/syscalls/dup2/dup206.c   | 51 +++++++++++++++++++++++
 3 files changed, 53 insertions(+)
 create mode 100644 testcases/kernel/syscalls/dup2/dup206.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 29d7752c7..068fba456 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -153,6 +153,7 @@ dup202 dup202
 dup203 dup203
 dup204 dup204
 dup205 dup205
+dup206 dup206
 
 dup3_01 dup3_01
 dup3_02 dup3_02
diff --git a/testcases/kernel/syscalls/dup2/.gitignore b/testcases/kernel/syscalls/dup2/.gitignore
index 6c4685b80..e2e008b58 100644
--- a/testcases/kernel/syscalls/dup2/.gitignore
+++ b/testcases/kernel/syscalls/dup2/.gitignore
@@ -3,3 +3,4 @@
 /dup203
 /dup204
 /dup205
+/dup206
diff --git a/testcases/kernel/syscalls/dup2/dup206.c b/testcases/kernel/syscalls/dup2/dup206.c
new file mode 100644
index 000000000..e5074ea83
--- /dev/null
+++ b/testcases/kernel/syscalls/dup2/dup206.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * If oldfd is a valid file descriptor, and newfd has the same value as oldfd,
+ * then dup2() does nothing, and returns newfd.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include "tst_test.h"
+
+static int fd = -1;
+
+static void verify_dup2(void)
+{
+	TST_EXP_FD_SILENT(dup2(fd, fd), "dup2(%d, %d)", fd, fd);
+
+	if (TST_RET != fd) {
+		tst_res(TFAIL, "dup2(%d, %d) returns wrong newfd(%ld)", fd, fd, TST_RET);
+		SAFE_CLOSE(TST_RET);
+		return;
+	}
+	tst_res(TPASS, "dup2(%d, %d) returns newfd(%d)", fd, fd, fd);
+}
+
+static void setup(void)
+{
+	char testfile[40];
+
+	sprintf(testfile, "dup206.%d", getpid());
+	fd = SAFE_OPEN(testfile, O_RDWR | O_CREAT, 0666);
+}
+
+static void cleanup(void)
+{
+	if (fd > -1)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_dup2,
+};
-- 
2.23.0


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

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

* [LTP] [PATCH v1 3/3] syscalls/dup207: Add file offset check test
  2021-09-22  5:39 [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2 Yang Xu
  2021-09-22  5:39 ` [LTP] [PATCH v1 2/3] syscalls/dup206: Add a test when newfd equals oldfd Yang Xu
@ 2021-09-22  5:39 ` Yang Xu
  2021-10-07 13:36   ` Cyril Hrubis
  2021-10-07 12:53 ` [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2 Cyril Hrubis
  2 siblings, 1 reply; 6+ messages in thread
From: Yang Xu @ 2021-09-22  5:39 UTC (permalink / raw)
  To: ltp

Since the two file descriptors refer to the same open file description, they share file offset.
If the file offset is modified by using lseek(2) on one of the file descriptors, the offset is
also changed for the other file descriptor.

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/dup2/.gitignore |  1 +
 testcases/kernel/syscalls/dup2/dup207.c   | 82 +++++++++++++++++++++++
 3 files changed, 84 insertions(+)
 create mode 100644 testcases/kernel/syscalls/dup2/dup207.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 068fba456..b19316805 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -154,6 +154,7 @@ dup203 dup203
 dup204 dup204
 dup205 dup205
 dup206 dup206
+dup207 dup207
 
 dup3_01 dup3_01
 dup3_02 dup3_02
diff --git a/testcases/kernel/syscalls/dup2/.gitignore b/testcases/kernel/syscalls/dup2/.gitignore
index e2e008b58..f5938a182 100644
--- a/testcases/kernel/syscalls/dup2/.gitignore
+++ b/testcases/kernel/syscalls/dup2/.gitignore
@@ -4,3 +4,4 @@
 /dup204
 /dup205
 /dup206
+/dup207
diff --git a/testcases/kernel/syscalls/dup2/dup207.c b/testcases/kernel/syscalls/dup2/dup207.c
new file mode 100644
index 000000000..8badf4229
--- /dev/null
+++ b/testcases/kernel/syscalls/dup2/dup207.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test whether the file offset are the same for both file descriptors.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+
+#define WRITE_STR "abcdefg"
+
+static int ofd = -1, nfd = 10;
+
+static struct tcase {
+	off_t offset;
+	size_t exp_size;
+	/* 0 - change offset before dup2, 1 - change offset after dup2 */
+	int flag;
+	char *exp_data;
+	char *desc;
+} tcases[] = {
+	{1, 6, 0, "bcdefg", "Test offset check when using lseek before dup2"},
+	{2, 5, 1, "cdefg", "Test offset check when using lseek after dup2"},
+};
+
+static void setup(void)
+{
+	char testfile[40];
+
+	sprintf(testfile, "dup207.%d", getpid());
+	ofd = SAFE_OPEN(testfile, O_RDWR | O_CREAT, 0644);
+	SAFE_WRITE(1, ofd, WRITE_STR, sizeof(WRITE_STR) - 1);
+}
+
+static void cleanup(void)
+{
+	if (ofd > 0)
+		SAFE_CLOSE(ofd);
+	close(nfd);
+}
+
+static void run(unsigned int i)
+{
+	struct tcase *tc = tcases + i;
+	char read_buf[20];
+
+	memset(read_buf, 0, sizeof(read_buf));
+	tst_res(TINFO, "%s", tc->desc);
+	if (!tc->flag)
+		SAFE_LSEEK(ofd, tc->offset, SEEK_SET);
+
+	TEST(dup2(ofd, nfd));
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
+		return;
+	}
+	if (tc->flag)
+		SAFE_LSEEK(ofd, tc->offset, SEEK_SET);
+
+	SAFE_READ(1, nfd, read_buf, tc->exp_size);
+	if (strncmp(read_buf, tc->exp_data, tc->exp_size))
+		tst_res(TFAIL, "Expect %s, but get %s.", tc->exp_data, read_buf);
+	else
+		tst_res(TPASS, "Get expected buf %s", read_buf);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
2.23.0


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

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

* Re: [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2
  2021-09-22  5:39 [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2 Yang Xu
  2021-09-22  5:39 ` [LTP] [PATCH v1 2/3] syscalls/dup206: Add a test when newfd equals oldfd Yang Xu
  2021-09-22  5:39 ` [LTP] [PATCH v1 3/3] syscalls/dup207: Add file offset check test Yang Xu
@ 2021-10-07 12:53 ` Cyril Hrubis
  2 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2021-10-07 12:53 UTC (permalink / raw)
  To: Yang Xu; +Cc: ltp

Hi!
>   * Test whether the access mode are the same for both file descriptors.
> + * Style: creat mode, dup2, [change mode], check mode
>   *
> - * - 0: read only ? "0444"
> - * - 1: write only ? "0222"
> - * - 2: read/write ? "0666"
> + * - 0: read only, dup2, null, read only ? "0444"
> + * - 1: write only, dup2, null, write only ? "0222"
> + * - 2: read/write, dup2, null, read/write ? "0666"
> + * - 3: read/write/execute, dup2, read only, read only ? "0444"
> + * - 4: read/write/execute, dup2, write only, write only ? "0222"
> + * - 5: read/write/execute, dup2, read/write, read/write ? "0666"
>   */

I've changed this description slightly so that it renders nicely in
asciidoc and pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v1 2/3] syscalls/dup206: Add a test when newfd equals oldfd
  2021-09-22  5:39 ` [LTP] [PATCH v1 2/3] syscalls/dup206: Add a test when newfd equals oldfd Yang Xu
@ 2021-10-07 13:15   ` Cyril Hrubis
  0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2021-10-07 13:15 UTC (permalink / raw)
  To: Yang Xu; +Cc: ltp

Hi!
> +static void setup(void)
> +{
> +	char testfile[40];
> +
> +	sprintf(testfile, "dup206.%d", getpid());
> +	fd = SAFE_OPEN(testfile, O_RDWR | O_CREAT, 0666);

There is no reason to append the pid here, so I've changed this to just
SAFE_OPEN("testfile", ...) and pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v1 3/3] syscalls/dup207: Add file offset check test
  2021-09-22  5:39 ` [LTP] [PATCH v1 3/3] syscalls/dup207: Add file offset check test Yang Xu
@ 2021-10-07 13:36   ` Cyril Hrubis
  0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2021-10-07 13:36 UTC (permalink / raw)
  To: Yang Xu; +Cc: ltp

Hi!
Pushed with minor changes, thanks.

- removed the pind from the filename as well
- shortened the description messages a bit

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

end of thread, other threads:[~2021-10-07 13:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-22  5:39 [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2 Yang Xu
2021-09-22  5:39 ` [LTP] [PATCH v1 2/3] syscalls/dup206: Add a test when newfd equals oldfd Yang Xu
2021-10-07 13:15   ` Cyril Hrubis
2021-09-22  5:39 ` [LTP] [PATCH v1 3/3] syscalls/dup207: Add file offset check test Yang Xu
2021-10-07 13:36   ` Cyril Hrubis
2021-10-07 12:53 ` [LTP] [PATCH v1 1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2 Cyril Hrubis

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.