public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Jan Stancek <jstancek@redhat.com>
To: Matus Marhefka <mmarhefk@redhat.com>
Cc: ltp-list@lists.sourceforge.net
Subject: Re: [LTP] [PATCH 3/3 v2] containers: added mountns/mountns02.c
Date: Mon, 25 Aug 2014 06:33:19 -0400 (EDT)	[thread overview]
Message-ID: <743848590.12257585.1408962799605.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <1408115809-18653-1-git-send-email-mmarhefk@redhat.com>


----- Original Message -----
> From: "Matus Marhefka" <mmarhefk@redhat.com>
> To: ltp-list@lists.sourceforge.net
> Sent: Friday, 15 August, 2014 5:16:49 PM
> Subject: [LTP] [PATCH 3/3 v2] containers: added mountns/mountns02.c
> 
> * Tests a private mount: private mount does not forward or receive
>   propagation.
> 
> Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
> ---
>  runtest/containers                              |   1 +
>  testcases/kernel/containers/mountns/mountns02.c | 190
>  ++++++++++++++++++++++++
>  2 files changed, 191 insertions(+)
>  create mode 100644 testcases/kernel/containers/mountns/mountns02.c
> 
> diff --git a/runtest/containers b/runtest/containers
> index bf50ae4..ab16bc0 100644
> --- a/runtest/containers
> +++ b/runtest/containers
> @@ -15,5 +15,6 @@ pidns30 pidns30
>  pidns31 pidns31
>  
>  mountns01 mountns01
> +mountns02 mountns02
>  
>  Containers	container_test.sh
> diff --git a/testcases/kernel/containers/mountns/mountns02.c
> b/testcases/kernel/containers/mountns/mountns02.c
> new file mode 100644
> index 0000000..8a677b7
> --- /dev/null
> +++ b/testcases/kernel/containers/mountns/mountns02.c
> @@ -0,0 +1,190 @@
> +/* Copyright (c) 2014 Red Hat, Inc.
> + *
> + * This program is free software: you can redistribute it and/or modify
> + * it under the terms of version 2 the GNU General Public License as
> + * published by the Free Software Foundation.
> + *
> + * 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, see <http://www.gnu.org/licenses/>.
> + ***********************************************************************
> + * File: mountns02.c
> + *
> + * Tests a private mount: private mount does not forward or receive
> + * propagation.
> + * Description:
> + * 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)
> + * 3. Bind mounts directory "A" to "A" and "B" to "B"
> + * 4. Makes both directories ("A" and "B") shared
> + * 5. Clones a new child process with CLONE_NEWNS flag - the new child
> + *    then mounts directory "A" as private and bind mounts directory
> + *    "B" to "A" (now child must wait for parent to make check - wait
> + *    is needed because mount ns in child stops exist as soon as child
> + *    terminates)
> + * 6. Parent checks if directory "A" doesn't contain the file "B"
> + *    (changes in child should not be visible in parent as mount in child
> + *    was private):
> + *    - if it doesn't, test passes
> + *    - if it does, test fails
> + * 7. Parent allows child to terminate
> + ***********************************************************************/
> +
> +#define _GNU_SOURCE
> +#include <sys/wait.h>
> +#include <sys/mount.h>
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <errno.h>
> +#include "test.h"
> +#include "usctest.h"
> +#include "libclone.h"
> +#include "safe_macros.h"
> +#include "safe_file_ops.h"
> +#include "mountns_helper.h"
> +
> +
> +#define DIRA "A"
> +#define DIRB "B"
> +char *TCID	= "mountns02";
> +int TST_TOTAL	= 1;
> +int pipefd1[2];
> +int pipefd2[2];
> +
> +
> +static void cleanup(void)
> +{
> +	close(pipefd1[0]);
> +	close(pipefd1[1]);
> +	close(pipefd2[0]);
> +	close(pipefd2[1]);
> +	umount(DIRA);
> +	umount(DIRA);
> +	umount(DIRB);
> +	tst_rmdir();

As in mountns01, try with -i 2:

# ./mountns02 -i 2
mountns02    1  TPASS  :  private mount passed
mountns02    2  TPASS  :  private mount passed
mountns02    0  TWARN  :  tst_tmpdir.c:206: tst_rmdir: rmobj(/tmp/mouY0X2xg) failed: remove(/tmp/mouY0X2xg/B) failed; errno=16: Device or resource busy

> +}
> +
> +static void setup(void)
> +{
> +	tst_require_root(NULL);
> +	check_newns();	/* from mountns_helper.h */
> +	tst_tmpdir();
> +	SAFE_MKDIR(cleanup, DIRA, 0777);
> +	SAFE_MKDIR(cleanup, DIRB, 0777);
> +	SAFE_TOUCH(cleanup, DIRA"/A", 0, NULL);
> +	SAFE_TOUCH(cleanup, DIRB"/B", 0, NULL);
> +}
> +
> +int child_func(void *arg)
> +{
> +	char buf;
> +
> +	/* makes DIRA private */
> +	if (mount("none", DIRA, "none", MS_PRIVATE, NULL) == -1) {
> +		perror("mount");
> +		write(pipefd1[1], "1", 1);
> +		return 1;
> +	}
> +
> +	/* bind mounts DIRB to DIRA making contents of DIRB visible
> +	 * in DIRA (should apply only for child as DIRA is private) */
> +        if (mount(DIRB, DIRA, "none", MS_BIND, NULL) == -1) {
> +		perror("mount");
> +		write(pipefd1[1], "1", 1);
> +		return 1;
> +	}
> +
> +	/* tells parent to stop waiting and continue */
> +	write(pipefd1[1], "0", 1);
> +
> +	/* waits for parent approval to terminate */
> +	read(pipefd2[0], &buf, 1);
> +
> +	return 0;
> +}
> +
> +static void test(void)
> +{
> +	int status, pass;
> +	char buf;
> +
> +	/* creates a pipe for synchronization between parent and child */
> +	SAFE_PIPE(cleanup, pipefd1);
> +	SAFE_PIPE(cleanup, pipefd2);
> +
> +	/* unshares the mount ns */
> +	if (unshare(CLONE_NEWNS) == -1)
> +		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
> +	/* makes sure parent mounts/umounts have no effect on a real system */
> +	SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL);
> +
> +	/* bind mounts DIRA to itself */
> +	SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL);
> +	/* bind mounts DIRB to itself */
> +	SAFE_MOUNT(cleanup, DIRB, DIRB, "none", MS_BIND, NULL);
> +
> +	/* makes mount DIRA shared */
> +	SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_SHARED, NULL);
> +	/* makes mount DIRB shared */
> +	SAFE_MOUNT(cleanup, "none", DIRB, "none", MS_SHARED, NULL);
> +
> +
> +	if (do_clone_tests(CLONE_NEWNS, child_func, NULL, NULL, NULL) == -1)
> +		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
> +
> +	/* waits for child bind mount */
> +	SAFE_READ(cleanup, 0, pipefd1[0], &buf, 1);
> +	/* in case some mount failed in child */
> +	if (buf == '1')
> +		tst_brkm(TBROK, cleanup, "child failed");

