idmapped-mounts: split out run_test() function
[xfstests-dev.git] / src / idmapped-mounts / idmapped-mounts.c
index 30f8f6e045afa263aba94866d1762e1c3111e92f..2c212131d2664247361ba4f98510e085ed5767c4 100644 (file)
@@ -80,6 +80,8 @@
 
 #define die(format, ...) die_errno(errno, format, ##__VA_ARGS__)
 
+#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
+
 uid_t t_overflowuid = 65534;
 gid_t t_overflowgid = 65534;
 
@@ -369,11 +371,6 @@ static bool is_sticky(int dfd, const char *path, int flags)
        return (st.st_mode & S_ISVTX) > 0;
 }
 
-static inline int set_cloexec(int fd)
-{
-       return fcntl(fd, F_SETFD, FD_CLOEXEC);
-}
-
 static inline bool switch_fsids(uid_t fsuid, gid_t fsgid)
 {
        if (setfsgid(fsgid))
@@ -623,7 +620,7 @@ __attribute__((unused)) static int print_r(int fd, const char *path)
                ret = fstatat(fd, path, &st,
                              AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW);
        if (!ret)
-               fprintf(stderr, "mode(%o):uid(%d):gid(%d) -> %s",
+               fprintf(stderr, "mode(%o):uid(%d):gid(%d) -> %s\n",
                        (st.st_mode & ~S_IFMT), st.st_uid, st.st_gid,
                        (path && *path) ? path : "(null)");
 
@@ -8738,7 +8735,7 @@ static const struct option longopts[] = {
 struct t_idmapped_mounts {
        int (*test)(void);
        const char *description;
-} t_idmapped_mounts[] = {
+} basic_suite[] = {
        { acls,                                                         "posix acls on regular mounts",                                                                 },
        { create_in_userns,                                             "create operations in user namespace",                                                          },
        { device_node_in_userns,                                        "device node in user namespace",                                                                },
@@ -8790,9 +8787,44 @@ struct t_idmapped_mounts {
        { threaded_idmapped_mount_interactions,                         "threaded operations on idmapped mounts",                                                       },
 };
 
+static bool run_test(struct t_idmapped_mounts suite[], size_t suite_size)
+{
+       int i;
+
+       for (i = 0; i < suite_size; i++) {
+               struct t_idmapped_mounts *t = &suite[i];
+               int ret;
+               pid_t pid;
+
+               test_setup();
+
+               pid = fork();
+               if (pid < 0)
+                       return false;
+
+               if (pid == 0) {
+                       ret = t->test();
+                       if (ret) {
+                               fprintf(stderr, "failure: %s\n", t->description);
+                               exit(EXIT_FAILURE);
+                       }
+
+                       exit(EXIT_SUCCESS);
+               }
+
+               ret = wait_for_pid(pid);
+               test_cleanup();
+
+               if (ret)
+                       return false;
+       }
+
+       return true;
+}
+
 int main(int argc, char *argv[])
 {
-       int i, fret, ret;
+       int fret, ret;
        int index = 0;
        bool supported = false;
 
@@ -8879,33 +8911,8 @@ int main(int argc, char *argv[])
 
        fret = EXIT_FAILURE;
 
-       /* Proper test suite run. */
-       for (i = 0; i < (sizeof(t_idmapped_mounts) / sizeof(t_idmapped_mounts[0])); i++) {
-               struct t_idmapped_mounts *t = &t_idmapped_mounts[i];
-               pid_t pid;
-
-               test_setup();
-
-               pid = fork();
-               if (pid < 0)
-                       goto out;
-
-               if (pid == 0) {
-                       ret = t->test();
-                       if (ret) {
-                               fprintf(stderr, "failure: %s\n", t->description);
-                               exit(EXIT_FAILURE);
-                       }
-
-                       exit(EXIT_SUCCESS);
-               }
-
-               ret = wait_for_pid(pid);
-               test_cleanup();
-
-               if (ret)
-                       goto out;
-       }
+       if (!run_test(basic_suite, ARRAY_SIZE(basic_suite)))
+               goto out;
 
        fret = EXIT_SUCCESS;