]> granicus.if.org Git - strace/commitdiff
tests/btrfs.c: add pid suffix to the created directory, remove it on exit
authorEugene Syromyatnikov <evgsyr@gmail.com>
Sun, 18 Mar 2018 06:56:52 +0000 (07:56 +0100)
committerDmitry V. Levin <ldv@altlinux.org>
Fri, 20 Apr 2018 12:39:05 +0000 (12:39 +0000)
If multiple tests are running simultaneously in the same directory,
conflicts (and test failures) may happen.

* tests/btrfs.c (path, dir_name_fmt, dir_name): New variables.
(btrfs_test_ino_path_ioctls): Use dir_name for printing directory name.
(rm_test_dir): New function.
(main): Generate dir_name, use it instead of fixed name, add rm_test_dir
as an atexit handler.

tests/btrfs.c

index e4059aa76817526a28b39db3f8dbdfb9fd6adb2c..a8540c63d58a4c26e486014cb03be7e9e5939566 100644 (file)
@@ -64,6 +64,10 @@ static bool verbose;
 static bool write_ok;
 static bool verbose_xlat;
 
+static const char *path;
+static const char dir_name_fmt[] = "strace-test-%d";
+static char dir_name[sizeof(dir_name_fmt) + sizeof(int) * 3];
+
 const unsigned char uuid_reference[BTRFS_UUID_SIZE] = {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
@@ -1298,7 +1302,7 @@ btrfs_test_ino_path_ioctls(void)
                        data->bytes_left, data->bytes_missing, data->elem_cnt,
                        data->elem_missed);
                if (verbose) {
-                       printf("val=[\"strace-test\"]");
+                       printf("val=[\"%s\"]", dir_name);
                } else
                        printf("...");
                printf("}}) = 0\n");
@@ -1934,13 +1938,20 @@ btrfs_test_read_ioctls(void)
        }
 }
 
+static void
+rm_test_dir(void)
+{
+       int rootfd = open(path, O_RDONLY|O_DIRECTORY);
+
+       unlinkat(rootfd, dir_name, AT_REMOVEDIR);
+}
+
 int
 main(int argc, char *argv[])
 {
 
        int opt;
        int ret;
-       const char *path;
 
        while ((opt = getopt(argc, argv, "wvX")) != -1) {
                switch (opt) {
@@ -1962,6 +1973,10 @@ main(int argc, char *argv[])
                }
        }
 
+       ret = snprintf(dir_name, sizeof(dir_name), dir_name_fmt, getpid());
+       if (ret < 0)
+               perror_msg_and_fail("snprintf(dir_name)");
+
        /*
         * This will enable optional tests that require a valid file descriptor
         */
@@ -1983,14 +1998,18 @@ main(int argc, char *argv[])
                if (rootfd < 0)
                        perror_msg_and_fail("open(%s) failed", path);
 
-               ret = mkdirat(rootfd, "strace-test", 0755);
+               ret = mkdirat(rootfd, dir_name, 0755);
                if (ret < 0 && errno != EEXIST)
-                       perror_msg_and_fail("mkdirat(strace-test) failed");
+                       perror_msg_and_fail("mkdirat(%s) failed", dir_name);
+
+               /* Register removal of the created directory.  */
+               if (ret == 0)
+                       atexit(rm_test_dir);
 
-               btrfs_test_dir_fd = openat(rootfd, "strace-test",
+               btrfs_test_dir_fd = openat(rootfd, dir_name,
                                           O_RDONLY|O_DIRECTORY);
                if (btrfs_test_dir_fd < 0)
-                       perror_msg_and_fail("openat(strace-test) failed");
+                       perror_msg_and_fail("openat(%s) failed", dir_name);
                close(rootfd);
        } else
                write_ok = false;