blob: f4edc8532d4c3ee70bc017ee2837cc303f25a297 [file] [log] [blame]
Matus Marhefka2f8f5fb2014-10-02 14:27:55 +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: mountns02.c
16 *
17 * Tests a private mount: private mount does not forward or receive
18 * propagation.
19 * Description:
20 * 1. Creates directories "A", "B" and files "A/A", "B/B"
21 * 2. Unshares mount namespace and makes it private (so mounts/umounts
22 * have no effect on a real system)
23 * 3. Bind mounts directory "A" to "A"
24 * 4. Makes directory "A" private
25 * 5. Clones a new child process with CLONE_NEWNS flag
26 * 6. There are two test cases (where X is parent namespace and Y child
27 * namespace):
28 * 1)
29 * X: bind mounts "B" to "A"
30 * Y: must see "A/A" and must not see "A/B"
31 * X: umounts "A"
32 * 2)
33 * Y: bind mounts "B" to "A"
34 * X: must see "A/A" and must not see "A/B"
35 * Y: umounts A
36 ***********************************************************************/
37
38#define _GNU_SOURCE
39#include <sys/wait.h>
40#include <sys/mount.h>
41#include <stdio.h>
42#include <errno.h>
43#include "test.h"
Matus Marhefka2f8f5fb2014-10-02 14:27:55 +020044#include "libclone.h"
45#include "safe_macros.h"
46#include "safe_file_ops.h"
47#include "mountns_helper.h"
48
49
50char *TCID = "mountns02";
51int TST_TOTAL = 2;
52
53
54#if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
55
56int child_func(void *arg)
57{
58 int ret = 0;
59
60 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
61
62 if ((access(DIRA"/A", F_OK) != 0) || (access(DIRA"/B", F_OK) == 0))
63 ret = 2;
64
65 TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
66 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
67
68 /* bind mounts DIRB to DIRA making contents of DIRB visible
69 * in DIRA */
70 if (mount(DIRB, DIRA, "none", MS_BIND, NULL) == -1) {
71 perror("mount");
72 return 1;
73 }
74
75 TST_CHECKPOINT_SIGNAL_PARENT(&checkpoint1);
76 TST_CHECKPOINT_CHILD_WAIT(&checkpoint2);
77
78 umount(DIRA);
79 return ret;
80}
81
82static void test(void)
83{
84 int status;
85
86 /* unshares the mount ns */
87 if (unshare(CLONE_NEWNS) == -1)
88 tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
89 /* makes sure parent mounts/umounts have no effect on a real system */
90 SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL);
91
92 /* bind mounts DIRA to itself */
93 SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL);
94
95 /* makes mount DIRA private */
96 SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_PRIVATE, NULL);
97
98 if (do_clone_tests(CLONE_NEWNS, child_func, NULL, NULL, NULL) == -1)
99 tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
100
101 /* bind mounts DIRB to DIRA making contents of DIRB visible
102 * in DIRA */
103 SAFE_MOUNT(cleanup, DIRB, DIRA, "none", MS_BIND, NULL);
104
105 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
106 TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
107
108 SAFE_UMOUNT(cleanup, DIRA);
109
110 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
111 TST_CHECKPOINT_PARENT_WAIT(cleanup, &checkpoint1);
112
113 if ((access(DIRA"/A", F_OK) != 0) || (access(DIRA"/B", F_OK) == 0))
114 tst_resm(TFAIL, "private mount in child failed");
115 else
116 tst_resm(TPASS, "private mount in child passed");
117
118 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint2);
119
120
121 SAFE_WAIT(cleanup, &status);
122 if (WIFEXITED(status)) {
123 if ((WEXITSTATUS(status) == 0))
124 tst_resm(TPASS, "private mount in parent passed");
125 else
126 tst_resm(TFAIL, "private mount in parent failed");
127 }
128 if (WIFSIGNALED(status)) {
129 tst_resm(TBROK, "child was killed with signal %s",
130 tst_strsig(WTERMSIG(status)));
131 return;
132 }
133
134 SAFE_UMOUNT(cleanup, DIRA);
135}
136
137int main(int argc, char *argv[])
138{
139 const char *msg;
140 int lc;
141
142 msg = parse_opts(argc, argv, NULL, NULL);
143 if (msg != NULL)
144 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
145
146 setup();
147
148 for (lc = 0; TEST_LOOPING(lc); lc++)
149 test();
150
151 cleanup();
152 tst_exit();
153}
154
155#else
156int main(void)
157{
158 tst_brkm(TCONF, NULL, "needed mountflags are not defined");
159}
160#endif