From: Masatake YAMATO Date: Sat, 26 Oct 2024 17:05:45 +0000 (+0900) Subject: test_mkfds: reserve file descriptors in the early stage of execution X-Git-Tag: v2.42-start~162^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=35eda933acd8c094ea54777a62e628d3654d3374;p=thirdparty%2Futil-linux.git test_mkfds: reserve file descriptors in the early stage of execution 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 --- diff --git a/tests/helpers/test_mkfds.c b/tests/helpers/test_mkfds.c index a85abd229..6327e6d8f 100644 --- a/tests/helpers/test_mkfds.c +++ b/tests/helpers/test_mkfds.c @@ -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;