All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT()
@ 2014-08-15 10:52 Matus Marhefka
  2014-08-15 10:52 ` [LTP] [PATCH 2/3] containers: added mountns dir and mountns/mountns01.c Matus Marhefka
                   ` (4 more replies)
  0 siblings, 5 replies; 21+ messages in thread
From: Matus Marhefka @ 2014-08-15 10:52 UTC (permalink / raw)
  To: ltp-list

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 include/safe_macros.h |  9 +++++++++
 lib/safe_macros.c     | 19 +++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/include/safe_macros.h b/include/safe_macros.h
index a79c4ad..e1c2fd1 100644
--- a/include/safe_macros.h
+++ b/include/safe_macros.h
@@ -271,5 +271,14 @@ int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
 #define SAFE_RENAME(cleanup_fn, oldpath, newpath) \
 	safe_rename(__FILE__, __LINE__, (cleanup_fn), (oldpath), (newpath))
 
+int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
+	       const char *source, const char *target,
+	       const char *filesystemtype, unsigned long mountflags,
+	       const void *data);
+#define SAFE_MOUNT(cleanup_fn, source, target, filesystemtype, \
+		   mountflags, data) \
+	safe_mount(__FILE__, __LINE__, (cleanup_fn), (source), (target), \
+		   (filesystemtype), (mountflags), (data))
+
 #endif /* __SAFE_MACROS_H__ */
 #endif /* __TEST_H__ */
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index eefacae..7e49b36 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -784,3 +784,22 @@ int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
 
 	return rval;
 }
+
+int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
+	       const char *source, const char *target,
+	       const char *filesystemtype, unsigned long mountflags,
+	       const void *data)
+{
+	int rval;
+
+	rval = mount(source, target, filesystemtype, mountflags, data);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
+			 file, lineno, source, target, filesystemtype,
+			 mountflags, data);
+	}
+
+	return rval;
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 2/3] containers: added mountns dir and mountns/mountns01.c
  2014-08-15 10:52 [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT() Matus Marhefka
@ 2014-08-15 10:52 ` Matus Marhefka
  2014-08-25 10:26   ` Jan Stancek
                     ` (3 more replies)
  2014-08-15 10:52 ` [LTP] [PATCH 3/3] containers: added mountns/mountns02.c Matus Marhefka
                   ` (3 subsequent siblings)
  4 siblings, 4 replies; 21+ messages in thread
From: Matus Marhefka @ 2014-08-15 10:52 UTC (permalink / raw)
  To: ltp-list

* tests shared mount: shared mount can be replicated to as many
  mountpoints and all the replicas continue to be exactly same.

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 runtest/containers                                 |   2 +
 testcases/kernel/containers/mountns/Makefile       |  23 ++++
 testcases/kernel/containers/mountns/mountns01.c    | 142 +++++++++++++++++++++
 .../kernel/containers/mountns/mountns_helper.h     |  39 ++++++
 4 files changed, 206 insertions(+)
 create mode 100644 testcases/kernel/containers/mountns/Makefile
 create mode 100644 testcases/kernel/containers/mountns/mountns01.c
 create mode 100644 testcases/kernel/containers/mountns/mountns_helper.h

diff --git a/runtest/containers b/runtest/containers
index 5f5eeab..bf50ae4 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -14,4 +14,6 @@ pidns20 pidns20
 pidns30 pidns30
 pidns31 pidns31
 
+mountns01 mountns01
+
 Containers	container_test.sh
diff --git a/testcases/kernel/containers/mountns/Makefile b/testcases/kernel/containers/mountns/Makefile
new file mode 100644
index 0000000..f9b6b99
--- /dev/null
+++ b/testcases/kernel/containers/mountns/Makefile
@@ -0,0 +1,23 @@
+# 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/>.
+##############################################################################
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(abs_srcdir)/../Makefile.inc
+
+LDLIBS                  := -lclone -lltp
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/containers/mountns/mountns01.c b/testcases/kernel/containers/mountns/mountns01.c
new file mode 100644
index 0000000..0393e0a
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns01.c
@@ -0,0 +1,142 @@
+/* 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: mountns01.c
+ *
+ * Tests a shared mount: shared mount can be replicated to as many
+ * mountpoints and all the replicas continue to be exactly same.
+ * 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 bind mounts directory "B" to "A"
+ * 6. Parent then checks if directory "A" contains the file "B"
+ *    (changes in child should be visible in parent as mounts were
+ *    shared):
+ *    - if it does, test passes
+ *    - if it doesn't, test fails
+ ***********************************************************************/
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <sys/mount.h>
+#include <stdio.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	= "mountns01";
+int TST_TOTAL	= 1;
+
+
+static void cleanup(void)
+{
+	umount(DIRA);
+	umount(DIRA);
+	umount(DIRB);
+	tst_rmdir();
+}
+
+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)
+{
+	/* 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;
+	}
+	return 0;
+}
+
+static void test(void)
+{
+	int status;
+
+	/* 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");
+
+	SAFE_WAIT(cleanup, &status);
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
+		tst_brkm(TBROK, cleanup, "mount (in child) failed");
+
+	if (WIFSIGNALED(status)) {
+		tst_resm(TFAIL, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	/* as child shared mounts with parent, parent should also
+	 * see file "B" in DIRA; following checks if DIRA contains file "B" */
+	if (access(DIRA"/B", F_OK) != -1)
+		tst_resm(TPASS, "share mount passed");
+	else
+		tst_resm(TFAIL, "share 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();
+}
diff --git a/testcases/kernel/containers/mountns/mountns_helper.h b/testcases/kernel/containers/mountns/mountns_helper.h
new file mode 100644
index 0000000..e305028
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns_helper.h
@@ -0,0 +1,39 @@
+/* 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/>.
+ */
+
+#include "libclone.h"
+#include "test.h"
+#include "safe_macros.h"
+
+static int dummy_child(void *v)
+{
+	(void) v;
+	return 0;
+}
+
+static int check_newns(void)
+{
+	int pid, status;
+
+	if (tst_kvercmp(2, 4, 19) < 0)
+		tst_brkm(TCONF, NULL, "CLONE_NEWNS not supported");
+
+	pid = do_clone_unshare_test(T_CLONE, CLONE_NEWNS, dummy_child, NULL);
+	if (pid == -1)
+		tst_brkm(TCONF | TERRNO, NULL, "CLONE_NEWNS not supported");
+	SAFE_WAIT(NULL, &status);
+
+	return 0;
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 3/3] containers: added mountns/mountns02.c
  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-15 10:52 ` Matus Marhefka
  2014-08-15 15:16   ` [LTP] [PATCH 3/3 v2] " Matus Marhefka
  2014-08-25 10:11 ` [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT() Jan Stancek
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 21+ messages in thread
From: Matus Marhefka @ 2014-08-15 10:52 UTC (permalink / raw)
  To: ltp-list

* 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 | 151 ++++++++++++++++++++++++
 2 files changed, 152 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..8bd0100
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns02.c
@@ -0,0 +1,151 @@
+/* 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"
+ * 6. Parent then 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
+ ***********************************************************************/
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <sys/mount.h>
+#include <stdio.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;
+
+
+static void cleanup(void)
+{
+	umount(DIRA);
+	umount(DIRA);
+	umount(DIRB);
+	tst_rmdir();
+}
+
+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)
+{
+	/* makes DIRA private */
+	if (mount("none", DIRA, "none", MS_PRIVATE, NULL) == -1) {
+		perror("mount");
+		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");
+		return 1;
+	}
+
+	return 0;
+}
+
+static void test(void)
+{
+	int status;
+
+	/* 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");
+
+	SAFE_WAIT(cleanup, &status);
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
+		tst_brkm(TBROK, cleanup, "mount (in child) failed");
+
+	if (WIFSIGNALED(status)) {
+		tst_resm(TFAIL, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	/* 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)
+		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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 3/3 v2] containers: added mountns/mountns02.c
  2014-08-15 10:52 ` [LTP] [PATCH 3/3] containers: added mountns/mountns02.c Matus Marhefka
@ 2014-08-15 15:16   ` Matus Marhefka
  2014-08-25 10:33     ` Jan Stancek
                       ` (3 more replies)
  0 siblings, 4 replies; 21+ messages in thread
From: Matus Marhefka @ 2014-08-15 15:16 UTC (permalink / raw)
  To: ltp-list

* 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();
+}
+
+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");
+
+	/* 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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT()
  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-15 10:52 ` [LTP] [PATCH 3/3] containers: added mountns/mountns02.c Matus Marhefka
@ 2014-08-25 10:11 ` 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
  4 siblings, 0 replies; 21+ messages in thread
From: Jan Stancek @ 2014-08-25 10:11 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list





----- Original Message -----
> From: "Matus Marhefka" <mmarhefk@redhat.com>
> To: ltp-list@lists.sourceforge.net
> Sent: Friday, 15 August, 2014 12:52:43 PM
> Subject: [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT()
> 
> Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
> ---
>  include/safe_macros.h |  9 +++++++++
>  lib/safe_macros.c     | 19 +++++++++++++++++++
>  2 files changed, 28 insertions(+)
> 
> diff --git a/include/safe_macros.h b/include/safe_macros.h
> index a79c4ad..e1c2fd1 100644
> --- a/include/safe_macros.h
> +++ b/include/safe_macros.h
> @@ -271,5 +271,14 @@ int safe_rename(const char *file, const int lineno, void
> (*cleanup_fn)(void),
>  #define SAFE_RENAME(cleanup_fn, oldpath, newpath) \
>  	safe_rename(__FILE__, __LINE__, (cleanup_fn), (oldpath), (newpath))
>  
> +int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
> +	       const char *source, const char *target,
> +	       const char *filesystemtype, unsigned long mountflags,
> +	       const void *data);
> +#define SAFE_MOUNT(cleanup_fn, source, target, filesystemtype, \
> +		   mountflags, data) \
> +	safe_mount(__FILE__, __LINE__, (cleanup_fn), (source), (target), \
> +		   (filesystemtype), (mountflags), (data))
> +
>  #endif /* __SAFE_MACROS_H__ */
>  #endif /* __TEST_H__ */
> diff --git a/lib/safe_macros.c b/lib/safe_macros.c
> index eefacae..7e49b36 100644
> --- a/lib/safe_macros.c
> +++ b/lib/safe_macros.c
> @@ -784,3 +784,22 @@ int safe_rename(const char *file, const int lineno, void
> (*cleanup_fn)(void),
>  
>  	return rval;
>  }

Just a small nit: Can you add "#include <sys/mount.h>" to get rid of this warning:
safe_macros.c:686: warning: implicit declaration of function ‘mount’

Regards,
Jan

> +
> +int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
> +	       const char *source, const char *target,
> +	       const char *filesystemtype, unsigned long mountflags,
> +	       const void *data)
> +{
> +	int rval;
> +
> +	rval = mount(source, target, filesystemtype, mountflags, data);
> +
> +	if (rval == -1) {
> +		tst_brkm(TBROK | TERRNO, cleanup_fn,
> +			 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
> +			 file, lineno, source, target, filesystemtype,
> +			 mountflags, data);
> +	}
> +
> +	return rval;
> +}
> --
> 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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 2/3] containers: added mountns dir and mountns/mountns01.c
  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
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 21+ messages in thread
From: Jan Stancek @ 2014-08-25 10:26 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list




