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

Li Wang liwang@redhat.com
Sun Jun 29 11:05:40 CEST 2025


On Sat, Jun 28, 2025 at 12:49 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 |   1 +
>  testcases/kernel/mem/shmt/shmt04.c | 216
> +++++++------------------------------
>  2 files changed, 37 insertions(+), 180 deletions(-)
>
> diff --git a/testcases/kernel/mem/shmt/Makefile
> b/testcases/kernel/mem/shmt/Makefile
> index
> 4c5f926c4e394445c7d2d7005c47414255670d40..e8317a8ae642b0f69ac57288cb86a316ec09c88d
> 100644
> --- a/testcases/kernel/mem/shmt/Makefile
> +++ b/testcases/kernel/mem/shmt/Makefile
> @@ -28,5 +28,6 @@ include $(top_srcdir)/include/mk/testcases.mk
>
>  shmt02: LTPLDLIBS = -lltpnewipc
>  shmt03: LTPLDLIBS = -lltpnewipc
> +shmt04: LTPLDLIBS = -lltpnewipc
>
>  include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/mem/shmt/shmt04.c
> b/testcases/kernel/mem/shmt/shmt04.c
> index
> c9a654e7d20aa9789b750f7f189585a2004ac5b7..83c12a6fe287d7feabd1fbec5a33f17886ba1c13
> 100644
> --- a/testcases/kernel/mem/shmt/shmt04.c
> +++ b/testcases/kernel/mem/shmt/shmt04.c
> @@ -1,197 +1,53 @@
> +// 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
> + *     03/21/2003      enable ia64     Jacky.Malcles
> + *     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>
>   */
>
> -/* 03/21/2003   enable ia64     Jacky.Malcles */
> -/* 12/20/2002   Port to LTP     robbiew@us.ibm.com */
> -/* 06/30/2001   Port to Linux   nsharoff@us.ibm.com */
> -
> -/*
> - * NAME
> - *              shmt04
> - *
> - * CALLS
> - *              shmctl(2) shmget(2) shmat(2)
> - *
> - * ALGORITHM
> +/*\
>   * Parent process forks a child. Child pauses until parent has created
>   * a shared memory segment, attached to it and written to it too. At that
>   * time child gets the shared memory segment id, attaches to it and
>   * verifies that its contents are the same as the contents of the
>   * parent attached segment.
> - *
>   */
>
> -#include <stdio.h>
> -#include <sys/types.h>
> -#include <sys/ipc.h>
> -#include <sys/shm.h>
> -#include <sys/wait.h>
> -#include <sys/utsname.h>
> -#include <signal.h>
> -#include <errno.h>
> -#include <stdlib.h>
> -#include <unistd.h>
> -
> -/** LTP Port **/
> -#include "test.h"
> -
> -char *TCID = "shmt04";         /* Test program identifier.    */
> -int TST_TOTAL = 2;             /* Total number of test cases. */
> -/**************/
> -
> -key_t key;
> -sigset_t set;
> -
> -#define  SIZE  16*1024
> -
> -int child();
> -static int rm_shm(int);
> -
> -int main(void)
> -{
> -       char *cp = NULL;
> -       int pid, pid1, shmid;
> -       int status;
> -
> -       key = (key_t) getpid();
> -
> -       sigemptyset(&set);
> -       sigaddset(&set, SIGUSR1);
> -       sigprocmask(SIG_BLOCK, &set, NULL);
> -
> -       pid = fork();
> -       switch (pid) {
> -       case -1:
> -               tst_brkm(TBROK, NULL, "fork failed");
> -       case 0:
> -               child();
> -       }
> -
> -/*----------------------------------------------------------*/
> -
> -       if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
> -               perror("shmget");
> -               tst_resm(TFAIL, "Error: shmget: shmid = %d, errno = %d",
> -                        shmid, errno);
> -               /*
> -                * kill the child if parent failed to do the attach
> -                */
> -               (void)kill(pid, SIGINT);
> -       } else {
> -               cp = shmat(shmid, NULL, 0);
> -
> -               if (cp == (char *)-1) {
> -                       perror("shmat");
> -                       tst_resm(TFAIL,
> -                                "Error: shmat: shmid = %d, errno = %d",
> -                                shmid, errno);
> -
> -/* kill the child if parent failed to do the attch */
> -
> -                       kill(pid, SIGINT);
> -
> -/* remove shared memory segment */
> +#include "tst_test.h"
> +#include "tst_safe_sysv_ipc.h"
> +#include "tst_rand_data.h"
> +#include "libnewipc.h"
>
> -                       rm_shm(shmid);
> +#define SHMSIZE 16
>
> -                       tst_exit();
> -               }
> -               *cp = 'A';
> -               *(cp + 1) = 'B';
> -               *(cp + 2) = 'C';
> -
> -               kill(pid, SIGUSR1);
> -               while ((pid1 = wait(&status)) < 0 && (errno == EINTR)) ;
> -               if (pid1 != pid) {
> -                       tst_resm(TFAIL, "Waited on the wrong child");
> -                       tst_resm(TFAIL,
> -                                "Error: wait_status = %d, pid1= %d",
> status,
> -                                pid1);
> -               }
> -       }
> -
> -       tst_resm(TPASS, "shmget,shmat");
> -
> -/*----------------------------------------------------------*/
> -
> -       if (shmdt(cp) < 0) {
> -               tst_resm(TFAIL, "shmdt");
> -       }
> -
> -       tst_resm(TPASS, "shmdt");
> -
> -/*----------------------------------------------------------*/
> -
> -       rm_shm(shmid);
> -       tst_exit();
> -}
> -
> -int child(void)
> +static void run(void)
>  {
> -       int shmid, chld_pid;
>         char *cp;
> -       int sig;
> -
> -       sigwait(&set, &sig);
> -       chld_pid = getpid();
> -/*--------------------------------------------------------*/
> -
> -       if ((shmid = shmget(key, SIZE, 0)) < 0) {
> -               perror("shmget:child process");
> -               tst_resm(TFAIL,
> -                        "Error: shmget: errno=%d, shmid=%d, child_pid=%d",
> -                        errno, shmid, chld_pid);
> -       } else {
> -               cp = shmat(shmid, NULL, 0);
> -
> -               if (cp == (char *)-1) {
> -                       perror("shmat:child process");
> -                       tst_resm(TFAIL,
> -                                "Error: shmat: errno=%d, shmid=%d,
> child_pid=%d",
> -                                errno, shmid, chld_pid);
> -               } else {
> -                       if (*cp != 'A') {
> -                               tst_resm(TFAIL, "child: not A");
> -                       }
> -                       if (*(cp + 1) != 'B') {
> -                               tst_resm(TFAIL, "child: not B");
> -                       }
> -                       if (*(cp + 2) != 'C') {
> -                               tst_resm(TFAIL, "child: not C");
> -                       }
> -                       if (*(cp + 8192) != 0) {
> -                               tst_resm(TFAIL, "child: not 0");
> -                       }
> -               }
> -
> +       int shmid;
> +       key_t key;
> +
> +       key = GETIPCKEY();
> +
> +       if (!SAFE_FORK()) {
> +               TST_CHECKPOINT_WAIT(0);
> +               shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
> +               cp = SAFE_SHMAT(shmid, NULL, 0);
> +               TST_EXP_EQ_LI(memcmp(cp, tst_rand_data, SHMSIZE), 0);
> +               SAFE_SHMCTL(shmid, IPC_RMID, NULL);
> +               return;
>         }
> -       tst_exit();
> -}
>
> -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);
> +       shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
> +       cp = SAFE_SHMAT(shmid, NULL, 0);
> +       memcpy(cp, tst_rand_data, SHMSIZE);
> +       TST_CHECKPOINT_WAKE(0);
>


