1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2014 Red Hat, Inc.
4 * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Tests a shared mount: shared mount can be replicated to as many
11 * mountpoints and all the replicas continue to be exactly same.
12 *
13 * [Algorithm]
14 *
15 * - Creates directories DIR_A, DIR_B and files DIR_A/"A", DIR_B/"B"
16 * - Unshares mount namespace and makes it private (so mounts/umounts have no
17 * effect on a real system)
18 * - Bind mounts directory DIR_A to DIR_A
19 * - Makes directory DIR_A shared
20 * - Clones a new child process with CLONE_NEWNS flag
21 * - There are two test cases (where X is parent namespace and Y child namespace):
22 * 1. First test case
23 * .. X: bind mounts DIR_B to DIR_A
24 * .. Y: must see DIR_A/"B"
25 * .. X: umounts DIR_A
26 * 2. Second test case
27 * .. Y: bind mounts DIR_B to DIR_A
28 * .. X: must see DIR_A/"B"
29 * .. Y: umounts DIR_A
30 */
31
32 #include <sys/wait.h>
33 #include <sys/mount.h>
34 #include "mountns.h"
35 #include "tst_test.h"
36
child_func(LTP_ATTRIBUTE_UNUSED void * arg)37 static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
38 {
39 TST_CHECKPOINT_WAIT(0);
40
41 if (access(DIRA "/B", F_OK) == 0)
42 tst_res(TPASS, "shared mount in parent passed");
43 else
44 tst_res(TFAIL, "shared mount in parent failed");
45
46 TST_CHECKPOINT_WAKE_AND_WAIT(0);
47
48 /* bind mounts DIRB to DIRA making contents of DIRB visible in DIRA */
49 SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
50
51 TST_CHECKPOINT_WAKE_AND_WAIT(0);
52
53 SAFE_UMOUNT(DIRA);
54
55 return 0;
56 }
57
run(void)58 static void run(void)
59 {
60 int ret;
61
62 SAFE_UNSHARE(CLONE_NEWNS);
63
64 /* makes sure parent mounts/umounts have no effect on a real system */
65 SAFE_MOUNT("none", "/", "none", MS_REC | MS_PRIVATE, NULL);
66
67 SAFE_MOUNT(DIRA, DIRA, "none", MS_BIND, NULL);
68 SAFE_MOUNT("none", DIRA, "none", MS_SHARED, NULL);
69
70 ret = ltp_clone_quick(CLONE_NEWNS | SIGCHLD, child_func, NULL);
71 if (ret < 0)
72 tst_brk(TBROK, "clone failed");
73
74 SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
75
76 TST_CHECKPOINT_WAKE_AND_WAIT(0);
77
78 SAFE_UMOUNT(DIRA);
79
80 TST_CHECKPOINT_WAKE_AND_WAIT(0);
81
82 if (access(DIRA "/B", F_OK) == 0)
83 tst_res(TPASS, "shared mount in child passed");
84 else
85 tst_res(TFAIL, "shared mount in child failed");
86
87 TST_CHECKPOINT_WAKE(0);
88
89 SAFE_WAIT(NULL);
90
91 SAFE_UMOUNT(DIRA);
92 }
93
setup(void)94 static void setup(void)
95 {
96 check_newns();
97 create_folders();
98 }
99
cleanup(void)100 static void cleanup(void)
101 {
102 umount_folders();
103 }
104
105 static struct tst_test test = {
106 .setup = setup,
107 .cleanup = cleanup,
108 .test_all = run,
109 .needs_root = 1,
110 .needs_checkpoints = 1,
111 };
112