----- Original Message -----
> From: "Matus Marhefka" <mmarhefk@redhat.com>
> To: ltp-list@lists.sourceforge.net
> Sent: Friday, 15 August, 2014 12:52:44 PM
> Subject: [LTP] [PATCH 2/3] containers: added mountns dir and	mountns/mountns01.c
> 
> * tests shared mount: shared mount can be replicated to as many
>   mountpoints and all the replicas continue to be exactly same.
> 
> Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
> ---
>  runtest/containers                                 |   2 +
>  testcases/kernel/containers/mountns/Makefile       |  23 ++++
>  testcases/kernel/containers/mountns/mountns01.c    | 142
>  +++++++++++++++++++++
>  .../kernel/containers/mountns/mountns_helper.h     |  39 ++++++
>  4 files changed, 206 insertions(+)
>  create mode 100644 testcases/kernel/containers/mountns/Makefile
>  create mode 100644 testcases/kernel/containers/mountns/mountns01.c
>  create mode 100644 testcases/kernel/containers/mountns/mountns_helper.h

Hi,

for both testcases, can you add them to "../.gitignore"?

> 
> diff --git a/runtest/containers b/runtest/containers
> index 5f5eeab..bf50ae4 100644
> --- a/runtest/containers
> +++ b/runtest/containers
> @@ -14,4 +14,6 @@ pidns20 pidns20
>  pidns30 pidns30
>  pidns31 pidns31
>  
> +mountns01 mountns01
> +
>  Containers	container_test.sh
> diff --git a/testcases/kernel/containers/mountns/Makefile
> b/testcases/kernel/containers/mountns/Makefile
> new file mode 100644
> index 0000000..f9b6b99
> --- /dev/null
> +++ b/testcases/kernel/containers/mountns/Makefile
> @@ -0,0 +1,23 @@
> +# 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/>.
> +##############################################################################
> +
> +top_srcdir              ?= ../../../..
> +
> +include $(top_srcdir)/include/mk/testcases.mk
> +include $(abs_srcdir)/../Makefile.inc
> +
> +LDLIBS                  := -lclone -lltp
> +
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/containers/mountns/mountns01.c
> b/testcases/kernel/containers/mountns/mountns01.c
> new file mode 100644
> index 0000000..0393e0a
> --- /dev/null
> +++ b/testcases/kernel/containers/mountns/mountns01.c
> @@ -0,0 +1,142 @@
> +/* 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: mountns01.c
> + *
> + * Tests a shared mount: shared mount can be replicated to as many
> + * mountpoints and all the replicas continue to be exactly same.
> + * 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 bind mounts directory "B" to "A"
> + * 6. Parent then checks if directory "A" contains the file "B"
> + *    (changes in child should be visible in parent as mounts were
> + *    shared):
> + *    - if it does, test passes
> + *    - if it doesn't, test fails
> + ***********************************************************************/
> +
> +#define _GNU_SOURCE
> +#include <sys/wait.h>
> +#include <sys/mount.h>
> +#include <stdio.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	= "mountns01";
> +int TST_TOTAL	= 1;
> +
> +
> +static void cleanup(void)
> +{
> +	umount(DIRA);
> +	umount(DIRA);
> +	umount(DIRB);

Try "./mountns01 -i 2":

# ./mountns01 -i 2
mountns01    1  TPASS  :  share mount passed
mountns01    2  TPASS  :  share mount passed
mountns01    0  TWARN  :  tst_tmpdir.c:206: tst_rmdir: rmobj(/tmp/mour49V5j) failed: remove(/tmp/mour49V5j/A) failed; errno=16: Device or resource busy

> +	tst_rmdir();
> +}
> +
> +static void setup(void)
> +{
> +	tst_require_root(NULL);
> +	check_newns();	/* from mountns_helper.h */

We should probably also check availability of MS_SHARED/MS_PRIVATE/MS_REC/etc.
For example, an old distro like RHEL5.3 has kernel support, but these
flags are not defined in glibc headers ().

Regards,
Jan

> +	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);
> +}
> +