Is this check needed if child returns same value via ret code,
which is checked few lines below in EXISTSTATUS?

Regards,
Jan

> +
> +	/* as child made private mount from DIRA, parent should not
> +	 * see file "B" in DIRA; following checks if DIRA doesn't contain
> +	 * file "B" */
> +	if (access(DIRA"/B", F_OK) == -1)
> +		pass = 1;
> +	else
> +		pass = 0;
> +
> +	/* tells child to terminate */
> +	SAFE_WRITE(cleanup, 0, pipefd2[1], "0", 1);
> +
> +	SAFE_WAIT(cleanup, &status);
> +
> +	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
> +		tst_brkm(TBROK, cleanup, "child failed");
> +
> +	if (WIFSIGNALED(status)) {
> +		tst_resm(TFAIL, "child was killed with signal %s",
> +			 tst_strsig(WTERMSIG(status)));
> +		return;
> +	}
> +
> +	if (pass)
> +		tst_resm(TPASS, "private mount passed");
> +	else
> +		tst_resm(TFAIL, "private mount failed");
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	const char *msg;
> +	int lc;
> +
> +	msg = parse_opts(argc, argv, NULL, NULL);
> +	if (msg != NULL)
> +		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
> +
> +	setup();
> +
> +	for (lc = 0; TEST_LOOPING(lc); lc++)
> +		test();
> +
> +	cleanup();
> +	tst_exit();
> +}
> --
> 1.8.3.1
> 
> 
> ------------------------------------------------------------------------------
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list
> 

------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

  reply	other threads:[~2014-08-25 10:33 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-15 10:52 [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT() Matus Marhefka
2014-08-15 10:52 ` [LTP] [PATCH 2/3] containers: added mountns dir and mountns/mountns01.c Matus Marhefka
2014-08-25 10:26   ` Jan Stancek
2014-09-09 13:39     ` chrubis
2014-08-25 14:08   ` [LTP] [PATCH 2/3 v2] " Matus Marhefka
2014-08-29 12:23   ` [LTP] [PATCH 2/3 v3] " Matus Marhefka
2014-09-09 13:28     ` chrubis
2014-10-02 12:26   ` [LTP] [PATCH 2/3 v4] " Matus Marhefka
2014-08-15 10:52 ` [LTP] [PATCH 3/3] containers: added mountns/mountns02.c Matus Marhefka
2014-08-15 15:16   ` [LTP] [PATCH 3/3 v2] " Matus Marhefka
2014-08-25 10:33     ` Jan Stancek [this message]
2014-08-25 14:09     ` [LTP] [PATCH 3/3 v3] " Matus Marhefka
2014-08-29 12:24     ` [LTP] [PATCH 3/3 v4] " Matus Marhefka
2014-08-29 12:45       ` Jan Stancek
2014-09-09 13:40       ` chrubis
2014-10-02 12:27     ` [LTP] [PATCH 3/3 v5] " Matus Marhefka
2014-10-02 13:35       ` Cyril Hrubis
2014-08-25 10:11 ` [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT() Jan Stancek
2014-08-25 14:06 ` [LTP] [PATCH 1/3 v2] " Matus Marhefka
2014-08-29 12:22 ` [LTP] [PATCH 1/3 v3] lib: Added SAFE_MOUNT() and SAFE_UMOUNT() Matus Marhefka
2014-10-02 12:55   ` Cyril Hrubis

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=743848590.12257585.1408962799605.JavaMail.zimbra@redhat.com \
    --to=jstancek@redhat.com \
    --cc=ltp-list@lists.sourceforge.net \
    --cc=mmarhefk@redhat.com \
    /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