Skip to content

Commit 898d79f

Browse files
committed
mount: Fix trailing / when a file is bind-mounted
E.g. I have a /etc/hosts in workspace mounted from the host, and get the following message. (00.141008) 1: mnt-v2: Create plain mountpoint /tmp/.criu.mntns.K1biY1/mnt-0000000938 for 938 (00.141546) 1: mnt-v2: Mounting unsupported @938 (0) (00.141887) 1: mnt-v2: Bind /tmp/agent/1-d8c746c6fda3a8b2/workspace/etc/hosts/ to /tmp/.criu.mntns.K1biY1/mnt-0000000938 (00.142179) 1: Error (criu/mount-v2.c:319): mnt-v2: Failed to open_tree /tmp/agent/1-d8c746c6fda3a8b2/workspace/etc/hosts/: Not a directory (00.143774) Error (criu/cr-restore.c:2320): Restoring FAILED. Signed-off-by: Chuan Qiu <[email protected]>
1 parent b24f6e2 commit 898d79f

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

criu/mount.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,11 @@ static int resolve_external_mounts(struct mount_info *info)
888888

889889
cut_root = cut_root_for_bind(m->root, match->root);
890890

891-
p = xsprintf("%s/%s", match->ns_mountpoint + 1, cut_root);
891+
if (cut_root[0] == '\0') {
892+
p = xstrdup(match->ns_mountpoint + 1);
893+
} else {
894+
p = xsprintf("%s/%s", match->ns_mountpoint + 1, cut_root);
895+
}
892896
if (!p)
893897
return -1;
894898

test/zdtm/static/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ TST_DIR = \
442442
mnt_ext_root \
443443
mnt_root_ext \
444444
mnt_ext_collision \
445+
mntns_file_bind_same_root \
445446
mntns_pivot_root \
446447
mntns_pivot_root_ro \
447448
mnt_ext_sharing \
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <sched.h>
2+
#include <signal.h>
3+
#include <fcntl.h>
4+
#include <sys/mman.h>
5+
#include <sys/mount.h>
6+
#include <sys/stat.h>
7+
#include <sys/wait.h>
8+
#include <linux/limits.h>
9+
#include <sys/syscall.h>
10+
#include <unistd.h>
11+
#include <errno.h>
12+
13+
#include "zdtmtst.h"
14+
#include "lock.h"
15+
16+
const char *test_doc = "Check bind mount for file with multiple mount-points can be restored.";
17+
const char *test_author = "Chuan Qiu <[email protected]>";
18+
19+
char *dirname = "mntns_file_bind_same_root.test";
20+
TEST_OPTION(dirname, string, "directory name", 1);
21+
22+
char *source = "mntns_file_bind_same_root";
23+
24+
int main(int argc, char **argv)
25+
{
26+
char *testfile = "testfile_mnt";
27+
char testfile_mounted[PATH_MAX];
28+
29+
int fd;
30+
char *zdtm_newns = getenv("ZDTM_NEWNS");
31+
32+
snprintf(testfile_mounted, sizeof(testfile_mounted), "%s/%s", dirname, testfile);
33+
34+
if (!zdtm_newns) {
35+
pr_perror("ZDTM_NEWNS is not set");
36+
return 1;
37+
} else if (strcmp(zdtm_newns, "1")) {
38+
goto test;
39+
}
40+
41+
fd = open(testfile, O_RDWR | O_CREAT, 0666);
42+
if (fd < 0) {
43+
pr_perror("open");
44+
goto err;
45+
}
46+
write(fd, "test data", 9);
47+
close(fd);
48+
49+
// Mount a file bind mount in a shared mountpoint.
50+
mkdir(dirname, 0755);
51+
52+
if (mount(source, dirname, "tmpfs", 0, NULL)) {
53+
pr_perror("mount tmpfs");
54+
goto err;
55+
}
56+
57+
if (mount(NULL, dirname, NULL, MS_SHARED, NULL)) {
58+
pr_perror("make shared");
59+
goto err;
60+
}
61+
62+
fd = open(testfile_mounted, O_RDWR | O_CREAT, 0666);
63+
if (fd < 0) {
64+
pr_perror("open");
65+
goto err;
66+
}
67+
close(fd);
68+
69+
test:
70+
test_init(argc, argv);
71+
72+
if (mount(testfile, testfile_mounted, NULL, MS_BIND, NULL)) {
73+
pr_perror("mount bind file %s to %s",
74+
testfile, testfile_mounted);
75+
goto err;
76+
}
77+
if (unshare(CLONE_NEWNS)) {
78+
pr_perror("unshare");
79+
goto err;
80+
}
81+
// This will create a file mount with same root path.
82+
if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL)) {
83+
pr_perror("make private");
84+
goto err;
85+
}
86+
test_daemon();
87+
test_waitsig();
88+
pass();
89+
return 0;
90+
err:
91+
return 1;
92+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'feature': 'mnt_id', 'flavor': 'ns', 'flags': 'suid', 'opts': '--external mnt[]:m --enable-external-masters'}

0 commit comments

Comments
 (0)