------------------------------------------------------------------------------
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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 3/3 v2] containers: added mountns/mountns02.c
  2014-08-15 15:16   ` [LTP] [PATCH 3/3 v2] " Matus Marhefka
@ 2014-08-25 10:33     ` Jan Stancek
  2014-08-25 14:09     ` [LTP] [PATCH 3/3 v3] " Matus Marhefka
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 21+ messages in thread
From: Jan Stancek @ 2014-08-25 10:33 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list


----- 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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 1/3 v2] lib: Add SAFE_MOUNT()
  2014-08-15 10:52 [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT() Matus Marhefka
                   ` (2 preceding siblings ...)
  2014-08-25 10:11 ` [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT() Jan Stancek
@ 2014-08-25 14:06 ` Matus Marhefka
  2014-08-29 12:22 ` [LTP] [PATCH 1/3 v3] lib: Added SAFE_MOUNT() and SAFE_UMOUNT() Matus Marhefka
  4 siblings, 0 replies; 21+ messages in thread
From: Matus Marhefka @ 2014-08-25 14:06 UTC (permalink / raw)
  To: ltp-list

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 include/safe_macros.h |  8 ++++++++
 lib/safe_macros.c     | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/include/safe_macros.h b/include/safe_macros.h
index 5ee4d42..4648f2a 100644
--- a/include/safe_macros.h
+++ b/include/safe_macros.h
@@ -363,6 +363,14 @@ static inline off_t safe_lseek(const char *file, const int lineno,
 #define SAFE_LSEEK(cleanup_fn, fd, offset, whence) \
 	safe_lseek(__FILE__, __LINE__, cleanup_fn, (fd), (offset), (whence))
 
+int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
+	       const char *source, const char *target,
+	       const char *filesystemtype, unsigned long mountflags,
+	       const void *data);
+#define SAFE_MOUNT(cleanup_fn, source, target, filesystemtype, \
+		   mountflags, data) \
+	safe_mount(__FILE__, __LINE__, (cleanup_fn), (source), (target), \
+		   (filesystemtype), (mountflags), (data))
 
 #endif /* __SAFE_MACROS_H__ */
 #endif /* __TEST_H__ */
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 76d781f..c949826 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -4,6 +4,7 @@
 #include <sys/resource.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <sys/mount.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <libgen.h>
@@ -675,3 +676,22 @@ int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
 
 	return rval;
 }
+
+int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
+	       const char *source, const char *target,
+	       const char *filesystemtype, unsigned long mountflags,
+	       const void *data)
+{
+	int rval;
+
+	rval = mount(source, target, filesystemtype, mountflags, data);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
+			 file, lineno, source, target, filesystemtype,
+			 mountflags, data);
+	}
+
+	return rval;
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 2/3 v2] containers: added mountns dir and mountns/mountns01.c
  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-08-25 14:08   ` Matus Marhefka
  2014-08-29 12:23   ` [LTP] [PATCH 2/3 v3] " Matus Marhefka
  2014-10-02 12:26   ` [LTP] [PATCH 2/3 v4] " Matus Marhefka
  3 siblings, 0 replies; 21+ messages in thread
From: Matus Marhefka @ 2014-08-25 14:08 UTC (permalink / raw)
  To: ltp-list

* tests shared mount: shared mount can be replicated to as many
  mountpoints and all the replicas continue to be exactly same.

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 runtest/containers                                 |   2 +
 testcases/kernel/containers/.gitignore             |   1 +
 testcases/kernel/containers/mountns/Makefile       |  23 ++++
 testcases/kernel/containers/mountns/mountns01.c    | 152 +++++++++++++++++++++
 .../kernel/containers/mountns/mountns_helper.h     |  39 ++++++
 5 files changed, 217 insertions(+)
 create mode 100644 testcases/kernel/containers/mountns/Makefile
 create mode 100644 testcases/kernel/containers/mountns/mountns01.c
 create mode 100644 testcases/kernel/containers/mountns/mountns_helper.h

diff --git a/runtest/containers b/runtest/containers
index 558d7eb..f298c7e 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -55,3 +55,5 @@ utstest_clone_2 utstest clone 2
 utstest_clone_3 utstest clone 3
 utstest_clone_4 utstest clone 4
 utstest_clone_5 utstest clone 5
+
+mountns01 mountns01
diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
index f6f5b14..254d1b9 100644
--- a/testcases/kernel/containers/.gitignore
+++ b/testcases/kernel/containers/.gitignore
@@ -1 +1,2 @@
 /check_for_unshare
