[LTP] [PATCH v1 8/9] Rewrite shmem_2nstest.c using new LTP API

Andrea Cervesato andrea.cervesato@suse.de
Tue Feb 8 11:09:47 CET 2022


Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 .../kernel/containers/sysvipc/shmem_2nstest.c | 262 ++++++++----------
 1 file changed, 122 insertions(+), 140 deletions(-)

diff --git a/testcases/kernel/containers/sysvipc/shmem_2nstest.c b/testcases/kernel/containers/sysvipc/shmem_2nstest.c
index b172ee07c..8f64f365c 100644
--- a/testcases/kernel/containers/sysvipc/shmem_2nstest.c
+++ b/testcases/kernel/containers/sysvipc/shmem_2nstest.c
@@ -1,187 +1,169 @@
-/* *************************************************************************
-* Copyright (c) International Business Machines Corp., 2009
-* 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
-*
-* Author: Veerendra C <vechandr@in.ibm.com>
-*
-* Test Assertion:
-* This testcase verifies the Shared Memory isoloation in 2 containers.
-* It tries to create/access a Shared Memory created with the same KEY.
-*
-* Description:
-* Create 2 'containers' with the below flag value
-*   Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
-* In Cont1, create Shared Memory segment with key 124426L
-* In Cont2, try to access the MQ created in Cont1.
-* PASS :
-* 		If flag = None and the shmem seg is accessible in Cont2.
-*		If flag = unshare/clone and the shmem seg is not accessible in Cont2.
-* 		If shmem seg is not accessible in Cont2,
-*		creates new shmem with same key to double check isloation in IPCNS.
-*
-* FAIL :
-* 		If flag = none and the shmem seg is not accessible.
-* 		If flag = unshare/clone and shmem seg is accessible in Cont2.
-*		If the new shmem seg creation Fails.
-***************************************************************************/
-
-#define _GNU_SOURCE 1
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines Corp., 2009
+ *				Veerendra C <vechandr@in.ibm.com>
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Create 2 'containers' with the below flag value
+ *   Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
+ * In Cont1, create Shared Memory segment with key 124426L
+ * In Cont2, try to access the MQ created in Cont1.
+ * PASS :
+ *  If flag = None and the shmem seg is accessible in Cont2.
+ *  If flag = unshare/clone and the shmem seg is not accessible in
+ * Cont2. If shmem seg is not accessible in Cont2, creates new shmem with same
+ * key to double check isloation in IPCNS.
+ *
+ * FAIL :
+ *  If flag = none and the shmem seg is not accessible.
+ *  If flag = unshare/clone and shmem seg is accessible in Cont2.
+ *  If the new shmem seg creation Fails.
+ */
+
+#define _GNU_SOURCE
+
 #include <sys/ipc.h>
 #include <sys/shm.h>
-#include <libclone.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "ipcns_helper.h"
+#include <sys/wait.h>
+#include <sys/msg.h>
+#include <sys/types.h>
+#include "tst_test.h"
+#include "common.h"
+
+#define TESTKEY 124426L
 
-#define TESTKEY    124426L
-#define UNSHARESTR "unshare"
-#define CLONESTR   "clone"
-#define NONESTR    "none"
+static char *str_op = "clone";
 
-char *TCID = "shmem_2nstest";
-int TST_TOTAL = 1;
-int p2[2];
-int p1[2];
+static int p1[2];
+static int p2[2];
 
 /*
  * check_shmem1() does not read -- it writes to check_shmem2() when it's done.
  */
-int check_shmem1(void *vtest)
+static int check_shmem1(LTP_ATTRIBUTE_UNUSED void *vtest)
 {
-	int id1;
+	SAFE_CLOSE(p1[0]);
 
-	(void) vtest;
+	/* first create the key */
+	TEST(shmget(TESTKEY, 100, IPC_CREAT));
+	if (TST_RET < 0)
+		tst_brk(TBROK, "shmget: %s", tst_strerrno(-TST_ERR));
 
-	close(p1[0]);
+	tst_res(TINFO, "Cont1: Able to create shared mem segment");
 
-	/* first create the key */
-	id1 = shmget(TESTKEY, 100, IPC_CREAT);
-	if (id1 == -1)
-		tst_brkm(TFAIL | TERRNO, NULL, "shmget failed");
+	SAFE_WRITE(1, p1[1], "done", 5);
 
-	tst_resm(TINFO, "Cont1: Able to create shared mem segment");
-	write(p1[1], "done", 5);
-	tst_exit();
+	return 0;
 }
 
 /*
  * check_shmem2() reads from check_shmem1() and writes to main() when it's done.
  */
