blob: d979659495f1198d77cd8a884d27c1eaa10ee491 [file] [log] [blame]
Matus Marhefkace30a7b2014-10-02 14:28:47 +02001/* Copyright (c) 2014 Red Hat, Inc.
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of version 2 the GNU General Public License as
5 * published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 ***********************************************************************
15 * File: mountns03.c
16 *
17 * Tests a slave mount: slave mount is like a shared mount except that
18 * mount and umount events only propagate towards it.
19 *
20 * Description:
21 * 1. Creates directories "A", "B" and files "A/A", "B/B"
22 * 2. Unshares mount namespace and makes it private (so mounts/umounts
23 * have no effect on a real system)
24 * 3. Bind mounts directory "A" to itself
25 * 4. Makes directory "A" shared
26 * 5. Clones a new child process with CLONE_NEWNS flag and makes "A"
27 * a slave mount
28 * 6. There are two testcases (where X is parent namespace and Y child
29 * namespace):
30 * 1)
31 * X: bind mounts "B" to "A"
32 * Y: must see the file "A/B"
33 * X: umounts "A"
34 * 2)
35 * Y: bind mounts "B" to "A"
36 * X: must see only the "A/A" and must not see "A/B" (as slave
37 * mount does not forward propagation)
38 * Y: umounts "A"
39 ***********************************************************************/
40
41#define _GNU_SOURCE
42#include <sys/wait.h>
43#include <sys/mount.h>
44#include <stdio.h>
45#include <unistd.h>
46#include <errno.h>
47#include "test.h"
Matus Marhefkace30a7b2014-10-02 14:28:47 +020048#include "libclone.h"
49#include "safe_macros.h"
50#include "safe_file_ops.h"
51#include "mountns_helper.h"
52
53
54char *TCID = "mountns03";
55int TST_TOTAL = 2;
56
57
58#if defined(MS_SHARED) && defined(MS_PRIVATE) \
59 && defined(MS_REC) && defined(MS_SLAVE)
60
61int child_func(void *arg)
62{
63 int ret = 0;
64
65 /* makes mount DIRA a slave of DIRA (all slave mounts have
66 * a master mount which is a shared mount) */
67 if (mount("none", DIRA, "none", MS_SLAVE, NULL) == -1) {
68 perror("mount");
69 return 1;
70 }
71
72 TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
73 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
74
75 /* checks that shared mounts propagates to slave mount */
76 if (access(DIRA"/B", F_OK) == -1)
77 ret = 2;
78
79 TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
80 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
81
82 /* bind mounts DIRB to DIRA making contents of DIRB visible
83 * in DIRA */
84 if (mount(DIRB, DIRA, "none", MS_BIND, NULL) == -1) {
85 perror("mount");
86 return 1;
87 }
88
89 TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
90 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
91
92 umount(DIRA);
93 return ret;
94}
95
96static void test(void)
97{
98 int status;
99
100 /* unshares the mount ns */
101 if (unshare(CLONE_NEWNS) == -1)
102 tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
103 /* makes sure parent mounts/umounts have no effect on a real system */
104 SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL);
105
106 /* bind mounts DIRA to itself */
107 SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL);
108
109 /* makes mount DIRA shared */
110 SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_SHARED, NULL);
111
112 if (do_clone_tests(CLONE_NEWNS, child_func, NULL, NULL, NULL) == -1)
113 tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
114
115 /* waits for child to make a slave mount */
116 TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
117
118 /* bind mounts DIRB to DIRA making contents of DIRB visible
119 * in DIRA */
120 SAFE_MOUNT(cleanup, DIRB, DIRA, "none", MS_BIND, NULL);
121
122 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
123 TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
124
125 SAFE_UMOUNT(cleanup, DIRA);
126
127 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
128 TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
129
130 /* checks that slave mount doesn't propagate to shared mount */
131 if ((access(DIRA"/A", F_OK) == 0) && (access(DIRA"/B", F_OK) == -1))
132 tst_resm(TPASS, "propagation from slave mount passed");
133 else
134 tst_resm(TFAIL, "propagation form slave mount failed");
135
136 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
137
138
139 SAFE_WAIT(cleanup, &status);
140 if (WIFEXITED(status)) {
141 if (WEXITSTATUS(status) == 0)
142 tst_resm(TPASS, "propagation to slave mount passed");
143 else
144 tst_resm(TFAIL, "propagation to slave mount failed");
145 }
146 if (WIFSIGNALED(status)) {
147 tst_resm(TBROK, "child was killed with signal %s",
148 tst_strsig(WTERMSIG(status)));
149 return;
150 }
151
152 SAFE_UMOUNT(cleanup, DIRA);
153}
154
155int main(int argc, char *argv[])
156{
157 const char *msg;
158 int lc;
159
160 msg = parse_opts(argc, argv, NULL, NULL);
161 if (msg != NULL)
162 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
163
164 setup();
165
166 for (lc = 0; TEST_LOOPING(lc); lc++)
167 test();
168
169 cleanup();
170 tst_exit();
171}
172
173#else
174int main(void)
175{
176 tst_brkm(TCONF, NULL, "needed mountflags are not defined");
177}
178#endif