+mountns/mountns01
diff --git a/testcases/kernel/containers/mountns/Makefile b/testcases/kernel/containers/mountns/Makefile
new file mode 100644
index 0000000..f9b6b99
--- /dev/null
+++ b/testcases/kernel/containers/mountns/Makefile
@@ -0,0 +1,23 @@
+# 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/>.
+##############################################################################
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(abs_srcdir)/../Makefile.inc
+
+LDLIBS                  := -lclone -lltp
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/containers/mountns/mountns01.c b/testcases/kernel/containers/mountns/mountns01.c
new file mode 100644
index 0000000..5932300
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns01.c
@@ -0,0 +1,152 @@
+/* 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: mountns01.c
+ *
+ * Tests a shared mount: shared mount can be replicated to as many
+ * mountpoints and all the replicas continue to be exactly same.
+ * 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 bind mounts directory "B" to "A"
+ * 6. Parent then checks if directory "A" contains the file "B"
+ *    (changes in child should be visible in parent as mounts were
+ *    shared):
+ *    - if it does, test passes
+ *    - if it doesn't, test fails
+ ***********************************************************************/
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <sys/mount.h>
+#include <stdio.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	= "mountns01";
+int TST_TOTAL	= 1;
+
+
+static void cleanup(void)
+{
+	umount(DIRA);
+	umount(DIRA);
+	umount(DIRB);
+	tst_rmdir();
+}
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	check_newns();	/* from mountns_helper.h */
+
+	/* checks if MS_SHARED/MS_PRIVATE/MS_REC mountflags are defined */
+	#if !defined(MS_SHARED) || !defined(MS_PRIVATE) || !defined(MS_SHARED)
+		tst_brkm(TCONF, NULL, "needed mountflags are not defined");
+	#endif
+
+	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)
+{
+	/* 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;
+	}
+	return 0;
+}
+
+static void test(void)
+{
+	int status;
+
+	/* 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");
+
+	SAFE_WAIT(cleanup, &status);
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
+		tst_brkm(TBROK, cleanup, "mount (in child) failed");
+
+	if (WIFSIGNALED(status)) {
+		tst_resm(TFAIL, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	/* as child shared mounts with parent, parent should also
+	 * see file "B" in DIRA; following checks if DIRA contains file "B" */
+	if (access(DIRA"/B", F_OK) != -1)
+		tst_resm(TPASS, "share mount passed");
+	else
+		tst_resm(TFAIL, "share mount failed");
+
+	umount(DIRA);
+	umount(DIRA);
+	umount(DIRB);
+}
+
+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();
+}
diff --git a/testcases/kernel/containers/mountns/mountns_helper.h b/testcases/kernel/containers/mountns/mountns_helper.h
new file mode 100644
index 0000000..e305028
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns_helper.h
@@ -0,0 +1,39 @@
+/* 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/>.
+ */
+
+#include "libclone.h"
+#include "test.h"
+#include "safe_macros.h"
+
+static int dummy_child(void *v)
+{
+	(void) v;
+	return 0;
+}
+
+static int check_newns(void)
+{
+	int pid, status;
+
+	if (tst_kvercmp(2, 4, 19) < 0)
+		tst_brkm(TCONF, NULL, "CLONE_NEWNS not supported");
+
+	pid = do_clone_unshare_test(T_CLONE, CLONE_NEWNS, dummy_child, NULL);
+	if (pid == -1)
+		tst_brkm(TCONF | TERRNO, NULL, "CLONE_NEWNS not supported");
+	SAFE_WAIT(NULL, &status);
+
+	return 0;
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 3/3 v3] containers: added mountns/mountns02.c
  2014-08-15 15:16   ` [LTP] [PATCH 3/3 v2] " Matus Marhefka
  2014-08-25 10:33     ` Jan Stancek
@ 2014-08-25 14:09     ` Matus Marhefka
  2014-08-29 12:24     ` [LTP] [PATCH 3/3 v4] " Matus Marhefka
  2014-10-02 12:27     ` [LTP] [PATCH 3/3 v5] " Matus Marhefka
  3 siblings, 0 replies; 21+ messages in thread
From: Matus Marhefka @ 2014-08-25 14:09 UTC (permalink / raw)
  To: ltp-list

* 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/.gitignore          |   1 +
 testcases/kernel/containers/mountns/mountns02.c | 201 ++++++++++++++++++++++++
 3 files changed, 203 insertions(+)
 create mode 100644 testcases/kernel/containers/mountns/mountns02.c

diff --git a/runtest/containers b/runtest/containers
index f298c7e..56977c0 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -57,3 +57,4 @@ utstest_clone_4 utstest clone 4
 utstest_clone_5 utstest clone 5
 
 mountns01 mountns01
+mountns02 mountns02
diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
index 254d1b9..f175296 100644
--- a/testcases/kernel/containers/.gitignore
+++ b/testcases/kernel/containers/.gitignore
@@ -1,2 +1,3 @@
 /check_for_unshare
 mountns/mountns01
+mountns/mountns02
diff --git a/testcases/kernel/containers/mountns/mountns02.c b/testcases/kernel/containers/mountns/mountns02.c
new file mode 100644
index 0000000..2890748
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns02.c
@@ -0,0 +1,201 @@
+/* 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(DIRB);
+	tst_rmdir();
+}
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	check_newns();	/* from mountns_helper.h */
+
+	/* checks if MS_SHARED/MS_PRIVATE/MS_REC mountflags are defined */
+	#if !defined(MS_SHARED) || !defined(MS_PRIVATE) || !defined(MS_SHARED)
+		tst_brkm(TCONF, NULL, "needed mountflags are not defined");
+	#endif
+
+	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);
+
+	umount(DIRA);
+	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);
+
+	/* 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 (if no error occured in child) */
+	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;
+	}
+
+	close(pipefd1[0]);
+	close(pipefd1[1]);
+	close(pipefd2[0]);
+	close(pipefd2[1]);
+	umount(DIRA);
+	umount(DIRB);
+
+	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


------------------------------------------------------------------------------
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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 1/3 v3] lib: Added SAFE_MOUNT() and SAFE_UMOUNT()
  2014-08-15 10:52 [LTP] [PATCH 1/3] lib: Add SAFE_MOUNT() Matus Marhefka
                   ` (3 preceding siblings ...)
  2014-08-25 14:06 ` [LTP] [PATCH 1/3 v2] " Matus Marhefka
@ 2014-08-29 12:22 ` Matus Marhefka
  2014-10-02 12:55   ` Cyril Hrubis
  4 siblings, 1 reply; 21+ messages in thread
From: Matus Marhefka @ 2014-08-29 12:22 UTC (permalink / raw)
  To: ltp-list

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 include/safe_macros.h | 15 ++++++++++++++-
 lib/safe_macros.c     | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/include/safe_macros.h b/include/safe_macros.h
index abd5f6e..d832d4f 100644
--- a/include/safe_macros.h
+++ b/include/safe_macros.h
@@ -226,6 +226,20 @@ int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
 #define SAFE_RENAME(cleanup_fn, oldpath, newpath) \
 	safe_rename(__FILE__, __LINE__, (cleanup_fn), (oldpath), (newpath))
 
+int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
+	       const char *source, const char *target,
+	       const char *filesystemtype, unsigned long mountflags,
+	       const void *data);
+#define SAFE_MOUNT(cleanup_fn, source, target, filesystemtype, \
+		   mountflags, data) \
+	safe_mount(__FILE__, __LINE__, (cleanup_fn), (source), (target), \
+		   (filesystemtype), (mountflags), (data))
+
+int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
+		const char *target);
+#define SAFE_UMOUNT(cleanup_fn, target) \
+	safe_umount(__FILE__, __LINE__, (cleanup_fn), (target))
+
 /*
  * following functions are inline because the behaviour may depend on
  * -D_FILE_OFFSET_BITS=64 -DOFF_T=__off64_t compile flags
@@ -354,7 +368,6 @@ static inline off_t safe_lseek(const char *file, const int lineno,
 #define SAFE_LSEEK(cleanup_fn, fd, offset, whence) \
 	safe_lseek(__FILE__, __LINE__, cleanup_fn, (fd), (offset), (whence))
 
-
 static inline int safe_getrlimit(const char *file, const int lineno,
 	void (cleanup_fn)(void), int resource, struct rlimit *rlim)
 {
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 05cbebd..90885fd 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -4,6 +4,7 @@
 #include <sys/resource.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <sys/mount.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <libgen.h>
@@ -643,3 +644,38 @@ int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
 
 	return rval;
 }
+
+int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
+	       const char *source, const char *target,
+	       const char *filesystemtype, unsigned long mountflags,
+	       const void *data)
+{
+	int rval;
+
+	rval = mount(source, target, filesystemtype, mountflags, data);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
+			 file, lineno, source, target, filesystemtype,
+			 mountflags, data);
+	}
+
+	return rval;
+}
+
+int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
+		const char *target)
+{
+	int rval;
+
+	rval = umount(target);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: umount(%s) failed",
+			 file, lineno, target);
+	}
+
+	return rval;
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 2/3 v3] containers: added mountns dir and mountns/mountns01.c
  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-08-25 14:08   ` [LTP] [PATCH 2/3 v2] " Matus Marhefka