-int check_shmem2(void *vtest)
+static int check_shmem2(LTP_ATTRIBUTE_UNUSED void *vtest)
 {
 	char buf[3];
-	int id2;
 
-	(void) vtest;
+	SAFE_CLOSE(p1[1]);
+	SAFE_CLOSE(p2[0]);
 
-	close(p1[1]);
-	close(p2[0]);
+	SAFE_READ(1, p1[0], buf, 3);
 
-	read(p1[0], buf, 3);
 	/* Trying to access shmem, if not existing create new shmem */
-	id2 = shmget(TESTKEY, 100, 0);
-	if (id2 == -1) {
-		id2 = shmget(TESTKEY, 100, IPC_CREAT);
-		if (id2 == -1)
-			tst_resm(TFAIL | TERRNO, "shmget failed");
-		else
-			tst_resm(TINFO,
-				 "Cont2: Able to allocate shmem seg with "
-				 "the same key");
-		write(p2[1], "notfnd", 7);
-	} else
-		write(p2[1], "exists", 7);
-
-	tst_exit();
-}
+	TEST(shmget(TESTKEY, 100, 0));
+	if (TST_RET < 0) {
+		TEST(shmget(TESTKEY, 100, IPC_CREAT));
+
+		if (TST_RET < 0) {
+			tst_brk(TBROK, "shmget: %s", tst_strerrno(-TST_ERR));
+		} else {
+			tst_res(TINFO, "Cont2: Able to allocate shmem seg with "
+				       "the same key");
+		}
+
+		SAFE_WRITE(1, p2[1], "notfnd", 7);
+	} else {
+		SAFE_WRITE(1, p2[1], "exists", 7);
+	}
 
-static void setup(void)
-{
-	tst_require_root();
-	check_newipc();
+	return 0;
 }
 
-int main(int argc, char *argv[])
+static void run(void)
 {
-	int ret, use_clone = T_NONE;
-	char *tsttype = NONESTR;
+	int use_clone = T_NONE;
 	char buf[7];
 	int id;
 
-	setup();
-
-	if (argc != 2) {
-		tst_resm(TINFO, "Usage: %s <clone| unshare| none>", argv[0]);
-		tst_resm(TINFO, " where clone, unshare, or fork specifies"
-			 " unshare method.");
-		tst_exit();
-	}
-
-	/* Using PIPE's to sync between containers and Parent */
-	SAFE_PIPE(NULL, p1);
-	SAFE_PIPE(NULL, p2);
+	/* Using PIPE's to sync between container and Parent */
+	SAFE_PIPE(p1);
+	SAFE_PIPE(p2);
 
-	if (strcmp(argv[1], "clone") == 0) {
+	if (!strcmp(str_op, "clone"))
 		use_clone = T_CLONE;
-		tsttype = CLONESTR;
-	} else if (strcmp(argv[1], "unshare") == 0) {
+	else if (!strcmp(str_op, "unshare"))
 		use_clone = T_UNSHARE;
-		tsttype = UNSHARESTR;
-	}
 
-	tst_resm(TINFO, "Shared Memory namespace test : %s", tsttype);
+	tst_res(TINFO, "Shared Memory namespace test : %s", str_op);
 
 	/* Create 2 containers */
-	ret =
-	    do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem1, NULL);
-	if (ret < 0)
-		tst_brkm(TFAIL, NULL, "clone/unshare failed");
-
-	ret =
-	    do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem2, NULL);
-	if (ret < 0)
-		tst_brkm(TFAIL, NULL, "clone/unshare failed");
+	clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem1, NULL);
+	clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem2, NULL);
 
-	close(p2[1]);
-	read(p2[0], buf, 7);
+	SAFE_CLOSE(p2[1]);
+	SAFE_READ(1, p2[0], buf, 7);
 
 	if (strcmp(buf, "exists") == 0) {
-		if (use_clone == T_NONE)
-			tst_resm(TPASS,
-				 "Plain cloned process able to access shmem "
-				 "segment created");
-		else
-			tst_resm(TFAIL,
-				 "%s : In namespace2 found the shmem segment "
-				 "created in Namespace1", tsttype);
+		if (use_clone == T_NONE) {
+			tst_res(TPASS,
+				"Plain cloned process able to access shmem "
+				"segment created");
+		} else {
+			tst_res(TFAIL,
+				"%s : In namespace2 found the shmem segment "
+				"created in Namespace1",
+				str_op);
+		}
 	} else {
-		if (use_clone == T_NONE)
-			tst_resm(TFAIL,
-				 "Plain cloned process didn't find shmem seg");
-		else
-			tst_resm(TPASS,
-				 "%s : In namespace2 unable to access the shmem seg "
-				 "created in Namespace1", tsttype);
+		if (use_clone == T_NONE) {
+			tst_res(TFAIL,
+				"Plain cloned process didn't find shmem seg");
+		} else {
+			tst_res(TPASS,
+				"%s : In namespace2 unable to access the "
+				"shmem seg "
+				"created in Namespace1",
+				str_op);
+		}
 	}
-	/* destroy the key */
 
+	/* destroy the key */
 	id = shmget(TESTKEY, 100, 0);
 	shmctl(id, IPC_RMID, NULL);
+}
 
-	tst_exit();
+static void setup(void)
+{
+	check_newipc();
+
+	if (strcmp(str_op, "clone") && strcmp(str_op, "unshare") &&
+	    strcmp(str_op, "none"))
+		tst_brk(TBROK, "Test execution mode <clone|unshare|none>");
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.forks_child = 1,
+	.needs_root = 1,
+	.needs_checkpoints = 1,
+	.options =
+		(struct tst_option[]){
+			{ "m:", &str_op,
+			  "Test execution mode <clone|unshare|none>" },
+			{},
+		},
+};
-- 
2.35.1



More information about the ltp mailing list