All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH V8 1/5] syscalls/semop: Migrate to new test framework
@ 2020-07-29  7:55 Viresh Kumar
  2020-07-29  7:55 ` [LTP] [PATCH V8 2/5] syscalls: semop: Do code cleanup Viresh Kumar
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Viresh Kumar @ 2020-07-29  7:55 UTC (permalink / raw)
  To: ltp

This migrates the semop tests to the new test framework.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
V8:
- 1/5 remains unchanged and I have added four new patches as suggested
  by Cyril.
- I have kept 2/5 separate to make it more reviewable, but you can merge
  it while applying if you want to.
- I was required to keep 3/5 and 4/5 separate as well to make review
  easier.

 testcases/kernel/syscalls/ipc/semop/Makefile  |   4 +-
 testcases/kernel/syscalls/ipc/semop/semop01.c | 140 +++++-------
 testcases/kernel/syscalls/ipc/semop/semop02.c | 145 +++++-------
 testcases/kernel/syscalls/ipc/semop/semop03.c | 150 +++++--------
 testcases/kernel/syscalls/ipc/semop/semop04.c | 165 +++++---------
 testcases/kernel/syscalls/ipc/semop/semop05.c | 303 +++++++++++---------------
 6 files changed, 348 insertions(+), 559 deletions(-)

diff --git a/testcases/kernel/syscalls/ipc/semop/Makefile b/testcases/kernel/syscalls/ipc/semop/Makefile
index f62cd1f481d9..a11cbcf2e699 100644
--- a/testcases/kernel/syscalls/ipc/semop/Makefile
+++ b/testcases/kernel/syscalls/ipc/semop/Makefile
@@ -3,10 +3,10 @@
 
 top_srcdir              ?= ../../../../..
 
-LTPLIBS = ltpipc
+LTPLIBS = ltpnewipc
 
 include $(top_srcdir)/include/mk/testcases.mk
 
-LDLIBS  += -lltpipc
+LDLIBS  += -lltpnewipc
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/ipc/semop/semop01.c b/testcases/kernel/syscalls/ipc/semop/semop01.c
index ea05c53eb919..bcb45fa69320 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop01.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop01.c
@@ -1,21 +1,5 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) International Business Machines  Corp., 2001 */
 
 /*
  * NAME
@@ -52,92 +36,73 @@
  *	none
  */
 
-#include "ipcsem.h"
+#include <stdlib.h>
+#include <sys/sem.h>
+#include "tst_test.h"
+#include "libnewipc.h"
+#include "lapi/semun.h"
 
 #define NSEMS	4		/* the number of primitive semaphores to test */
 
-char *TCID = "semop01";
-int TST_TOTAL = 1;
-
-int sem_id_1 = -1;		/* a semaphore set with read & alter permissions */
+static int sem_id = -1;		/* a semaphore set with read & alter permissions */
+static key_t semkey;
 
-union semun get_arr;
-struct sembuf sops[PSEMS];
+static union semun get_arr;
+static struct sembuf sops[PSEMS];
 
-int main(int ac, char **av)
+static void run(void)
 {
-	int lc;
-	int i;
+	union semun arr = { .val = 0 };
 	int fail = 0;
+	int i;
 
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		TEST(semop(sem_id_1, sops, NSEMS));
-
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL, "%s call failed - errno = %d : %s",
-				 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
-		} else {
-			/* get the values and make sure they */
-			/* are the same as what was set      */
-			if (semctl(sem_id_1, 0, GETALL, get_arr) == -1) {
-				tst_brkm(TBROK, cleanup,
-					 "semctl() failed in functional test");
-			}
-
-			for (i = 0; i < NSEMS; i++) {
-				if (get_arr.array[i] != i * i) {
-					fail = 1;
-				}
-			}
-			if (fail)
-				tst_resm(TFAIL,
-					 "semaphore values are wrong");
-			else
-				tst_resm(TPASS,
-					 "semaphore values are correct");
-		}
+	TEST(semop(sem_id, sops, NSEMS));
 
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "semop() failed");
+	} else {
 		/*
-		 * clean up things in case we are looping
+		 * Get the values and make sure they are the same as what was
+		 * set
 		 */
-		union semun set_arr;
-		set_arr.val = 0;
+		if (semctl(sem_id, 0, GETALL, get_arr) == -1) {
+			tst_brk(TBROK | TERRNO, "semctl() failed in functional test");
+		}
+
 		for (i = 0; i < NSEMS; i++) {
-			if (semctl(sem_id_1, i, SETVAL, set_arr) == -1) {
-				tst_brkm(TBROK, cleanup, "semctl failed");
+			if (get_arr.array[i] != i * i) {
+				fail = 1;
 			}
 		}
+		if (fail)
+			tst_res(TFAIL | TERRNO, "semaphore values are wrong");
+		else
+			tst_res(TPASS, "semaphore values are correct");
 	}
 
-	cleanup();
-	tst_exit();
+	/*
+	 * clean up things in case we are looping
+	 */
+	for (i = 0; i < NSEMS; i++) {
+		if (semctl(sem_id, i, SETVAL, arr) == -1) {
+			tst_brk(TBROK | TERRNO, "semctl failed");
+		}
+	}
 }
 
-void setup(void)
+static void setup(void)
 {
 	int i;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
 	get_arr.array = malloc(sizeof(unsigned short int) * PSEMS);
 	if (get_arr.array == NULL)
-		tst_brkm(TBROK, cleanup, "malloc failed");
+		tst_brk(TBROK, "malloc failed");
 
-	semkey = getipckey();
+	semkey = GETIPCKEY();
 
-	sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
-	if (sem_id_1 == -1)
-		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
+	sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
+	if (sem_id == -1)
+		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 
 	for (i = 0; i < NSEMS; i++) {
 		sops[i].sem_num = i;
@@ -146,11 +111,20 @@ void setup(void)
 	}
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
-	rm_sema(sem_id_1);
+	union semun arr;
 
+	if (sem_id != -1) {
+		if (semctl(sem_id, 0, IPC_RMID, arr) == -1)
+			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+	}
 	free(get_arr.array);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+};
diff --git a/testcases/kernel/syscalls/ipc/semop/semop02.c b/testcases/kernel/syscalls/ipc/semop/semop02.c
index f067229b1cf5..f24d284776a4 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop02.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop02.c
@@ -1,21 +1,5 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) International Business Machines  Corp., 2001 */
 
 /*
  * DESCRIPTION
@@ -31,30 +15,27 @@
 
 #define _GNU_SOURCE
 #include <pwd.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "ipcsem.h"
-
-char *TCID = "semop02";
-
-static void semop_verify(int i);
-int sem_id_1 = -1;	/* a semaphore set with read & alter permissions */
-int sem_id_2 = -1;	/* a semaphore set without read & alter permissions */
-int bad_id = -1;
-
-struct sembuf s_buf[PSEMS];
-
-int badbuf = -1;
+#include <sys/ipc.h>
+#include <sys/sem.h>
+#include "tst_test.h"
+#include "libnewipc.h"
+#include "lapi/semun.h"
+
+static int sem_id_1 = -1;	/* a semaphore set with read & alter permissions */
+static int sem_id_2 = -1;	/* a semaphore set without read & alter permissions */
+static int bad_id = -1;
+static key_t semkey;
+static struct sembuf s_buf[PSEMS];
 
 #define NSOPS	5		/* a resonable number of operations */
 #define	BIGOPS	1024		/* a value that is too large for the number */
 				/* of semop operations that are permitted   */
-struct test_case_t {
+static struct test_case_t {
 	int *semid;
 	struct sembuf *t_sbuf;
 	unsigned t_ops;
 	int error;
-} TC[] = {
+} tc[] = {
 	{&sem_id_1, (struct sembuf *)&s_buf, BIGOPS, E2BIG},
 	{&sem_id_2, (struct sembuf *)&s_buf, NSOPS, EACCES},
 	{&sem_id_1, (struct sembuf *)-1, NSOPS, EFAULT},
@@ -63,29 +44,7 @@ struct test_case_t {
 	{&sem_id_1, (struct sembuf *)&s_buf, 1, ERANGE}
 };
 
-int TST_TOTAL = ARRAY_SIZE(TC);
-
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++)
-			semop_verify(i);
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-void setup(void)
+static void setup(void)
 {
 	char nobody_uid[] = "nobody";
 	struct passwd *ltpuser;
@@ -93,71 +52,73 @@ void setup(void)
 	struct seminfo ipc_buf;
 	union semun arr;
 
-	tst_require_root();
-
-	ltpuser = SAFE_GETPWNAM(NULL, nobody_uid);
-	SAFE_SETUID(NULL, ltpuser->pw_uid);
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
+	ltpuser = SAFE_GETPWNAM(nobody_uid);
+	SAFE_SETUID(ltpuser->pw_uid);
 
 	/* get an IPC resource key */
-	semkey = getipckey();
+	semkey = GETIPCKEY();
 
 	/* create a semaphore set with read and alter permissions */
 	sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
-	if (sem_id_1 == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "couldn't create semaphore in setup");
-	}
+	if (sem_id_1 == -1)
+		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 
 	/* Get an new IPC resource key. */
-	semkey2 = getipckey();
+	semkey2 = GETIPCKEY();
 
 	/* create a semaphore set without read and alter permissions */
 	sem_id_2 = semget(semkey2, PSEMS, IPC_CREAT | IPC_EXCL);
-	if (sem_id_2 == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "couldn't create semaphore in setup");
-	}
+	if (sem_id_2 == -1)
+		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 
 	arr.__buf = &ipc_buf;
 	if (semctl(sem_id_1, 0, IPC_INFO, arr) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "semctl() IPC_INFO failed");
+		tst_brk(TBROK | TERRNO, "semctl() IPC_INFO failed");
 
 	/* for ERANGE errno test */
 	arr.val = 1;
 	s_buf[0].sem_op = ipc_buf.semvmx;
 	if (semctl(sem_id_1, 0, SETVAL, arr) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "semctl() SETVAL failed");
+		tst_brk(TBROK | TERRNO, "semctl() SETVAL failed");
 }
 
-static void semop_verify(int i)
+static void run(unsigned int i)
 {
-	TEST(semop(*(TC[i].semid), TC[i].t_sbuf, TC[i].t_ops));
+	TEST(semop(*(tc[i].semid), tc[i].t_sbuf, tc[i].t_ops));
 
-	if (TEST_RETURN != -1) {
-		tst_resm(TFAIL, "call succeeded unexpectedly");
+	if (TST_RET != -1) {
+		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
 		return;
 	}
 
-	if (TEST_ERRNO == TC[i].error) {
-		tst_resm(TPASS | TTERRNO, "semop failed as expected");
+	if (TST_ERR == tc[i].error) {
+		tst_res(TPASS | TTERRNO, "semop failed as expected");
 	} else {
-		tst_resm(TFAIL | TTERRNO,
+		tst_res(TFAIL | TTERRNO,
 			 "semop failed unexpectedly; expected: "
-			 "%d - %s", TC[i].error, strerror(TC[i].error));
+			 "%d - %s", tc[i].error, strerror(tc[i].error));
 	}
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
-	/* if they exist, remove the semaphore resources */
-	rm_sema(sem_id_1);
-	rm_sema(sem_id_2);
+	union semun arr;
 
-	tst_rmdir();
+	if (sem_id_1 != -1) {
+		if (semctl(sem_id_1, 0, IPC_RMID, arr) == -1)
+			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+	}
+	if (sem_id_2 != -1) {
+		if (semctl(sem_id_2, 0, IPC_RMID, arr) == -1)
+			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+	}
 }
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tc),
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
diff --git a/testcases/kernel/syscalls/ipc/semop/semop03.c b/testcases/kernel/syscalls/ipc/semop/semop03.c
index 9c1c58202ad3..4f5f78eb6d8d 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop03.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop03.c
@@ -1,21 +1,5 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) International Business Machines  Corp., 2001 */
 
 /*
  * NAME
@@ -50,109 +34,75 @@
  *	none
  */
 
-#include "ipcsem.h"
-
-char *TCID = "semop03";
-int TST_TOTAL = 2;
+#include <sys/sem.h>
+#include "tst_test.h"
+#include "libnewipc.h"
+#include "lapi/semun.h"
 
-int sem_id_1 = -1;
+static key_t semkey;
+static int sem_id = -1;
+static struct sembuf s_buf;
 
-struct sembuf s_buf;
+static int tc[] = { -1, PSEMS + 1 }; /* negative and too many "primitive" semas */
 
-int TC[] = { -1, PSEMS + 1 };	/* negative and too many "primitive" semas */
-
-int main(int ac, char **av)
+static void run(unsigned int i)
 {
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();		/* global setup */
-
 	/* initialize two fields in the sembuf structure here */
 	s_buf.sem_op = 1;	/* add this value to struct sem.semval */
 	s_buf.sem_flg = SEM_UNDO;	/* undo when process exits */
 
-	/* The following loop checks looping state if -i option given */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			/* initialize the last field in the sembuf */
-			/* structure to the test dependent value   */
-			s_buf.sem_num = TC[i];
-
-			/*
-			 * use the TEST macro to make the call
-			 */
+	/*
+	 * initialize the last field in the sembuf structure to the test
+	 * dependent value.
+	 */
+	s_buf.sem_num = tc[i];
 
-			TEST(semop(sem_id_1, &s_buf, 1));
+	/*
+	 * use the TEST macro to make the call
+	 */
 
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
-				continue;
-			}
+	TEST(semop(sem_id, &s_buf, 1));
 
-			switch (TEST_ERRNO) {
-			case EFBIG:
-				tst_resm(TPASS, "expected failure - errno = "
-					 "%d : %s", TEST_ERRNO,
-					 strerror(TEST_ERRNO));
-				break;
-			default:
-				tst_resm(TFAIL, "unexpected error - "
-					 "%d : %s", TEST_ERRNO,
-					 strerror(TEST_ERRNO));
-				break;
-			}
-		}
+	if (TST_RET != -1) {
+		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
+		return;
 	}
 
-	cleanup();
-
-	tst_exit();
+	switch (TST_ERR) {
+	case EFBIG:
+		tst_res(TPASS | TTERRNO, "expected failure");
+		break;
+	default:
+		tst_res(TFAIL | TTERRNO, "unexpected failure");
+		break;
+	}
 }
 
-/*
- * setup() - performs all the ONE TIME setup for this test.
- */
-void setup(void)
+static void setup(void)
 {
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	/*
-	 * Create a temporary directory and cd into it.
-	 * This helps to ensure that a unique msgkey is created.
-	 * See libs/libltpipc/libipc.c for more information.
-	 */
-	tst_tmpdir();
-
 	/* get an IPC resource key */
-	semkey = getipckey();
+	semkey = GETIPCKEY();
 
 	/* create a semaphore with read and alter permissions */
-	if ((sem_id_1 =
-	     semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) {
-		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
-	}
+	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
+	    -1)
+		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 }
 
-/*
- * cleanup() - performs all the ONE TIME cleanup for this test@completion
- * 	       or premature exit.
- */
-void cleanup(void)
+static void cleanup(void)
 {
-	/* if it exists, remove the semaphore resource */
-	rm_sema(sem_id_1);
-
-	tst_rmdir();
+	union semun arr;
 
+	if (sem_id != -1) {
+		if (semctl(sem_id, 0, IPC_RMID, arr) == -1)
+			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+	}
 }
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tc),
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+};
diff --git a/testcases/kernel/syscalls/ipc/semop/semop04.c b/testcases/kernel/syscalls/ipc/semop/semop04.c
index c1fa2eba7584..0faf00a3585f 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop04.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop04.c
@@ -1,21 +1,5 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) International Business Machines  Corp., 2001 */
 
 /*
  * NAME
@@ -50,22 +34,24 @@
  *	none
  */
 
-#include "ipcsem.h"
-
-char *TCID = "semop04";
-int TST_TOTAL = 2;
+#include <sys/sem.h>
+#include "tst_test.h"
+#include "libnewipc.h"
+#include "lapi/semun.h"
 
-int sem_id_1 = -1;
+static int sem_id = -1;
+static int val; /* value for SETVAL */
 
-struct sembuf s_buf;
+static key_t semkey;
+static struct sembuf s_buf;
 
-struct test_case_t {
+static struct test_case_t {
 	union semun get_arr;
 	short op;
 	short flg;
 	short num;
 	int error;
-} TC[] = {
+} tc[] = {
 	/* EAGAIN sem_op = 0 */
 	{ {
 	1}, 0, IPC_NOWAIT, 2, EAGAIN},
@@ -74,104 +60,61 @@ struct test_case_t {
 	0}, -1, IPC_NOWAIT, 2, EAGAIN}
 };
 
-int main(int ac, char **av)
+static void run(unsigned int i)
 {
-	int lc;
-	int val;		/* value for SETVAL */
-
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();		/* global setup */
-
-	/* The following loop checks looping state if -i option given */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		val = 1;
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			/* initialize the s_buf buffer */
-			s_buf.sem_op = TC[i].op;
-			s_buf.sem_flg = TC[i].flg;
-			s_buf.sem_num = TC[i].num;
-
-			/* initialize all the primitive semaphores */
-			TC[i].get_arr.val = val--;
-			if (semctl(sem_id_1, TC[i].num, SETVAL, TC[i].get_arr)
-			    == -1) {
-				tst_brkm(TBROK, cleanup, "semctl() failed");
-			}
-
-			/*
-			 * make the call with the TEST macro
-			 */
-
-			TEST(semop(sem_id_1, &s_buf, 1));
-
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
-				continue;
-			}
-
-			if (TEST_ERRNO == TC[i].error) {
-				tst_resm(TPASS,
-					 "expected failure - errno = %d"
-					 " : %s", TEST_ERRNO,
-					 strerror(TEST_ERRNO));
-			} else {
-				tst_resm(TFAIL, "unexpected error - "
-					 "%d : %s", TEST_ERRNO,
-					 strerror(TEST_ERRNO));
-			}
-		}
+	/* initialize the s_buf buffer */
+	s_buf.sem_op = tc[i].op;
+	s_buf.sem_flg = tc[i].flg;
+	s_buf.sem_num = tc[i].num;
+
+	/* initialize all the primitive semaphores */
+	tc[i].get_arr.val = val--;
+	if (semctl(sem_id, tc[i].num, SETVAL, tc[i].get_arr) == -1)
+		tst_brk(TBROK | TERRNO, "semctl() failed");
+
+	TEST(semop(sem_id, &s_buf, 1));
+	if (TST_RET != -1) {
+		tst_res(TFAIL, "call succeeded unexpectedly");
+		return;
 	}
 
-	cleanup();
-
-	tst_exit();
+	if (TST_ERR == tc[i].error)
+		tst_res(TPASS | TTERRNO, "expected failure");
+	else
+		tst_res(TFAIL | TTERRNO, "unexpected failure");
 }
 
-/*
- * setup() - performs all the ONE TIME setup for this test.
- */
-void setup(void)
+static void setup(void)
 {
+	val = 1;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
+	/* get an IPC resource key */
+	semkey = GETIPCKEY();
 
 	/*
-	 * Create a temporary directory and cd into it.
-	 * This helps to ensure that a unique msgkey is created.
-	 * See libs/libltpipc/libipc.c for more information.
+	 * create a semaphore set with read and alter permissions and PSEMS
+	 * "primitive" semaphores.
 	 */
-	tst_tmpdir();
-
-	/* get an IPC resource key */
-	semkey = getipckey();
-
-	/* create a semaphore set with read and alter permissions */
-	/* and PSEMS "primitive" semaphores                       */
-	if ((sem_id_1 =
-	     semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) {
-		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
+	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
+	     -1) {
+		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 	}
 }
 
-/*
- * cleanup() - performs all the ONE TIME cleanup for this test@completion
- * 	       or premature exit.
- */
-void cleanup(void)
+static void cleanup(void)
 {
-	/* if it exists, remove the semaphore resource */
-	rm_sema(sem_id_1);
-
-	tst_rmdir();
+	union semun arr;
 
+	if (sem_id != -1) {
+		if (semctl(sem_id, 0, IPC_RMID, arr) == -1)
+			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+	}
 }
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tc),
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+};
diff --git a/testcases/kernel/syscalls/ipc/semop/semop05.c b/testcases/kernel/syscalls/ipc/semop/semop05.c
index e97ad42fe01d..9e8e040b0b19 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop05.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop05.c
@@ -1,21 +1,5 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) International Business Machines  Corp., 2001 */
 
 /*
  * NAME
@@ -60,18 +44,18 @@
  *	none
  */
 
-#include "ipcsem.h"
-
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include "safe_macros.h"
-
-char *TCID = "semop05";
-int TST_TOTAL = 4;
-
-int sem_id_1 = -1;
+#include <sys/sem.h>
+#include "tst_test.h"
+#include "libnewipc.h"
+#include "lapi/semun.h"
 
-struct sembuf s_buf;
+static key_t semkey;
+static int sem_id = -1;
+static struct sembuf s_buf;
 
 struct test_case_t {
 	union semun semunptr;
@@ -79,7 +63,7 @@ struct test_case_t {
 	short flg;
 	short num;
 	int error;
-} TC[] = {
+} tc[] = {
 	/* EIRDM sem_op = 0 */
 	{ {
 	1}, 0, 0, 2, EIDRM},
@@ -96,189 +80,166 @@ struct test_case_t {
 
 #ifdef UCLINUX
 #define PIPE_NAME	"semop05"
-void do_child_uclinux();
+static void do_child_uclinux();
 static int i_uclinux;
 #endif
 
-int main(int ac, char **av)
+static inline int process_state_wait2(pid_t pid, const char state)
 {
-	int lc;
-	int i;
-	pid_t pid;
-	void do_child();
+	char proc_path[128], cur_state;
 
-	tst_parse_opts(ac, av, NULL, NULL);
+	snprintf(proc_path, sizeof(proc_path), "/proc/%i/stat", pid);
 
-#ifdef UCLINUX
-	maybe_run_child(&do_child_uclinux, "dd", &i_uclinux, &sem_id_1);
-#endif
-
-	setup();		/* global setup */
-
-	/* The following loop checks looping state if -i option given */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			/* initialize the s_buf buffer */
-			s_buf.sem_op = TC[i].op;
-			s_buf.sem_flg = TC[i].flg;
-			s_buf.sem_num = TC[i].num;
-
-			/* initialize all of the primitive semaphores */
-			if (semctl(sem_id_1, TC[i].num, SETVAL, TC[i].semunptr)
-			    == -1) {
-				tst_brkm(TBROK, cleanup, "semctl() failed");
-			}
-
-			if ((pid = FORK_OR_VFORK()) == -1) {
-				tst_brkm(TBROK, cleanup, "could not fork");
-			}
-
-			if (pid == 0) {	/* child */
+	for (;;) {
+		FILE *f = fopen(proc_path, "r");
+		if (!f) {
+			tst_res(TFAIL, "Failed to open '%s': %s\n", proc_path,
+				strerror(errno));
+			return 1;
+		}
 
-#ifdef UCLINUX
-				if (self_exec(av[0], "dd", i, sem_id_1) < 0) {
-					tst_brkm(TBROK, cleanup,
-						 "could not self_exec");
-				}
-#else
-				do_child(i);
-#endif
-			} else {
-				TST_PROCESS_STATE_WAIT(cleanup, pid, 'S');
-
-				/*
-				 * If we are testing for EIDRM then remove
-				 * the semaphore, else send a signal that
-				 * must be caught as we are testing for
-				 * EINTR.
-				 */
-				if (TC[i].error == EIDRM) {
-					/* remove the semaphore resource */
-					rm_sema(sem_id_1);
-				} else {
-					SAFE_KILL(cleanup, pid, SIGHUP);
-				}
-
-				/* let the child carry on */
-				waitpid(pid, NULL, 0);
-			}
-
-			/*
-			 * recreate the semaphore resource if needed
-			 */
-			if (TC[i].error == EINTR) {
-				continue;
-			}
-
-			if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT |
-					       IPC_EXCL | SEM_RA)) == -1) {
-				tst_brkm(TBROK, cleanup, "couldn't recreate "
-					 "semaphore");
-			}
+		if (fscanf(f, "%*i %*s %c", &cur_state) != 1) {
+			fclose(f);
+			tst_res(TFAIL, "Failed to read '%s': %s\n", proc_path,
+				strerror(errno));
+			return 1;
 		}
-	}
+		fclose(f);
 
-	cleanup();
+		if (state == cur_state)
+			return 0;
 
-	tst_exit();
+		usleep(10000);
+	}
 }
 
-/*
- * do_child()
- */
-void do_child(int i)
+static void do_child(int i)
 {
-	/*
-	 * make the call with the TEST macro
-	 */
-
-	TEST(semop(sem_id_1, &s_buf, 1));
-
-	if (TEST_RETURN != -1) {
-		tst_resm(TFAIL, "call succeeded when error expected");
+	TEST(semop(sem_id, &s_buf, 1));
+	if (TST_RET != -1) {
+		tst_res(TFAIL, "call succeeded when error expected");
 		exit(-1);
 	}
 
-	if (TEST_ERRNO == TC[i].error) {
-		tst_resm(TPASS, "expected failure - errno = %d"
-			 " : %s", TEST_ERRNO, strerror(TEST_ERRNO));
-	} else {
-		tst_resm(TFAIL, "unexpected error - "
-			 "%d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
-	}
+	if (TST_ERR == tc[i].error)
+		tst_res(TPASS | TTERRNO, "expected failure");
+	else
+		tst_res(TFAIL | TTERRNO, "unexpected failure");
 
 	exit(0);
 }
 
-void sighandler(int sig)
+static void sighandler(int sig)
 {
-	if (sig == SIGHUP)
-		return;
-	else
-		tst_brkm(TBROK, NULL, "unexpected signal %d received", sig);
+	if (sig != SIGHUP)
+		tst_brk(TBROK, "unexpected signal %d received", sig);
 }
 
-#ifdef UCLINUX
-/*
- * do_child_uclinux() - capture signals, re-initialize s_buf then call do_child
- *                      with the appropriate argument
- */
-void do_child_uclinux(void)
+static void setup(void)
 {
-	int i = i_uclinux;
+	SAFE_SIGNAL(SIGHUP, sighandler);
 
-	tst_sig(FORK, sighandler, cleanup);
+	/* get an IPC resource key */
+	semkey = GETIPCKEY();
 
-	/* initialize the s_buf buffer */
-	s_buf.sem_op = TC[i].op;
-	s_buf.sem_flg = TC[i].flg;
-	s_buf.sem_num = TC[i].num;
+	/*
+	 * create a semaphore set with read and alter permissions and PSEMS
+	 * "primitive" semaphores.
+	 */
+	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
+	    -1)
+		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
+}
 
-	do_child(i);
+static void cleanup(void)
+{
+	union semun arr;
+
+	if (sem_id != -1) {
+		if (semctl(sem_id, 0, IPC_RMID, arr) == -1)
+			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+	}
 }
-#endif
 
-/*
- * setup() - performs all the ONE TIME setup for this test.
- */
-void setup(void)
+static void run(unsigned int i)
 {
+	pid_t pid;
 
-	tst_sig(FORK, sighandler, cleanup);
+#ifdef UCLINUX
+	maybe_run_child(&do_child_uclinux, "dd", &i_uclinux, &sem_id);
+#endif
+	/* initialize the s_buf buffer */
+	s_buf.sem_op = tc[i].op;
+	s_buf.sem_flg = tc[i].flg;
+	s_buf.sem_num = tc[i].num;
 
-	TEST_PAUSE;
+	/* initialize all of the primitive semaphores */
+	if (semctl(sem_id, tc[i].num, SETVAL, tc[i].semunptr) == -1)
+		tst_brk(TBROK | TERRNO, "semctl() failed");
 
-	/*
-	 * Create a temporary directory and cd into it.
-	 * This helps to ensure that a unique msgkey is created.
-	 * See libs/libltpipc/libipc.c for more information.
-	 */
-	tst_tmpdir();
+	pid = SAFE_FORK();
 
-	/* get an IPC resource key */
-	semkey = getipckey();
+	if (pid == 0) {	/* child */
+#ifdef UCLINUX
+		if (self_exec(av[0], "dd", i, sem_id) < 0)
+			tst_brk(TBROK, "could not self_exec");
+#else
+		do_child(i);
+#endif
+	} else {
+		process_state_wait2(pid, 'S');
+
+		/*
+		 * If we are testing for EIDRM then remove
+		 * the semaphore, else send a signal that
+		 * must be caught as we are testing for
+		 * EINTR.
+		 */
+		if (tc[i].error == EIDRM) {
+			/* remove the semaphore resource */
+			cleanup();
+		} else {
+			SAFE_KILL(pid, SIGHUP);
+		}
 
-	/* create a semaphore set with read and alter permissions */
-	/* and PSEMS "primitive" semaphores                       */
-	if ((sem_id_1 =
-	     semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) {
-		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
+		/* let the child carry on */
+		waitpid(pid, NULL, 0);
 	}
+
+	/*
+	 * recreate the semaphore resource if needed
+	 */
+	if (tc[i].error == EINTR)
+		return;
+
+	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
+	    -1)
+		tst_brk(TBROK | TERRNO, "couldn't recreate semaphore");
 }
 
+#ifdef UCLINUX
 /*
- * cleanup() - performs all the ONE TIME cleanup for this test@completion
- *	       or premature exit.
+ * do_child_uclinux() - capture signals, re-initialize s_buf then call do_child
+ *                      with the appropriate argument
  */
-void cleanup(void)
+static void do_child_uclinux(void)
 {
-	/* if it exists, remove the semaphore resource */
-	rm_sema(sem_id_1);
+	int i = i_uclinux;
+
+	/* initialize the s_buf buffer */
+	s_buf.sem_op = tc[i].op;
+	s_buf.sem_flg = tc[i].flg;
+	s_buf.sem_num = tc[i].num;
 
-	tst_rmdir();
+	do_child(i);
 }
+#endif
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tc),
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+};
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V8 2/5] syscalls: semop: Do code cleanup
  2020-07-29  7:55 [LTP] [PATCH V8 1/5] syscalls/semop: Migrate to new test framework Viresh Kumar
@ 2020-07-29  7:55 ` Viresh Kumar
  2020-07-29  7:55 ` [LTP] [PATCH V8 3/5] syscalls: semop: Merge semop0[234].c files Viresh Kumar
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: Viresh Kumar @ 2020-07-29  7:55 UTC (permalink / raw)
  To: ltp

This gets rid of not so useful stuff like unnecessary comments and
uClinux support which isn't used anymore.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 testcases/kernel/syscalls/ipc/semop/semop01.c |  46 +----------
 testcases/kernel/syscalls/ipc/semop/semop02.c |  31 +++-----
 testcases/kernel/syscalls/ipc/semop/semop03.c |  49 +-----------
 testcases/kernel/syscalls/ipc/semop/semop04.c |  50 ++----------
 testcases/kernel/syscalls/ipc/semop/semop05.c | 105 ++------------------------
 5 files changed, 31 insertions(+), 250 deletions(-)

diff --git a/testcases/kernel/syscalls/ipc/semop/semop01.c b/testcases/kernel/syscalls/ipc/semop/semop01.c
index bcb45fa69320..19607ee6d653 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop01.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop01.c
@@ -1,39 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
-/* Copyright (c) International Business Machines  Corp., 2001 */
-
 /*
- * NAME
- *	semop01.c
- *
- * DESCRIPTION
- *	semop01 - test that semop() basic functionality is correct
- *
- * ALGORITHM
- *	create a semaphore set and initialize some values
- *	loop if that option was specified
- *	call semop() to set values for the primitive semaphores
- *	check the return code
- *	  if failure, issue a FAIL message.
- *	otherwise,
- *	  if doing functionality testing
- *		get the semaphore values and compare with expected values
- *		if correct,
- *			issue a PASS message
- *		otherwise
- *			issue a FAIL message
- *	  else issue a PASS message
- *	call cleanup
+ * Test that semop() basic functionality is correct
  *
- * HISTORY
- *	03/2001  - Written by Wayne Boyer
+ * Copyright (c) International Business Machines Corp., 2001
+ *	03/2001 - Written by Wayne Boyer
  *	17/01/02 - Modified. Manoj Iyer, IBM Austin. TX. manjo@austin.ibm.com
- *	           4th argument to semctl() system call was modified according
- *	           to man pages.
- *	           In my opinion The test should not even have compiled but
- *	           it was working due to some mysterious reason.
- *
- * RESTRICTIONS
- *	none
  */
 
 #include <stdlib.h>
@@ -57,14 +28,9 @@ static void run(void)
 	int i;
 
 	TEST(semop(sem_id, sops, NSEMS));
-
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "semop() failed");
 	} else {
-		/*
-		 * Get the values and make sure they are the same as what was
-		 * set
-		 */
 		if (semctl(sem_id, 0, GETALL, get_arr) == -1) {
 			tst_brk(TBROK | TERRNO, "semctl() failed in functional test");
 		}
@@ -80,13 +46,9 @@ static void run(void)
 			tst_res(TPASS, "semaphore values are correct");
 	}
 
-	/*
-	 * clean up things in case we are looping
-	 */
 	for (i = 0; i < NSEMS; i++) {
-		if (semctl(sem_id, i, SETVAL, arr) == -1) {
+		if (semctl(sem_id, i, SETVAL, arr) == -1)
 			tst_brk(TBROK | TERRNO, "semctl failed");
-		}
 	}
 }
 
diff --git a/testcases/kernel/syscalls/ipc/semop/semop02.c b/testcases/kernel/syscalls/ipc/semop/semop02.c
index f24d284776a4..66b0bef56257 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop02.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop02.c
@@ -1,16 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
-/* Copyright (c) International Business Machines  Corp., 2001 */
-
 /*
- * DESCRIPTION
- *	semop02 - test for E2BIG, EACCES, EFAULT, EINVAL and ERANGE errors
+ * semop02 - test for E2BIG, EACCES, EFAULT, EINVAL and ERANGE errors
  *
- * HISTORY
+ * Copyright (c) International Business Machines  Corp., 2001
  *	03/2001 - Written by Wayne Boyer
- *
- *      10/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
- *      - Fix concurrency issue. The second key used for this test could
- *        conflict with the key from another task.
+ *	10/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
  */
 
 #define _GNU_SOURCE
@@ -27,21 +21,21 @@ static int bad_id = -1;
 static key_t semkey;
 static struct sembuf s_buf[PSEMS];
 
-#define NSOPS	5		/* a resonable number of operations */
+#define NSOPS	5		/* a reasonable number of operations */
 #define	BIGOPS	1024		/* a value that is too large for the number */
-				/* of semop operations that are permitted   */
+				/* of semop operations that are permitted */
 static struct test_case_t {
 	int *semid;
 	struct sembuf *t_sbuf;
 	unsigned t_ops;
 	int error;
 } tc[] = {
-	{&sem_id_1, (struct sembuf *)&s_buf, BIGOPS, E2BIG},
-	{&sem_id_2, (struct sembuf *)&s_buf, NSOPS, EACCES},
+	{&sem_id_1, s_buf, BIGOPS, E2BIG},
+	{&sem_id_2, s_buf, NSOPS, EACCES},
 	{&sem_id_1, (struct sembuf *)-1, NSOPS, EFAULT},
-	{&sem_id_1, (struct sembuf *)&s_buf, 0, EINVAL},
-	{&bad_id, (struct sembuf *)&s_buf, NSOPS, EINVAL},
-	{&sem_id_1, (struct sembuf *)&s_buf, 1, ERANGE}
+	{&sem_id_1, s_buf, 0, EINVAL},
+	{&bad_id, s_buf, NSOPS, EINVAL},
+	{&sem_id_1, s_buf, 1, ERANGE}
 };
 
 static void setup(void)
@@ -55,18 +49,14 @@ static void setup(void)
 	ltpuser = SAFE_GETPWNAM(nobody_uid);
 	SAFE_SETUID(ltpuser->pw_uid);
 
-	/* get an IPC resource key */
 	semkey = GETIPCKEY();
 
-	/* create a semaphore set with read and alter permissions */
 	sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
 	if (sem_id_1 == -1)
 		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 
-	/* Get an new IPC resource key. */
 	semkey2 = GETIPCKEY();
 
-	/* create a semaphore set without read and alter permissions */
 	sem_id_2 = semget(semkey2, PSEMS, IPC_CREAT | IPC_EXCL);
 	if (sem_id_2 == -1)
 		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
@@ -75,7 +65,6 @@ static void setup(void)
 	if (semctl(sem_id_1, 0, IPC_INFO, arr) == -1)
 		tst_brk(TBROK | TERRNO, "semctl() IPC_INFO failed");
 
-	/* for ERANGE errno test */
 	arr.val = 1;
 	s_buf[0].sem_op = ipc_buf.semvmx;
 	if (semctl(sem_id_1, 0, SETVAL, arr) == -1)
diff --git a/testcases/kernel/syscalls/ipc/semop/semop03.c b/testcases/kernel/syscalls/ipc/semop/semop03.c
index 4f5f78eb6d8d..c569033eebc2 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop03.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop03.c
@@ -1,37 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
-/* Copyright (c) International Business Machines  Corp., 2001 */
-
 /*
- * NAME
- *	semop03.c
- *
- * DESCRIPTION
- *	semop03 - test for EFBIG error
- *
- * ALGORITHM
- *	create a semaphore set with read and alter permissions
- *	loop if that option was specified
- *	call semop() using two different invalid cases
- *	check the errno value
- *	  issue a PASS message if we get EFBIG
- *	otherwise, the tests fails
- *	  issue a FAIL message
- *	call cleanup
- *
- * USAGE:  <for command-line>
- *  semop03 [-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.
+ * semop03 - test for EFBIG error
  *
- * HISTORY
+ * Copyright (c) International Business Machines  Corp., 2001
  *	03/2001 - Written by Wayne Boyer
- *
- * RESTRICTIONS
- *	none
  */
 
 #include <sys/sem.h>
@@ -47,22 +19,11 @@ static int tc[] = { -1, PSEMS + 1 }; /* negative and too many "primitive" semas
 
 static void run(unsigned int i)
 {
-	/* initialize two fields in the sembuf structure here */
-	s_buf.sem_op = 1;	/* add this value to struct sem.semval */
-	s_buf.sem_flg = SEM_UNDO;	/* undo when process exits */
-
-	/*
-	 * initialize the last field in the sembuf structure to the test
-	 * dependent value.
-	 */
+	s_buf.sem_op = 1;
+	s_buf.sem_flg = SEM_UNDO;
 	s_buf.sem_num = tc[i];
 
-	/*
-	 * use the TEST macro to make the call
-	 */
-
 	TEST(semop(sem_id, &s_buf, 1));
-
 	if (TST_RET != -1) {
 		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
 		return;
@@ -80,10 +41,8 @@ static void run(unsigned int i)
 
 static void setup(void)
 {
-	/* get an IPC resource key */
 	semkey = GETIPCKEY();
 
-	/* create a semaphore with read and alter permissions */
 	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
 	    -1)
 		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
diff --git a/testcases/kernel/syscalls/ipc/semop/semop04.c b/testcases/kernel/syscalls/ipc/semop/semop04.c
index 0faf00a3585f..1904b177f71b 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop04.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop04.c
@@ -1,37 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
-/* Copyright (c) International Business Machines  Corp., 2001 */
-
 /*
- * NAME
- *	semop04.c
- *
- * DESCRIPTION
- *	semop04 - test for EAGAIN error
- *
- * ALGORITHM
- *	create a semaphore set with read and alter permissions
- *	loop if that option was specified
- *	call semop() with two different invalid cases
- *	check the errno value
- *	  issue a PASS message if we get EAGAIN
- *	otherwise, the tests fails
- *	  issue a FAIL message
- *	call cleanup
+ * semop04 - test for EAGAIN error
  *
- * USAGE:  <for command-line>
- *  semop04 [-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.
- *
- * HISTORY
+ * Copyright (c) International Business Machines  Corp., 2001
  *	03/2001 - Written by Wayne Boyer
- *
- * RESTRICTIONS
- *	none
  */
 
 #include <sys/sem.h>
@@ -40,7 +12,7 @@
 #include "lapi/semun.h"
 
 static int sem_id = -1;
-static int val; /* value for SETVAL */
+static int val;
 
 static key_t semkey;
 static struct sembuf s_buf;
@@ -52,22 +24,16 @@ static struct test_case_t {
 	short num;
 	int error;
 } tc[] = {
-	/* EAGAIN sem_op = 0 */
-	{ {
-	1}, 0, IPC_NOWAIT, 2, EAGAIN},
-	    /* EAGAIN sem_op = -1 */
-	{ {
-	0}, -1, IPC_NOWAIT, 2, EAGAIN}
+	{{1}, 0, IPC_NOWAIT, 2, EAGAIN},
+	{{0}, -1, IPC_NOWAIT, 2, EAGAIN}
 };
 
 static void run(unsigned int i)
 {
-	/* initialize the s_buf buffer */
 	s_buf.sem_op = tc[i].op;
 	s_buf.sem_flg = tc[i].flg;
 	s_buf.sem_num = tc[i].num;
 
-	/* initialize all the primitive semaphores */
 	tc[i].get_arr.val = val--;
 	if (semctl(sem_id, tc[i].num, SETVAL, tc[i].get_arr) == -1)
 		tst_brk(TBROK | TERRNO, "semctl() failed");
@@ -87,14 +53,8 @@ static void run(unsigned int i)
 static void setup(void)
 {
 	val = 1;
-
-	/* get an IPC resource key */
 	semkey = GETIPCKEY();
 
-	/*
-	 * create a semaphore set with read and alter permissions and PSEMS
-	 * "primitive" semaphores.
-	 */
 	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
 	     -1) {
 		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
diff --git a/testcases/kernel/syscalls/ipc/semop/semop05.c b/testcases/kernel/syscalls/ipc/semop/semop05.c
index 9e8e040b0b19..df8ce7d16ee5 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop05.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop05.c
@@ -1,47 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
-/* Copyright (c) International Business Machines  Corp., 2001 */
-
 /*
- * NAME
- *	semop05.c
- *
- * DESCRIPTION
- *	semop05 - test for EINTR and EIDRM errors
- *
- * ALGORITHM
- *	create a semaphore set with read and alter permissions
- *	loop if that option was specified
- *	set up the s_buf buffer
- *	initialize the primitive semaphores
- *	fork a child process
- *	child calls semop() and sleeps
- *	parent either removes the semaphore set or sends a signal to the child
- *	parent then exits
- *	child gets a return from the semop() call
- *	check the errno value
- *	  issue a PASS message if we get EINTR or EIDRM
- *	otherwise, the tests fails
- *	  issue a FAIL message
- *	call cleanup
+ * semop05 - test for EINTR and EIDRM errors
  *
- * USAGE:  <for command-line>
- *  semop05 [-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.
- *
- * HISTORY
+ * Copyright (c) International Business Machines  Corp., 2001
  *	03/2001 - Written by Wayne Boyer
- *      14/03/2008 Matthieu Fertr? (Matthieu.Fertre@irisa.fr)
- *      - Fix concurrency issue. Due to the use of usleep function to
- *        synchronize processes, synchronization issues can occur on a loaded
- *        system. Fix this by using pipes to synchronize processes.
- *
- * RESTRICTIONS
- *	none
+ *	14/03/2008 Matthieu Fertr? (Matthieu.Fertre@irisa.fr)
  */
 
 #include <stdio.h>
@@ -64,26 +27,12 @@ struct test_case_t {
 	short num;
 	int error;
 } tc[] = {
-	/* EIRDM sem_op = 0 */
-	{ {
-	1}, 0, 0, 2, EIDRM},
-	    /* EIRDM sem_op = -1 */
-	{ {
-	0}, -1, 0, 3, EIDRM},
-	    /* EINTR sem_op = 0 */
-	{ {
-	1}, 0, 0, 4, EINTR},
-	    /* EINTR sem_op = -1 */
-	{ {
-	0}, -1, 0, 5, EINTR}
+	{{1}, 0, 0, 2, EIDRM},
+	{{0}, -1, 0, 3, EIDRM},
+	{{1}, 0, 0, 4, EINTR},
+	{{0}, -1, 0, 5, EINTR}
 };
 
-#ifdef UCLINUX
-#define PIPE_NAME	"semop05"
-static void do_child_uclinux();
-static int i_uclinux;
-#endif
-
 static inline int process_state_wait2(pid_t pid, const char state)
 {
 	char proc_path[128], cur_state;
@@ -138,14 +87,8 @@ static void sighandler(int sig)
 static void setup(void)
 {
 	SAFE_SIGNAL(SIGHUP, sighandler);
-
-	/* get an IPC resource key */
 	semkey = GETIPCKEY();
 
-	/*
-	 * create a semaphore set with read and alter permissions and PSEMS
-	 * "primitive" semaphores.
-	 */
 	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
 	    -1)
 		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
@@ -165,27 +108,17 @@ static void run(unsigned int i)
 {
 	pid_t pid;
 
-#ifdef UCLINUX
-	maybe_run_child(&do_child_uclinux, "dd", &i_uclinux, &sem_id);
-#endif
-	/* initialize the s_buf buffer */
 	s_buf.sem_op = tc[i].op;
 	s_buf.sem_flg = tc[i].flg;
 	s_buf.sem_num = tc[i].num;
 
-	/* initialize all of the primitive semaphores */
 	if (semctl(sem_id, tc[i].num, SETVAL, tc[i].semunptr) == -1)
 		tst_brk(TBROK | TERRNO, "semctl() failed");
 
 	pid = SAFE_FORK();
 
-	if (pid == 0) {	/* child */
-#ifdef UCLINUX
-		if (self_exec(av[0], "dd", i, sem_id) < 0)
-			tst_brk(TBROK, "could not self_exec");
-#else
+	if (pid == 0) {
 		do_child(i);
-#endif
 	} else {
 		process_state_wait2(pid, 'S');
 
@@ -202,13 +135,9 @@ static void run(unsigned int i)
 			SAFE_KILL(pid, SIGHUP);
 		}
 
-		/* let the child carry on */
 		waitpid(pid, NULL, 0);
 	}
 
-	/*
-	 * recreate the semaphore resource if needed
-	 */
 	if (tc[i].error == EINTR)
 		return;
 
@@ -217,24 +146,6 @@ static void run(unsigned int i)
 		tst_brk(TBROK | TERRNO, "couldn't recreate semaphore");
 }
 
-#ifdef UCLINUX
-/*
- * do_child_uclinux() - capture signals, re-initialize s_buf then call do_child
- *                      with the appropriate argument
- */
-static void do_child_uclinux(void)
-{
-	int i = i_uclinux;
-
-	/* initialize the s_buf buffer */
-	s_buf.sem_op = tc[i].op;
-	s_buf.sem_flg = tc[i].flg;
-	s_buf.sem_num = tc[i].num;
-
-	do_child(i);
-}
-#endif
-
 static struct tst_test test = {
 	.test = run,
 	.tcnt = ARRAY_SIZE(tc),
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V8 3/5] syscalls: semop: Merge semop0[234].c files
  2020-07-29  7:55 [LTP] [PATCH V8 1/5] syscalls/semop: Migrate to new test framework Viresh Kumar
  2020-07-29  7:55 ` [LTP] [PATCH V8 2/5] syscalls: semop: Do code cleanup Viresh Kumar