@ 2014-08-29 12:23   ` Matus Marhefka
  2014-09-09 13:28     ` chrubis
  2014-10-02 12:26   ` [LTP] [PATCH 2/3 v4] " Matus Marhefka
  3 siblings, 1 reply; 21+ messages in thread
From: Matus Marhefka @ 2014-08-29 12:23 UTC (permalink / raw)
  To: ltp-list

* tests shared mount: shared mount can be replicated to as many
  mountpoints and all the replicas continue to be exactly same.

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 runtest/containers                                 |   2 +
 testcases/kernel/containers/.gitignore             |   1 +
 testcases/kernel/containers/mountns/Makefile       |  23 +++
 testcases/kernel/containers/mountns/mountns01.c    | 156 +++++++++++++++++++++
 .../kernel/containers/mountns/mountns_helper.h     |  39 ++++++
 5 files changed, 221 insertions(+)
 create mode 100644 testcases/kernel/containers/mountns/Makefile
 create mode 100644 testcases/kernel/containers/mountns/mountns01.c
 create mode 100644 testcases/kernel/containers/mountns/mountns_helper.h

diff --git a/runtest/containers b/runtest/containers
index 558d7eb..f298c7e 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -55,3 +55,5 @@ utstest_clone_2 utstest clone 2
 utstest_clone_3 utstest clone 3
 utstest_clone_4 utstest clone 4
 utstest_clone_5 utstest clone 5
+
+mountns01 mountns01
diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
index f6f5b14..254d1b9 100644
--- a/testcases/kernel/containers/.gitignore
+++ b/testcases/kernel/containers/.gitignore
@@ -1 +1,2 @@
 /check_for_unshare
+mountns/mountns01
diff --git a/testcases/kernel/containers/mountns/Makefile b/testcases/kernel/containers/mountns/Makefile
new file mode 100644
index 0000000..f9b6b99
--- /dev/null
+++ b/testcases/kernel/containers/mountns/Makefile
@@ -0,0 +1,23 @@
+# 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/>.
+##############################################################################
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(abs_srcdir)/../Makefile.inc
+
+LDLIBS                  := -lclone -lltp
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/containers/mountns/mountns01.c b/testcases/kernel/containers/mountns/mountns01.c
new file mode 100644
index 0000000..93600f4
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns01.c
@@ -0,0 +1,156 @@
+/* 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: mountns01.c
+ *
+ * Tests a shared mount: shared mount can be replicated to as many
+ * mountpoints and all the replicas continue to be exactly same.
+ * 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 bind mounts directory "B" to "A"
+ * 6. Parent then checks if directory "A" contains the file "B"
+ *    (changes in child should be visible in parent as mounts were
+ *    shared):
+ *    - if it does, test passes
+ *    - if it doesn't, test fails
+ ***********************************************************************/
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <sys/mount.h>
+#include <stdio.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	= "mountns01";
+int TST_TOTAL	= 1;
+
+
+/* checks if following mountflags are defined */
+#if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
+
+static void cleanup(void)
+{
+	umount(DIRA);
+	umount(DIRA);
+	umount(DIRB);
+	tst_rmdir();
+}
+
+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)
+{
+	/* 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;
+	}
+	return 0;
+}
+
+static void test(void)
+{
+	int status;
+
+	/* 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");
+
+	SAFE_WAIT(cleanup, &status);
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
+		tst_brkm(TBROK, cleanup, "mount (in child) failed");
+
+	if (WIFSIGNALED(status)) {
+		tst_resm(TFAIL, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	/* as child shared mounts with parent, parent should also
+	 * see file "B" in DIRA; following checks if DIRA contains file "B" */
+	if (access(DIRA"/B", F_OK) != -1)
+		tst_resm(TPASS, "share mount passed");
+	else
+		tst_resm(TFAIL, "share mount failed");
+
+	SAFE_UMOUNT(cleanup, DIRA);
+	SAFE_UMOUNT(cleanup, DIRA);
+	SAFE_UMOUNT(cleanup, DIRB);
+}
+
+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();
+}
+
+#else /* MS_SHARED && MS_PRIVATE && MS_REC */
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
+}
+#endif
diff --git a/testcases/kernel/containers/mountns/mountns_helper.h b/testcases/kernel/containers/mountns/mountns_helper.h
new file mode 100644
index 0000000..e305028
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns_helper.h
@@ -0,0 +1,39 @@
+/* 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/>.
+ */
+
+#include "libclone.h"
+#include "test.h"
+#include "safe_macros.h"
+
+static int dummy_child(void *v)
+{
+	(void) v;
+	return 0;
+}
+
+static int check_newns(void)
+{
+	int pid, status;
+
+	if (tst_kvercmp(2, 4, 19) < 0)
+		tst_brkm(TCONF, NULL, "CLONE_NEWNS not supported");
+
+	pid = do_clone_unshare_test(T_CLONE, CLONE_NEWNS, dummy_child, NULL);
+	if (pid == -1)
+		tst_brkm(TCONF | TERRNO, NULL, "CLONE_NEWNS not supported");
+	SAFE_WAIT(NULL, &status);
+
+	return 0;
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 3/3 v4] containers: added mountns/mountns02.c
  2014-08-15 15:16   ` [LTP] [PATCH 3/3 v2] " Matus Marhefka
  2014-08-25 10:33     ` Jan Stancek
  2014-08-25 14:09     ` [LTP] [PATCH 3/3 v3] " Matus Marhefka
