Skip to contentSkip to conversation

Increase pipe2() coverage.

PATCH New [email protected] 3 participants 9 messages Apr 4, 2020 – Apr 9, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Increase pipe2() coverage.

From: Francis Laniel <[email protected]>

A new test was added (pipe2_03.c), this test checks the following:
1. Create a pipe with O_NONBLOCK.
2. Check that this flag is set.
3. Check that pipe size is 65536B.
4. Reduce pipe size to 4096B.
5. Write buffer bigger than page size and see that second write fails.
6. Set pipe's flags to default.
7. Check that pipe is still full with select.

---
 testcases/kernel/syscalls/pipe2/.gitignore |   1 +
 testcases/kernel/syscalls/pipe2/pipe2_03.c | 215 +++++++++++++++++++++
 2 files changed, 216 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c
testcases/kernel/syscalls/pipe2/.gitignore +1 -0
index cd38bb309..01d980dba 100644
@@ -1,2 +1,3 @@
 /pipe2_01
 /pipe2_02
+/pipe2_03
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +215 -0

216 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/1 Increase pipe2() coverage.

Hi laniel

Thanks for you patch, here are some small nits

Also, I think we can change a accurate subject, ie "add new test for pipe2 with/without O_NONBLOCK mode"

Quoted 6 lines

From: Francis Laniel <[email protected]>

A new test was added (pipe2_03.c), this test checks the following:
1. Create a pipe with O_NONBLOCK.
2. Check that this flag is set.
3. Check that pipe size is 65536B.

16 * page_size

Quoted 1 line

4. Reduce pipe size to 4096B.

page_size

Quoted 1 line

5. Write buffer bigger than page size and see that second write fails.

we should also check errno num

Quoted 2 lines

6. Set pipe's flags to default.
7. Check that pipe is still full with select.

IMO, we can move this function into child process, so we can check its status(it should be in s status when we write data into full pipe with block mode). And then we can kill it.

Quoted 20 lines
---
  testcases/kernel/syscalls/pipe2/.gitignore |   1 +
  testcases/kernel/syscalls/pipe2/pipe2_03.c | 215 +++++++++++++++++++++
  2 files changed, 216 insertions(+)It miss runtest/syscalls file.

create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c

testcases/kernel/syscalls/pipe2/.gitignore +1 -0
index cd38bb309..01d980dba 100644
@@ -1,2 +1,3 @@
  /pipe2_01
  /pipe2_02
+/pipe2_03
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +0 -0
new file mode 100644
index 000000000..ea225fddc

testcases/kernel/syscalls/pipe2/pipe2_03.c line 1

Quoted 1 line
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +1 -0
@@ -0,0 +1,215 @@
+// SPDX-License-Iventifier: GPL-2.0

It should use SPDX-License-Identifier: GPL-2.0-or-later.

Quoted 9 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +9 -0
+/* Copyright (c) Francis Laniel, 2020                                         */
+/******************************************************************************/
+/******************************************************************************/
+/*                                                                            */
+/* File:        pipe2_03.c                                                    */
+/*                                                                            */
+/* Description: This Program tests getting and setting the pipe size.         */
+/*              It also tests what happen when you write to a full pipe       */
+/*              depending on whether O_NONBLOCK is or not.                    */

testcases/kernel/syscalls/pipe2/pipe2_03.c line 64

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+/*                                                                            */
+/* Usage:  <for command-line>                                                 */
+/* pipe2_03 [-c n] [-e][-i n] [-I x] [-p x] [-t]                              */
+/*      where,  -c n : Run n copies concurrently.                             */
+/*              -e   : Turn on errno logging.                                 */
+/*              -i n : Execute test n times.                                  */
+/*              -I x : Execute test for x seconds.                            */
+/*              -P x : Pause for x seconds between iterations.                */
+/*              -t   : Turn on syscall timing.                                */
+/*

This is old style. I have mentioned that you may see pipe12.c for reference. In this case, option has been hanlded by ltp library, ie -i -t option. So we don't need this info.

     */

testcases/kernel/syscalls/pipe2/pipe2_03.c line 32

Quoted 12 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +12 -0
+/* Total Tests: 1                                                             */
+/*                                                                            */
+/* Test Name:   pipe2_03                                                      */
+/*                                                                            */
+/* Author:      Francis Laniel                                                */
+/*                                                                            */
+/* History:     Created - Mar 28 2020 - Francis Laniel                        */
+/******************************************************************************/
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <features.h>
+#include <fcntl.h>

We should use lapi/fcntl.h to avoid undefined error(F_GETPIPE_SZ) on centos6.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 42

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+#include <unistd.h>
+#include <stdio.h>
+#include <assert.h>
+#include <sys/select.h>
+
+#include "tst_test.h"
+
+#define PAGE_NR 16
+#define READ_SIDE 0
+#define WRITE_SIDE 1

I guess we can drop READ_SIDE and WRITE_SIDE macro. We all know fd[0] represent read side and fd[1] represent write side.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 48

Quoted 6 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +6 -0
+#define SECONDS 3
+#define MICROSECONDS 0
+
+/**
+ * The two file descriptors of the pipe.
+ */

the comment is surplus.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 53

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+static int fds[2];
+
+/**
+ * This variable will contain the system page size after setup.
+ */

here as well

testcases/kernel/syscalls/pipe2/pipe2_03.c line 61

Quoted 8 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+static long page_size;
+
+/**
+ * Setup everything.
+ * This function will:
+ * 1. Create the pipe with O_NONBLOCK.
+ * 2. Get system page size with sysconf().
+ */

here as well

testcases/kernel/syscalls/pipe2/pipe2_03.c line 70