> +       SAFE_SHMCTL(shmid, IPC_RMID, NULL);
>

This line in the parent is duplicated, which makes test fails:

# ./shmt04
tst_tmpdir.c:316: TINFO: Using /tmp/LTP_shmdP4dy3 as tmpdir (xfs filesystem)
tst_test.c:1999: TINFO: LTP version: 20250530-63-ga5a030795
tst_test.c:2002: TINFO: Tested kernel: 6.12.0-92.el10.x86_64 #1 SMP
PREEMPT_DYNAMIC Wed Jun  4 13:45:25 UTC 2025 x86_64
tst_kconfig.c:88: TINFO: Parsing kernel config
'/lib/modules/6.12.0-92.el10.x86_64/config'
tst_test.c:1820: TINFO: Overall timeout per run is 0h 00m 30s
shmt04.c:37: TFAIL: memcmp(cp, tst_rand_data, SHMSIZE) (-165) != 0 (0)
shmt04.c:43: TFAIL: memcmp(cp, tst_rand_data, SHMSIZE) (-165) != 0 (0)

Summary:
passed   0
failed   2
broken   0
skipped  0
warnings 0


And, for better readable code, we need to utilize blank lines and reap/wait
the children.

--- a/testcases/kernel/mem/shmt/shmt04.c
+++ b/testcases/kernel/mem/shmt/shmt04.c
@@ -32,24 +32,33 @@ static void run(void)

        if (!SAFE_FORK()) {
                TST_CHECKPOINT_WAIT(0);
+
                shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
                cp = SAFE_SHMAT(shmid, NULL, 0);
                TST_EXP_EQ_LI(memcmp(cp, tst_rand_data, SHMSIZE), 0);
+
                /*
                 * Attach the segment to a different address
                 * and verify it's contents again.
                 */
                cp = SAFE_SHMAT(shmid, NULL, 0);
                TST_EXP_EQ_LI(memcmp(cp, tst_rand_data, SHMSIZE), 0);
+
                SAFE_SHMCTL(shmid, IPC_RMID, NULL);
-               return;
+
+               _exit(0);
        }

        shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
+
        cp = SAFE_SHMAT(shmid, NULL, 0);
        memcpy(cp, tst_rand_data, SHMSIZE);
+
        TST_CHECKPOINT_WAKE(0);
-       SAFE_SHMCTL(shmid, IPC_RMID, NULL);
+
+       tst_reap_children();
+
+       return;
 }

 static struct tst_test test = {



>  }
> +
> +static struct tst_test test = {
> +       .test_all = run,
> +       .needs_checkpoints = 1,
> +       .forks_child = 1,
> +};
>
> --
> 2.50.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>


-- 
Regards,
Li Wang


More information about the ltp mailing list