@ 2014-08-29 12:24     ` 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
  3 siblings, 2 replies; 21+ messages in thread
From: Matus Marhefka @ 2014-08-29 12:24 UTC (permalink / raw)
  To: ltp-list

* 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/.gitignore          |   1 +
 testcases/kernel/containers/mountns/mountns02.c | 204 ++++++++++++++++++++++++
 3 files changed, 206 insertions(+)
 create mode 100644 testcases/kernel/containers/mountns/mountns02.c

diff --git a/runtest/containers b/runtest/containers
index f298c7e..56977c0 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -57,3 +57,4 @@ utstest_clone_4 utstest clone 4
 utstest_clone_5 utstest clone 5
 
 mountns01 mountns01
+mountns02 mountns02
diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
index 254d1b9..f175296 100644
--- a/testcases/kernel/containers/.gitignore
+++ b/testcases/kernel/containers/.gitignore
@@ -1,2 +1,3 @@
 /check_for_unshare
 mountns/mountns01
+mountns/mountns02
diff --git a/testcases/kernel/containers/mountns/mountns02.c b/testcases/kernel/containers/mountns/mountns02.c
new file mode 100644
index 0000000..bbc2b5b
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns02.c
@@ -0,0 +1,204 @@
+/* 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];
+
+/* checks if following mountflags are defined */
+#if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
+
+static void cleanup(void)
+{
+	close(pipefd1[0]);
+	close(pipefd1[1]);
+	close(pipefd2[0]);
+	close(pipefd2[1]);
+	umount(DIRA);
+	umount(DIRB);
+	tst_rmdir();
+}
+
+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);
+
+	umount(DIRA);
+	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);
+
+	/* 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 (if no error occured in child) */
+	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;
+	}
+
+	close(pipefd1[0]);
+	close(pipefd1[1]);
+	close(pipefd2[0]);
+	close(pipefd2[1]);
+	SAFE_UMOUNT(cleanup, DIRA);
+	SAFE_UMOUNT(cleanup, DIRB);
+
+	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();
+}
+
+#else /* MS_SHARED && MS_PRIVATE && MS_REC */
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
+}
+#endif
-- 
1.8.3.1


------------------------------------------------------------------------------
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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 3/3 v4] containers: added mountns/mountns02.c
  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
  1 sibling, 0 replies; 21+ messages in thread
From: Jan Stancek @ 2014-08-29 12:45 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list



----- Original Message -----
> From: "Matus Marhefka" <mmarhefk@redhat.com>
> To: ltp-list@lists.sourceforge.net
> Sent: Friday, 29 August, 2014 2:24:30 PM
> Subject: [LTP] [PATCH 3/3 v4] 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/.gitignore          |   1 +
>  testcases/kernel/containers/mountns/mountns02.c | 204
>  ++++++++++++++++++++++++
>  3 files changed, 206 insertions(+)
>  create mode 100644 testcases/kernel/containers/mountns/mountns02.c

This series looks good to me. I also tested it on RHEL5.3/RHEL6.5/RHEL7.0.

Reviewed-by: Jan Stancek <jstancek@redhat.com>

Regards,
Jan

> 
> diff --git a/runtest/containers b/runtest/containers
> index f298c7e..56977c0 100644
> --- a/runtest/containers
> +++ b/runtest/containers
> @@ -57,3 +57,4 @@ utstest_clone_4 utstest clone 4
>  utstest_clone_5 utstest clone 5
>  
>  mountns01 mountns01
> +mountns02 mountns02
> diff --git a/testcases/kernel/containers/.gitignore
> b/testcases/kernel/containers/.gitignore
> index 254d1b9..f175296 100644
> --- a/testcases/kernel/containers/.gitignore
> +++ b/testcases/kernel/containers/.gitignore
> @@ -1,2 +1,3 @@
>  /check_for_unshare
>  mountns/mountns01
> +mountns/mountns02
> diff --git a/testcases/kernel/containers/mountns/mountns02.c
> b/testcases/kernel/containers/mountns/mountns02.c
> new file mode 100644
> index 0000000..bbc2b5b
> --- /dev/null
> +++ b/testcases/kernel/containers/mountns/mountns02.c
> @@ -0,0 +1,204 @@
> +/* 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];
> +
> +/* checks if following mountflags are defined */
> +#if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
> +
> +static void cleanup(void)
> +{
> +	close(pipefd1[0]);
> +	close(pipefd1[1]);
> +	close(pipefd2[0]);
> +	close(pipefd2[1]);
> +	umount(DIRA);
> +	umount(DIRB);
> +	tst_rmdir();
> +}
> +
> +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);
> +
> +	umount(DIRA);
> +	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);
> +
> +	/* 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 (if no error occured in child) */
> +	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;
> +	}
> +
> +	close(pipefd1[0]);
> +	close(pipefd1[1]);
> +	close(pipefd2[0]);
> +	close(pipefd2[1]);
> +	SAFE_UMOUNT(cleanup, DIRA);
> +	SAFE_UMOUNT(cleanup, DIRB);
> +
> +	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();
> +}
> +
> +#else /* MS_SHARED && MS_PRIVATE && MS_REC */
> +int main(void)
> +{
> +	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
> +}
> +#endif
> --
> 1.8.3.1
> 
> 
> ------------------------------------------------------------------------------
> 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
> 

------------------------------------------------------------------------------
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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 2/3 v3] containers: added mountns dir and mountns/mountns01.c
  2014-08-29 12:23   ` [LTP] [PATCH 2/3 v3] " Matus Marhefka