Quoted 9 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +9 -0
+static void setup(void)
+{
+	/*
+	 * Check that Linux version is greater than 2.6.35 to be able to use
+	 * F_GETPIPE_SZ and F_SETPIPE_SZ.
+	 */
+	if (tst_kvercmp(2, 6, 35) < 0)
+		tst_brk(TCONF, "This test can only run on kernels that are 2.6.35 and higher");
+       

AFAIK, now linux kernel has reached to 5.7, 2.6.35 is so old that we don't need to compare this. Only ensuring this case can be compile is ok. ltp community has a discussion about minimal supported kernel and glibc version, more info see[1]

[1]https://github.com/linux-test-project/ltp/issues/657

testcases/kernel/syscalls/pipe2/pipe2_03.c line 75

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+	/*
+	 * Create the pip with O_NONBLOCK.
+	 */
+	if (pipe2(fds, O_NONBLOCK))
+		tst_brk(TBROK | TERRNO, "pipe2() failed");

I guess we can add SAFE_PIPE2 macro for this like SAFE_PIPE.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 85

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+
+	/*
+	 * Get the system page size.
+	 */
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+}
+
+/**
+ * Test everything.
+ */

remove this comment.

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+static void test_pipe2(void)
+{
+	int ret;
+
+	long flags;

testcases/kernel/syscalls/pipe2/pipe2_03.c line 100

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+	long pipe_size;
+
+	char *buf;
+
+	fd_set set;
+	struct timeval timeout;
+
+	/*
+	 * Get the flags of the pipe.
+	 */

remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 108

Quoted 8 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+	flags = SAFE_FCNTL(fds[0], F_GETFL);
+
+	if (!(flags & O_NONBLOCK))
+		tst_res(TFAIL, "O_NONBLOCK flag must be set.");
+
+	/*
+	 * Get the size of the pipe.
+	 */

remove

Quoted 4 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +4 -0
+	pipe_size = SAFE_FCNTL(fds[0], F_GETPIPE_SZ);
+
+	if (pipe_size != page_size * PAGE_NR)
+		tst_res(TFAIL, "Default pipe page is 16 * 4096 = 65536B.");

testcases/kernel/syscalls/pipe2/pipe2_03.c

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+
+	/*
+	 * A pipe has two file descriptors.
+	 * But in the kernel these two file descriptors point to the same pipe.
+	 * So setting size from first file handle set size for the pipe.
+	 *
+	 * Moreover, the size of a pipe is exprimed in page.
+	 * So the minimal size of a pipe is a page size, setting its size to 0
+	 * leads to a pipe whom size is 4096B.
+	 */Remove...

We all know if we set a size less than page size, it will round up to a page size not 4096b.

Quoted 6 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +6 -0
+	SAFE_FCNTL(fds[0], F_SETPIPE_SZ, 0);
+
+	/*
+	 * So getting size from the second file descriptor return the size of
+	 * the pipe which was changed before with first file descriptor.
+	 */

testcases/kernel/syscalls/pipe2/pipe2_03.c line 138

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+	pipe_size = SAFE_FCNTL(fds[1], F_GETPIPE_SZ);
+
+	if (pipe_size != page_size)
+		tst_res(TFAIL, "Pipe size (%ld) must be page size (%ld)",
+			pipe_size, page_size);
+
+	/*
+	 * Create a buffer of page size.
+	 * We create it in stack because we do not care of its lifetime.
+	 */

Remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 139

Quoted 1 line
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +1 -0
+	buf = alloca(page_size);

Maybe we can use tst_alloc function. more info see[2]

https://githuber/doc/test-writing-g.com/linux-test-project/ltp/blob/mastuidelines.txt

testcases/kernel/syscalls/pipe2/pipe2_03.c line 152

Quoted 13 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +13 -0
+
+	SAFE_WRITE(1, fds[WRITE_SIDE], buf, page_size);
+
+	/*
+	 * This write should return -1 because pipe is already full.
+	 */
+	if (write(fds[WRITE_SIDE], buf, page_size) != -1)
+		tst_res(TFAIL | TERRNO, "write() succeeded and should not");
+
+	/*
+	 * Remove the O_NONBLOCK parameter for the pipe.
+	 * After this call write to the pipe will be blocking.
+	 */

remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 157

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+	SAFE_FCNTL(fds[0], F_SETFL, flags & ~O_NONBLOCK);
+
+	/*
+	 * Get again the flags of the pipe.
+	 */

remove...

Quoted 40 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +40 -0
+	flags = SAFE_FCNTL(fds[0], F_GETFL);
+
+	if (flags & O_NONBLOCK)
+		tst_res(TFAIL, "O_NONBLOCK flag must not be set.");
+
+	/*
+	 * Empty the set so no garbage value is in it.
+	 */
+	FD_ZERO(&set);
+
+	/*
+	 * Add the write side of the pipe to the set.
+	 */
+	FD_SET(fds[WRITE_SIDE], &set);
+
+	if (!FD_ISSET(fds[WRITE_SIDE], &set))
+		tst_res(TFAIL, "Pipe must be in the set.");
+
+	timeout.tv_sec = SECONDS;
+	timeout.tv_usec = MICROSECONDS;
+
+	/*
+	 * Since writes are now blocking we use select to check if the pipe is
+	 * available to receive write.
+	 * Wait SECONDS seconds and MICROSECONDS microseconds on write side of
+	 * the pipe.
+	 */
+	ret = select(1, NULL, &set, NULL, &timeout);
+
+	if (ret == -1)
+		tst_res(TFAIL | TERRNO, "select() failed");
+
+	/*
+	 * The pipe is still full so select should return after the waiting time
+	 * returning 0 because write side of the pipe is not available because
+	 * it is full.
+	 */
+	if (ret)
+		tst_res(TFAIL, "Pipe is not full.");
+	else

testcases/kernel/syscalls/pipe2/pipe2_03.c line 207

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+		tst_res(TPASS, "Pipe is still full.");
+}
+
+/**
+ * Clean everything either if test is finished or if something failed.
+ */
+static void cleanup(void)
+{
+	for (int i = 0; i < 2; i++)
+		if (fds[i])

it should be if (fds[i] > 0)

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+			SAFE_CLOSE(fds[i]);
+}
+
+static struct tst_test test = {
+	.test_all = test_pipe2,
+	.setup = setup,
+	.cleanup = cleanup,
+};
\ No newline at end of file
#2 lore raw