@ 2020-07-29  7:55 ` Viresh Kumar
  2020-07-29  7:55 ` [LTP] [PATCH V8 4/5] syscalls: semop: Rename semop05.c Viresh Kumar
  2020-07-29  7:55 ` [LTP] [PATCH V8 5/5] syscalls/semtimedop: Add support for semtimedop and its time64 version Viresh Kumar
  3 siblings, 0 replies; 13+ messages in thread
From: Viresh Kumar @ 2020-07-29  7:55 UTC (permalink / raw)
  To: ltp

All these can be handled together in semop02.c easily.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 runtest/syscalls                               |  2 -
 runtest/syscalls-ipc                           |  2 -
 testcases/kernel/syscalls/ipc/semop/.gitignore |  2 -
 testcases/kernel/syscalls/ipc/semop/semop02.c  | 47 ++++++++++-----
 testcases/kernel/syscalls/ipc/semop/semop03.c  | 67 ---------------------
 testcases/kernel/syscalls/ipc/semop/semop04.c  | 80 --------------------------
 6 files changed, 34 insertions(+), 166 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/ipc/semop/semop03.c
 delete mode 100644 testcases/kernel/syscalls/ipc/semop/semop04.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 70b3277d38ea..99831f6aa36e 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1169,8 +1169,6 @@ semget06 semget06
 
 semop01 semop01
 semop02 semop02
-semop03 semop03
-semop04 semop04
 semop05 semop05
 
 send01 send01
diff --git a/runtest/syscalls-ipc b/runtest/syscalls-ipc
index c3a35896c5a8..1a63071b22cf 100644
--- a/runtest/syscalls-ipc
+++ b/runtest/syscalls-ipc
@@ -44,8 +44,6 @@ semget06 semget06
 
 semop01 semop01
 semop02 semop02
-semop03 semop03
-semop04 semop04
 semop05 semop05
 
 shmat01 shmat01
diff --git a/testcases/kernel/syscalls/ipc/semop/.gitignore b/testcases/kernel/syscalls/ipc/semop/.gitignore
index cc67b1862f41..35b7af66c465 100644
--- a/testcases/kernel/syscalls/ipc/semop/.gitignore
+++ b/testcases/kernel/syscalls/ipc/semop/.gitignore
@@ -1,5 +1,3 @@
 /semop01
 /semop02
-/semop03
-/semop04
 /semop05
diff --git a/testcases/kernel/syscalls/ipc/semop/semop02.c b/testcases/kernel/syscalls/ipc/semop/semop02.c
index 66b0bef56257..7a49b2648b2b 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop02.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop02.c
@@ -18,24 +18,37 @@
 static int sem_id_1 = -1;	/* a semaphore set with read & alter permissions */
 static int sem_id_2 = -1;	/* a semaphore set without read & alter permissions */
 static int bad_id = -1;
+static short sem_op_dyn, sem_op_1 = 1, sem_op_negative = -1, sem_op_zero = 0;
 static key_t semkey;
 static struct sembuf s_buf[PSEMS];
+static struct sembuf *faulty_buf;
+static struct sembuf *valid_buf = s_buf;
+static union semun arr;
 
 #define NSOPS	5		/* a reasonable number of operations */
 #define	BIGOPS	1024		/* a value that is too large for the number */
 				/* of semop operations that are permitted */
 static struct test_case_t {
 	int *semid;
-	struct sembuf *t_sbuf;
+	struct sembuf **buf;
+	short *sem_op; /* semaphore operation */
+	unsigned short ctl_sem_num;
+	unsigned short sem_num; /* semaphore index in array */
+	short sem_flg; /* operation flags */
 	unsigned t_ops;
+	int arr_val;
 	int error;
 } tc[] = {
-	{&sem_id_1, s_buf, BIGOPS, E2BIG},
-	{&sem_id_2, s_buf, NSOPS, EACCES},
-	{&sem_id_1, (struct sembuf *)-1, NSOPS, EFAULT},
-	{&sem_id_1, s_buf, 0, EINVAL},
-	{&bad_id, s_buf, NSOPS, EINVAL},
-	{&sem_id_1, s_buf, 1, ERANGE}
+	{&sem_id_1, &valid_buf, &sem_op_dyn, 0, 0, 0, BIGOPS, 1, E2BIG},
+	{&sem_id_2, &valid_buf, &sem_op_dyn, 0, 0, 0, NSOPS, 1, EACCES},
+	{&sem_id_1, &faulty_buf, &sem_op_dyn, 0, 0, 0, NSOPS, 1, EFAULT},
+	{&sem_id_1, &valid_buf, &sem_op_dyn, 0, 0, 0, 0, 1, EINVAL},
+	{&bad_id, &valid_buf, &sem_op_dyn, 0, 0, 0, NSOPS, 1, EINVAL},
+	{&sem_id_1, &valid_buf, &sem_op_dyn, 0, 0, 0, 1, 1, ERANGE},
+	{&sem_id_1, &valid_buf, &sem_op_1, 0, -1, SEM_UNDO, 1, 1, EFBIG},
+	{&sem_id_1, &valid_buf, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1, EFBIG},
+	{&sem_id_1, &valid_buf, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1, EAGAIN},
+	{&sem_id_1, &valid_buf, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0, EAGAIN}
 };
 
 static void setup(void)
@@ -44,7 +57,6 @@ static void setup(void)
 	struct passwd *ltpuser;
 	key_t semkey2;
 	struct seminfo ipc_buf;
-	union semun arr;
 
 	ltpuser = SAFE_GETPWNAM(nobody_uid);
 	SAFE_SETUID(ltpuser->pw_uid);
@@ -65,15 +77,24 @@ static void setup(void)
 	if (semctl(sem_id_1, 0, IPC_INFO, arr) == -1)
 		tst_brk(TBROK | TERRNO, "semctl() IPC_INFO failed");
 
-	arr.val = 1;
-	s_buf[0].sem_op = ipc_buf.semvmx;
-	if (semctl(sem_id_1, 0, SETVAL, arr) == -1)
-		tst_brk(TBROK | TERRNO, "semctl() SETVAL failed");
+	sem_op_dyn = ipc_buf.semvmx;
+	faulty_buf = tst_get_bad_addr(NULL);
 }
 
 static void run(unsigned int i)
 {
-	TEST(semop(*(tc[i].semid), tc[i].t_sbuf, tc[i].t_ops));
+	if (*tc[i].buf != faulty_buf) {
+		arr.val = tc[i].arr_val;
+
+		if (semctl(sem_id_1, tc[i].ctl_sem_num, SETVAL, arr) == -1)
+			tst_brk(TBROK | TERRNO, "semctl() SETVAL failed");
+
+		s_buf[0].sem_op = *tc[i].sem_op;
+		s_buf[0].sem_flg = tc[i].sem_flg;
+		s_buf[0].sem_num = tc[i].sem_num;
+	}
+
+	TEST(semop(*(tc[i].semid), *tc[i].buf, tc[i].t_ops));
 
 	if (TST_RET != -1) {
 		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
diff --git a/testcases/kernel/syscalls/ipc/semop/semop03.c b/testcases/kernel/syscalls/ipc/semop/semop03.c
deleted file mode 100644
index c569033eebc2..000000000000
--- a/testcases/kernel/syscalls/ipc/semop/semop03.c
+++ /dev/null
@@ -1,67 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-or-later
-/*
- * semop03 - test for EFBIG error
- *
- * Copyright (c) International Business Machines  Corp., 2001
- *	03/2001 - Written by Wayne Boyer
- */
-
-#include <sys/sem.h>
-#include "tst_test.h"
-#include "libnewipc.h"
-#include "lapi/semun.h"
-
-static key_t semkey;
-static int sem_id = -1;
-static struct sembuf s_buf;
-
-static int tc[] = { -1, PSEMS + 1 }; /* negative and too many "primitive" semas */
-
-static void run(unsigned int i)
-{
-	s_buf.sem_op = 1;
-	s_buf.sem_flg = SEM_UNDO;
-	s_buf.sem_num = tc[i];
-
-	TEST(semop(sem_id, &s_buf, 1));
-	if (TST_RET != -1) {
-		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
-		return;
-	}
-
-	switch (TST_ERR) {
-	case EFBIG:
-		tst_res(TPASS | TTERRNO, "expected failure");
-		break;
-	default:
-		tst_res(TFAIL | TTERRNO, "unexpected failure");
-		break;
-	}
-}
-
-static void setup(void)
-{
-	semkey = GETIPCKEY();
-
-	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
-	    -1)
-		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
-}
-
-static void cleanup(void)
-{
-	union semun arr;
-
-	if (sem_id != -1) {
-		if (semctl(sem_id, 0, IPC_RMID, arr) == -1)
-			tst_res(TINFO, "WARNING: semaphore deletion failed.");
-	}
-}
-
-static struct tst_test test = {
-	.test = run,
-	.tcnt = ARRAY_SIZE(tc),
-	.setup = setup,
-	.cleanup = cleanup,
-	.needs_tmpdir = 1,
-};
diff --git a/testcases/kernel/syscalls/ipc/semop/semop04.c b/testcases/kernel/syscalls/ipc/semop/semop04.c
deleted file mode 100644
index 1904b177f71b..000000000000
--- a/testcases/kernel/syscalls/ipc/semop/semop04.c
+++ /dev/null
@@ -1,80 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-or-later
-/*
- * semop04 - test for EAGAIN error
- *
- * Copyright (c) International Business Machines  Corp., 2001
- *	03/2001 - Written by Wayne Boyer
- */
-
-#include <sys/sem.h>
-#include "tst_test.h"
-#include "libnewipc.h"
-#include "lapi/semun.h"
-
-static int sem_id = -1;
-static int val;
-
-static key_t semkey;
-static struct sembuf s_buf;
-
-static struct test_case_t {
-	union semun get_arr;
-	short op;
-	short flg;
-	short num;
-	int error;
-} tc[] = {
-	{{1}, 0, IPC_NOWAIT, 2, EAGAIN},
-	{{0}, -1, IPC_NOWAIT, 2, EAGAIN}
-};
-
-static void run(unsigned int i)
-{
-	s_buf.sem_op = tc[i].op;
-	s_buf.sem_flg = tc[i].flg;
-	s_buf.sem_num = tc[i].num;
-
-	tc[i].get_arr.val = val--;
-	if (semctl(sem_id, tc[i].num, SETVAL, tc[i].get_arr) == -1)
-		tst_brk(TBROK | TERRNO, "semctl() failed");
-
-	TEST(semop(sem_id, &s_buf, 1));
-	if (TST_RET != -1) {
-		tst_res(TFAIL, "call succeeded unexpectedly");
-		return;
-	}
-
-	if (TST_ERR == tc[i].error)
-		tst_res(TPASS | TTERRNO, "expected failure");
-	else
-		tst_res(TFAIL | TTERRNO, "unexpected failure");
-}
-
-static void setup(void)
-{
-	val = 1;
-	semkey = GETIPCKEY();
-
-	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
-	     -1) {
-		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
-	}
-}
-
-static void cleanup(void)
-{
-	union semun arr;
-
-	if (sem_id != -1) {
-		if (semctl(sem_id, 0, IPC_RMID, arr) == -1)
-			tst_res(TINFO, "WARNING: semaphore deletion failed.");
-	}
-}
-
-static struct tst_test test = {
-	.test = run,
-	.tcnt = ARRAY_SIZE(tc),
-	.setup = setup,
-	.cleanup = cleanup,
-	.needs_tmpdir = 1,
-};
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V8 4/5] syscalls: semop: Rename semop05.c
  2020-07-29  7:55 [LTP] [PATCH V8 1/5] syscalls/semop: Migrate to new test framework Viresh Kumar
  2020-07-29  7:55 ` [LTP] [PATCH V8 2/5] syscalls: semop: Do code cleanup Viresh Kumar
  2020-07-29  7:55 ` [LTP] [PATCH V8 3/5] syscalls: semop: Merge semop0[234].c files Viresh Kumar
@ 2020-07-29  7:55 ` Viresh Kumar
  2020-08-07 13:10   ` Cyril Hrubis
  2020-07-29  7:55 ` [LTP] [PATCH V8 5/5] syscalls/semtimedop: Add support for semtimedop and its time64 version Viresh Kumar
  3 siblings, 1 reply; 13+ messages in thread