@ 2014-09-09 13:28     ` chrubis
  0 siblings, 0 replies; 21+ messages in thread
From: chrubis @ 2014-09-09 13:28 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list

Hi!
> +#define _GNU_SOURCE
> +#include <sys/wait.h>
> +#include <sys/mount.h>
> +#include <stdio.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	= "mountns01";
> +int TST_TOTAL	= 1;
> +
> +
> +/* checks if following mountflags are defined */

This comment is close to "commenting the obvious" category. Everybody
can tell what the preprocessor #if below do. I would personaly omit this
comment.

> +#if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
> +
> +static void cleanup(void)
> +{
> +	umount(DIRA);
> +	umount(DIRA);
> +	umount(DIRB);
> +	tst_rmdir();
> +}
> +
> +static void setup(void)
> +{
> +	tst_require_root(NULL);
> +	check_newns();	/* from mountns_helper.h */

Here as well. It's not a good idea to comment where functions are
implemented, it only makes the source code longer and less clear.


The rest of the test looks good to me.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 2/3] containers: added mountns dir and mountns/mountns01.c
  2014-08-25 10:26   ` Jan Stancek
@ 2014-09-09 13:39     ` chrubis
  0 siblings, 0 replies; 21+ messages in thread
From: chrubis @ 2014-09-09 13:39 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

Hi!
> We should probably also check availability of MS_SHARED/MS_PRIVATE/MS_REC/etc.
> For example, an old distro like RHEL5.3 has kernel support, but these
> flags are not defined in glibc headers ().

We can also add these to an header to include/lapi/ to ensure that they
are defined (we do have these definitions allready in
testcases/kernel/fs/fs_bind/bin/smount.c so technically we just need to
move them there). Then we can get rid of the ifdefs in the test code,
but I'm wondering if the kernel version check is enough to be sure these
flags are implemented (there seems to be no error checking for unknown
flags in fs/namespace.c do_mount() function and it looks like these are
simply ignored).

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 3/3 v4] containers: added mountns/mountns02.c
  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
  1 sibling, 0 replies; 21+ messages in thread
From: chrubis @ 2014-09-09 13:40 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list

Hi!
The same two minor nits about the comments apply here as well, otherwise
it looks good.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 2/3 v4] containers: added mountns dir and mountns/mountns01.c
  2014-08-15 10:52 ` [LTP] [PATCH 2/3] containers: added mountns dir and mountns/mountns01.c Matus Marhefka
                     ` (2 preceding siblings ...)
  2014-08-29 12:23   ` [LTP] [PATCH 2/3 v3] " Matus Marhefka
@ 2014-10-02 12:26   ` Matus Marhefka
  3 siblings, 0 replies; 21+ messages in thread
From: Matus Marhefka @ 2014-10-02 12:26 UTC (permalink / raw)
  To: ltp-list

* tests shared mount: shared mount can be replicated to as many
  mountpoints and all the replicas continue to be exactly same.

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 runtest/containers                                 |   2 +
 testcases/kernel/containers/.gitignore             |   1 +
 testcases/kernel/containers/mountns/Makefile       |  23 +++
 testcases/kernel/containers/mountns/mountns01.c    | 161 +++++++++++++++++++++
 .../kernel/containers/mountns/mountns_helper.h     |  66 +++++++++
 5 files changed, 253 insertions(+)
 create mode 100644 testcases/kernel/containers/mountns/Makefile
 create mode 100644 testcases/kernel/containers/mountns/mountns01.c
 create mode 100644 testcases/kernel/containers/mountns/mountns_helper.h

diff --git a/runtest/containers b/runtest/containers
index 558d7eb..f298c7e 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -55,3 +55,5 @@ utstest_clone_2 utstest clone 2
 utstest_clone_3 utstest clone 3
 utstest_clone_4 utstest clone 4
 utstest_clone_5 utstest clone 5
+
+mountns01 mountns01
diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
index f6f5b14..254d1b9 100644
--- a/testcases/kernel/containers/.gitignore
+++ b/testcases/kernel/containers/.gitignore
@@ -1 +1,2 @@
 /check_for_unshare
+mountns/mountns01
diff --git a/testcases/kernel/containers/mountns/Makefile b/testcases/kernel/containers/mountns/Makefile
new file mode 100644
index 0000000..f9b6b99
--- /dev/null
+++ b/testcases/kernel/containers/mountns/Makefile
@@ -0,0 +1,23 @@
+# 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/>.
+##############################################################################
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(abs_srcdir)/../Makefile.inc
+
+LDLIBS                  := -lclone -lltp
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/containers/mountns/mountns01.c b/testcases/kernel/containers/mountns/mountns01.c
new file mode 100644
index 0000000..4c541ea
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns01.c
@@ -0,0 +1,161 @@
+/* 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: mountns01.c
+ *
+ * Tests a shared mount: shared mount can be replicated to as many
+ * mountpoints and all the replicas continue to be exactly same.
+ * 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"
+ * 4. Makes directory "A" shared
+ * 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)
+ *	X: bind mounts "B" to "A"
+ *	Y: must see "A/B"
+ *	X: umounts "A"
+ *    2)
+ *	Y: bind mounts "B" to "A"
+ *	X: must see "A/B"
+ *	Y: umounts "A"
+ ***********************************************************************/
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <sys/mount.h>
+#include <stdio.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"
+
+
+char *TCID	= "mountns01";
+int TST_TOTAL	= 2;
+
+
+#if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
+
+int child_func(void *arg)
+{
+	int ret = 0;
+
+	TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
+
+	if (access(DIRA"/B", F_OK) == -1)
+		ret = 2;
+
+	TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
+	TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
+
+	/* 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;
+	}
+
+	TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
+	TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
+
+	umount(DIRA);
+	return ret;
+}
+
+static void test(void)
+{
+	int status;
+
+	/* 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);
+
+	/* makes mount DIRA shared */
+	SAFE_MOUNT(cleanup, "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");
+
+	/* bind mounts DIRB to DIRA making contents of DIRB visible
+	 * in DIRA */
+	SAFE_MOUNT(cleanup, DIRB, DIRA, "none", MS_BIND, NULL);
+
+	TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
+	TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
+
+	SAFE_UMOUNT(cleanup, DIRA);
+
+	TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
+	TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
+
+	if (access(DIRA"/B", F_OK) == 0)
+		tst_resm(TPASS, "shared mount in child passed");
+	else
+		tst_resm(TFAIL, "shared mount in child failed");
+
+	TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
+
+
+	SAFE_WAIT(cleanup, &status);
+	if (WIFEXITED(status)) {
+		if ((WEXITSTATUS(status) == 0))
+			tst_resm(TPASS, "shared mount in parent passed");
+		else
+			tst_resm(TFAIL, "shared mount in parent failed");
+	}
+	if (WIFSIGNALED(status)) {
+		tst_resm(TBROK, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	SAFE_UMOUNT(cleanup, DIRA);
+}
+
+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();
+}
+
+#else
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
+}
+#endif
diff --git a/testcases/kernel/containers/mountns/mountns_helper.h b/testcases/kernel/containers/mountns/mountns_helper.h
new file mode 100644
index 0000000..cb9c16d
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns_helper.h
@@ -0,0 +1,66 @@
+/* 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/>.
+ */
+
+#include "libclone.h"
+#include "test.h"
+#include "safe_macros.h"
+
+
+#define DIRA "A"
+#define DIRB "B"
+struct tst_checkpoint checkpoint1;
+struct tst_checkpoint checkpoint2;
+
+
+static int dummy_child(void *v)
+{
+	(void) v;
+	return 0;
+}
+
+static int check_newns(void)
+{
+	int pid, status;
+
+	if (tst_kvercmp(2, 4, 19) < 0)
+		tst_brkm(TCONF, NULL, "CLONE_NEWNS not supported");
+
+	pid = do_clone_unshare_test(T_CLONE, CLONE_NEWNS, dummy_child, NULL);
+	if (pid == -1)
+		tst_brkm(TCONF | TERRNO, NULL, "CLONE_NEWNS not supported");
+	SAFE_WAIT(NULL, &status);
+
+	return 0;
+}
+
+static void cleanup(void)
+{
+	umount(DIRA);
+	umount(DIRB);
+	tst_rmdir();
+}
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	check_newns();
+	tst_tmpdir();
+	TST_CHECKPOINT_INIT(&checkpoint1);
+	TST_CHECKPOINT_INIT(&checkpoint2);
+	SAFE_MKDIR(cleanup, DIRA, 0777);
+	SAFE_MKDIR(cleanup, DIRB, 0777);
+	SAFE_TOUCH(cleanup, DIRA"/A", 0, NULL);
+	SAFE_TOUCH(cleanup, DIRB"/B", 0, NULL);
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [LTP] [PATCH 3/3 v5] containers: added mountns/mountns02.c
  2014-08-15 15:16   ` [LTP] [PATCH 3/3 v2] " Matus Marhefka
                       ` (2 preceding siblings ...)
  2014-08-29 12:24     ` [LTP] [PATCH 3/3 v4] " Matus Marhefka
