All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Palethorpe <rpalethorpe@suse.de>
To: Andrea Cervesato <andrea.cervesato@suse.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2] Refactor pidns17 test using new LTP API
Date: Tue, 14 Feb 2023 08:55:31 +0000	[thread overview]
Message-ID: <87v8k4oak3.fsf@suse.de> (raw)
In-Reply-To: <20230207143758.31331-1-andrea.cervesato@suse.com>

Hello,

Andrea Cervesato via ltp <ltp@lists.linux.it> writes:

> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
> Use more TST_* macros
> Use SAFE_CLONE
> Better TINFO messages
>
>  testcases/kernel/containers/pidns/pidns17.c | 192 ++++++--------------
>  1 file changed, 54 insertions(+), 138 deletions(-)
>
> diff --git a/testcases/kernel/containers/pidns/pidns17.c b/testcases/kernel/containers/pidns/pidns17.c
> index cf0c5826f..c45d9fef8 100644
> --- a/testcases/kernel/containers/pidns/pidns17.c
> +++ b/testcases/kernel/containers/pidns/pidns17.c
> @@ -1,162 +1,78 @@
> +// 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.

                                                          ^^ they were signaled?

> + */
>  
> -int child_fn(void *);
> +#include <sys/wait.h>
> +#include "tst_test.h"
> +#include "lapi/sched.h"
>  
> -#define CHILD_PID       1
> -#define PARENT_PID      0
> +#define CHILDREN_NUM 10
>  
> -/*
> - * child_fn() - Inside container
> - */
> -int child_fn(void *arg)
> +static void child_func(void)
>  {
> -	int children[10], exit_val, i, status;
> -	pid_t pid, ppid;
> +	int children[CHILDREN_NUM], status;
> +	unsigned int sig, 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;
> +	TST_EXP_EQ_LI(cpid, 1);
> +	TST_EXP_EQ_LI(ppid, 0);
> +
> +	tst_res(TINFO, "Spawning %d children", CHILDREN_NUM);
>  
> -	/* 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;
>  		}
>  	}
> -	/* wait for last child to get scheduled */
> -	sleep(1);
>  
> -	if (kill(-1, SIGUSR1) == -1) {
> -		perror("cinit: kill(-1, SIGUSR1) failed");
> -		exit_val = 1;
> -	}
> +	tst_res(TINFO, "Terminate children with SIGUSR1");
>  
> -	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));
> -			exit_val = 1;
> +	SAFE_KILL(-1, SIGUSR1);
> +
> +	for (i = 0; i < CHILDREN_NUM; i++) {
> +		SAFE_WAITPID(children[i], &status, 0);
> +
> +		if (WIFSIGNALED(status)) {
> +			sig = WTERMSIG(status);
> +			if (sig != SIGUSR1) {
> +				tst_res(TFAIL, "%d child died by %s", i, tst_strsig(sig));
> +				return;

Why not check the other children?

If only one died by another sig that is different to all of them.

> +			}
> +
> +			counter++;
>  		}

Why have this counter instead of calling
tst_res(TFAIL, "%d child exited with %d instead of being signaled", i, WEXITSTATUS(status))
if we have exited?

>  	}
> -	if (exit_val == 0)
> -		printf("cinit: all children have terminated.\n");
> -
> -	exit(exit_val);
> -}
>  
> -static void setup(void)
> -{
> -	tst_require_root();
> -	check_newpid();
> +	TST_EXP_EQ_LI(counter, CHILDREN_NUM);
>  }
>  
> -int main(void)
> +static void run(void)
>  {
> -	int status;
> -	pid_t pid;
> -
> -	setup();
> -
> -	pid = getpid();
> +	const struct tst_clone_args args = { CLONE_NEWPID, SIGCHLD };
>  
> -	/* Container creation on PID namespace */
> -	TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn, NULL));
> -	if (TEST_RETURN == -1) {
> -		tst_brkm(TBROK | TTERRNO, NULL, "clone failed");
> +	if (!SAFE_CLONE(&args)) {
> +		child_func();
> +		return;
>  	}
> -
> -	sleep(1);
> -	if (wait(&status) == -1)
> -		tst_resm(TFAIL, "waitpid failed");
> -
> -	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
> -		tst_resm(TFAIL, "container exited abnormally");
> -	else if (WIFSIGNALED(status))
> -		tst_resm(TFAIL,
> -			 "container was signaled with signal = %d",
> -			 WTERMSIG(status));
> -
> -	tst_exit();
> -
>  }
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.needs_root = 1,
> +	.forks_child = 1,
> +};
> -- 
> 2.35.3


-- 
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

      parent reply	other threads:[~2023-02-14  9:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-07 14:37 [LTP] [PATCH v2] Refactor pidns17 test using new LTP API Andrea Cervesato via ltp
2023-02-10 12:45 ` Cyril Hrubis
2023-02-14  8:55 ` Richard Palethorpe [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v8k4oak3.fsf@suse.de \
    --to=rpalethorpe@suse.de \
    --cc=andrea.cervesato@suse.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.