From: Viresh Kumar @ 2020-07-29  7:55 UTC (permalink / raw)
  To: ltp

Rename it to semop03.c.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 runtest/syscalls                                             | 2 +-
 runtest/syscalls-ipc                                         | 2 +-
 testcases/kernel/syscalls/ipc/semop/.gitignore               | 2 +-
 testcases/kernel/syscalls/ipc/semop/{semop05.c => semop03.c} | 0
 4 files changed, 3 insertions(+), 3 deletions(-)
 rename testcases/kernel/syscalls/ipc/semop/{semop05.c => semop03.c} (100%)

diff --git a/runtest/syscalls b/runtest/syscalls
index 99831f6aa36e..7bbf3a76611f 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1169,7 +1169,7 @@ semget06 semget06
 
 semop01 semop01
 semop02 semop02
-semop05 semop05
+semop03 semop03
 
 send01 send01
 
diff --git a/runtest/syscalls-ipc b/runtest/syscalls-ipc
index 1a63071b22cf..690ccb214b15 100644
--- a/runtest/syscalls-ipc
+++ b/runtest/syscalls-ipc
@@ -44,7 +44,7 @@ semget06 semget06
 
 semop01 semop01
 semop02 semop02
-semop05 semop05
+semop03 semop03
 
 shmat01 shmat01
 shmat02 shmat02