@ 2014-10-02 12:27     ` Matus Marhefka
  2014-10-02 13:35       ` Cyril Hrubis
  3 siblings, 1 reply; 21+ messages in thread
From: Matus Marhefka @ 2014-10-02 12:27 UTC (permalink / raw)
  To: ltp-list

* 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/.gitignore          |   1 +
 testcases/kernel/containers/mountns/mountns02.c | 161 ++++++++++++++++++++++++
 3 files changed, 163 insertions(+)
 create mode 100644 testcases/kernel/containers/mountns/mountns02.c

diff --git a/runtest/containers b/runtest/containers
index f298c7e..56977c0 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -57,3 +57,4 @@ utstest_clone_4 utstest clone 4
 utstest_clone_5 utstest clone 5
 
 mountns01 mountns01
+mountns02 mountns02
diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
index 254d1b9..f175296 100644
--- a/testcases/kernel/containers/.gitignore
+++ b/testcases/kernel/containers/.gitignore
@@ -1,2 +1,3 @@
 /check_for_unshare
 mountns/mountns01
+mountns/mountns02
diff --git a/testcases/kernel/containers/mountns/mountns02.c b/testcases/kernel/containers/mountns/mountns02.c
new file mode 100644
index 0000000..782f1c3
--- /dev/null
+++ b/testcases/kernel/containers/mountns/mountns02.c
@@ -0,0 +1,161 @@
+/* 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"
+ * 4. Makes directory "A" private
+ * 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)
+ *	X: bind mounts "B" to "A"
+ *	Y: must see "A/A" and must not see "A/B"
+ *	X: umounts "A"
+ *    2)
+ *	Y: bind mounts "B" to "A"
+ *	X: must see "A/A" and must not see "A/B"
+ *	Y: umounts A
+ ***********************************************************************/
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <sys/mount.h>
+#include <stdio.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"
+
+
+char *TCID	= "mountns02";
+int TST_TOTAL	= 2;
+
+
+#if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
+
+int child_func(void *arg)
+{
+	int ret = 0;
+
+	TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
+
+	if ((access(DIRA"/A", F_OK) != 0) || (access(DIRA"/B", F_OK) == 0))
+		ret = 2;
+
+	TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
+	TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
+
+	/* 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;
+	}
+
+	TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
+	TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
+
+	umount(DIRA);
+	return ret;
+}
+
+static void test(void)
+{
+	int status;
+
+	/* 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);
+
+	/* makes mount DIRA private */
+	SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_PRIVATE, NULL);
+
+	if (do_clone_tests(CLONE_NEWNS, child_func, NULL, NULL, NULL) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
+
+	/* bind mounts DIRB to DIRA making contents of DIRB visible
+	 * in DIRA */
+	SAFE_MOUNT(cleanup, DIRB, DIRA, "none", MS_BIND, NULL);
+
+	TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
+	TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
+
+	SAFE_UMOUNT(cleanup, DIRA);
+
+	TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
+	TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
+
+	if ((access(DIRA"/A", F_OK) != 0) || (access(DIRA"/B", F_OK) == 0))
+		tst_resm(TFAIL, "private mount in child failed");
+	else
+		tst_resm(TPASS, "private mount in child passed");
+
+	TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
+
+
+	SAFE_WAIT(cleanup, &status);
+	if (WIFEXITED(status)) {
+		if ((WEXITSTATUS(status) == 0))
+			tst_resm(TPASS, "private mount in parent passed");
+		else
+			tst_resm(TFAIL, "private mount in parent failed");
+	}
+	if (WIFSIGNALED(status)) {
+		tst_resm(TBROK, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	SAFE_UMOUNT(cleanup, DIRA);
+}
+
+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();
+}
+
+#else
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
+}
+#endif
-- 
1.8.3.1


------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 1/3 v3] lib: Added SAFE_MOUNT() and SAFE_UMOUNT()
  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
  0 siblings, 0 replies; 21+ messages in thread
From: Cyril Hrubis @ 2014-10-02 12:55 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list

Hi!
> Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
> ---
>  include/safe_macros.h | 15 ++++++++++++++-
>  lib/safe_macros.c     | 36 ++++++++++++++++++++++++++++++++++++
>  2 files changed, 50 insertions(+), 1 deletion(-)

Pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [LTP] [PATCH 3/3 v5] containers: added mountns/mountns02.c
  2014-10-02 12:27     ` [LTP] [PATCH 3/3 v5] " Matus Marhefka
@ 2014-10-02 13:35       ` Cyril Hrubis
  0 siblings, 0 replies; 21+ messages in thread
From: Cyril Hrubis @ 2014-10-02 13:35 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list

Hi!
> * Tests a private mount: private mount does not forward or receive
>   propagation.

Rest of the patchset is pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2014-10-02 13:36 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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.