PATCH 1/1 Increase pipe2() coverage.

Hi!

Thank you for the advice!

I just have a small question (see below).

Quoted 23 lines

Le lundi 6 avril 2020, 07:57:02 CEST Yang Xu a ?crit :
Hi laniel

Thanks for you patch, here are some small nits

Also, I think we can change a accurate subject, ie "add new test for pipe2 with/without O_NONBLOCK mode"

Quoted 6 lines

From: Francis Laniel <[email protected]>

A new test was added (pipe2_03.c), this test checks the following:
1. Create a pipe with O_NONBLOCK.
2. Check that this flag is set.
3. Check that pipe size is 65536B.

16 * page_size

Quoted 1 line

4. Reduce pipe size to 4096B.

page_size

Quoted 1 line

5. Write buffer bigger than page size and see that second write fails.

Quoted 8 lines

we should also check errno num

Quoted 2 lines

6. Set pipe's flags to default.
7. Check that pipe is still full with select.

IMO, we can move this function into child process, so we can check its status(it should be in s status when we write data into full pipe with block mode). And then we can kill it.

I did not understand what the child should execute?
If it writes it will block forever and so the father if it waits for its child.

Quoted 314 lines
Quoted 25 lines

---

  testcases/kernel/syscalls/pipe2/.gitignore |   1 +
  testcases/kernel/syscalls/pipe2/pipe2_03.c | 215 +++++++++++++++++++++
  2 files changed, 216 insertions(+)It miss runtest/syscalls file.

create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c

(quoted diff) +0 -0

b/testcases/kernel/syscalls/pipe2/.gitignore index cd38bb309..01d980dba 100644

testcases/kernel/syscalls/pipe2/.gitignore +1 -0
@@ -1,2 +1,3 @@
 
  /pipe2_01
  /pipe2_02
 
+/pipe2_03
(quoted diff) +0 -0

b/testcases/kernel/syscalls/pipe2/pipe2_03.c new file mode 100644 index 000000000..ea225fddc

testcases/kernel/syscalls/pipe2/pipe2_03.c line 1

Quoted 1 line
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +1 -0
@@ -0,0 +1,215 @@
+// SPDX-License-Iventifier: GPL-2.0

It should use SPDX-License-Identifier: GPL-2.0-or-later.

Quoted 18 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +2 -0
+/* Copyright (c) Francis Laniel, 2020                                    
    */
+/***********************************************************************

*******/ +/*********************************************************************** *******/ +/*

             */ +/* File:        pipe2_03.c                              
                     */ +/*                                              
                             */ +/* Description: This Program tests
getting and setting the pipe size.         */ +/*              It also
tests what happen when you write to a full pipe       */ +/*             
depending on whether O_NONBLOCK is or not.                    */ +/*     
                                                                      */
+/* Usage:  <for command-line>                                           
     */ +/* pipe2_03 [-c n] [-e][-i n] [-I x] [-p x] [-t]                
             */ +/*      where,  -c n : Run n copies concurrently.       
                     */ +/*              -e   : Turn on errno logging.   
                             */ +/*              -i n : Execute test n
Quoted 5 lines

times. */ +/* -I x :
Execute test for x seconds. */ +/*
-P x : Pause for x seconds between iterations. */ +/*
-t : Turn on syscall timing. */
+/*

This is old style. I have mentioned that you may see pipe12.c for reference. In this case, option has been hanlded by ltp library, ie -i -t option. So we don't need this info.

     */

testcases/kernel/syscalls/pipe2/pipe2_03.c line 32

Quoted 13 lines

+/* Total Tests: 1

    */ +/*                                                               
            */ +/* Test Name:   pipe2_03                                 
                    */ +/*                                               
                            */ +/* Author:      Francis Laniel           
                                    */ +/*                               
                                            */ +/* History:     Created -

Mar 28 2020 - Francis Laniel */
+/*********************************************************************** *******/ +#define _GNU_SOURCE

testcases/kernel/syscalls/pipe2/pipe2_03.c new file +3 -0
+#include <stdlib.h>
+#include <features.h>
+#include <fcntl.h>

We should use lapi/fcntl.h to avoid undefined error(F_GETPIPE_SZ) on centos6.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 42

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+#include <unistd.h>
+#include <stdio.h>
+#include <assert.h>
+#include <sys/select.h>
+
+#include "tst_test.h"
+
+#define PAGE_NR 16
+#define READ_SIDE 0
+#define WRITE_SIDE 1

I guess we can drop READ_SIDE and WRITE_SIDE macro. We all know fd[0] represent read side and fd[1] represent write side.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 48

Quoted 6 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +6 -0
+#define SECONDS 3
+#define MICROSECONDS 0
+
+/**
+ * The two file descriptors of the pipe.
+ */

the comment is surplus.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 53

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+static int fds[2];
+
+/**
+ * This variable will contain the system page size after setup.
+ */

here as well

testcases/kernel/syscalls/pipe2/pipe2_03.c line 61

Quoted 8 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+static long page_size;
+
+/**
+ * Setup everything.
+ * This function will:
+ * 1. Create the pipe with O_NONBLOCK.
+ * 2. Get system page size with sysconf().
+ */

here as well

