Linux Test Project
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Andrea Cervesato <andrea.cervesato@suse.de>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2 1/6] Rewrite mountns01 test using new LTP API
Date: Mon, 28 Feb 2022 16:42:45 +0100	[thread overview]
Message-ID: <Yhztdezj+WVzfved@yuki> (raw)
In-Reply-To: <20220228133256.12763-2-andrea.cervesato@suse.de>

Hi!
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2014 Red Hat, Inc.
> + * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*\
> + * [Description]
>   *
>   * Tests a shared mount: shared mount can be replicated to as many
>   * mountpoints and all the replicas continue to be exactly same.
> - * Description:
> + *
> + * [Algorithm]
> + *
>   * 1. Creates directories "A", "B" and files "A/A", "B/B"
>   * 2. Unshares mount namespace and makes it private (so mounts/umounts
>   *    have no effect on a real system)
> @@ -25,125 +20,110 @@
>   * 5. Clones a new child process with CLONE_NEWNS flag
>   * 6. There are two test cases (where X is parent namespace and Y child
>   *    namespace):
> - *    1)
> + * 1)
>   *	X: bind mounts "B" to "A"
>   *	Y: must see "A/B"
>   *	X: umounts "A"
> - *    2)
> + * 2)
>   *	Y: bind mounts "B" to "A"
>   *	X: must see "A/B"
>   *	Y: umounts "A"

This does not render as lists in asciidoc at all, please fix.

> -#define _GNU_SOURCE
>  #include <sys/wait.h>
>  #include <sys/mount.h>
> -#include <stdio.h>
> -#include <errno.h>
> -#include "mountns_helper.h"
> -#include "test.h"
> -#include "safe_macros.h"
> -
> -char *TCID	= "mountns01";
> -int TST_TOTAL	= 2;
> +#include "mountns.h"
> +#include "tst_test.h"
>  
> -#if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
> -
> -int child_func(void *arg LTP_ATTRIBUTE_UNUSED)
> +static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
>  {
>  	int ret = 0;
>  
> -	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
> +	TST_CHECKPOINT_WAIT(0);
>  
> -	if (access(DIRA"/B", F_OK) == -1)
> +	if (access(DIRA "/B", F_OK) < 0)
>  		ret = 2;
>  
> -	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
> +	TST_CHECKPOINT_WAKE_AND_WAIT(0);
>  
> -	/* bind mounts DIRB to DIRA making contents of DIRB visible
> -	 * in DIRA */
> -	if (mount(DIRB, DIRA, "none", MS_BIND, NULL) == -1) {
> -		perror("mount");
> -		return 1;
> -	}
> +	/* bind mounts DIRB to DIRA making contents of DIRB visible in DIRA */
> +	SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
>  
> -	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
> +	TST_CHECKPOINT_WAKE_AND_WAIT(0);
> +
> +	SAFE_UMOUNT(DIRA);
>  
> -	umount(DIRA);
>  	return ret;
>  }
>  
> -static void test(void)
> +static void run(void)
>  {
> -	int status;
> +	int status, ret;
>  
>  	/* unshares the mount ns */
> -	if (unshare(CLONE_NEWNS) == -1)
> -		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
> +	SAFE_UNSHARE(CLONE_NEWNS);
> +
>  	/* makes sure parent mounts/umounts have no effect on a real system */
> -	SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL);
> +	SAFE_MOUNT("none", "/", "none", MS_REC | MS_PRIVATE, NULL);
>  
>  	/* bind mounts DIRA to itself */
> -	SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL);
> +	SAFE_MOUNT(DIRA, DIRA, "none", MS_BIND, NULL);
>  
>  	/* makes mount DIRA shared */
> -	SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_SHARED, NULL);
> +	SAFE_MOUNT("none", DIRA, "none", MS_SHARED, NULL);
>  
> -	if (do_clone_tests(CLONE_NEWNS, child_func, NULL, NULL, NULL) == -1)
> -		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
> +	ret = ltp_clone_quick(CLONE_NEWNS | SIGCHLD, child_func, NULL);
> +	if (ret < 0)
> +		tst_brk(TBROK, "clone failed");
>  
> -	/* bind mounts DIRB to DIRA making contents of DIRB visible
> -	 * in DIRA */
> -	SAFE_MOUNT(cleanup, DIRB, DIRA, "none", MS_BIND, NULL);
> +	/* bind mounts DIRB to DIRA making contents of DIRB visible in DIRA */
> +	SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
>  
> -	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(cleanup, 0);
> +	TST_CHECKPOINT_WAKE_AND_WAIT(0);
>  
> -	SAFE_UMOUNT(cleanup, DIRA);
> +	SAFE_UMOUNT(DIRA);
>  
> -	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(cleanup, 0);
> +	TST_CHECKPOINT_WAKE_AND_WAIT(0);
>  
> -	if (access(DIRA"/B", F_OK) == 0)
> -		tst_resm(TPASS, "shared mount in child passed");
> +	if (access(DIRA "/B", F_OK) == 0)
> +		tst_res(TPASS, "shared mount in child passed");
>  	else
> -		tst_resm(TFAIL, "shared mount in child failed");
> +		tst_res(TFAIL, "shared mount in child failed");
>  
> -	TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
> +	TST_CHECKPOINT_WAKE(0);
>  
> +	SAFE_WAIT(&status);
>  
> -	SAFE_WAIT(cleanup, &status);
>  	if (WIFEXITED(status)) {
>  		if ((WEXITSTATUS(status) == 0))
> -			tst_resm(TPASS, "shared mount in parent passed");
> +			tst_res(TPASS, "shared mount in parent passed");
>  		else
> -			tst_resm(TFAIL, "shared mount in parent failed");
> +			tst_res(TFAIL, "shared mount in parent failed");
>  	}

