[LTP] [PATCH] syscalls/pipe08: Rewrite test using new LTP API

Avinesh Kumar akumar@suse.de
Fri Aug 4 07:26:17 CEST 2023


Background [1]

Signed-off-by: Avinesh Kumar <akumar@suse.de>

[1] https://lore.kernel.org/ltp/ZMopSzK0MpPIj3p4@yuki/
---
 testcases/kernel/syscalls/pipe/pipe08.c | 152 ++++++------------------
 1 file changed, 37 insertions(+), 115 deletions(-)

diff --git a/testcases/kernel/syscalls/pipe/pipe08.c b/testcases/kernel/syscalls/pipe/pipe08.c
index 173ec788a..1eac7e578 100644
--- a/testcases/kernel/syscalls/pipe/pipe08.c
+++ b/testcases/kernel/syscalls/pipe/pipe08.c
@@ -1,137 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines  Corp., 2001
+ *  07/2001 Ported by Wayne Boyer
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
  */
 
-/*
- * NAME
- *	pipe08.c
- *
- * DESCRIPTION
- *	Check that a SIGPIPE signal is generated when a write is
- *	attempted on an empty pipe.
- *
- * ALGORITHM
- *	1. Write to a pipe after closing the read side.
- *	2. Check for the signal SIGPIPE to be received.
+/*\
+ * [Description]
  *
- * USAGE:  <for command-line>
- *  pipe08 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *             -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.
- *
- * USAGE
- *	pipe08
- *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	None
+ * Verify that, on any attempt to write to a pipe which is closed for
+ * reading will generate a SIGPIPE signal and write will fail with
+ * EPIPE errno.
  */
-#include <errno.h>
-#include <unistd.h>
-#include <signal.h>
-#include <string.h>
-#include "test.h"
 
-char *TCID = "pipe08";
-int TST_TOTAL = 1;
+#include "tst_test.h"
 
-void setup(void);
-void cleanup(void);
-void sighandler(int);
+static int pipefd[2];
+static int sigpipe_cnt;
 
-int main(int ac, char **av)
+static void sighandler(int sig)
 {
-	int lc;
-
-	int pipefd[2];		/* fds for pipe read/write */
-	char wrbuf[BUFSIZ];
-	int written, length;
-	int close_stat;		/*  exit status of close(read fd) */
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		TEST(pipe(pipefd));
+	if (sig == SIGPIPE)
+		sigpipe_cnt++;
+}
 
-		if (TEST_RETURN != 0) {
-			tst_resm(TFAIL, "call failed unexpectedly");
-			continue;
-		}
+static void run(void)
+{
+	char wrbuf[] = "abcdefghijklmnopqrstuvwxyz";
 
-		if ((close_stat = close(pipefd[0])) == -1) {
-			tst_brkm(TBROK, cleanup, "close of read side failed");
-		}
+	sigpipe_cnt = 0;
 
-		strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz\0");
-		length = strlen(wrbuf);
+	SAFE_PIPE(pipefd);
+	SAFE_CLOSE(pipefd[0]);
 
-		/*
-		 * the SIGPIPE signal will be caught here or else
-		 * the program will dump core when the signal is
-		 * sent
-		 */
-		written = write(pipefd[1], wrbuf, length);
-		if (written > 0)
-			tst_brkm(TBROK, cleanup, "write succeeded unexpectedly");
-	}
-	cleanup();
-	tst_exit();
+	TST_EXP_FAIL2_SILENT(write(pipefd[1], wrbuf, sizeof(wrbuf)), EPIPE);
+	TST_EXP_EQ_LI(sigpipe_cnt, 1);
 
+	SAFE_CLOSE(pipefd[1]);
 }
 
-/*
- * sighandler - catch signals and look for SIGPIPE
- */
-void sighandler(int sig)
+static void setup(void)
 {
-	if (sig != SIGPIPE)
-		tst_resm(TFAIL, "expected SIGPIPE, got %d", sig);
-	else
-		tst_resm(TPASS, "got expected SIGPIPE signal");
+	SAFE_SIGNAL(SIGPIPE, sighandler);
 }
 
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
-void setup(void)
+static void cleanup(void)
 {
-
-	tst_sig(NOFORK, sighandler, cleanup);
-
-	TEST_PAUSE;
+	if (pipefd[0] > 0)
+		SAFE_CLOSE(pipefd[0]);
+	if (pipefd[1] > 0)
+		SAFE_CLOSE(pipefd[1]);
 }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+	.cleanup = cleanup
+};
-- 
2.41.0



More information about the ltp mailing list