[LTP] [PATCH v1 4/9] Rewrite sem_comm.c using new LTP API

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


Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 .../kernel/containers/sysvipc/sem_comm.c      | 160 +++++++-----------
 1 file changed, 65 insertions(+), 95 deletions(-)

diff --git a/testcases/kernel/containers/sysvipc/sem_comm.c b/testcases/kernel/containers/sysvipc/sem_comm.c
index a2c354a08..5ec21bab5 100644
--- a/testcases/kernel/containers/sysvipc/sem_comm.c
+++ b/testcases/kernel/containers/sysvipc/sem_comm.c
@@ -1,20 +1,12 @@
-/* Copyright (c) 2014 Red Hat, Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of version 2 the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * 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, see <http://www.gnu.org/licenses/>.
- ***********************************************************************
- * File: sem_comm.c
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
  *
- * Description:
  * 1. Clones two child processes with CLONE_NEWIPC flag, each child
  *    creates System V semaphore (sem) with the _identical_ key.
  * 2. Child1 locks the semaphore.
@@ -26,166 +18,144 @@
  */
 
 #define _GNU_SOURCE
+
 #include <sys/ipc.h>
-#include <sys/types.h>
 #include <sys/wait.h>
-#include <stdio.h>
-#include <errno.h>
-#include "ipcns_helper.h"
-#include "test.h"
-#include "safe_macros.h"
+#include <sys/msg.h>
+#include <sys/types.h>
+#include "tst_test.h"
 #include "lapi/sem.h"
+#include "common.h"
 
 #define TESTKEY 124426L
-char *TCID	= "sem_comm";
-int TST_TOTAL	= 1;
-
-static void cleanup(void)
-{
-	tst_rmdir();
-}
 
-static void setup(void)
+static int chld1_sem(LTP_ATTRIBUTE_UNUSED void *arg)
 {
-	tst_require_root();
-	check_newipc();
-	tst_tmpdir();
-	TST_CHECKPOINT_INIT(tst_rmdir);
-}
-
-int chld1_sem(void *arg)
-{
-	int id;
+	int id, ret;
 	union semun su;
 	struct sembuf sm;
 
 	id = semget(TESTKEY, 1, IPC_CREAT);
-	if (id == -1) {
-		perror("semget");
-		return 2;
-	}
+	if (id < 0)
+		tst_brk(TBROK, "semget: %s", tst_strerrno(-id));
 
 	su.val = 1;
-	if (semctl(id, 0, SETVAL, su) == -1) {
-		perror("semctl");
+
+	ret = semctl(id, 0, SETVAL, su);
+	if (ret < 0) {
 		semctl(id, 0, IPC_RMID);
-		return 2;
+		tst_brk(TBROK, "semctl: %s", tst_strerrno(-ret));
 	}
 
 	/* tell child2 to continue and wait for it to create the semaphore */
-	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
 	sm.sem_num = 0;
 	sm.sem_op = -1;
 	sm.sem_flg = IPC_NOWAIT;
-	if (semop(id, &sm, 1) == -1) {
-		perror("semop");
+
+	ret = semop(id, &sm, 1);
+	if (ret < 0) {
 		semctl(id, 0, IPC_RMID);
-		return 2;
+		tst_brk(TBROK, "semop: %s", tst_strerrno(-ret));
 	}
 
 	/* tell child2 to continue and wait for it to lock the semaphore */
-	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
 	sm.sem_op = 1;
 	semop(id, &sm, 1);
 
 	semctl(id, 0, IPC_RMID);
+
 	return 0;
 }
 
-int chld2_sem(void *arg)
+static int chld2_sem(LTP_ATTRIBUTE_UNUSED void *arg)
 {
-	int id, rval = 0;
+	int id, ret, rval = 0;
 	struct sembuf sm;
 	union semun su;
 
 	/* wait for child1 to create the semaphore */
-	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+	TST_CHECKPOINT_WAIT(0);
 
 	id = semget(TESTKEY, 1, IPC_CREAT);
-	if (id == -1) {
-		perror("semget");
-		return 2;
-	}
+	if (id < 0)
+		tst_brk(TBROK, "semget: %s", tst_strerrno(-id));
 
 	su.val = 1;
-	if (semctl(id, 0, SETVAL, su) == -1) {
-		perror("semctl");
+
+	ret = semctl(id, 0, SETVAL, su);
+	if (ret < 0) {
 		semctl(id, 0, IPC_RMID);
-		return 2;
+		tst_brk(TBROK, "semctl: %s", tst_strerrno(-ret));
 	}
 
 	/* tell child1 to continue and wait for it to lock the semaphore */
-	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
 	sm.sem_num = 0;
 	sm.sem_op = -1;
 	sm.sem_flg = IPC_NOWAIT;
-	if (semop(id, &sm, 1) == -1) {
+
+	ret = semop(id, &sm, 1);
+	if (ret < 0) {
 		if (errno == EAGAIN) {
 			rval = 1;
 		} else {
-			perror("semop");
 			semctl(id, 0, IPC_RMID);
-			return 2;
+			tst_brk(TBROK, "semop: %s", tst_strerrno(-ret));
 		}
 	}
 
 	/* tell child1 to continue */
-	TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
+	TST_CHECKPOINT_WAKE(0);
 
 	sm.sem_op = 1;
 	semop(id, &sm, 1);
 
 	semctl(id, 0, IPC_RMID);
+
 	return rval;
 }
 
-static void test(void)
+static void run(void)
 {
 	int status, ret = 0;
 
-	ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_sem, NULL);
-	if (ret == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
-	ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_sem, NULL);
-	if (ret == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
+	clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_sem, NULL);
+	clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_sem, NULL);
 
 	while (wait(&status) > 0) {
 		if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
 			ret = 1;
+
 		if (WIFEXITED(status) && WEXITSTATUS(status) == 2)
-			tst_brkm(TBROK | TERRNO, cleanup, "error in child");
+			tst_brk(TBROK, "error in child");
+
 		if (WIFSIGNALED(status)) {
-			tst_resm(TFAIL, "child was killed with signal %s",
-					tst_strsig(WTERMSIG(status)));
-			return;
+			tst_brk(TBROK, "child was killed with signal %s",
+				tst_strsig(WTERMSIG(status)));
 		}
 	}
 
 	if (ret)
-		tst_resm(TFAIL, "SysV sem: communication with identical keys"
-				" between namespaces");
+		tst_res(TFAIL, "SysV sem: communication with identical keys"
+			       " between namespaces");
 	else
-		tst_resm(TPASS, "SysV sem: communication with identical keys"
-				" between namespaces");
+		tst_res(TPASS, "SysV sem: communication with identical keys"
+			       " between namespaces");
 }
 
-int main(int argc, char *argv[])
+static void setup(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++)
-		test();
-
-	cleanup();
-	tst_exit();
+	check_newipc();
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.needs_root = 1,
+	.needs_checkpoints = 1,
+};
-- 
2.35.1



More information about the ltp mailing list