[CRIU] [PATCH 5/5] test: Add unlink_dir test
Cyrill Gorcunov
gorcunov at gmail.com
Fri Feb 22 19:02:57 MSK 2019
To check ghost directories cleanup.
Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
test/zdtm/static/Makefile | 1 +
test/zdtm/static/unlink_dir.c | 150 ++++++++++++++++++++++++++++++++++
2 files changed, 151 insertions(+)
create mode 100644 test/zdtm/static/unlink_dir.c
diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index a31c202d5409..303ed7d40ffe 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -345,6 +345,7 @@ TST_DIR = \
private_bind_propagation \
ghost_on_rofs \
overmounted_file \
+ unlink_dir \
TST_DIR_FILE = \
chroot \
diff --git a/test/zdtm/static/unlink_dir.c b/test/zdtm/static/unlink_dir.c
new file mode 100644
index 000000000000..86a627f6405d
--- /dev/null
+++ b/test/zdtm/static/unlink_dir.c
@@ -0,0 +1,150 @@
+#include <stdlib.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <linux/limits.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc = "Check cleanup order of ghost directory and files inside";
+const char *test_author = "Cyrill Gorcunov <gorcunov at gmail.com>";
+
+char *dirname;
+TEST_OPTION(dirname, string, "directory name", 1);
+
+int main(int argc, char ** argv)
+{
+ int dirfd1, dirfd2, dirfd2_dup, dirfd3, dirfd4;
+ char path_dir1[PATH_MAX];
+ char path_dir2[PATH_MAX];
+ char path_dir3[PATH_MAX];
+ char path_dir4[PATH_MAX];
+
+ /* Order does matter */
+ char *path_dirs[] = {
+ path_dir4,
+ path_dir3,
+ path_dir2,
+ path_dir1,
+ };
+
+ char path[PATH_MAX];
+ int fds[4], i;
+
+ int lo = ARRAY_SIZE(fds) / 2;
+ int hi = ARRAY_SIZE(fds);
+
+ test_init(argc, argv);
+
+ if (mkdir(dirname, 0700) < 0) {
+ pr_perror("Can't create directory %s", dirname);
+ return 1;
+ }
+
+ ssprintf(path_dir1, "%s/%s", dirname, "gd1");
+ if (mkdir(path_dir1, 0700) < 0) {
+ pr_perror("Can't create directory %s", path_dir1);
+ return 1;
+ }
+
+ ssprintf(path_dir2, "%s/%s/%s", dirname, "gd1", "gd2");
+ if (mkdir(path_dir2, 0700) < 0) {
+ pr_perror("Can't create directory %s", path_dir2);
+ return 1;
+ }
+
+ ssprintf(path_dir3, "%s/%s/%s/%s", dirname, "gd1", "gd2", "gd3");
+ if (mkdir(path_dir3, 0700) < 0) {
+ pr_perror("Can't create directory %s", path_dir3);
+ return 1;
+ }
+
+ ssprintf(path_dir4, "%s/%s/%s/%s/%s", dirname, "gd1", "gd2", "gd3", "gd4");
+ if (mkdir(path_dir4, 0700) < 0) {
+ pr_perror("Can't create directory %s", path_dir4);
+ return 1;
+ }
+
+ for (i = 0; i < lo; i++) {
+ ssprintf(path, "%s/%d", path_dir1, i);
+ fds[i] = open(path, O_RDONLY | O_CREAT | O_TRUNC);
+ if (fds[i] < 0) {
+ pr_perror("Can't open %s", path);
+ return 1;
+ }
+ if (unlink(path)) {
+ pr_perror("Can't unlink %s", path);
+ return 1;
+ }
+ }
+
+ dirfd2 = open(path_dir2, O_RDONLY | O_DIRECTORY);
+ if (dirfd2 < 0) {
+ pr_perror("Can't open %s", path_dir2);
+ return 1;
+ }
+
+ dirfd2_dup = dup(dirfd2);
+ if (dirfd2_dup < 0) {
+ pr_perror("Can't dup on %s\n", path_dir2);
+ return 1;
+ }
+
+ dirfd3 = open(path_dir3, O_RDONLY | O_DIRECTORY);
+ if (dirfd3 < 0) {
+ pr_perror("Can't open %s", path_dir3);
+ return 1;
+ }
+ close(dirfd3);
+
+ dirfd4 = open(path_dir4, O_RDONLY | O_DIRECTORY);
+ if (dirfd4 < 0) {
+ pr_perror("Can't open %s", path_dir4);
+ return 1;
+ }
+
+ for (i = lo; i < hi; i++) {
+ ssprintf(path, "%s/%d", path_dir2, i);
+ fds[i] = open(path, O_RDONLY | O_CREAT | O_TRUNC);
+ if (fds[i] < 0) {
+ pr_perror("Can't open %s", path);
+ return 1;
+ }
+ if (unlink(path)) {
+ pr_perror("Can't unlink %s", path);
+ return 1;
+ }
+ }
+
+ dirfd1 = open(path_dir1, O_RDONLY | O_DIRECTORY);
+ if (dirfd1 < 0) {
+ pr_perror("Can't open %s", path_dir1);
+ return 1;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(path_dirs); i++) {
+ if (rmdir(path_dirs[i])) {
+ pr_perror("Can't rmdir %s", path_dirs[i]);
+ return 1;
+ }
+ }
+
+ test_daemon();
+ test_waitsig();
+
+ for (i = 0; i < ARRAY_SIZE(path_dirs); i++) {
+ if (access(path_dirs[i], F_OK)) {
+ if (errno == ENOENT)
+ continue;
+ fail("Unexpected error on %s", path_dirs[i]);
+ exit(1);
+ }
+ }
+ pass();
+ return 0;
+}
--
2.20.1
More information about the CRIU
mailing list