Quoted 9 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+static void setup(void)
+{
+	/*
+	 * Check that Linux version is greater than 2.6.35 to be able to use
+	 * F_GETPIPE_SZ and F_SETPIPE_SZ.
+	 */
+	if (tst_kvercmp(2, 6, 35) < 0)
+		tst_brk(TCONF, "This test can only run on kernels that are 2.6.35 and

higher"); +

AFAIK, now linux kernel has reached to 5.7, 2.6.35 is so old that we don't need to compare this. Only ensuring this case can be compile is ok. ltp community has a discussion about minimal supported kernel and glibc version, more info see[1]

[1]https://github.com/linux-test-project/ltp/issues/657

testcases/kernel/syscalls/pipe2/pipe2_03.c line 75

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+	/*
+	 * Create the pip with O_NONBLOCK.
+	 */
+	if (pipe2(fds, O_NONBLOCK))
+		tst_brk(TBROK | TERRNO, "pipe2() failed");

I guess we can add SAFE_PIPE2 macro for this like SAFE_PIPE.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 85

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+
+	/*
+	 * Get the system page size.
+	 */
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+}
+
+/**
+ * Test everything.
+ */

remove this comment.

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+static void test_pipe2(void)
+{
+	int ret;
+
+	long flags;

testcases/kernel/syscalls/pipe2/pipe2_03.c line 100

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+	long pipe_size;
+
+	char *buf;
+
+	fd_set set;
+	struct timeval timeout;
+
+	/*
+	 * Get the flags of the pipe.
+	 */

remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 108

Quoted 8 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+	flags = SAFE_FCNTL(fds[0], F_GETFL);
+
+	if (!(flags & O_NONBLOCK))
+		tst_res(TFAIL, "O_NONBLOCK flag must be set.");
+
+	/*
+	 * Get the size of the pipe.
+	 */

remove

Quoted 4 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +4 -0
+	pipe_size = SAFE_FCNTL(fds[0], F_GETPIPE_SZ);
+
+	if (pipe_size != page_size * PAGE_NR)
+		tst_res(TFAIL, "Default pipe page is 16 * 4096 = 65536B.");

testcases/kernel/syscalls/pipe2/pipe2_03.c

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+
+	/*
+	 * A pipe has two file descriptors.
+	 * But in the kernel these two file descriptors point to the same pipe.
+	 * So setting size from first file handle set size for the pipe.
+	 *
+	 * Moreover, the size of a pipe is exprimed in page.
+	 * So the minimal size of a pipe is a page size, setting its size to 0
+	 * leads to a pipe whom size is 4096B.
+	 */Remove...

We all know if we set a size less than page size, it will round up to a page size not 4096b.

Quoted 6 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +6 -0
+	SAFE_FCNTL(fds[0], F_SETPIPE_SZ, 0);
+
+	/*
+	 * So getting size from the second file descriptor return the size of
+	 * the pipe which was changed before with first file descriptor.
+	 */

testcases/kernel/syscalls/pipe2/pipe2_03.c line 138

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+	pipe_size = SAFE_FCNTL(fds[1], F_GETPIPE_SZ);
+
+	if (pipe_size != page_size)
+		tst_res(TFAIL, "Pipe size (%ld) must be page size (%ld)",
+			pipe_size, page_size);
+
+	/*
+	 * Create a buffer of page size.
+	 * We create it in stack because we do not care of its lifetime.
+	 */

Remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 139

Quoted 1 line
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +1 -0
+	buf = alloca(page_size);

Maybe we can use tst_alloc function. more info see[2]

https://githuber/doc/test-writing-g.com/linux-test-project/ltp/blob/mastuide
lines.txt

testcases/kernel/syscalls/pipe2/pipe2_03.c line 152

Quoted 13 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +13 -0
+
+	SAFE_WRITE(1, fds[WRITE_SIDE], buf, page_size);
+
+	/*
+	 * This write should return -1 because pipe is already full.
+	 */
+	if (write(fds[WRITE_SIDE], buf, page_size) != -1)
+		tst_res(TFAIL | TERRNO, "write() succeeded and should not");
+
+	/*
+	 * Remove the O_NONBLOCK parameter for the pipe.
+	 * After this call write to the pipe will be blocking.
+	 */

remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 157

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+	SAFE_FCNTL(fds[0], F_SETFL, flags & ~O_NONBLOCK);
+
+	/*
+	 * Get again the flags of the pipe.
+	 */

remove...

Quoted 40 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +40 -0
+	flags = SAFE_FCNTL(fds[0], F_GETFL);
+
+	if (flags & O_NONBLOCK)
+		tst_res(TFAIL, "O_NONBLOCK flag must not be set.");
+
+	/*
+	 * Empty the set so no garbage value is in it.
+	 */
+	FD_ZERO(&set);
+
+	/*
+	 * Add the write side of the pipe to the set.
+	 */
+	FD_SET(fds[WRITE_SIDE], &set);
+
+	if (!FD_ISSET(fds[WRITE_SIDE], &set))
+		tst_res(TFAIL, "Pipe must be in the set.");
+
+	timeout.tv_sec = SECONDS;
+	timeout.tv_usec = MICROSECONDS;
+
+	/*
+	 * Since writes are now blocking we use select to check if the pipe is
+	 * available to receive write.
+	 * Wait SECONDS seconds and MICROSECONDS microseconds on write side of
+	 * the pipe.
+	 */
+	ret = select(1, NULL, &set, NULL, &timeout);
+
+	if (ret == -1)
+		tst_res(TFAIL | TERRNO, "select() failed");
+
+	/*
+	 * The pipe is still full so select should return after the waiting time
+	 * returning 0 because write side of the pipe is not available because
+	 * it is full.
+	 */
+	if (ret)
+		tst_res(TFAIL, "Pipe is not full.");
+	else

testcases/kernel/syscalls/pipe2/pipe2_03.c line 207

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+		tst_res(TPASS, "Pipe is still full.");
+}
+
+/**
+ * Clean everything either if test is finished or if something failed.
+ */
+static void cleanup(void)
+{
+	for (int i = 0; i < 2; i++)
+		if (fds[i])

it should be if (fds[i] > 0)

Quoted 9 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+			SAFE_CLOSE(fds[i]);
+}
+
+static struct tst_test test = {
+	.test_all = test_pipe2,
+	.setup = setup,
+	.cleanup = cleanup,
+};
\ No newline at end of file
#3 lore raw

PATCH 1/1 Increase pipe2() coverage.

Hi Francis

Quoted 34 lines

Hi!

Thank you for the advice!

I just have a small question (see below).

Quoted 28 lines

Le lundi 6 avril 2020, 07:57:02 CEST Yang Xu a ?crit :
Hi laniel

Thanks for you patch, here are some small nits

Also, I think we can change a accurate subject, ie "add new test for pipe2 with/without O_NONBLOCK mode"

Quoted 6 lines

From: Francis Laniel <[email protected]>

A new test was added (pipe2_03.c), this test checks the following:
1. Create a pipe with O_NONBLOCK.
2. Check that this flag is set.
3. Check that pipe size is 65536B.

16 * page_size

Quoted 1 line

4. Reduce pipe size to 4096B.

page_size

Quoted 1 line

5. Write buffer bigger than page size and see that second write fails.

we should also check errno num

Quoted 2 lines

6. Set pipe's flags to default.
7. Check that pipe is still full with select.

Quoted 7 lines
Quoted 3 lines

IMO, we can move this function into child process, so we can check its status(it should be in s status when we write data into full pipe with block mode). And then we can kill it.

I did not understand what the child should execute?
If it writes it will block forever and so the father if it waits for its child.Parent will kill its child and wait. We have TST_PROCESS_STATE_WAIT to

check whether child is in block stat.

We have a test case vmsplice04, it is similar to this case, I think you can refer to it[1].

[1]https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/syscalls/vmsplice/vmsplice04.c

Quoted 320 lines
Quoted 315 lines
Quoted 26 lines

---

   testcases/kernel/syscalls/pipe2/.gitignore |   1 +
   testcases/kernel/syscalls/pipe2/pipe2_03.c | 215 +++++++++++++++++++++
   2 files changed, 216 insertions(+)It miss runtest/syscalls file.

create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c

(quoted diff) +0 -0

b/testcases/kernel/syscalls/pipe2/.gitignore index cd38bb309..01d980dba 100644

testcases/kernel/syscalls/pipe2/.gitignore +1 -0
@@ -1,2 +1,3 @@
 
   /pipe2_01
   /pipe2_02
 
+/pipe2_03
(quoted diff) +0 -0

b/testcases/kernel/syscalls/pipe2/pipe2_03.c new file mode 100644 index 000000000..ea225fddc

testcases/kernel/syscalls/pipe2/pipe2_03.c line 1

Quoted 1 line
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +1 -0
@@ -0,0 +1,215 @@
+// SPDX-License-Iventifier: GPL-2.0

It should use SPDX-License-Identifier: GPL-2.0-or-later.

Quoted 18 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +2 -0
+/* Copyright (c) Francis Laniel, 2020
     */
+/***********************************************************************

*******/ +/*********************************************************************** *******/ +/*

              */ +/* File:        pipe2_03.c
                      */ +/*
                              */ +/* Description: This Program tests
getting and setting the pipe size.         */ +/*              It also
tests what happen when you write to a full pipe       */ +/*
depending on whether O_NONBLOCK is or not.                    */ +/*
                                                                       */
+/* Usage:  <for command-line>
      */ +/* pipe2_03 [-c n] [-e][-i n] [-I x] [-p x] [-t]
              */ +/*      where,  -c n : Run n copies concurrently.
                      */ +/*              -e   : Turn on errno logging.
                              */ +/*              -i n : Execute test n
Quoted 5 lines

times. */ +/* -I x :
Execute test for x seconds. */ +/*
-P x : Pause for x seconds between iterations. */ +/*
-t : Turn on syscall timing. */
+/*

This is old style. I have mentioned that you may see pipe12.c for reference. In this case, option has been hanlded by ltp library, ie -i -t option. So we don't need this info.

      */

testcases/kernel/syscalls/pipe2/pipe2_03.c line 32

Quoted 13 lines

+/* Total Tests: 1

     */ +/*
             */ +/* Test Name:   pipe2_03
                     */ +/*
                             */ +/* Author:      Francis Laniel
                                     */ +/*
                                             */ +/* History:     Created -

Mar 28 2020 - Francis Laniel */
+/*********************************************************************** *******/ +#define _GNU_SOURCE

testcases/kernel/syscalls/pipe2/pipe2_03.c new file +3 -0
+#include <stdlib.h>
+#include <features.h>
+#include <fcntl.h>

We should use lapi/fcntl.h to avoid undefined error(F_GETPIPE_SZ) on centos6.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 42

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+#include <unistd.h>
+#include <stdio.h>
+#include <assert.h>
+#include <sys/select.h>
+
+#include "tst_test.h"
+
+#define PAGE_NR 16
+#define READ_SIDE 0
+#define WRITE_SIDE 1

I guess we can drop READ_SIDE and WRITE_SIDE macro. We all know fd[0] represent read side and fd[1] represent write side.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 48

Quoted 6 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +6 -0
+#define SECONDS 3
+#define MICROSECONDS 0
+
+/**
+ * The two file descriptors of the pipe.
+ */

the comment is surplus.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 53

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+static int fds[2];
+
+/**
+ * This variable will contain the system page size after setup.
+ */

here as well

testcases/kernel/syscalls/pipe2/pipe2_03.c line 61

Quoted 8 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+static long page_size;
+
+/**
+ * Setup everything.
+ * This function will:
+ * 1. Create the pipe with O_NONBLOCK.
+ * 2. Get system page size with sysconf().
+ */

here as well

Quoted 9 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+static void setup(void)
+{
+	/*
+	 * Check that Linux version is greater than 2.6.35 to be able to use
+	 * F_GETPIPE_SZ and F_SETPIPE_SZ.
+	 */
+	if (tst_kvercmp(2, 6, 35) < 0)
+		tst_brk(TCONF, "This test can only run on kernels that are 2.6.35 and

higher"); +

AFAIK, now linux kernel has reached to 5.7, 2.6.35 is so old that we don't need to compare this. Only ensuring this case can be compile is ok. ltp community has a discussion about minimal supported kernel and glibc version, more info see[1]

[1]https://github.com/linux-test-project/ltp/issues/657

testcases/kernel/syscalls/pipe2/pipe2_03.c line 75

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+	/*
+	 * Create the pip with O_NONBLOCK.
+	 */
+	if (pipe2(fds, O_NONBLOCK))
+		tst_brk(TBROK | TERRNO, "pipe2() failed");

I guess we can add SAFE_PIPE2 macro for this like SAFE_PIPE.

testcases/kernel/syscalls/pipe2/pipe2_03.c line 85

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+
+	/*
+	 * Get the system page size.
+	 */
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+}
+
+/**
+ * Test everything.
+ */

remove this comment.

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+static void test_pipe2(void)
+{
+	int ret;
+
+	long flags;

testcases/kernel/syscalls/pipe2/pipe2_03.c line 100

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+	long pipe_size;
+
+	char *buf;
+
+	fd_set set;
+	struct timeval timeout;
+
+	/*
+	 * Get the flags of the pipe.
+	 */

remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 108

Quoted 8 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+	flags = SAFE_FCNTL(fds[0], F_GETFL);
+
+	if (!(flags & O_NONBLOCK))
+		tst_res(TFAIL, "O_NONBLOCK flag must be set.");
+
+	/*
+	 * Get the size of the pipe.
+	 */

remove

Quoted 4 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +4 -0
+	pipe_size = SAFE_FCNTL(fds[0], F_GETPIPE_SZ);
+
+	if (pipe_size != page_size * PAGE_NR)
+		tst_res(TFAIL, "Default pipe page is 16 * 4096 = 65536B.");

testcases/kernel/syscalls/pipe2/pipe2_03.c

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+
+	/*
+	 * A pipe has two file descriptors.
+	 * But in the kernel these two file descriptors point to the same pipe.
+	 * So setting size from first file handle set size for the pipe.
+	 *
+	 * Moreover, the size of a pipe is exprimed in page.
+	 * So the minimal size of a pipe is a page size, setting its size to 0
+	 * leads to a pipe whom size is 4096B.
+	 */Remove...

We all know if we set a size less than page size, it will round up to a page size not 4096b.

Quoted 6 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +6 -0
+	SAFE_FCNTL(fds[0], F_SETPIPE_SZ, 0);
+
+	/*
+	 * So getting size from the second file descriptor return the size of
+	 * the pipe which was changed before with first file descriptor.
+	 */

testcases/kernel/syscalls/pipe2/pipe2_03.c line 138

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+	pipe_size = SAFE_FCNTL(fds[1], F_GETPIPE_SZ);
+
+	if (pipe_size != page_size)
+		tst_res(TFAIL, "Pipe size (%ld) must be page size (%ld)",
+			pipe_size, page_size);
+
+	/*
+	 * Create a buffer of page size.
+	 * We create it in stack because we do not care of its lifetime.
+	 */

Remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 139

Quoted 1 line
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +1 -0
+	buf = alloca(page_size);

Maybe we can use tst_alloc function. more info see[2]

https://githuber/doc/test-writing-g.com/linux-test-project/ltp/blob/mastuide
lines.txt

testcases/kernel/syscalls/pipe2/pipe2_03.c line 152

Quoted 13 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +13 -0
+
+	SAFE_WRITE(1, fds[WRITE_SIDE], buf, page_size);
+
+	/*
+	 * This write should return -1 because pipe is already full.
+	 */
+	if (write(fds[WRITE_SIDE], buf, page_size) != -1)
+		tst_res(TFAIL | TERRNO, "write() succeeded and should not");
+
+	/*
+	 * Remove the O_NONBLOCK parameter for the pipe.
+	 * After this call write to the pipe will be blocking.
+	 */

remove...

testcases/kernel/syscalls/pipe2/pipe2_03.c line 157

Quoted 5 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +5 -0
+	SAFE_FCNTL(fds[0], F_SETFL, flags & ~O_NONBLOCK);
+
+	/*
+	 * Get again the flags of the pipe.
+	 */

remove...

Quoted 40 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +40 -0
+	flags = SAFE_FCNTL(fds[0], F_GETFL);
+
+	if (flags & O_NONBLOCK)
+		tst_res(TFAIL, "O_NONBLOCK flag must not be set.")
+
+	/*
+	 * Empty the set so no garbage value is in it.
+	 */
+	FD_ZERO(&set);
+
+	/*
+	 * Add the write side of the pipe to the set.
+	 */
+	FD_SET(fds[WRITE_SIDE], &set);
+
+	if (!FD_ISSET(fds[WRITE_SIDE], &set))
+		tst_res(TFAIL, "Pipe must be in the set.");
+
+	timeout.tv_sec = SECONDS;
+	timeout.tv_usec = MICROSECONDS;
+
+	/*
+	 * Since writes are now blocking we use select to check if the pipe is
+	 * available to receive write.
+	 * Wait SECONDS seconds and MICROSECONDS microseconds on write side of
+	 * the pipe.
+	 */
+	ret = select(1, NULL, &set, NULL, &timeout);
+
+	if (ret == -1)
+		tst_res(TFAIL | TERRNO, "select() failed");
+
+	/*
+	 * The pipe is still full so select should return after the waiting time
+	 * returning 0 because write side of the pipe is not available because
+	 * it is full.
+	 */
+	if (ret)
+		tst_res(TFAIL, "Pipe is not full.");
+	else

testcases/kernel/syscalls/pipe2/pipe2_03.c line 207

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+		tst_res(TPASS, "Pipe is still full.");
+}
+
+/**
+ * Clean everything either if test is finished or if something failed.
+ */
+static void cleanup(void)
+{
+	for (int i = 0; i < 2; i++)
+		if (fds[i])

it should be if (fds[i] > 0)

Quoted 9 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +8 -0
+			SAFE_CLOSE(fds[i]);
+}
+
+static struct tst_test test = {
+	.test_all = test_pipe2,
+	.setup = setup,
+	.cleanup = cleanup,
+};
\ No newline at end of file
#4 lore raw

[LTP] Add new test for pipe2 with/without O_NONBLOCK mode

I added a new test file (pipe2_03.c) to test pipe2 system call with and without O_NONBLOCK enabled.
This test uses the macro SAFE_PIPE2 that I added, this macro is just the equivalent of SAFE_PIPE for pipe2.

The diff --stat output is the following:

include/safe_macros_fn.h                   |   3 +++
include/tst_safe_macros.h                  |   3 +++
lib/safe_macros.c                          |  15 ++++++++++++++
testcases/kernel/syscalls/pipe2/.gitignore |   1 +
testcases/kernel/syscalls/pipe2/pipe2_03.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 152 insertions(+)

Best regards.

#5 lore raw

PATCH v2 1/2 Add SAFE_PIPE2.

In reply to Yang Xu

From: Francis Laniel <[email protected]>

This macro is the equivalent of SAFE_PIPE for pipe2 system call.

---
 include/safe_macros_fn.h  |  3 +++
 include/tst_safe_macros.h |  3 +++
 lib/safe_macros.c         | 15 +++++++++++++++
 3 files changed, 21 insertions(+)
include/safe_macros_fn.h +3 -0
index 3df952811..ec2d34ae3 100644
@@ -67,6 +67,9 @@ int safe_open(const char *file, const int lineno,
 int safe_pipe(const char *file, const int lineno,
               void (*cleanup_fn)(void), int fildes[2]);
 
+int safe_pipe2(const char *file, const int lineno, void (*cleanup_fn) (void),
+               int fildes[2], int flags);
+
 ssize_t safe_read(const char *file, const int lineno,
                   void (*cleanup_fn)(void), char len_strict, int fildes,
                   void *buf, size_t nbyte);
include/tst_safe_macros.h +3 -0
index d95d26219..1738d3cc6 100644
@@ -87,6 +87,9 @@ static inline int safe_dup(const char *file, const int lineno,
 #define SAFE_PIPE(fildes) \
 	safe_pipe(__FILE__, __LINE__, NULL, (fildes))
 
+#define SAFE_PIPE2(fildes, flags) \
+	safe_pipe2(__FILE__, __LINE__, NULL, (fildes), (flags))
+
 #define SAFE_READ(len_strict, fildes, buf, nbyte) \
 	safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
 
lib/safe_macros.c +15 -0
index dde9b7b5e..780245821 100644
@@ -248,6 +248,21 @@ int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
 	return rval;
 }
 
+int safe_pipe2(const char *file, const int lineno, void (*cleanup_fn) (void),
+               int fildes[2], int flags)
+{
+	int rval;
+
+	rval = pipe2(fildes, flags);
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: pipe2({%d,%d}) failed",
+			 file, lineno, fildes[0], fildes[1]);
+	}
+
+	return rval;
+}
+
 ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
                   char len_strict, int fildes, void *buf, size_t nbyte)
 {
Signature · 1 line
2.20.1
#6 lore raw

PATCH v2 1/2 Add SAFE_PIPE2.

Hi Laniel

Usually, we should add safe macro for new and old api. We have old/safe_macros.h.

But ltp only has three places to use pipe2(pipe2_01,02,03), and pipe2_01,02 use ltp_syscall to call pipe2, so I think it is ok.

only a small nit:
move safe_pipe2() declaration to tst_safe_macros.h. We don't need to add it into safe_macros_fn.h. ie commit f59fa0de ("Add SAFE_PTRACE() to LTP library").

Best Regards
Yang Xu

Quoted 64 lines

From: Francis Laniel <[email protected]>

This macro is the equivalent of SAFE_PIPE for pipe2 system call.

---
  include/safe_macros_fn.h  |  3 +++
  include/tst_safe_macros.h |  3 +++
  lib/safe_macros.c         | 15 +++++++++++++++
  3 files changed, 21 insertions(+)
include/safe_macros_fn.h +3 -0
index 3df952811..ec2d34ae3 100644
@@ -67,6 +67,9 @@ int safe_open(const char *file, const int lineno,
  int safe_pipe(const char *file, const int lineno,
                void (*cleanup_fn)(void), int fildes[2]);
 
+int safe_pipe2(const char *file, const int lineno, void (*cleanup_fn) (void),
+               int fildes[2], int flags);
+
  ssize_t safe_read(const char *file, const int lineno,
                    void (*cleanup_fn)(void), char len_strict, int fildes,
                    void *buf, size_t nbyte);
include/tst_safe_macros.h +3 -0
index d95d26219..1738d3cc6 100644
@@ -87,6 +87,9 @@ static inline int safe_dup(const char *file, const int lineno,
  #define SAFE_PIPE(fildes) \
  	safe_pipe(__FILE__, __LINE__, NULL, (fildes))
 
+#define SAFE_PIPE2(fildes, flags) \
+	safe_pipe2(__FILE__, __LINE__, NULL, (fildes), (flags))
+
  #define SAFE_READ(len_strict, fildes, buf, nbyte) \
  	safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
 
lib/safe_macros.c +15 -0
index dde9b7b5e..780245821 100644
@@ -248,6 +248,21 @@ int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
  	return rval;
  }
 
+int safe_pipe2(const char *file, const int lineno, void (*cleanup_fn) (void),
+               int fildes[2], int flags)
+{
+	int rval;
+
+	rval = pipe2(fildes, flags);
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: pipe2({%d,%d}) failed",
+			 file, lineno, fildes[0], fildes[1]);
+	}
+
+	return rval;
+}
+
  ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
                    char len_strict, int fildes, void *buf, size_t nbyte)
  {
#7 lore raw

PATCH v2 2/2 Add new test for pipe2 with/without O_NONBLOCK mode.

In reply to Yang Xu

From: Francis Laniel <[email protected]>

The new test (pipe2_03.c) checks the following:
1. Create a pipe with O_NONBLOCK.
2. Check that this flag is set.
3. Check that pipe size is 16 * PAGE_SIZE.
4. Reduce pipe size to PAGE_SIZE.
5. Write buffer bigger than page size and see that second write fails.
6. Set pipe's flags to default.
7. Fork and do a write in the child, its state must be 'S' and is checked from the father.

---
 testcases/kernel/syscalls/pipe2/.gitignore |   1 +
 testcases/kernel/syscalls/pipe2/pipe2_03.c | 128 +++++++++++++++++++++
 2 files changed, 129 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c
testcases/kernel/syscalls/pipe2/.gitignore +1 -0
index cd38bb309..01d980dba 100644
@@ -1,2 +1,3 @@
 /pipe2_01
 /pipe2_02
+/pipe2_03
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +128 -0

129 lines not on this page · raw message

Signature · 1 line
2.20.1
#8 lore raw

PATCH v2 2/2 Add new test for pipe2 with/without O_NONBLOCK mode.

Hi laniel

Quoted 11 lines

From: Francis Laniel <[email protected]>

The new test (pipe2_03.c) checks the following:
1. Create a pipe with O_NONBLOCK.
2. Check that this flag is set.
3. Check that pipe size is 16 * PAGE_SIZE.
4. Reduce pipe size to PAGE_SIZE.
5. Write buffer bigger than page size and see that second write fails.
6. Set pipe's flags to default.
7. Fork and do a write in the child, its state must be 'S' and is checked from the father.

Quoted 3 lines
---
  testcases/kernel/syscalls/pipe2/.gitignore |   1 +
  testcases/kernel/syscalls/pipe2/pipe2_03.c | 128 +++++++++++++++++++++

We should add pipe2_03 into runtest/syscalls file, so we can use runltp command to run this case when make install. Also see Contribution Checklist in ltp/doc/test-writing-guidelines.txt

4. Test Contribution Checklist
------------------------------

1. Test compiles and runs fine (check with -i 10 too)
2. Checkpatch does not report any errors
3. The runtest entires are in place
4. Test files are added into corresponding .gitignore files
5. Patches apply over the latest git

Quoted 32 lines
  2 files changed, 129 insertions(+)

create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c

testcases/kernel/syscalls/pipe2/.gitignore +1 -0
index cd38bb309..01d980dba 100644
@@ -1,2 +1,3 @@
  /pipe2_01
  /pipe2_02
+/pipe2_03
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +15 -0
new file mode 100644
index 000000000..c2b182e02
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Francis Laniel. All rights reserved.
+ * Author: Francis Laniel <[email protected]>
+ *
+ * Test Description:
+ * This Program tests getting and setting the pipe size.
+ * It also tests what happen when you write to a full pipe depending on whether
+ * O_NONBLOCK is set or not.
+ */
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <features.h>
+#include <fcntl.h>
+#include <unistd.h>

testcases/kernel/syscalls/pipe2/pipe2_03.c line 25

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+#include <stdio.h>
+#include <assert.h>
+#include <sys/select.h>
+
+#include "lapi/fcntl.h"
+#include "tst_test.h"
+
+#define PAGE_NR 16
+#define SECONDS 3
+#define MICROSECONDS 0

The two variables is useless.

Quoted 26 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +26 -0
+
+static int fds[2];
+static long page_size;
+
+static void setup(void)
+{
+	/*
+	 * Create the pipe with O_NONBLOCK.
+	 */
+	SAFE_PIPE2(fds, O_NONBLOCK);
+
+	/*
+	 * Get the system page size.
+	 */
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+}
+
+static void test_pipe2(void)
+{
+	long flags;
+	long pipe_size;
+
+	char *buf;
+
+	pid_t pid;
+	int status;

testcases/kernel/syscalls/pipe2/pipe2_03.c line 61

Quoted 10 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +10 -0
+
+	flags = SAFE_FCNTL(fds[0], F_GETFL);
+
+	if (!(flags & O_NONBLOCK))
+		tst_res(TFAIL, "O_NONBLOCK flag must be set.");
+
+	pipe_size = SAFE_FCNTL(fds[0], F_GETPIPE_SZ);
+
+	if (pipe_size != page_size * PAGE_NR)
+		tst_res(TFAIL, "Default pipe page is 16 * 4096 = 65536B.");

I guess we can remove this check. We can not ensure kernel will keep default pipe size is equal to 16 pagesize in future. We only ensure this pipe is full is ok. We can fill this pipe in setup parse.

Quoted 69 lines
testcases/kernel/syscalls/pipe2/pipe2_03.c new file +67 -0
+
+	/*
+	 * A pipe has two file descriptors.
+	 * But in the kernel these two file descriptors point to the same pipe.
+	 * So setting size from first file handle set size for the pipe.
+	 */
+	SAFE_FCNTL(fds[0], F_SETPIPE_SZ, 0);
+
+	/*
+	 * So getting size from the second file descriptor return the size of
+	 * the pipe which was changed before with first file descriptor.
+	 */
+	pipe_size = SAFE_FCNTL(fds[1], F_GETPIPE_SZ);
+
+	if (pipe_size != page_size)
+		tst_res(TFAIL, "Pipe size (%ld) must be page size (%ld)",
+			pipe_size, page_size);
+
+	buf = alloca(page_size);
+
+	SAFE_WRITE(1, fds[1], buf, page_size);
+
+	/*
+	 * This write should return -1 because pipe is already full.
+	 */
+	if (write(fds[1], buf, page_size) != -1)
+		tst_res(TFAIL | TERRNO, "write() succeeded and should not");
+
+	SAFE_FCNTL(fds[1], F_SETFL, flags & ~O_NONBLOCK);
+
+	flags = SAFE_FCNTL(fds[1], F_GETFL);
+
+	if (flags & O_NONBLOCK)
+		tst_res(TFAIL, "O_NONBLOCK flag must not be set.");
+
+	pid = SAFE_FORK();
+
+	/*
+	 * Since writes are now blocking the child must wait forever on this
+	 * write.
+	 */
+	if (!pid)
+		SAFE_WRITE(1, fds[1], buf, page_size);
+
+	if (TST_PROCESS_STATE_WAIT(pid, 'S', 1000))
+		tst_res(TFAIL, "Child must be stopped.");
+	else
+		tst_res(TPASS, "Child is stopped.");
+
+	SAFE_KILL(pid, SIGKILL);
+
+	SAFE_WAIT(&status);
+}
+
+static void cleanup(void)
+{
+	for (int i = 0; i < 2; i++)
+		if (fds[i] > 0)
+			SAFE_CLOSE(fds[i]);
+}
+
+static struct tst_test test = {
+	.test_all = test_pipe2,
+	.setup = setup,
+	.cleanup = cleanup,
+	.forks_child = 1,
+};
\ No newline at end of file