diff --git a/testcases/kernel/syscalls/ipc/semop/.gitignore b/testcases/kernel/syscalls/ipc/semop/.gitignore
index 35b7af66c465..bb57f08afda8 100644
--- a/testcases/kernel/syscalls/ipc/semop/.gitignore
+++ b/testcases/kernel/syscalls/ipc/semop/.gitignore
@@ -1,3 +1,3 @@
 /semop01
 /semop02
-/semop05
+/semop03
diff --git a/testcases/kernel/syscalls/ipc/semop/semop05.c b/testcases/kernel/syscalls/ipc/semop/semop03.c
similarity index 100%
rename from testcases/kernel/syscalls/ipc/semop/semop05.c
rename to testcases/kernel/syscalls/ipc/semop/semop03.c
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V8 5/5] syscalls/semtimedop: Add support for semtimedop and its time64 version
  2020-07-29  7:55 [LTP] [PATCH V8 1/5] syscalls/semop: Migrate to new test framework Viresh Kumar
                   ` (2 preceding siblings ...)
  2020-07-29  7:55 ` [LTP] [PATCH V8 4/5] syscalls: semop: Rename semop05.c Viresh Kumar
@ 2020-07-29  7:55 ` Viresh Kumar
  2020-08-07 13:27   ` Cyril Hrubis
  3 siblings, 1 reply; 13+ messages in thread