Again there is no need to propagate results via exit value like this,
the tst_res() function will work in the child just fine.

>  	if (WIFSIGNALED(status)) {
> -		tst_resm(TBROK, "child was killed with signal %s",
> -			 tst_strsig(WTERMSIG(status)));
> -		return;
> +		tst_brk(TBROK, "child was killed with signal %s",
> +			tst_strsig(WTERMSIG(status)));
>  	}

And the segfault will be caught by the test library, so no need to wait
for the child here as well.

> -	SAFE_UMOUNT(cleanup, DIRA);
> +	SAFE_UMOUNT(DIRA);
>  }
>  
> -int main(int argc, char *argv[])
> +static void setup(void)
>  {
> -	int lc;
> -
> -	tst_parse_opts(argc, argv, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++)
> -		test();
> -
> -	cleanup();
> -	tst_exit();
> +	check_newns();
> +	create_folders();
>  }
>  
> -#else
> -int main(void)
> +static void cleanup(void)
>  {
> -	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
> +	umount_folders();
>  }
> -#endif
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = run,
> +	.needs_root = 1,
> +	.needs_checkpoints = 1,
> +};
> -- 
> 2.35.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

  reply	other threads:[~2022-02-28 15:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-28 13:32 [LTP] [PATCH v2 0/6] Rewrite mountns testing suite Andrea Cervesato
2022-02-28 13:32 ` [LTP] [PATCH v2 1/6] Rewrite mountns01 test using new LTP API Andrea Cervesato
2022-02-28 15:42   ` Cyril Hrubis [this message]
2022-02-28 15:53     ` Cyril Hrubis
2022-02-28 13:32 ` [LTP] [PATCH v2 2/6] Rewrite mountns02 " Andrea Cervesato
2022-02-28 13:32 ` [LTP] [PATCH v2 3/6] Rewrite mountns03 " Andrea Cervesato
2022-02-28 13:32 ` [LTP] [PATCH v2 4/6] Rewrite mountns04 " Andrea Cervesato
2022-02-28 13:32 ` [LTP] [PATCH v2 5/6] Removed obsolete mountns_helper.h Andrea Cervesato
2022-02-28 13:32 ` [LTP] [PATCH v2 6/6] Removed libclone usage from mountns testing suite Andrea Cervesato

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=Yhztdezj+WVzfved@yuki \
    --to=chrubis@suse.cz \
    --cc=andrea.cervesato@suse.de \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox