All of lore.kernel.org
 help / color / mirror / Atom feed
From: Avinesh Kumar <akumar@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] syscalls/pipe08: Rewrite test using new LTP API
Date: Fri,  4 Aug 2023 10:56:17 +0530	[thread overview]
Message-ID: <20230804053022.22008-1-akumar@suse.de> (raw)

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


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

             reply	other threads:[~2023-08-04  5:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-04  5:26 Avinesh Kumar [this message]
2023-08-04  9:20 ` [LTP] [PATCH] syscalls/pipe08: Rewrite test using new LTP API Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230804053022.22008-1-akumar@suse.de \
    --to=akumar@suse.de \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.