From: Viresh Kumar @ 2020-07-29  7:55 UTC (permalink / raw)
  To: ltp

This adds support for semtimedop() and its time64 variant to the
existing semop() syscall tests.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 testcases/kernel/syscalls/ipc/semop/semop.h   | 55 +++++++++++++++++++++++++++
 testcases/kernel/syscalls/ipc/semop/semop01.c | 14 ++++++-
 testcases/kernel/syscalls/ipc/semop/semop02.c | 15 +++++++-
 testcases/kernel/syscalls/ipc/semop/semop03.c | 16 +++++++-
 4 files changed, 97 insertions(+), 3 deletions(-)
 create mode 100644 testcases/kernel/syscalls/ipc/semop/semop.h

diff --git a/testcases/kernel/syscalls/ipc/semop/semop.h b/testcases/kernel/syscalls/ipc/semop/semop.h
new file mode 100644
index 000000000000..584d12c68e0d
--- /dev/null
+++ b/testcases/kernel/syscalls/ipc/semop/semop.h
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#ifndef SEMOP_VAR__
+#define SEMOP_VAR__
+
+#include <sys/sem.h>
+#include "tst_timer.h"
+
+static inline int sys_semtimedop(int semid, struct sembuf *sops, size_t nsops,
+		void *timeout)
+{
+	return tst_syscall(__NR_semtimedop, semid, sops, nsops, timeout);
+}
+
+static inline int sys_semtimedop_time64(int semid, struct sembuf *sops,
+					size_t nsops, void *timeout)
+{
+	return tst_syscall(__NR_semtimedop_time64, semid, sops, nsops, timeout);
+}
+
+struct test_variants {
+	int (*semop)(int semid, struct sembuf *sops, size_t nsops);
+	int (*semtimedop)(int semid, struct sembuf *sops, size_t nsops, void *timeout);
+	enum tst_ts_type type;
+	char *desc;
+} variants[] = {
+	{ .semop = semop, .type = TST_LIBC_TIMESPEC, .desc = "semop: vDSO or syscall"},
+
+#if (__NR_semtimedop != __LTP__NR_INVALID_SYSCALL)
+	{ .semtimedop = sys_semtimedop, .type = TST_KERN_OLD_TIMESPEC, .desc = "semtimedop: syscall with old kernel spec"},
+#endif
+
+#if (__NR_semtimedop_time64 != __LTP__NR_INVALID_SYSCALL)
+	{ .semtimedop = sys_semtimedop_time64, .type = TST_KERN_TIMESPEC, .desc = "semtimedop: syscall time64 with kernel spec"},
+#endif
+};
+
+static inline int call_semop(struct test_variants *tv, int semid,
+		struct sembuf *sops, size_t nsops, struct tst_ts *timeout)
+{
+	if (tv->semop)
+		return tv->semop(semid, sops, nsops);
+
+	return tv->semtimedop(semid, sops, nsops, tst_ts_get(timeout));
+}
+
+static inline void semop_supported_by_kernel(struct test_variants *tv)
+{
+       /* Check if the syscall is implemented on the platform */
+       TEST(call_semop(tv, 0, NULL, 0, NULL));
+       if (TST_RET == -1 && TST_ERR == ENOSYS)
+               tst_brk(TCONF, "Test not supported on kernel/platform");
+}
+
+#endif /* SEMOP_VAR__ */
diff --git a/testcases/kernel/syscalls/ipc/semop/semop01.c b/testcases/kernel/syscalls/ipc/semop/semop01.c
index 19607ee6d653..c3443da58af4 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop01.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop01.c
@@ -12,6 +12,7 @@
 #include "tst_test.h"
 #include "libnewipc.h"
 #include "lapi/semun.h"
+#include "semop.h"
 
 #define NSEMS	4		/* the number of primitive semaphores to test */
 
