[LTP] [PATCH 9/9] mem: shmt10: Convert to new API

Li Wang liwang@redhat.com
Sun Jun 29 11:44:20 CEST 2025


On Sat, Jun 28, 2025 at 12:52 AM Ricardo B. Marlière via ltp <
ltp@lists.linux.it> wrote:

> 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 | 188
> +++++++++----------------------------
>  2 files changed, 46 insertions(+), 152 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..b55fa87ff0d0a3a52e4d680284fe32e489cc272e
> 100644
> --- a/testcases/kernel/mem/shmt/shmt10.c
> +++ b/testcases/kernel/mem/shmt/shmt10.c
> @@ -1,166 +1,68 @@
> +// 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
> +#include "tst_test.h"
> +#include "tst_safe_sysv_ipc.h"
> +#include "libnewipc.h"
>
> -/** LTP Port **/
> -#include "test.h"
> +#define SHMSIZE 0x32768
>
> -char *TCID = "shmt10";         /* Test program identifier.    */
> -int TST_TOTAL = 2;             /* Total number of test cases. */
> -/**************/
> +static char *str_iter_n;
> +static long long iter_n = 500;
> +static int shmid;
>
> -int shmid;
> -key_t key;
> -
> -static int child(int);
> -static int rm_shm(int);
> -static void fini(int);
> -
> -int main(int argc, char *argv[])
> +static void run(void)
>  {
>         char *c1 = NULL;
> -       int pid, st;
> -       register int i;
> -       int iter = 500;
> -       int c;
> -       extern char *optarg;
> -
> -       key = (key_t) getpid();
> -       signal(SIGTERM, fini);
> +       key_t key;
>
> -/*--------------------------------------------------------*/
> +       key = GETIPCKEY();
>
> -       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]);
> +       shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
> +       if (!SAFE_FORK()) {
> +               for (int i = 0; i < iter_n; i++) {
> +                       c1 = SAFE_SHMAT(shmid, NULL, 0);
> +                       SAFE_SHMDT(c1);
>                 }
> +               return;
>         }
>
>
> -/*------------------------------------------------------------------------*/
> -
> -       if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
> -               tst_resm(TFAIL, "shmget");
> -               tst_brkm(TFAIL, NULL, "Error: shmid = %d", shmid);
> -       }
> -
> -       pid = fork();
> -       switch (pid) {
> -       case -1:
> -               tst_brkm(TBROK, NULL, "fork failed");
> -       case 0:
> -               child(iter);
> -               tst_exit();
> -       }
> -
> -       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;
> -               }
>


> +       for (int i = 0; i < iter_n; i++) {
> +               c1 = SAFE_SHMAT(shmid, NULL, 0);
> +               SAFE_SHMDT(c1);
>         }
>

Can we encapsulate this part into a function and invoke it in two places?
Also, I would like to dirty the addr[0] inside that.


--- a/testcases/kernel/mem/shmt/shmt10.c
+++ b/testcases/kernel/mem/shmt/shmt10.c
@@ -24,26 +24,33 @@ static char *str_iter_n;
 static long long iter_n = 500;
 static int shmid;

+static void do_shm_cycle(long long count)
+{
+       char *addr;
+
+       for (long long i = 0; i < count; i++) {
+               addr = SAFE_SHMAT(shmid, NULL, 0);
+               addr[0] = 'a';
+               SAFE_SHMDT(addr);
+       }
+
+       return;
+}
+
+
 static void run(void)
 {
-       char *c1 = NULL;
        key_t key;

        key = GETIPCKEY();

        shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
        if (!SAFE_FORK()) {
-               for (int i = 0; i < iter_n; i++) {
-                       c1 = SAFE_SHMAT(shmid, NULL, 0);
-                       SAFE_SHMDT(c1);
-               }
+               do_shm_cycle(iter_n);
                return;
        }

-       for (int i = 0; i < iter_n; i++) {
-               c1 = SAFE_SHMAT(shmid, NULL, 0);
-               SAFE_SHMDT(c1);
-       }
+       do_shm_cycle(iter_n);

        tst_reap_children();
        SAFE_SHMCTL(shmid, IPC_RMID, NULL);



> -       while (wait(&st) < 0 && errno == EINTR) ;
> -       tst_resm(TPASS, "shmat,shmdt");
>
> -/*------------------------------------------------------------------------*/
>
> -       rm_shm(shmid);
> -       tst_exit();
> -}
> +       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
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>


-- 
Regards,
Li Wang


More information about the ltp mailing list