[LTP] [PATCH v1] Refactor pidns17 test using new LTP API
Richard Palethorpe
rpalethorpe@suse.de
Tue Nov 1 11:38:33 CET 2022
Hello,
Andrea Cervesato via ltp <ltp@lists.linux.it> writes:
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
> testcases/kernel/containers/pidns/pidns17.c | 189 ++++++--------------
> 1 file changed, 51 insertions(+), 138 deletions(-)
>
> diff --git a/testcases/kernel/containers/pidns/pidns17.c b/testcases/kernel/containers/pidns/pidns17.c
> index cf0c5826f..1d3dd53bb 100644
> --- a/testcases/kernel/containers/pidns/pidns17.c
> +++ b/testcases/kernel/containers/pidns/pidns17.c
> @@ -1,162 +1,75 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> -* Copyright (c) International Business Machines Corp., 2007
> -* 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
> -*
> -***************************************************************************
> -* File: pidns17.c
> -* *
> -* * Description:
> -* * The pidns17.c testcase verifies inside the container, if kill(-1, SIGUSR1)
> -* * terminates all children running inside.
> -* *
> -* * Test Assertion & Strategy:
> -* * Create a PID namespace container.
> -* * Spawn many children inside it.
> -* * Invoke kill(-1, SIGUSR1) inside container and check if it terminates
> -* * all children.
> -* *
> -* * Usage: <for command-line>
> -* * pidns17
> -* *
> -* * History:
> -* * DATE NAME DESCRIPTION
> -* * 13/11/08 Gowrishankar M Creation of this test.
> -* * <gowrishankar.m@in.ibm.com>
> -*
> -******************************************************************************/
> -#define _GNU_SOURCE 1
> -#include <sys/wait.h>
> -#include <sys/types.h>
> -#include <string.h>
> -#include <stdlib.h>
> -#include <unistd.h>
> -#include <stdio.h>
> -#include <errno.h>
> -#include "pidns_helper.h"
> -#include "test.h"
> + * Copyright (c) International Business Machines Corp., 2007
> + * 13/11/08 Gowrishankar M <gowrishankar.m@in.ibm.com>
> + * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
>
> -char *TCID = "pidns17";
> -int TST_TOTAL = 1;
> +/*\
> + * [Description]
> + *
> + * Clone a process with CLONE_NEWPID flag and spawn many children inside the
> + * container. Then terminate all children and check if they ended up.
> + */
>
> -int child_fn(void *);
> +#include <sys/wait.h>
> +#include "tst_test.h"
> +#include "lapi/namespaces_constants.h"
>
> -#define CHILD_PID 1
> -#define PARENT_PID 0
> +#define CHILDREN_NUM 10
>
> -/*
> - * child_fn() - Inside container
> - */
> -int child_fn(void *arg)
> +static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
> {
> - int children[10], exit_val, i, status;
> - pid_t pid, ppid;
> + int children[CHILDREN_NUM], status;
> + unsigned int i, counter = 0;
> + pid_t cpid, ppid;
>
> - /* Set process id and parent pid */
> - pid = getpid();
> + cpid = getpid();
> ppid = getppid();
> - if (pid != CHILD_PID || ppid != PARENT_PID) {
> - printf("cinit: pidns was not created\n");
> - exit(1);
> - }
>
> - exit_val = 0;
> + if (cpid != 1 || ppid != 0) {
> + tst_res(TFAIL, "Got unexpected result of cpid=%d ppid=%d", cpid, ppid);
> + return 1;
> + }
Can use TST_EXP_EQ here as in pidns12 (or whatever I just merged).
>
> - /* Spawn many children */
> - for (i = 0; i < ARRAY_SIZE(children); i++) {
> - switch ((children[i] = fork())) {
> - case -1:
> - perror("fork failed");
> - exit_val = 1;
> - break;
> - case 0:
> + for (i = 0; i < CHILDREN_NUM; i++) {
> + children[i] = SAFE_FORK();
> + if (!children[i]) {
> pause();
> - /* XXX (garrcoop): why exit with an exit code of 2? */
> - exit(2);
> - break;
> - default:
> - /* fork succeeded. */
> - break;
> + return 0;
> }
> }
> - /* wait for last child to get scheduled */
> - sleep(1);
>
> - if (kill(-1, SIGUSR1) == -1) {
> - perror("cinit: kill(-1, SIGUSR1) failed");
> - exit_val = 1;
> + SAFE_KILL(-1, SIGUSR1);
> +
> + for (i = 0; i < CHILDREN_NUM; i++) {
> + SAFE_WAITPID(children[i], &status, 0);
> +
> + if (WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR1)
> + counter++;
How do we debug this if the test fails? It would be nice to at least
know how the child died.
> }
>
> - for (i = 0; i < ARRAY_SIZE(children); i++) {
> - if (waitpid(children[i], &status, 0) == -1) {
> - perror("cinit: waitpid failed");
> - kill(children[i], SIGTERM);
> - waitpid(children[i], &status, 0);
> - exit_val = 1;
> - }
> - if (!(WIFSIGNALED(status) || WTERMSIG(status) == SIGUSR1)) {
> - /*
> - * XXX (garrcoop): this status reporting is overly
> - * noisy. Someone obviously needs to read the
> - * constraints documented in wait(2) a bit more
> - * closely -- in particular the relationship between
> - * WIFEXITED and WEXITSTATUS, and WIFSIGNALED and
> - * WTERMSIG.
> - */
> - printf("cinit: found a child alive still "
> - "%d exit: %d, %d, signal %d, %d", i,
> - WIFEXITED(status), WEXITSTATUS(status),
> - WIFSIGNALED(status), WTERMSIG(status));
This only gets printed when the test fails, so it's not too noisy. The
message is not correct either, but it would at least print how the child
died.
--
Thank you,
Richard.
More information about the ltp
mailing list