@@ -23,11 +24,17 @@ static struct sembuf sops[PSEMS];
 
 static void run(void)
 {
+	struct test_variants *tv = &variants[tst_variant];
 	union semun arr = { .val = 0 };
+	struct tst_ts timeout;
 	int fail = 0;
 	int i;
 
-	TEST(semop(sem_id, sops, NSEMS));
+	timeout.type = tv->type;
+	tst_ts_set_sec(&timeout, 1);
+	tst_ts_set_nsec(&timeout, 10000);
+
+	TEST(call_semop(tv, sem_id, sops, NSEMS, &timeout));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "semop() failed");
 	} else {
@@ -54,8 +61,12 @@ static void run(void)
 
 static void setup(void)
 {
+	struct test_variants *tv = &variants[tst_variant];
 	int i;
 
+	tst_res(TINFO, "Testing variant: %s", tv->desc);
+	semop_supported_by_kernel(tv);
+
 	get_arr.array = malloc(sizeof(unsigned short int) * PSEMS);
 	if (get_arr.array == NULL)
 		tst_brk(TBROK, "malloc failed");
@@ -86,6 +97,7 @@ static void cleanup(void)
 
 static struct tst_test test = {
 	.test_all = run,
+	.test_variants = ARRAY_SIZE(variants),
 	.setup = setup,
 	.cleanup = cleanup,
 	.needs_tmpdir = 1,
diff --git a/testcases/kernel/syscalls/ipc/semop/semop02.c b/testcases/kernel/syscalls/ipc/semop/semop02.c
index 7a49b2648b2b..1b9b768f5e63 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop02.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop02.c
@@ -14,6 +14,7 @@
 #include "tst_test.h"
 #include "libnewipc.h"
 #include "lapi/semun.h"
+#include "semop.h"
 
 static int sem_id_1 = -1;	/* a semaphore set with read & alter permissions */
 static int sem_id_2 = -1;	/* a semaphore set without read & alter permissions */
@@ -53,11 +54,15 @@ static struct test_case_t {
 
 static void setup(void)
 {
+	struct test_variants *tv = &variants[tst_variant];
 	char nobody_uid[] = "nobody";
 	struct passwd *ltpuser;
 	key_t semkey2;
 	struct seminfo ipc_buf;
 
+	tst_res(TINFO, "Testing variant: %s", tv->desc);
+	semop_supported_by_kernel(tv);
+
 	ltpuser = SAFE_GETPWNAM(nobody_uid);
 	SAFE_SETUID(ltpuser->pw_uid);
 
@@ -83,6 +88,13 @@ static void setup(void)
 
 static void run(unsigned int i)
 {
+	struct test_variants *tv = &variants[tst_variant];
+	struct tst_ts timeout;
+
+	timeout.type = tv->type;
+	tst_ts_set_sec(&timeout, 1);
+	tst_ts_set_nsec(&timeout, 10000);
+
 	if (*tc[i].buf != faulty_buf) {
 		arr.val = tc[i].arr_val;
 
@@ -94,7 +106,7 @@ static void run(unsigned int i)
 		s_buf[0].sem_num = tc[i].sem_num;
 	}
 
-	TEST(semop(*(tc[i].semid), *tc[i].buf, tc[i].t_ops));
+	TEST(call_semop(tv, *(tc[i].semid), *tc[i].buf, tc[i].t_ops, &timeout));
 
 	if (TST_RET != -1) {
 		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
@@ -127,6 +139,7 @@ static void cleanup(void)
 static struct tst_test test = {
 	.test = run,
 	.tcnt = ARRAY_SIZE(tc),
+	.test_variants = ARRAY_SIZE(variants),
 	.setup = setup,
 	.cleanup = cleanup,
 	.needs_tmpdir = 1,
diff --git a/testcases/kernel/syscalls/ipc/semop/semop03.c b/testcases/kernel/syscalls/ipc/semop/semop03.c
index df8ce7d16ee5..d0fea1ed1dd5 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop03.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop03.c
@@ -15,6 +15,7 @@
 #include "tst_test.h"
 #include "libnewipc.h"
 #include "lapi/semun.h"
+#include "semop.h"
 
 static key_t semkey;
 static int sem_id = -1;
@@ -64,7 +65,14 @@ static inline int process_state_wait2(pid_t pid, const char state)
 
 static void do_child(int i)
 {
-	TEST(semop(sem_id, &s_buf, 1));
+	struct test_variants *tv = &variants[tst_variant];
+	struct tst_ts timeout;
+
+	timeout.type = tv->type;
+	tst_ts_set_sec(&timeout, 1);
+	tst_ts_set_nsec(&timeout, 10000);
+
+	TEST(call_semop(tv, sem_id, &s_buf, 1, &timeout));
 	if (TST_RET != -1) {
 		tst_res(TFAIL, "call succeeded when error expected");
 		exit(-1);
@@ -86,6 +94,11 @@ static void sighandler(int sig)
 
 static void setup(void)
 {
+	struct test_variants *tv = &variants[tst_variant];
+
+	tst_res(TINFO, "Testing variant: %s", tv->desc);
+	semop_supported_by_kernel(tv);
+
 	SAFE_SIGNAL(SIGHUP, sighandler);
 	semkey = GETIPCKEY();
 
@@ -149,6 +162,7 @@ static void run(unsigned int i)
 static struct tst_test test = {
 	.test = run,
 	.tcnt = ARRAY_SIZE(tc),
+	.test_variants = ARRAY_SIZE(variants),
 	.setup = setup,
 	.cleanup = cleanup,
 	.needs_tmpdir = 1,
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V8 4/5] syscalls: semop: Rename semop05.c
  2020-07-29  7:55 ` [LTP] [PATCH V8 4/5] syscalls: semop: Rename semop05.c Viresh Kumar
@ 2020-08-07 13:10   ` Cyril Hrubis
  0 siblings, 0 replies; 13+ messages in thread
From: Cyril Hrubis @ 2020-08-07 13:10 UTC (permalink / raw)
  To: ltp

Hi!
I've merget all these changes into a single patch and added bunch of
cleanups on the top. My diff against your patchset follows. Thanks a lot
for the cleanups.

diff --git a/testcases/kernel/syscalls/ipc/semop/semop01.c b/testcases/kernel/syscalls/ipc/semop/semop01.c
index 19607ee6d..2daf6bbc5 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop01.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop01.c
@@ -13,12 +13,13 @@
 #include "libnewipc.h"
 #include "lapi/semun.h"
 
-#define NSEMS	4		/* the number of primitive semaphores to test */
+#define NSEMS 4
 
-static int sem_id = -1;		/* a semaphore set with read & alter permissions */
+static int sem_id = -1;
 static key_t semkey;
 
-static union semun get_arr;
+static unsigned short int sarr[PSEMS];
+static union semun get_arr = {.array = sarr};
 static struct sembuf sops[PSEMS];
 
 static void run(void)
@@ -30,25 +31,26 @@ static void run(void)
 	TEST(semop(sem_id, sops, NSEMS));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "semop() failed");
-	} else {
-		if (semctl(sem_id, 0, GETALL, get_arr) == -1) {
-			tst_brk(TBROK | TERRNO, "semctl() failed in functional test");
-		}
+		return;
+	}
 
-		for (i = 0; i < NSEMS; i++) {
-			if (get_arr.array[i] != i * i) {
-				fail = 1;
-			}
+	if (semctl(sem_id, 0, GETALL, get_arr) == -1)
+		tst_brk(TBROK | TERRNO, "semctl(%i, 0, GETALL, ...)", sem_id);
+
+	for (i = 0; i < NSEMS; i++) {
+		if (get_arr.array[i] != i * i) {
+			fail = 1;
 		}
-		if (fail)
-			tst_res(TFAIL | TERRNO, "semaphore values are wrong");
-		else
-			tst_res(TPASS, "semaphore values are correct");
 	}
 
+	if (fail)
+		tst_res(TFAIL, "semaphore values are wrong");
+	else
+		tst_res(TPASS, "semaphore values are correct");
+
 	for (i = 0; i < NSEMS; i++) {
 		if (semctl(sem_id, i, SETVAL, arr) == -1)
-			tst_brk(TBROK | TERRNO, "semctl failed");
+			tst_brk(TBROK | TERRNO, "semctl(%i, %i, SETVAL, ...)", sem_id, i);
 	}
 }
 
@@ -56,10 +58,6 @@ static void setup(void)
 {
 	int i;
 
-	get_arr.array = malloc(sizeof(unsigned short int) * PSEMS);
-	if (get_arr.array == NULL)
-		tst_brk(TBROK, "malloc failed");
-
 	semkey = GETIPCKEY();
 
 	sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
@@ -75,13 +73,10 @@ static void setup(void)
 
 static void cleanup(void)
 {
-	union semun arr;
-
 	if (sem_id != -1) {
-		if (semctl(sem_id, 0, IPC_RMID, arr) == -1)
-			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+		if (semctl(sem_id, 0, IPC_RMID) == -1)
+			tst_res(TWARN, "semaphore deletion failed.");
 	}
-	free(get_arr.array);
 }
 
 static struct tst_test test = {
diff --git a/testcases/kernel/syscalls/ipc/semop/semop02.c b/testcases/kernel/syscalls/ipc/semop/semop02.c
index 7a49b2648..0dff8e537 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop02.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop02.c
@@ -1,6 +1,15 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
- * semop02 - test for E2BIG, EACCES, EFAULT, EINVAL and ERANGE errors
+ * E2BIG   - nsops is greater than max number of operations allowed per syscall
+ * EACCESS - calling process does not have permission to alter semaphore
+ * EFAULT  - invalid address passed for sops
+ * EINVAL  - nonpositive nsops
+ * EINVAL  - negative semid
+ * ERANGE  - semop + semval > semvmx a maximal semaphore value
+ * EFBIG   - sem_num less than zero
+ * EFBIG   - sem_num > number of semaphores in a set
+ * EAGAIN  - semop = 0 for non-zero semaphore and IPC_NOWAIT passed in flags
+ * EAGAIN  - semop = -1 for zero semaphore and IPC_NOWAIT passed in flags
  *
  * Copyright (c) International Business Machines  Corp., 2001
  *	03/2001 - Written by Wayne Boyer
@@ -15,86 +24,87 @@
 #include "libnewipc.h"
 #include "lapi/semun.h"
 
-static int sem_id_1 = -1;	/* a semaphore set with read & alter permissions */
-static int sem_id_2 = -1;	/* a semaphore set without read & alter permissions */
-static int bad_id = -1;
-static short sem_op_dyn, sem_op_1 = 1, sem_op_negative = -1, sem_op_zero = 0;
-static key_t semkey;
-static struct sembuf s_buf[PSEMS];
+static int valid_sem_id = -1;
+static int noperm_sem_id = -1;
+static int bad_sem_id = -1;
+static short sem_op_max, sem_op_1 = 1, sem_op_negative = -1, sem_op_zero = 0;
 static struct sembuf *faulty_buf;
-static struct sembuf *valid_buf = s_buf;
-static union semun arr;
 
-#define NSOPS	5		/* a reasonable number of operations */
-#define	BIGOPS	1024		/* a value that is too large for the number */
-				/* of semop operations that are permitted */
+#define NSOPS	1
+#define	BIGOPS	1024
+
 static struct test_case_t {
 	int *semid;
 	struct sembuf **buf;
-	short *sem_op; /* semaphore operation */
+	short *sem_op;
 	unsigned short ctl_sem_num;
-	unsigned short sem_num; /* semaphore index in array */
-	short sem_flg; /* operation flags */
+	unsigned short sem_num;
+	short sem_flg;
 	unsigned t_ops;
 	int arr_val;
 	int error;
 } tc[] = {
-	{&sem_id_1, &valid_buf, &sem_op_dyn, 0, 0, 0, BIGOPS, 1, E2BIG},
-	{&sem_id_2, &valid_buf, &sem_op_dyn, 0, 0, 0, NSOPS, 1, EACCES},
-	{&sem_id_1, &faulty_buf, &sem_op_dyn, 0, 0, 0, NSOPS, 1, EFAULT},
-	{&sem_id_1, &valid_buf, &sem_op_dyn, 0, 0, 0, 0, 1, EINVAL},
-	{&bad_id, &valid_buf, &sem_op_dyn, 0, 0, 0, NSOPS, 1, EINVAL},
-	{&sem_id_1, &valid_buf, &sem_op_dyn, 0, 0, 0, 1, 1, ERANGE},
-	{&sem_id_1, &valid_buf, &sem_op_1, 0, -1, SEM_UNDO, 1, 1, EFBIG},
-	{&sem_id_1, &valid_buf, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1, EFBIG},
-	{&sem_id_1, &valid_buf, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1, EAGAIN},
-	{&sem_id_1, &valid_buf, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0, EAGAIN}
+	{&valid_sem_id, NULL, &sem_op_1, 0, 0, 0, BIGOPS, 1, E2BIG},
+	{&noperm_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EACCES},
+	{&valid_sem_id, &faulty_buf, &sem_op_1, 0, 0, 0, NSOPS, 1, EFAULT},
+	{&valid_sem_id, NULL, &sem_op_1, 0, 0, 0, 0, 1, EINVAL},
+	{&bad_sem_id,   NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EINVAL},
+	{&valid_sem_id, NULL, &sem_op_max, 0, 0, 0, 1, 1, ERANGE},
+	{&valid_sem_id, NULL, &sem_op_1, 0, -1, SEM_UNDO, 1, 1, EFBIG},
+	{&valid_sem_id, NULL, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1, EFBIG},
+	{&valid_sem_id, NULL, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1, EAGAIN},
+	{&valid_sem_id, NULL, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0, EAGAIN}
 };
 
 static void setup(void)
 {
-	char nobody_uid[] = "nobody";
 	struct passwd *ltpuser;
-	key_t semkey2;
+	key_t semkey;
+	union semun arr;
 	struct seminfo ipc_buf;
 
-	ltpuser = SAFE_GETPWNAM(nobody_uid);
+	ltpuser = SAFE_GETPWNAM("nobody");
 	SAFE_SETUID(ltpuser->pw_uid);
 
 	semkey = GETIPCKEY();
 
-	sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
-	if (sem_id_1 == -1)
+	valid_sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
+	if (valid_sem_id == -1)
 		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 
-	semkey2 = GETIPCKEY();
+	semkey = GETIPCKEY();
 
-	sem_id_2 = semget(semkey2, PSEMS, IPC_CREAT | IPC_EXCL);
-	if (sem_id_2 == -1)
+	noperm_sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL);
+	if (noperm_sem_id == -1)
 		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 
 	arr.__buf = &ipc_buf;
-	if (semctl(sem_id_1, 0, IPC_INFO, arr) == -1)
+	if (semctl(valid_sem_id, 0, IPC_INFO, arr) == -1)
 		tst_brk(TBROK | TERRNO, "semctl() IPC_INFO failed");
 
-	sem_op_dyn = ipc_buf.semvmx;
+	sem_op_max = ipc_buf.semvmx;
 	faulty_buf = tst_get_bad_addr(NULL);
 }
 
 static void run(unsigned int i)
 {
-	if (*tc[i].buf != faulty_buf) {
-		arr.val = tc[i].arr_val;
-
-		if (semctl(sem_id_1, tc[i].ctl_sem_num, SETVAL, arr) == -1)
+	union semun arr = {.val = tc[i].arr_val};
+	struct sembuf buf = {
+		.sem_op = *tc[i].sem_op,
+		.sem_flg = tc[i].sem_flg,
+		.sem_num = tc[i].sem_num,
+	};
+	struct sembuf *ptr = &buf;
+
+	if (*tc[i].semid == valid_sem_id) {
+		if (semctl(valid_sem_id, tc[i].ctl_sem_num, SETVAL, arr) == -1)
 			tst_brk(TBROK | TERRNO, "semctl() SETVAL failed");
-
-		s_buf[0].sem_op = *tc[i].sem_op;
-		s_buf[0].sem_flg = tc[i].sem_flg;
-		s_buf[0].sem_num = tc[i].sem_num;
 	}
 
-	TEST(semop(*(tc[i].semid), *tc[i].buf, tc[i].t_ops));
+	if (tc[i].buf)
+		ptr = *tc[i].buf;
+
+	TEST(semop(*(tc[i].semid), ptr, tc[i].t_ops));
 
 	if (TST_RET != -1) {
 		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
@@ -105,22 +115,21 @@ static void run(unsigned int i)
 		tst_res(TPASS | TTERRNO, "semop failed as expected");
 	} else {
 		tst_res(TFAIL | TTERRNO,
-			 "semop failed unexpectedly; expected: "
-			 "%d - %s", tc[i].error, strerror(tc[i].error));
+		        "semop failed unexpectedly; expected: %s",
+		        tst_strerrno(tc[i].error));
 	}
 }
 
 static void cleanup(void)
 {
-	union semun arr;
-
-	if (sem_id_1 != -1) {
-		if (semctl(sem_id_1, 0, IPC_RMID, arr) == -1)
-			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+	if (valid_sem_id != -1) {
+		if (semctl(valid_sem_id, 0, IPC_RMID) == -1)
+			tst_res(TWARN, "semaphore deletion failed.");
 	}
-	if (sem_id_2 != -1) {
-		if (semctl(sem_id_2, 0, IPC_RMID, arr) == -1)
-			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+
+	if (noperm_sem_id != -1) {
+		if (semctl(noperm_sem_id, 0, IPC_RMID) == -1)
+			tst_res(TWARN, "semaphore deletion failed.");
 	}
 }
 
diff --git a/testcases/kernel/syscalls/ipc/semop/semop03.c b/testcases/kernel/syscalls/ipc/semop/semop03.c
index df8ce7d16..25123f08c 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop03.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop03.c
@@ -18,7 +18,6 @@
 
 static key_t semkey;
 static int sem_id = -1;
-static struct sembuf s_buf;
 
 struct test_case_t {
 	union semun semunptr;
@@ -33,41 +32,18 @@ struct test_case_t {
 	{{0}, -1, 0, 5, EINTR}
 };
 
-static inline int process_state_wait2(pid_t pid, const char state)
-{
-	char proc_path[128], cur_state;
-
-	snprintf(proc_path, sizeof(proc_path), "/proc/%i/stat", pid);
-
-	for (;;) {
-		FILE *f = fopen(proc_path, "r");
-		if (!f) {
-			tst_res(TFAIL, "Failed to open '%s': %s\n", proc_path,
-				strerror(errno));
-			return 1;
-		}
-
-		if (fscanf(f, "%*i %*s %c", &cur_state) != 1) {
-			fclose(f);
-			tst_res(TFAIL, "Failed to read '%s': %s\n", proc_path,
-				strerror(errno));
-			return 1;
-		}
-		fclose(f);
-
-		if (state == cur_state)
-			return 0;
-
-		usleep(10000);
-	}
-}
-
 static void do_child(int i)
 {
+	struct sembuf s_buf = {
+		.sem_op = tc[i].op,
+		.sem_flg = tc[i].flg,
+		.sem_num = tc[i].num,
+	};
+
 	TEST(semop(sem_id, &s_buf, 1));
 	if (TST_RET != -1) {
 		tst_res(TFAIL, "call succeeded when error expected");
-		exit(-1);
+		exit(0);
 	}
 
 	if (TST_ERR == tc[i].error)
@@ -89,18 +65,16 @@ static void setup(void)
 	SAFE_SIGNAL(SIGHUP, sighandler);
 	semkey = GETIPCKEY();
 
-	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
-	    -1)
+	sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
+	if (sem_id == -1)
 		tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup");
 }
 
 static void cleanup(void)
 {
-	union semun arr;
-
 	if (sem_id != -1) {
-		if (semctl(sem_id, 0, IPC_RMID, arr) == -1)
-			tst_res(TINFO, "WARNING: semaphore deletion failed.");
+		if (semctl(sem_id, 0, IPC_RMID) == -1)
+			tst_res(TWARN, "semaphore deletion failed.");
 	}
 }
 
@@ -108,10 +82,6 @@ static void run(unsigned int i)
 {
 	pid_t pid;
 
-	s_buf.sem_op = tc[i].op;
-	s_buf.sem_flg = tc[i].flg;
-	s_buf.sem_num = tc[i].num;
-
 	if (semctl(sem_id, tc[i].num, SETVAL, tc[i].semunptr) == -1)
 		tst_brk(TBROK | TERRNO, "semctl() failed");
 
@@ -120,7 +90,7 @@ static void run(unsigned int i)
 	if (pid == 0) {
 		do_child(i);
 	} else {
-		process_state_wait2(pid, 'S');
+		TST_PROCESS_STATE_WAIT(pid, 'S', 0);
 
 		/*
 		 * If we are testing for EIDRM then remove
@@ -141,8 +111,8 @@ static void run(unsigned int i)
 	if (tc[i].error == EINTR)
 		return;
 
-	if ((sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) ==
-	    -1)
+	sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
+	if (sem_id == -1)
 		tst_brk(TBROK | TERRNO, "couldn't recreate semaphore");
 }
 

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V8 5/5] syscalls/semtimedop: Add support for semtimedop and its time64 version
  2020-07-29  7:55 ` [LTP] [PATCH V8 5/5] syscalls/semtimedop: Add support for semtimedop and its time64 version Viresh Kumar
@ 2020-08-07 13:27   ` Cyril Hrubis
  2020-08-19 12:27     ` [LTP] [PATCH V9 1/2] " Viresh Kumar
  0 siblings, 1 reply; 13+ messages in thread
From: Cyril Hrubis @ 2020-08-07 13:27 UTC (permalink / raw)
  To: ltp

Hi!
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ipc/semop/semop.h
> @@ -0,0 +1,55 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +#ifndef SEMOP_VAR__
> +#define SEMOP_VAR__
> +
> +#include <sys/sem.h>
> +#include "tst_timer.h"
> +
> +static inline int sys_semtimedop(int semid, struct sembuf *sops, size_t nsops,
> +		void *timeout)
> +{
> +	return tst_syscall(__NR_semtimedop, semid, sops, nsops, timeout);
> +}
> +
> +static inline int sys_semtimedop_time64(int semid, struct sembuf *sops,
> +					size_t nsops, void *timeout)
> +{
> +	return tst_syscall(__NR_semtimedop_time64, semid, sops, nsops, timeout);
> +}
> +
> +struct test_variants {
> +	int (*semop)(int semid, struct sembuf *sops, size_t nsops);
> +	int (*semtimedop)(int semid, struct sembuf *sops, size_t nsops, void *timeout);
> +	enum tst_ts_type type;
> +	char *desc;
> +} variants[] = {
> +	{ .semop = semop, .type = TST_LIBC_TIMESPEC, .desc = "semop: vDSO or syscall"},

I'm pretty sure semop will never be vDSO :-).

> +#if (__NR_semtimedop != __LTP__NR_INVALID_SYSCALL)
> +	{ .semtimedop = sys_semtimedop, .type = TST_KERN_OLD_TIMESPEC, .desc = "semtimedop: syscall with old kernel spec"},
> +#endif
> +
> +#if (__NR_semtimedop_time64 != __LTP__NR_INVALID_SYSCALL)
> +	{ .semtimedop = sys_semtimedop_time64, .type = TST_KERN_TIMESPEC, .desc = "semtimedop: syscall time64 with kernel spec"},
> +#endif
> +};
> +
> +static inline int call_semop(struct test_variants *tv, int semid,
> +		struct sembuf *sops, size_t nsops, struct tst_ts *timeout)
> +{
> +	if (tv->semop)
> +		return tv->semop(semid, sops, nsops);
> +
> +	return tv->semtimedop(semid, sops, nsops, tst_ts_get(timeout));
> +}
> +
> +static inline void semop_supported_by_kernel(struct test_variants *tv)
> +{
> +       /* Check if the syscall is implemented on the platform */
> +       TEST(call_semop(tv, 0, NULL, 0, NULL));
> +       if (TST_RET == -1 && TST_ERR == ENOSYS)
> +               tst_brk(TCONF, "Test not supported on kernel/platform");
> +}
> +
> +#endif /* SEMOP_VAR__ */
> diff --git a/testcases/kernel/syscalls/ipc/semop/semop01.c b/testcases/kernel/syscalls/ipc/semop/semop01.c
> index 19607ee6d653..c3443da58af4 100644
> --- a/testcases/kernel/syscalls/ipc/semop/semop01.c
> +++ b/testcases/kernel/syscalls/ipc/semop/semop01.c
> @@ -12,6 +12,7 @@
>  #include "tst_test.h"
>  #include "libnewipc.h"
>  #include "lapi/semun.h"
> +#include "semop.h"
>  
>  #define NSEMS	4		/* the number of primitive semaphores to test */
>  
> @@ -23,11 +24,17 @@ static struct sembuf sops[PSEMS];
>  
>  static void run(void)
>  {
> +	struct test_variants *tv = &variants[tst_variant];
>  	union semun arr = { .val = 0 };
> +	struct tst_ts timeout;
>  	int fail = 0;
>  	int i;
>  
> -	TEST(semop(sem_id, sops, NSEMS));
> +	timeout.type = tv->type;
> +	tst_ts_set_sec(&timeout, 1);
> +	tst_ts_set_nsec(&timeout, 10000);

Can't we just set the timeout once in the test setup?


Other than these we really lack semtimedop testcases that really utilize
the timeout.

We need at least:

* Tests with NULL timeout

* A test that we get EAGAIN if semtimedop() timeouted either with:
  - semop == 0 and non-zero semaphore
  - semop < 0 and semaphore less than abs value of semop

* An EFAULT test for semtimedop() with bad address for timeout

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V9 1/2] syscalls/semtimedop: Add support for semtimedop and its time64 version
  2020-08-07 13:27   ` Cyril Hrubis
@ 2020-08-19 12:27     ` Viresh Kumar
  2020-08-19 12:27       ` [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer Viresh Kumar
  2020-08-31 11:14       ` [LTP] [PATCH V9 1/2] syscalls/semtimedop: Add support for semtimedop and its time64 version Viresh Kumar
  0 siblings, 2 replies; 13+ messages in thread
From: Viresh Kumar @ 2020-08-19 12:27 UTC (permalink / raw)
  To: ltp

This adds support for semtimedop() and its time64 variant to the
existing semop() syscall tests.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
V9:
- Removed vDSO from comment
- Add additional failure tests for EGAIN errors
- Moved timeout configuration change to setup()
- Test added for NULL timeout

 testcases/kernel/syscalls/ipc/semop/semop.h   | 55 +++++++++++++++++++
 testcases/kernel/syscalls/ipc/semop/semop01.c | 26 ++++++++-
 testcases/kernel/syscalls/ipc/semop/semop02.c | 42 ++++++++++----
 testcases/kernel/syscalls/ipc/semop/semop03.c | 15 ++++-
 4 files changed, 123 insertions(+), 15 deletions(-)
 create mode 100644 testcases/kernel/syscalls/ipc/semop/semop.h

diff --git a/testcases/kernel/syscalls/ipc/semop/semop.h b/testcases/kernel/syscalls/ipc/semop/semop.h
new file mode 100644
index 000000000000..e920d3bc6d5f
--- /dev/null
+++ b/testcases/kernel/syscalls/ipc/semop/semop.h
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#ifndef SEMOP_VAR__
+#define SEMOP_VAR__
+
+#include <sys/sem.h>
+#include "tst_timer.h"
+
+static inline int sys_semtimedop(int semid, struct sembuf *sops, size_t nsops,
+		void *timeout)
+{
+	return tst_syscall(__NR_semtimedop, semid, sops, nsops, timeout);
+}
+
+static inline int sys_semtimedop_time64(int semid, struct sembuf *sops,
+					size_t nsops, void *timeout)
+{
+	return tst_syscall(__NR_semtimedop_time64, semid, sops, nsops, timeout);
+}
+
+struct test_variants {
+	int (*semop)(int semid, struct sembuf *sops, size_t nsops);
+	int (*semtimedop)(int semid, struct sembuf *sops, size_t nsops, void *timeout);
+	enum tst_ts_type type;
+	char *desc;
+} variants[] = {
+	{ .semop = semop, .type = TST_LIBC_TIMESPEC, .desc = "semop: syscall"},
+
+#if (__NR_semtimedop != __LTP__NR_INVALID_SYSCALL)
+	{ .semtimedop = sys_semtimedop, .type = TST_KERN_OLD_TIMESPEC, .desc = "semtimedop: syscall with old kernel spec"},
+#endif
+
+#if (__NR_semtimedop_time64 != __LTP__NR_INVALID_SYSCALL)
+	{ .semtimedop = sys_semtimedop_time64, .type = TST_KERN_TIMESPEC, .desc = "semtimedop: syscall time64 with kernel spec"},
+#endif
+};
+
+static inline int call_semop(struct test_variants *tv, int semid,
+		struct sembuf *sops, size_t nsops, struct tst_ts *timeout)
+{
+	if (tv->semop)
+		return tv->semop(semid, sops, nsops);
+
+	return tv->semtimedop(semid, sops, nsops, tst_ts_get(timeout));
+}
+
+static inline void semop_supported_by_kernel(struct test_variants *tv)
+{
+       /* Check if the syscall is implemented on the platform */
+       TEST(call_semop(tv, 0, NULL, 0, NULL));
+       if (TST_RET == -1 && TST_ERR == ENOSYS)
+               tst_brk(TCONF, "Test not supported on kernel/platform");
+}
+
+#endif /* SEMOP_VAR__ */
diff --git a/testcases/kernel/syscalls/ipc/semop/semop01.c b/testcases/kernel/syscalls/ipc/semop/semop01.c
index 2daf6bbc5c4e..9e71a2744517 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop01.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop01.c
@@ -12,6 +12,7 @@
 #include "tst_test.h"
 #include "libnewipc.h"
 #include "lapi/semun.h"
+#include "semop.h"
 
 #define NSEMS 4
 
@@ -21,14 +22,23 @@ static key_t semkey;
 static unsigned short int sarr[PSEMS];
 static union semun get_arr = {.array = sarr};
 static struct sembuf sops[PSEMS];
+static struct tst_ts timeout;
 
-static void run(void)
+static struct test_case_t {
+	struct tst_ts *to;
+} tc[] = {
+	{NULL},
+	{&timeout}
+};
+
+static void run(unsigned int n)
 {
+	struct test_variants *tv = &variants[tst_variant];
 	union semun arr = { .val = 0 };
 	int fail = 0;
 	int i;
 
-	TEST(semop(sem_id, sops, NSEMS));
+	TEST(call_semop(tv, sem_id, sops, NSEMS, tc[n].to));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "semop() failed");
 		return;
@@ -56,8 +66,16 @@ static void run(void)
 
 static void setup(void)
 {
+	struct test_variants *tv = &variants[tst_variant];
 	int i;
 
+	tst_res(TINFO, "Testing variant: %s", tv->desc);
+	semop_supported_by_kernel(tv);
+
+	timeout.type = tv->type;
+	tst_ts_set_sec(&timeout, 1);
+	tst_ts_set_nsec(&timeout, 10000);
+
 	semkey = GETIPCKEY();
 
 	sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
@@ -80,7 +98,9 @@ static void cleanup(void)
 }
 
 static struct tst_test test = {
-	.test_all = run,
+	.test = run,
+	.tcnt = ARRAY_SIZE(tc),
+	.test_variants = ARRAY_SIZE(variants),
 	.setup = setup,
 	.cleanup = cleanup,
 	.needs_tmpdir = 1,
diff --git a/testcases/kernel/syscalls/ipc/semop/semop02.c b/testcases/kernel/syscalls/ipc/semop/semop02.c
index 0dff8e537719..d0fed5321502 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop02.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop02.c
@@ -23,17 +23,20 @@
 #include "tst_test.h"
 #include "libnewipc.h"
 #include "lapi/semun.h"
+#include "semop.h"
 
 static int valid_sem_id = -1;
 static int noperm_sem_id = -1;
 static int bad_sem_id = -1;
 static short sem_op_max, sem_op_1 = 1, sem_op_negative = -1, sem_op_zero = 0;
 static struct sembuf *faulty_buf;
+static struct tst_ts timeout;
 
 #define NSOPS	1
 #define	BIGOPS	1024
 
 static struct test_case_t {
+	int all_variants;
 	int *semid;
 	struct sembuf **buf;
 	short *sem_op;
@@ -44,25 +47,35 @@ static struct test_case_t {
 	int arr_val;
 	int error;
 } tc[] = {
-	{&valid_sem_id, NULL, &sem_op_1, 0, 0, 0, BIGOPS, 1, E2BIG},
-	{&noperm_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EACCES},
-	{&valid_sem_id, &faulty_buf, &sem_op_1, 0, 0, 0, NSOPS, 1, EFAULT},
-	{&valid_sem_id, NULL, &sem_op_1, 0, 0, 0, 0, 1, EINVAL},
-	{&bad_sem_id,   NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EINVAL},
-	{&valid_sem_id, NULL, &sem_op_max, 0, 0, 0, 1, 1, ERANGE},
-	{&valid_sem_id, NULL, &sem_op_1, 0, -1, SEM_UNDO, 1, 1, EFBIG},
-	{&valid_sem_id, NULL, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1, EFBIG},
-	{&valid_sem_id, NULL, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1, EAGAIN},
-	{&valid_sem_id, NULL, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0, EAGAIN}
+	{1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, BIGOPS, 1, E2BIG},
+	{1, &noperm_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EACCES},
+	{1, &valid_sem_id, &faulty_buf, &sem_op_1, 0, 0, 0, NSOPS, 1, EFAULT},
+	{1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, 0, 1, EINVAL},
+	{1, &bad_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EINVAL},
+	{1, &valid_sem_id, NULL, &sem_op_max, 0, 0, 0, 1, 1, ERANGE},
+	{1, &valid_sem_id, NULL, &sem_op_1, 0, -1, SEM_UNDO, 1, 1, EFBIG},
+	{1, &valid_sem_id, NULL, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1, EFBIG},
+	{1, &valid_sem_id, NULL, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1, EAGAIN},
+	{1, &valid_sem_id, NULL, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0, EAGAIN},
+	{0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1, EAGAIN},
+	{0, &valid_sem_id, NULL, &sem_op_negative, 0, 0, SEM_UNDO, 1, 0, EAGAIN},
 };
 
 static void setup(void)
 {
+	struct test_variants *tv = &variants[tst_variant];
 	struct passwd *ltpuser;
 	key_t semkey;
 	union semun arr;
 	struct seminfo ipc_buf;
 
+	tst_res(TINFO, "Testing variant: %s", tv->desc);
+	semop_supported_by_kernel(tv);
+
+	timeout.type = tv->type;
+	tst_ts_set_sec(&timeout, 1);
+	tst_ts_set_nsec(&timeout, 10000);
+
 	ltpuser = SAFE_GETPWNAM("nobody");
 	SAFE_SETUID(ltpuser->pw_uid);
 
@@ -88,6 +101,7 @@ static void setup(void)
 
 static void run(unsigned int i)
 {
+	struct test_variants *tv = &variants[tst_variant];
 	union semun arr = {.val = tc[i].arr_val};
 	struct sembuf buf = {
 		.sem_op = *tc[i].sem_op,
@@ -96,6 +110,11 @@ static void run(unsigned int i)
 	};
 	struct sembuf *ptr = &buf;
 
+	if (!tc[i].all_variants && tv->semop == semop) {
+		tst_res(TCONF, "Test not supported for variant");
+		return;
+	}
+
 	if (*tc[i].semid == valid_sem_id) {
 		if (semctl(valid_sem_id, tc[i].ctl_sem_num, SETVAL, arr) == -1)
 			tst_brk(TBROK | TERRNO, "semctl() SETVAL failed");
@@ -104,7 +123,7 @@ static void run(unsigned int i)
 	if (tc[i].buf)
 		ptr = *tc[i].buf;
 
-	TEST(semop(*(tc[i].semid), ptr, tc[i].t_ops));
+	TEST(call_semop(tv, *(tc[i].semid), ptr, tc[i].t_ops, &timeout));
 
 	if (TST_RET != -1) {
 		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
@@ -136,6 +155,7 @@ static void cleanup(void)
 static struct tst_test test = {
 	.test = run,
 	.tcnt = ARRAY_SIZE(tc),
+	.test_variants = ARRAY_SIZE(variants),
 	.setup = setup,
 	.cleanup = cleanup,
 	.needs_tmpdir = 1,
diff --git a/testcases/kernel/syscalls/ipc/semop/semop03.c b/testcases/kernel/syscalls/ipc/semop/semop03.c
index 25123f08c239..dc776a090f48 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop03.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop03.c
@@ -15,9 +15,11 @@
 #include "tst_test.h"
 #include "libnewipc.h"
 #include "lapi/semun.h"
+#include "semop.h"
 
 static key_t semkey;
 static int sem_id = -1;
+static struct tst_ts timeout;
 
 struct test_case_t {
 	union semun semunptr;
@@ -34,13 +36,14 @@ struct test_case_t {
 
 static void do_child(int i)
 {
+	struct test_variants *tv = &variants[tst_variant];
 	struct sembuf s_buf = {
 		.sem_op = tc[i].op,
 		.sem_flg = tc[i].flg,
 		.sem_num = tc[i].num,
 	};
 
-	TEST(semop(sem_id, &s_buf, 1));
+	TEST(call_semop(tv, sem_id, &s_buf, 1, &timeout));
 	if (TST_RET != -1) {
 		tst_res(TFAIL, "call succeeded when error expected");
 		exit(0);
@@ -62,6 +65,15 @@ static void sighandler(int sig)
 
 static void setup(void)
 {
+	struct test_variants *tv = &variants[tst_variant];
+
+	tst_res(TINFO, "Testing variant: %s", tv->desc);
+	semop_supported_by_kernel(tv);
+
+	timeout.type = tv->type;
+	tst_ts_set_sec(&timeout, 1);
+	tst_ts_set_nsec(&timeout, 10000);
+
 	SAFE_SIGNAL(SIGHUP, sighandler);
 	semkey = GETIPCKEY();
 
@@ -119,6 +131,7 @@ static void run(unsigned int i)
 static struct tst_test test = {
 	.test = run,
 	.tcnt = ARRAY_SIZE(tc),
+	.test_variants = ARRAY_SIZE(variants),
 	.setup = setup,
 	.cleanup = cleanup,
 	.needs_tmpdir = 1,
-- 
2.25.0.rc1.19.g042ed3e048af


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer
  2020-08-19 12:27     ` [LTP] [PATCH V9 1/2] " Viresh Kumar
@ 2020-08-19 12:27       ` Viresh Kumar
  2020-09-02  9:09         ` Li Wang
  2020-09-02  9:23         ` Li Wang
  2020-08-31 11:14       ` [LTP] [PATCH V9 1/2] syscalls/semtimedop: Add support for semtimedop and its time64 version Viresh Kumar
  1 sibling, 2 replies; 13+ messages in thread
From: Viresh Kumar @ 2020-08-19 12:27 UTC (permalink / raw)
  To: ltp

This adds test for invalid timeout pointer.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

---
Cyril: This doesn't work as expected and results in the crash of the
executable. I am not sure what's going on here though.
---
 testcases/kernel/syscalls/ipc/semop/semop02.c | 34 +++++++++++--------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/testcases/kernel/syscalls/ipc/semop/semop02.c b/testcases/kernel/syscalls/ipc/semop/semop02.c
index d0fed5321502..bf362d1edcc9 100644
--- a/testcases/kernel/syscalls/ipc/semop/semop02.c
+++ b/testcases/kernel/syscalls/ipc/semop/semop02.c
@@ -31,6 +31,7 @@ static int bad_sem_id = -1;
 static short sem_op_max, sem_op_1 = 1, sem_op_negative = -1, sem_op_zero = 0;
 static struct sembuf *faulty_buf;
 static struct tst_ts timeout;
+static struct tst_ts *valid_to = &timeout, *invalid_to;
 
 #define NSOPS	1
 #define	BIGOPS	1024
@@ -45,20 +46,22 @@ static struct test_case_t {
 	short sem_flg;
 	unsigned t_ops;
 	int arr_val;
+	struct tst_ts **to;
 	int error;
 } tc[] = {
-	{1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, BIGOPS, 1, E2BIG},
-	{1, &noperm_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EACCES},
-	{1, &valid_sem_id, &faulty_buf, &sem_op_1, 0, 0, 0, NSOPS, 1, EFAULT},
-	{1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, 0, 1, EINVAL},
-	{1, &bad_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EINVAL},
-	{1, &valid_sem_id, NULL, &sem_op_max, 0, 0, 0, 1, 1, ERANGE},
-	{1, &valid_sem_id, NULL, &sem_op_1, 0, -1, SEM_UNDO, 1, 1, EFBIG},
-	{1, &valid_sem_id, NULL, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1, EFBIG},
-	{1, &valid_sem_id, NULL, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1, EAGAIN},
-	{1, &valid_sem_id, NULL, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0, EAGAIN},
-	{0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1, EAGAIN},
-	{0, &valid_sem_id, NULL, &sem_op_negative, 0, 0, SEM_UNDO, 1, 0, EAGAIN},
+	{1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, BIGOPS, 1, &valid_to, E2BIG},
+	{1, &noperm_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, &valid_to, EACCES},
+	{1, &valid_sem_id, &faulty_buf, &sem_op_1, 0, 0, 0, NSOPS, 1, &valid_to, EFAULT},
+	{1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, 0, 1, &valid_to, EINVAL},
+	{1, &bad_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, &valid_to, EINVAL},
+	{1, &valid_sem_id, NULL, &sem_op_max, 0, 0, 0, 1, 1, &valid_to, ERANGE},
+	{1, &valid_sem_id, NULL, &sem_op_1, 0, -1, SEM_UNDO, 1, 1, &valid_to, EFBIG},
+	{1, &valid_sem_id, NULL, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1, &valid_to, EFBIG},
+	{1, &valid_sem_id, NULL, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1, &valid_to, EAGAIN},
+	{1, &valid_sem_id, NULL, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0, &valid_to, EAGAIN},
+	{0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1, &valid_to, EAGAIN},
+	{0, &valid_sem_id, NULL, &sem_op_negative, 0, 0, SEM_UNDO, 1, 0, &valid_to, EAGAIN},
+	{0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1, &invalid_to, EFAULT},
 };
 
 static void setup(void)
@@ -68,6 +71,7 @@ static void setup(void)
 	key_t semkey;
 	union semun arr;
 	struct seminfo ipc_buf;
+	void *faulty_address;
 
 	tst_res(TINFO, "Testing variant: %s", tv->desc);
 	semop_supported_by_kernel(tv);
@@ -96,7 +100,9 @@ static void setup(void)
 		tst_brk(TBROK | TERRNO, "semctl() IPC_INFO failed");
 
 	sem_op_max = ipc_buf.semvmx;
-	faulty_buf = tst_get_bad_addr(NULL);
+	faulty_address = tst_get_bad_addr(NULL);
+	invalid_to = faulty_address;
+	faulty_buf = faulty_address;
 }
 
 static void run(unsigned int i)
@@ -123,7 +129,7 @@ static void run(unsigned int i)
 	if (tc[i].buf)
 		ptr = *tc[i].buf;
 
-	TEST(call_semop(tv, *(tc[i].semid), ptr, tc[i].t_ops, &timeout));
+	TEST(call_semop(tv, *(tc[i].semid), ptr, tc[i].t_ops, *tc[i].to));
 
 	if (TST_RET != -1) {
 		tst_res(TFAIL | TTERRNO, "call succeeded unexpectedly");
-- 
2.25.0.rc1.19.g042ed3e048af


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V9 1/2] syscalls/semtimedop: Add support for semtimedop and its time64 version
  2020-08-19 12:27     ` [LTP] [PATCH V9 1/2] " Viresh Kumar
  2020-08-19 12:27       ` [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer Viresh Kumar
@ 2020-08-31 11:14       ` Viresh Kumar
  1 sibling, 0 replies; 13+ messages in thread
From: Viresh Kumar @ 2020-08-31 11:14 UTC (permalink / raw)
  To: ltp

On 19-08-20, 17:57, Viresh Kumar wrote:
> This adds support for semtimedop() and its time64 variant to the
> existing semop() syscall tests.
> 
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
> V9:
> - Removed vDSO from comment
> - Add additional failure tests for EGAIN errors
> - Moved timeout configuration change to setup()
> - Test added for NULL timeout

Ping.

-- 
viresh

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer
  2020-08-19 12:27       ` [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer Viresh Kumar
@ 2020-09-02  9:09         ` Li Wang
  2020-09-02 10:08           ` Viresh Kumar
  2020-09-02  9:23         ` Li Wang
  1 sibling, 1 reply; 13+ messages in thread
From: Li Wang @ 2020-09-02  9:09 UTC (permalink / raw)
  To: ltp

Hi Viresh,

On Wed, Aug 19, 2020 at 8:28 PM Viresh Kumar <viresh.kumar@linaro.org>
wrote:

>
> +       {1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, BIGOPS, 1, &valid_to,
> E2BIG},
> +       {1, &noperm_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, &valid_to,
> EACCES},
> +       {1, &valid_sem_id, &faulty_buf, &sem_op_1, 0, 0, 0, NSOPS, 1,
> &valid_to, EFAULT},
> +       {1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, 0, 1, &valid_to,
> EINVAL},
> +       {1, &bad_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, &valid_to,
> EINVAL},
> +       {1, &valid_sem_id, NULL, &sem_op_max, 0, 0, 0, 1, 1, &valid_to,
> ERANGE},
> +       {1, &valid_sem_id, NULL, &sem_op_1, 0, -1, SEM_UNDO, 1, 1,
> &valid_to, EFBIG},
> +       {1, &valid_sem_id, NULL, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1,
> &valid_to, EFBIG},
> +       {1, &valid_sem_id, NULL, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1,
> &valid_to, EAGAIN},
> +       {1, &valid_sem_id, NULL, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0,
> &valid_to, EAGAIN},
> +       {0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1,
> &valid_to, EAGAIN},
> +       {0, &valid_sem_id, NULL, &sem_op_negative, 0, 0, SEM_UNDO, 1, 0,
> &valid_to, EAGAIN},
>


> +       {0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1,
> &invalid_to, EFAULT},
>

This '&invalid_to' can't be passed to the semtimedop(.., timeout) correctly,
because in that wrapper function call_semop(), you invoke
tst_ts_get(timeout)
to resolve an invalid address which will be caused a segmental fault
eventually.

Apart from this, the rest code looks good to me.

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200902/6745afb8/attachment-0001.htm>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer
  2020-08-19 12:27       ` [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer Viresh Kumar
  2020-09-02  9:09         ` Li Wang
@ 2020-09-02  9:23         ` Li Wang
  1 sibling, 0 replies; 13+ messages in thread
From: Li Wang @ 2020-09-02  9:23 UTC (permalink / raw)
  To: ltp

On Wed, Aug 19, 2020 at 8:28 PM Viresh Kumar <viresh.kumar@linaro.org>
wrote:

-       {1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, BIGOPS, 1, E2BIG},
> -       {1, &noperm_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EACCES},
> -       {1, &valid_sem_id, &faulty_buf, &sem_op_1, 0, 0, 0, NSOPS, 1,
> EFAULT},
> -       {1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, 0, 1, EINVAL},
> -       {1, &bad_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, EINVAL},
> -       {1, &valid_sem_id, NULL, &sem_op_max, 0, 0, 0, 1, 1, ERANGE},
> -       {1, &valid_sem_id, NULL, &sem_op_1, 0, -1, SEM_UNDO, 1, 1, EFBIG},
> -       {1, &valid_sem_id, NULL, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1,
> EFBIG},
> -       {1, &valid_sem_id, NULL, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1,
> EAGAIN},
> -       {1, &valid_sem_id, NULL, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0,
> EAGAIN},
> -       {0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1,
> EAGAIN},
> -       {0, &valid_sem_id, NULL, &sem_op_negative, 0, 0, SEM_UNDO, 1, 0,
> EAGAIN},
> +       {1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, BIGOPS, 1, &valid_to,
> E2BIG},
> +       {1, &noperm_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, &valid_to,
> EACCES},
> +       {1, &valid_sem_id, &faulty_buf, &sem_op_1, 0, 0, 0, NSOPS, 1,
> &valid_to, EFAULT},
> +       {1, &valid_sem_id, NULL, &sem_op_1, 0, 0, 0, 0, 1, &valid_to,
> EINVAL},
> +       {1, &bad_sem_id, NULL, &sem_op_1, 0, 0, 0, NSOPS, 1, &valid_to,
> EINVAL},
> +       {1, &valid_sem_id, NULL, &sem_op_max, 0, 0, 0, 1, 1, &valid_to,
> ERANGE},
> +       {1, &valid_sem_id, NULL, &sem_op_1, 0, -1, SEM_UNDO, 1, 1,
> &valid_to, EFBIG},
> +       {1, &valid_sem_id, NULL, &sem_op_1, 0, PSEMS + 1, SEM_UNDO, 1, 1,
> &valid_to, EFBIG},
> +       {1, &valid_sem_id, NULL, &sem_op_zero, 2, 2, IPC_NOWAIT, 1, 1,
> &valid_to, EAGAIN},
> +       {1, &valid_sem_id, NULL, &sem_op_negative, 2, 2, IPC_NOWAIT, 1, 0,
> &valid_to, EAGAIN},
> +       {0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1,
> &valid_to, EAGAIN},
> +       {0, &valid_sem_id, NULL, &sem_op_negative, 0, 0, SEM_UNDO, 1, 0,
> &valid_to, EAGAIN},
> +       {0, &valid_sem_id, NULL, &sem_op_zero, 0, 0, SEM_UNDO, 1, 1,
> &invalid_to, EFAULT},
>  };
>

And btw, these new changes should also be updated in the code comments
above the head.

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200902/9c2f9f37/attachment.htm>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer
  2020-09-02  9:09         ` Li Wang
@ 2020-09-02 10:08           ` Viresh Kumar
  0 siblings, 0 replies; 13+ messages in thread
From: Viresh Kumar @ 2020-09-02 10:08 UTC (permalink / raw)
  To: ltp

On 02-09-20, 17:09, Li Wang wrote:
> This '&invalid_to' can't be passed to the semtimedop(.., timeout) correctly,
> because in that wrapper function call_semop(), you invoke
> tst_ts_get(timeout)
> to resolve an invalid address which will be caused a segmental fault
> eventually.

Thanks a lot for figuring out the crash that I wasn't able to solve
then :)

-- 
viresh

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-09-02 10:08 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-29  7:55 [LTP] [PATCH V8 1/5] syscalls/semop: Migrate to new test framework Viresh Kumar
2020-07-29  7:55 ` [LTP] [PATCH V8 2/5] syscalls: semop: Do code cleanup Viresh Kumar
2020-07-29  7:55 ` [LTP] [PATCH V8 3/5] syscalls: semop: Merge semop0[234].c files Viresh Kumar
2020-07-29  7:55 ` [LTP] [PATCH V8 4/5] syscalls: semop: Rename semop05.c Viresh Kumar
2020-08-07 13:10   ` Cyril Hrubis
2020-07-29  7:55 ` [LTP] [PATCH V8 5/5] syscalls/semtimedop: Add support for semtimedop and its time64 version Viresh Kumar
2020-08-07 13:27   ` Cyril Hrubis
2020-08-19 12:27     ` [LTP] [PATCH V9 1/2] " Viresh Kumar
2020-08-19 12:27       ` [LTP] [PATCH V9 2/2] syscalls/semtimedop: Add failure test for invalid timeout pointer Viresh Kumar
2020-09-02  9:09         ` Li Wang
2020-09-02 10:08           ` Viresh Kumar
2020-09-02  9:23         ` Li Wang
2020-08-31 11:14       ` [LTP] [PATCH V9 1/2] syscalls/semtimedop: Add support for semtimedop and its time64 version Viresh Kumar

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.