blob: eb64581b5285a013418e60f59b77f8da86fe76d4 [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"
48#include "usctest.h"
49#include "libclone.h"
50#include "safe_macros.h"
51#include "safe_file_ops.h"
52#include "mountns_helper.h"
53
54
55char *TCID = "mountns03";
56int TST_TOTAL = 2;
57
58
59#if defined(MS_SHARED) && defined(MS_PRIVATE) \
60 && defined(MS_REC) && defined(MS_SLAVE)
61
62int child_func(void *arg)
63{
64 int ret = 0;
65
66 /* makes mount DIRA a slave of DIRA (all slave mounts have
67 * a master mount which is a shared mount) */
68 if (mount("none", DIRA, "none", MS_SLAVE, NULL) == -1) {
69 perror("mount");
70 return 1;
71 }
72
73 TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
74 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
75
76 /* checks that shared mounts propagates to slave mount */
77 if (access(DIRA"/B", F_OK) == -1)
78 ret = 2;
79
80 TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
81 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
82
83 /* bind mounts DIRB to DIRA making contents of DIRB visible
84 * in DIRA */
85 if (mount(DIRB, DIRA, "none", MS_BIND, NULL) == -1) {
86 perror("mount");
87 return 1;
88 }
89
90 TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
91 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
92
93 umount(DIRA);
94 return ret;
95}
96
97static void test(void)
98{
99 int status;
100
101 /* unshares the mount ns */
102 if (unshare(CLONE_NEWNS) == -1)
103 tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
104 /* makes sure parent mounts/umounts have no effect on a real system */
105 SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL);
106
107 /* bind mounts DIRA to itself */
108 SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL);
109
110 /* makes mount DIRA shared */
111 SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_SHARED, NULL);
112
113 if (do_clone_tests(CLONE_NEWNS, child_func, NULL, NULL, NULL) == -1)
114 tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
115
116 /* waits for child to make a slave mount */
117 TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
118
119 /* bind mounts DIRB to DIRA making contents of DIRB visible
120 * in DIRA */
121 SAFE_MOUNT(cleanup, DIRB, DIRA, "none", MS_BIND, NULL);
122
123 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
124 TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
125
126 SAFE_UMOUNT(cleanup, DIRA);
127
128 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
129 TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
130
131 /* checks that slave mount doesn't propagate to shared mount */
132 if ((access(DIRA"/A", F_OK) == 0) && (access(DIRA"/B", F_OK) == -1))
133 tst_resm(TPASS, "propagation from slave mount passed");
134 else
135 tst_resm(TFAIL, "propagation form slave mount failed");
136
137 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
138
139
140 SAFE_WAIT(cleanup, &status);
141 if (WIFEXITED(status)) {
142 if (WEXITSTATUS(status) == 0)
143 tst_resm(TPASS, "propagation to slave mount passed");
144 else
145 tst_resm(TFAIL, "propagation to slave mount failed");
146 }
147 if (WIFSIGNALED(status)) {
148 tst_resm(TBROK, "child was killed with signal %s",
149 tst_strsig(WTERMSIG(status)));
150 return;
151 }
152
153 SAFE_UMOUNT(cleanup, DIRA);
154}
155
156int main(int argc, char *argv[])
157{
158 const char *msg;
159 int lc;
160
161 msg = parse_opts(argc, argv, NULL, NULL);
162 if (msg != NULL)
163 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
164
165 setup();
166
167 for (lc = 0; TEST_LOOPING(lc); lc++)
168 test();
169
170 cleanup();
171 tst_exit();
172}
173
174#else
175int main(void)
176{
177 tst_brkm(TCONF, NULL, "needed mountflags are not defined");
178}
179#endif