]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
test_mkfds: reserve file descriptors in the early stage of execution
authorMasatake YAMATO <yamato@redhat.com>
Sat, 26 Oct 2024 17:05:45 +0000 (02:05 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Thu, 31 Oct 2024 17:52:02 +0000 (02:52 +0900)
A factory specified with command line opens some files.  After
opening, the factory remaps the opened file descriptors (ofds) to file
descriptors (rfds) specified with the command line with dup2 system all.

This remapping may fail if there is an overlap between ofds and rfds.
With this change, there cannot be an overlap between ofds and rfds;
test_mkfds reserves rfds in the early stage of execution.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
tests/helpers/test_mkfds.c

index a85abd229c382c7b9d7ce2fc9b47e9c8117abb52..6327e6d8fc1e3bdbf62fa4231b4023233b7872d2 100644 (file)
@@ -353,6 +353,14 @@ static void abort_with_child_death_message(int signum _U_)
        _exit(EXIT_FAILURE);
 }
 
+static void reserve_fd(int fd)
+{
+       (void)close(fd);
+       if (dup2(0, fd) < 0)
+               errx(EXIT_FAILURE,
+                    "faild to reserve fd with dup2(%d, %d)", 0, fd);
+}
+
 static void *open_ro_regular_file(const struct factory *factory, struct fdesc fdescs[],
                                  int argc, char ** argv)
 {
@@ -745,6 +753,13 @@ static void *make_pipe(const struct factory *factory, struct fdesc fdescs[],
        xpd [0] = ARG_INTEGER(rdup);
        xpd [1] = ARG_INTEGER(wdup);
 
+       /* Reserve the fd */
+       for (int i = 0; i < 2; i++) {
+               if (xpd[i] < 0)
+                       continue;
+               reserve_fd(xpd[i]);
+       }
+
        for (int i = 0; i < 2; i++) {
                if (ARG_STRING(nonblock)[i] == '-')
                        continue;
@@ -4462,6 +4477,10 @@ int main(int argc, char **argv)
                        errx(EXIT_FAILURE, "fd number should not be negative: %s", str);
                if (fd < 3)
                        errx(EXIT_FAILURE, "fd 0, 1, 2 are reserved: %s", str);
+               if (fd > INT_MAX)
+                       errx(EXIT_FAILURE, "too large fd number for INT: %s", str);
+
+               reserve_fd(fd);
                fdescs[i].fd = fd;
        }
        optind += factory->N;