[LTP] [PATCH v2 9/9] mem: shmt10: Convert to new API
Ricardo B. Marlière
rbm@suse.com
Mon Jun 30 15:13:16 CEST 2025
From: Ricardo B. Marlière <rbm@suse.com>
Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
---
testcases/kernel/mem/shmt/Makefile | 10 +-
testcases/kernel/mem/shmt/shmt10.c | 193 ++++++++++---------------------------
2 files changed, 50 insertions(+), 153 deletions(-)
diff --git a/testcases/kernel/mem/shmt/Makefile b/testcases/kernel/mem/shmt/Makefile
index 7fe4e75b567367e6c4b0978dec831ca268e99567..75249fe647716d4bb01b83e7d564ffabeb5c8c02 100644
--- a/testcases/kernel/mem/shmt/Makefile
+++ b/testcases/kernel/mem/shmt/Makefile
@@ -23,15 +23,7 @@
top_srcdir ?= ../../../..
LTPLIBS = newipc
+LTPLDLIBS = -lltpnewipc
include $(top_srcdir)/include/mk/testcases.mk
-
-shmt02: LTPLDLIBS = -lltpnewipc
-shmt03: LTPLDLIBS = -lltpnewipc
-shmt04: LTPLDLIBS = -lltpnewipc
-shmt05: LTPLDLIBS = -lltpnewipc
-shmt07: LTPLDLIBS = -lltpnewipc
-shmt08: LTPLDLIBS = -lltpnewipc
-shmt09: LTPLDLIBS = -lltpnewipc
-
include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/mem/shmt/shmt10.c b/testcases/kernel/mem/shmt/shmt10.c
index c63bb15928dc958d5ac2787b63aa3c97398cefb4..2bd48b6b50bc1c84c857baf316fe44a6ce527148 100644
--- a/testcases/kernel/mem/shmt/shmt10.c
+++ b/testcases/kernel/mem/shmt/shmt10.c
@@ -1,166 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * Copyright (c) International Business Machines Corp., 2002
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines Corp., 2002
+ * 12/20/2002 Port to LTP robbiew@us.ibm.com
+ * 06/30/2001 Port to Linux nsharoff@us.ibm.com
+ * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <rbm@suse.com>
*/
-/* 12/20/2002 Port to LTP robbiew@us.ibm.com */
-/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
-
-/*
- * NAME
- * shmt10.c - test simultaneous shmat/shmdt
- *
- * CALLS
- * shmget, shmat, shmdt, shmctl
- *
- * ALGORITHM
- * Create a shared memory segment and fork a child. Both
- * parent and child spin in a loop attaching and detaching
- * the segment. After completing the specified number of
- * iterations, the child exits and the parent deletes the
- * segment.
- *
- * USAGE
- * shmt10 [-i 500]
- * -i # of iterations, default 500
- *
+/*\
+ * Create a shared memory segment and fork a child. Both
+ * parent and child spin in a loop attaching and detaching
+ * the segment. After completing the specified number of
+ * iterations, the child exits and the parent deletes the
+ * segment.
*/
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <signal.h>
-#include <errno.h>
-
-#define SIZE 0x32768
-
-/** LTP Port **/
-#include "test.h"
-
-char *TCID = "shmt10"; /* Test program identifier. */
-int TST_TOTAL = 2; /* Total number of test cases. */
-/**************/
+#include "tst_test.h"
+#include "tst_safe_sysv_ipc.h"
+#include "libnewipc.h"
-int shmid;
-key_t key;
+#define SHMSIZE 0x32768
-static int child(int);
-static int rm_shm(int);
-static void fini(int);
+static char *str_iter_n;
+static long long iter_n = 500;
+static int shmid;
-int main(int argc, char *argv[])
+static void do_shm_cycle(long long iter_n)
{
- char *c1 = NULL;
- int pid, st;
- register int i;
- int iter = 500;
- int c;
- extern char *optarg;
+ char *addr;
- key = (key_t) getpid();
- signal(SIGTERM, fini);
-
-/*--------------------------------------------------------*/
-
- while ((c = getopt(argc, argv, "i:")) != EOF) {
- switch (c) {
- case 'i':
- iter = atoi(optarg);
- break;
- default:
- tst_brkm(TCONF, NULL, "usage: %s [-i <# iterations>]",
- argv[0]);
- }
+ for (int i = 0; i < iter_n; i++) {
+ addr = SAFE_SHMAT(shmid, NULL, 0);
+ addr[0] = 'a';
+ SAFE_SHMDT(addr);
}
+}
-/*------------------------------------------------------------------------*/
-
- if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
- tst_resm(TFAIL, "shmget");
- tst_brkm(TFAIL, NULL, "Error: shmid = %d", shmid);
- }
+static void run(void)
+{
+ key_t key;
- pid = fork();
- switch (pid) {
- case -1:
- tst_brkm(TBROK, NULL, "fork failed");
- case 0:
- child(iter);
- tst_exit();
- }
+ key = GETIPCKEY();
- for (i = 0; i < iter; i++) {
- if ((c1 = shmat(shmid, NULL, 0)) == (char *)-1) {
- tst_resm(TFAIL,
- "Error shmat: iter %d, shmid = %d", i,
- shmid);
- break;
- }
- if (shmdt(c1) < 0) {
- tst_resm(TFAIL, "Error: shmdt: iter %d ", i);
- break;
- }
+ shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
+ if (!SAFE_FORK()) {
+ do_shm_cycle(iter_n);
+ _exit(0);
}
- while (wait(&st) < 0 && errno == EINTR) ;
- tst_resm(TPASS, "shmat,shmdt");
-/*------------------------------------------------------------------------*/
- rm_shm(shmid);
- tst_exit();
-}
+ do_shm_cycle(iter_n);
+ tst_reap_children();
+ SAFE_SHMCTL(shmid, IPC_RMID, NULL);
-static int rm_shm(int shmid)
-{
- if (shmctl(shmid, IPC_RMID, NULL) == -1) {
- perror("shmctl");
- tst_brkm(TFAIL,
- NULL,
- "shmctl Failed to remove: shmid = %d, errno = %d",
- shmid, errno);
- }
- return (0);
+ tst_res(TPASS, "Attached and detached %lld times", iter_n * 2);
}
-static int child(int iter)
+static void setup(void)
{
- register int i;
- char *c1;
-
- for (i = 0; i < iter; i++) {
- if ((c1 = shmat(shmid, NULL, 0)) == (char *)-1) {
- tst_brkm(TFAIL,
- NULL,
- "Error:child proc: shmat: iter %d, shmid = %d",
- i, shmid);
- }
- if (shmdt(c1) < 0) {
- tst_brkm(TFAIL,
- NULL, "Error: child proc: shmdt: iter %d ",
- i);
- }
- }
- return (0);
+ if (tst_parse_filesize(str_iter_n, &iter_n, 1, LLONG_MAX))
+ tst_brk(TBROK, "Invalid amount of iterations: %s", str_iter_n);
}
-static void fini(int sig)
-{
- rm_shm(shmid);
-}
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run,
+ .forks_child = 1,
+ .options = (struct tst_option[]) {
+ {"n:", &str_iter_n, "Amount of iterations (default 500)"},
+ {}
+ },
+};
--
2.50.0
More information about the ltp
mailing list