|
| 1 | +#include <sys/mount.h> |
| 2 | +#include <linux/limits.h> |
| 3 | +#include <sys/stat.h> |
| 4 | +#include <sched.h> |
| 5 | +#include <fcntl.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +#include "zdtmtst.h" |
| 10 | + |
| 11 | +const char *test_doc = "Check if external file mount works"; |
| 12 | +const char *test_author = "Pavel Tikhomirov <[email protected]>"; |
| 13 | + |
| 14 | +char *filename = "mnt_ext_file_bind_auto_bind_auto.file"; |
| 15 | +TEST_OPTION(filename, string, "file name", 1); |
| 16 | + |
| 17 | +char *source = "mnt_ext_file_bind_auto_bind_auto.source"; |
| 18 | + |
| 19 | +int create_file(const char *path) |
| 20 | +{ |
| 21 | + int fd; |
| 22 | + |
| 23 | + fd = open(path, O_CREAT | O_RDWR, 0644); |
| 24 | + if (fd < 0) { |
| 25 | + pr_perror("open"); |
| 26 | + return -1; |
| 27 | + } |
| 28 | + |
| 29 | + close(fd); |
| 30 | + return 0; |
| 31 | +} |
| 32 | + |
| 33 | +int main(int argc, char **argv) |
| 34 | +{ |
| 35 | + char *zdtm_newns = getenv("ZDTM_NEWNS"); |
| 36 | + char *tmp = "/tmp/zdtm_ext_file_bind_auto.tmp"; |
| 37 | + char *sourcefile = "/tmp/zdtm_ext_file_bind_auto.file"; |
| 38 | + char *root, tmpfile[PATH_MAX], testfile[PATH_MAX]; |
| 39 | + |
| 40 | + root = getenv("ZDTM_ROOT"); |
| 41 | + if (root == NULL) { |
| 42 | + pr_perror("root"); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + if (!zdtm_newns) { |
| 47 | + pr_perror("ZDTM_NEWNS is not set"); |
| 48 | + return 1; |
| 49 | + } else if (strcmp(zdtm_newns, "1")) { |
| 50 | + goto test; |
| 51 | + } |
| 52 | + |
| 53 | + /* Prepare file bindmount in criu root (source for external file bindmount) */ |
| 54 | + mkdir(tmp, 0755); |
| 55 | + if (mount(source, tmp, "tmpfs", 0, NULL)) { |
| 56 | + pr_perror("mount tmpfs"); |
| 57 | + return 1; |
| 58 | + } |
| 59 | + if (mount(NULL, tmp, NULL, MS_PRIVATE, NULL)) { |
| 60 | + pr_perror("make private"); |
| 61 | + return 1; |
| 62 | + } |
| 63 | + |
| 64 | + sprintf(tmpfile, "%s/%s", tmp, filename); |
| 65 | + if (create_file(tmpfile)) |
| 66 | + return 1; |
| 67 | + |
| 68 | + if (create_file(sourcefile)) |
| 69 | + return 1; |
| 70 | + |
| 71 | + if (mount(tmpfile, sourcefile, NULL, MS_BIND, NULL)) { |
| 72 | + pr_perror("bind"); |
| 73 | + return 1; |
| 74 | + } |
| 75 | + |
| 76 | + umount2(tmp, MNT_DETACH); |
| 77 | + |
| 78 | + /* Prepare file in test root (mount point for external file bindmount) */ |
| 79 | + sprintf(testfile, "%s/%s", root, filename); |
| 80 | + if (create_file(testfile)) |
| 81 | + return 1; |
| 82 | + |
| 83 | + /* |
| 84 | + * Create temporary mntns, next mounts will not show up in criu mntns |
| 85 | + * and will be inherited into test mntns |
| 86 | + */ |
| 87 | + if (unshare(CLONE_NEWNS)) { |
| 88 | + pr_perror("unshare"); |
| 89 | + return 1; |
| 90 | + } |
| 91 | + |
| 92 | + if (mount(sourcefile, testfile, NULL, MS_BIND, NULL)) { |
| 93 | + pr_perror("bind"); |
| 94 | + return 1; |
| 95 | + } |
| 96 | +test: |
| 97 | + test_init(argc, argv); |
| 98 | + |
| 99 | + test_daemon(); |
| 100 | + test_waitsig(); |
| 101 | + |
| 102 | + pass(); |
| 103 | + return 0; |
| 104 | +} |
0 commit comments