From: Masatake YAMATO Date: Fri, 27 Sep 2024 17:06:19 +0000 (+0900) Subject: tests: (test_mkfds) don't close fds and free memory objects when exiting with EXIT_FA... X-Git-Tag: v2.42-start~188^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=89cdf55eac5606509253c04ec8b74bad6f2963b0;p=thirdparty%2Futil-linux.git tests: (test_mkfds) don't close fds and free memory objects when exiting with EXIT_FAILURE However, we keep calling unlink even when exiting with EXIT_FAILURE not to pollute file system. Signed-off-by: Masatake YAMATO --- diff --git a/tests/helpers/test_mkfds.c b/tests/helpers/test_mkfds.c index a849d1b3f..d727c7e04 100644 --- a/tests/helpers/test_mkfds.c +++ b/tests/helpers/test_mkfds.c @@ -367,9 +367,6 @@ static void *open_ro_regular_file(const struct factory *factory, struct fdesc fd if (ARG_INTEGER(offset) != 0) { if (lseek(fd, (off_t)ARG_INTEGER(offset), SEEK_CUR) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to seek 0 -> %ld", ARG_INTEGER(offset)); } } @@ -377,9 +374,6 @@ static void *open_ro_regular_file(const struct factory *factory, struct fdesc fd if (ARG_BOOLEAN(lease_r)) { if (fcntl(fd, F_SETLEASE, F_RDLCK) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to take out a read lease"); } } @@ -387,9 +381,6 @@ static void *open_ro_regular_file(const struct factory *factory, struct fdesc fd if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } close(fd); @@ -412,19 +403,17 @@ static void unlink_and_close_fdesc(int fd, void *data) close(fd); } -typedef void (*lockFn)(int fd, const char *fname, int dupfd); +typedef void (*lockFn)(int fd, const char *fname); -static void lock_fn_none(int fd _U_, const char *fname _U_, int dupfd _U_) +static void lock_fn_none(int fd _U_, const char *fname _U_) { /* Do nothing */ } -static void lock_fn_flock_sh(int fd, const char *fname, int dupfd) +static void lock_fn_flock_sh(int fd, const char *fname) { if (flock(fd, LOCK_SH) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -432,12 +421,10 @@ static void lock_fn_flock_sh(int fd, const char *fname, int dupfd) } } -static void lock_fn_flock_ex(int fd, const char *fname, int dupfd) +static void lock_fn_flock_ex(int fd, const char *fname) { if (flock(fd, LOCK_EX) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -445,7 +432,7 @@ static void lock_fn_flock_ex(int fd, const char *fname, int dupfd) } } -static void lock_fn_posix_r_(int fd, const char *fname, int dupfd) +static void lock_fn_posix_r_(int fd, const char *fname) { struct flock r = { .l_type = F_RDLCK, @@ -455,8 +442,6 @@ static void lock_fn_posix_r_(int fd, const char *fname, int dupfd) }; if (fcntl(fd, F_SETLK, &r) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -464,7 +449,7 @@ static void lock_fn_posix_r_(int fd, const char *fname, int dupfd) } } -static void lock_fn_posix__w(int fd, const char *fname, int dupfd) +static void lock_fn_posix__w(int fd, const char *fname) { struct flock w = { .l_type = F_WRLCK, @@ -474,8 +459,6 @@ static void lock_fn_posix__w(int fd, const char *fname, int dupfd) }; if (fcntl(fd, F_SETLK, &w) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -483,7 +466,7 @@ static void lock_fn_posix__w(int fd, const char *fname, int dupfd) } } -static void lock_fn_posix_rw(int fd, const char *fname, int dupfd) +static void lock_fn_posix_rw(int fd, const char *fname) { struct flock r = { .l_type = F_RDLCK, @@ -499,8 +482,6 @@ static void lock_fn_posix_rw(int fd, const char *fname, int dupfd) }; if (fcntl(fd, F_SETLK, &r) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -508,8 +489,6 @@ static void lock_fn_posix_rw(int fd, const char *fname, int dupfd) } if (fcntl(fd, F_SETLK, &w) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -518,7 +497,7 @@ static void lock_fn_posix_rw(int fd, const char *fname, int dupfd) } #ifdef F_OFD_SETLK -static void lock_fn_ofd_r_(int fd, const char *fname, int dupfd) +static void lock_fn_ofd_r_(int fd, const char *fname) { struct flock r = { .l_type = F_RDLCK, @@ -529,8 +508,6 @@ static void lock_fn_ofd_r_(int fd, const char *fname, int dupfd) }; if (fcntl(fd, F_OFD_SETLK, &r) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -538,7 +515,7 @@ static void lock_fn_ofd_r_(int fd, const char *fname, int dupfd) } } -static void lock_fn_ofd__w(int fd, const char *fname, int dupfd) +static void lock_fn_ofd__w(int fd, const char *fname) { struct flock w = { .l_type = F_WRLCK, @@ -549,8 +526,6 @@ static void lock_fn_ofd__w(int fd, const char *fname, int dupfd) }; if (fcntl(fd, F_OFD_SETLK, &w) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -558,7 +533,7 @@ static void lock_fn_ofd__w(int fd, const char *fname, int dupfd) } } -static void lock_fn_ofd_rw(int fd, const char *fname, int dupfd) +static void lock_fn_ofd_rw(int fd, const char *fname) { struct flock r = { .l_type = F_RDLCK, @@ -576,8 +551,6 @@ static void lock_fn_ofd_rw(int fd, const char *fname, int dupfd) }; if (fcntl(fd, F_OFD_SETLK, &r) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -585,8 +558,6 @@ static void lock_fn_ofd_rw(int fd, const char *fname, int dupfd) } if (fcntl(fd, F_OFD_SETLK, &w) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -595,12 +566,10 @@ static void lock_fn_ofd_rw(int fd, const char *fname, int dupfd) } #endif /* F_OFD_SETLK */ -static void lock_fn_lease_w(int fd, const char *fname, int dupfd) +static void lock_fn_lease_w(int fd, const char *fname) { if (fcntl(fd, F_SETLEASE, F_WRLCK) < 0) { int e = errno; - close(fd); - close(dupfd); if (fname) unlink(fname); errno = e; @@ -698,9 +667,7 @@ static void *make_w_regular_file(const struct factory *factory, struct fdesc fde if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { int e = errno; - close(fd); unlink(fname); - free(fname); errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } @@ -710,9 +677,6 @@ static void *make_w_regular_file(const struct factory *factory, struct fdesc fde if (bDelete) { if (unlink(fname) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to unlink %s", fname); } free(fname); @@ -722,7 +686,6 @@ static void *make_w_regular_file(const struct factory *factory, struct fdesc fde for (int i = 0; i < iWrite_bytes; i++) { if (write(fd, "z", 1) != 1) { int e = errno; - close(fd); if (fname) unlink(fname); errno = e; @@ -733,7 +696,6 @@ static void *make_w_regular_file(const struct factory *factory, struct fdesc fde if (iDupfd >= 0) { if (dup2(fd, iDupfd) < 0) { int e = errno; - close(fd); if (fname) unlink(fname); errno = e; @@ -743,7 +705,7 @@ static void *make_w_regular_file(const struct factory *factory, struct fdesc fde *((int *)data) = iDupfd; } - lock_fn(fd, fname, iDupfd); + lock_fn(fd, fname); fdescs[0] = (struct fdesc){ .fd = fdescs[0].fd, @@ -805,10 +767,6 @@ static void *make_pipe(const struct factory *factory, struct fdesc fdescs[], if (nonblock_flags[i]) { int flags = fcntl(pd[i], F_GETFL); if (fcntl(pd[i], F_SETFL, flags|O_NONBLOCK) < 0) { - int e = errno; - close(pd[0]); - close(pd[1]); - errno = e; errx(EXIT_FAILURE, "failed to set NONBLOCK flag to the %s fd", (i == 0)? "read": "write"); } @@ -818,10 +776,6 @@ static void *make_pipe(const struct factory *factory, struct fdesc fdescs[], for (int i = 0; i < 2; i++) { if (pd[i] != fdescs[i].fd) { if (dup2(pd[i], fdescs[i].fd) < 0) { - int e = errno; - close(pd[0]); - close(pd[1]); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", pd[i], fdescs[i].fd); } @@ -838,12 +792,6 @@ static void *make_pipe(const struct factory *factory, struct fdesc fdescs[], for (int i = 0; i < 2; i++) { if (xpd[i] >= 0) { if (dup2(fdescs[i].fd, xpd[i]) < 0) { - int e = errno; - close(fdescs[0].fd); - close(fdescs[1].fd); - if (i > 0 && xpd[0] >= 0) - close(xpd[0]); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fdescs[i].fd, xpd[i]); } @@ -881,9 +829,6 @@ static void *open_directory(const struct factory *factory, struct fdesc fdescs[] if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } close(fd); @@ -892,17 +837,11 @@ static void *open_directory(const struct factory *factory, struct fdesc fdescs[] if (ARG_INTEGER(dentries) > 0) { dp = fdopendir(fdescs[0].fd); if (dp == NULL) { - int e = errno; - close(fdescs[0].fd); - errno = e; err(EXIT_FAILURE, "failed to make DIR* from fd: %s", ARG_STRING(dir)); } for (int i = 0; i < ARG_INTEGER(dentries); i++) { struct dirent *d = readdir(dp); if (!d) { - int e = errno; - closedir(dp); - errno = e; err(EXIT_FAILURE, "failed in readdir(3)"); } } @@ -929,9 +868,6 @@ static void *open_rw_chrdev(const struct factory *factory, struct fdesc fdescs[] if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } close(fd); @@ -985,10 +921,6 @@ static void *make_socketpair(const struct factory *factory, struct fdesc fdescs[ for (int i = 0; i < 2; i++) { if (sd[i] != fdescs[i].fd) { if (dup2(sd[i], fdescs[i].fd) < 0) { - int e = errno; - close(sd[0]); - close(sd[1]); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", sd[i], fdescs[i].fd); } @@ -1015,9 +947,6 @@ static void *open_with_opath(const struct factory *factory, struct fdesc fdescs[ if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } close(fd); @@ -1043,9 +972,6 @@ static void *open_ro_blkdev(const struct factory *factory, struct fdesc fdescs[] if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } close(fd); @@ -1076,16 +1002,10 @@ static int make_packet_socket(int socktype, const char *interface) addr.sll_family = AF_PACKET; addr.sll_ifindex = if_nametoindex(interface); if (addr.sll_ifindex == 0) { - int e = errno; - close(sd); - errno = e; err(EXIT_FAILURE, "failed to get the interface index for %s", interface); } if (bind(sd, (struct sockaddr *)&addr, sizeof(struct sockaddr_ll)) < 0) { - int e = errno; - close(sd); - errno = e; err(EXIT_FAILURE, "failed to get the interface index for %s", interface); } @@ -1143,9 +1063,6 @@ static void *make_mmapped_packet_socket(const struct factory *factory, struct fd req.tp_block_nr = 1; req.tp_frame_nr = 1; if (setsockopt(sd, SOL_PACKET, PACKET_TX_RING, (char *)&req, sizeof(req)) < 0) { - int e = errno; - close(sd); - errno = e; err((errno == ENOPROTOOPT? EXIT_ENOPROTOOPT: EXIT_FAILURE), "failed to specify a buffer spec to a packet socket"); } @@ -1154,20 +1071,11 @@ static void *make_mmapped_packet_socket(const struct factory *factory, struct fd munmap_data->len = (size_t) req.tp_block_size * req.tp_block_nr; munmap_data->ptr = mmap(NULL, munmap_data->len, PROT_WRITE, MAP_SHARED, sd, 0); if (munmap_data->ptr == MAP_FAILED) { - int e = errno; - close(sd); - free(munmap_data); - errno = e; err(EXIT_FAILURE, "failed to do mmap a packet socket"); } if (sd != fdescs[0].fd) { if (dup2(sd, fdescs[0].fd) < 0) { - int e = errno; - close(sd); - munmap(munmap_data->ptr, munmap_data->len); - free(munmap_data); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", sd, fdescs[0].fd); } close(sd); @@ -1195,9 +1103,6 @@ static void *make_pidfd(const struct factory *factory, struct fdesc fdescs[], if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } close(fd); @@ -1225,26 +1130,17 @@ static void *make_inotify_fd(const struct factory *factory _U_, struct fdesc fde err(EXIT_FAILURE, "failed in inotify_init()"); if (inotify_add_watch(fd, sdir, IN_DELETE) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed in inotify_add_watch(\"%s\")", sdir); } free_arg(&dir); if (inotify_add_watch(fd, sfile, IN_DELETE) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed in inotify_add_watch(\"%s\")", sfile); } free_arg(&file); if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } close(fd); @@ -1317,9 +1213,6 @@ static void *make_unix_stream_core(const struct factory *factory, struct fdesc f "failed to make a socket with AF_UNIX + SOCK_%s (server side)", typestr); if (ssd != fdescs[0].fd) { if (dup2(ssd, fdescs[0].fd) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", ssd, fdescs[0].fd); } close(ssd); @@ -1335,9 +1228,6 @@ static void *make_unix_stream_core(const struct factory *factory, struct fdesc f if (!babstract) unlink(un.sun_path); if (bind(ssd, (const struct sockaddr *)&un, un_len) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to bind a socket for listening"); } @@ -1357,7 +1247,6 @@ static void *make_unix_stream_core(const struct factory *factory, struct fdesc f if (csd != fdescs[1].fd) { if (dup2(csd, fdescs[1].fd) < 0) { int e = errno; - close(csd); close_unix_socket(ssd, fdescs[0].data); errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", csd, fdescs[1].fd); @@ -1374,7 +1263,6 @@ static void *make_unix_stream_core(const struct factory *factory, struct fdesc f if (connect(csd, (const struct sockaddr *)&un, un_len) < 0) { int e = errno; - close_fdesc(csd, NULL); close_unix_socket(ssd, fdescs[0].data); errno = e; err(EXIT_FAILURE, "failed to connect a socket to the listening socket"); @@ -1386,7 +1274,6 @@ static void *make_unix_stream_core(const struct factory *factory, struct fdesc f asd = accept(ssd, NULL, NULL); if (asd < 0) { int e = errno; - close_fdesc(csd, NULL); close_unix_socket(ssd, fdescs[0].data); errno = e; err(EXIT_FAILURE, "failed to accept a socket from the listening socket"); @@ -1394,8 +1281,6 @@ static void *make_unix_stream_core(const struct factory *factory, struct fdesc f if (asd != fdescs[2].fd) { if (dup2(asd, fdescs[2].fd) < 0) { int e = errno; - close(asd); - close_fdesc(csd, NULL); close_unix_socket(ssd, fdescs[0].data); errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", asd, fdescs[2].fd); @@ -1472,9 +1357,6 @@ static void *make_unix_dgram(const struct factory *factory, struct fdesc fdescs[ "failed to make a socket with AF_UNIX + SOCK_DGRAM (server side)"); if (ssd != fdescs[0].fd) { if (dup2(ssd, fdescs[0].fd) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", ssd, fdescs[0].fd); } close(ssd); @@ -1490,9 +1372,6 @@ static void *make_unix_dgram(const struct factory *factory, struct fdesc fdescs[ if (!babstract) unlink(un.sun_path); if (bind(ssd, (const struct sockaddr *)&un, un_len) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to bind a socket for server"); } @@ -1505,7 +1384,6 @@ static void *make_unix_dgram(const struct factory *factory, struct fdesc fdescs[ if (csd != fdescs[1].fd) { if (dup2(csd, fdescs[1].fd) < 0) { int e = errno; - close(csd); close_unix_socket(ssd, fdescs[0].data); errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", csd, fdescs[1].fd); @@ -1522,7 +1400,6 @@ static void *make_unix_dgram(const struct factory *factory, struct fdesc fdescs[ if (connect(csd, (const struct sockaddr *)&un, un_len) < 0) { int e = errno; - close_fdesc(csd, NULL); close_unix_socket(ssd, fdescs[0].data); errno = e; err(EXIT_FAILURE, "failed to connect a socket to the server socket"); @@ -1564,9 +1441,6 @@ static void *make_unix_in_new_netns(const struct factory *factory, struct fdesc typesym = SOCK_DGRAM; typestr = "DGRAM"; } else { - free_arg(&abstract); - free_arg(&path); - free_arg(&type); errx(EXIT_FAILURE, "unknown unix socket type: %s", stype); } @@ -1589,9 +1463,6 @@ static void *make_unix_in_new_netns(const struct factory *factory, struct fdesc err(EXIT_FAILURE, "failed to open /proc/self/ns/net"); if (self_netns != fdescs[0].fd) { if (dup2(self_netns, fdescs[0].fd) < 0) { - int e = errno; - close(self_netns); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", self_netns, fdescs[0].fd); } close(self_netns); @@ -1605,26 +1476,16 @@ static void *make_unix_in_new_netns(const struct factory *factory, struct fdesc }; if (unshare(CLONE_NEWNET) < 0) { - int e = errno; - close_fdesc(self_netns, NULL); - errno = e; err((errno == EPERM? EXIT_EPERM: EXIT_FAILURE), "failed in unshare"); } tmp_netns = open("/proc/self/ns/net", O_RDONLY); if (tmp_netns < 0) { - int e = errno; - close_fdesc(self_netns, NULL); - errno = e; err(EXIT_FAILURE, "failed to open /proc/self/ns/net for the new netns"); } if (tmp_netns != fdescs[1].fd) { if (dup2(tmp_netns, fdescs[1].fd) < 0) { - int e = errno; - close_fdesc(self_netns, NULL); - close(tmp_netns); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", tmp_netns, fdescs[1].fd); } close(tmp_netns); @@ -1639,10 +1500,6 @@ static void *make_unix_in_new_netns(const struct factory *factory, struct fdesc sd = socket(AF_UNIX, typesym, 0); if (sd < 0) { - int e = errno; - close_fdesc(self_netns, NULL); - close_fdesc(tmp_netns, NULL); - errno = e; err(EXIT_FAILURE, "failed to make a socket with AF_UNIX + SOCK_%s", typestr); @@ -1650,11 +1507,6 @@ static void *make_unix_in_new_netns(const struct factory *factory, struct fdesc if (sd != fdescs[2].fd) { if (dup2(sd, fdescs[2].fd) < 0) { - int e = errno; - close_fdesc(self_netns, NULL); - close_fdesc(tmp_netns, NULL); - close(sd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", sd, fdescs[2].fd); } close(sd); @@ -1670,11 +1522,6 @@ static void *make_unix_in_new_netns(const struct factory *factory, struct fdesc if (!babstract) unlink(un.sun_path); if (bind(sd, (const struct sockaddr *)&un, un_len) < 0) { - int e = errno; - close_fdesc(self_netns, NULL); - close_fdesc(tmp_netns, NULL); - close_unix_socket(sd, NULL); - errno = e; err(EXIT_FAILURE, "failed to bind a socket"); } @@ -1684,8 +1531,6 @@ static void *make_unix_in_new_netns(const struct factory *factory, struct fdesc if (typesym != SOCK_DGRAM) { if (listen(sd, 1) < 0) { int e = errno; - close_fdesc(self_netns, NULL); - close_fdesc(tmp_netns, NULL); close_unix_socket(sd, fdescs[2].data); errno = e; err(EXIT_FAILURE, "failed to listen a socket"); @@ -1694,8 +1539,6 @@ static void *make_unix_in_new_netns(const struct factory *factory, struct fdesc if (setns(self_netns, CLONE_NEWNET) < 0) { int e = errno; - close_fdesc(self_netns, NULL); - close_fdesc(tmp_netns, NULL); close_unix_socket(sd, fdescs[2].data); errno = e; err(EXIT_FAILURE, "failed to switch back to the original net namespace"); @@ -1730,17 +1573,11 @@ static void *make_tcp_common(const struct factory *factory, struct fdesc fdescs[ if (setsockopt(ssd, SOL_SOCKET, SO_REUSEADDR, (const char *)&y, sizeof(y)) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to setsockopt(SO_REUSEADDR)"); } if (ssd != fdescs[0].fd) { if (dup2(ssd, fdescs[0].fd) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", ssd, fdescs[0].fd); } close(ssd); @@ -1749,43 +1586,26 @@ static void *make_tcp_common(const struct factory *factory, struct fdesc fdescs[ init_addr(sin, iserver_port); if (bind(ssd, sin, addr_size) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to bind a listening socket"); } if (listen(ssd, 1) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to listen a socket"); } csd = socket(family, SOCK_STREAM, 0); if (csd < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to make a tcp client socket"); } if (setsockopt(csd, SOL_SOCKET, SO_REUSEADDR, (const char *)&y, sizeof(y)) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to setsockopt(SO_REUSEADDR)"); } if (csd != fdescs[1].fd) { if (dup2(csd, fdescs[1].fd) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", csd, fdescs[1].fd); } close(csd); @@ -1794,35 +1614,19 @@ static void *make_tcp_common(const struct factory *factory, struct fdesc fdescs[ init_addr(cin, iclient_port); if (bind(csd, cin, addr_size) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to bind a client socket"); } if (connect(csd, sin, addr_size) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to connect a client socket to the server socket"); } asd = accept(ssd, NULL, NULL); if (asd < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to accept a socket from the listening socket"); } if (asd != fdescs[2].fd) { if (dup2(asd, fdescs[2].fd) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", asd, fdescs[2].fd); } close(asd); @@ -1907,17 +1711,11 @@ static void *make_udp_common(const struct factory *factory, struct fdesc fdescs[ if (setsockopt(ssd, SOL_SOCKET, SO_REUSEADDR, (const char *)&y, sizeof(y)) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to setsockopt(SO_REUSEADDR)"); } if (ssd != fdescs[0].fd) { if (dup2(ssd, fdescs[0].fd) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", ssd, fdescs[0].fd); } close(ssd); @@ -1927,37 +1725,23 @@ static void *make_udp_common(const struct factory *factory, struct fdesc fdescs[ init_addr(sin, iserver_port); if (bserver_do_bind) { if (bind(ssd, sin, addr_size) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to bind a server socket"); } } csd = socket(family, SOCK_DGRAM, blite? IPPROTO_UDPLITE: 0); if (csd < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to make a udp client socket"); } if (setsockopt(csd, SOL_SOCKET, SO_REUSEADDR, (const char *)&y, sizeof(y)) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to setsockopt(SO_REUSEADDR)"); } if (csd != fdescs[1].fd) { if (dup2(csd, fdescs[1].fd) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", csd, fdescs[1].fd); } close(csd); @@ -1967,20 +1751,12 @@ static void *make_udp_common(const struct factory *factory, struct fdesc fdescs[ if (bclient_do_bind) { init_addr(cin, iclient_port); if (bind(csd, cin, addr_size) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to bind a client socket"); } } if (bclient_do_connect) { if (connect(csd, sin, addr_size) < 0) { - int e = errno; - close(ssd); - close(csd); - errno = e; err(EXIT_FAILURE, "failed to connect a client socket to the server socket"); } } @@ -2029,9 +1805,6 @@ static void *make_raw_common(const struct factory *factory, struct fdesc fdescs[ if (ssd != fdescs[0].fd) { if (dup2(ssd, fdescs[0].fd) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", ssd, fdescs[0].fd); } close(ssd); @@ -2040,17 +1813,11 @@ static void *make_raw_common(const struct factory *factory, struct fdesc fdescs[ init_addr(sin, false); if (bind(ssd, sin, addr_size) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed in bind(2)"); } init_addr(sin, true); if (connect(ssd, sin, addr_size) < 0) { - int e = errno; - close(ssd); - errno = e; err(EXIT_FAILURE, "failed in connect(2)"); } @@ -2110,9 +1877,6 @@ static void *make_ping_common(const struct factory *factory, struct fdesc fdescs if (sd != fdescs[0].fd) { if (dup2(sd, fdescs[0].fd) < 0) { - int e = errno; - close(sd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", sd, fdescs[0].fd); } close(sd); @@ -2122,9 +1886,6 @@ static void *make_ping_common(const struct factory *factory, struct fdesc fdescs if (bbind) { init_addr(sin, iid); if (bind(sd, sin, addr_size) < 0) { - int e = errno; - close(sd); - errno = e; err((errno == EACCES? EXIT_EACCES: EXIT_FAILURE), "failed in bind(2)"); } @@ -2133,9 +1894,6 @@ static void *make_ping_common(const struct factory *factory, struct fdesc fdescs if (bconnect) { init_addr(sin, 0); if (connect(sd, sin, addr_size) < 0) { - int e = errno; - close(sd); - errno = e; err(EXIT_FAILURE, "failed in connect(2)"); } } @@ -2259,9 +2017,6 @@ static void *make_netns(const struct factory *factory _U_, struct fdesc fdescs[] if (ns != fdescs[0].fd) { if (dup2(ns, fdescs[0].fd) < 0) { - int e = errno; - close(ns); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", ns, fdescs[0].fd); } close(ns); @@ -2295,9 +2050,6 @@ static void *make_netlink(const struct factory *factory, struct fdesc fdescs[], if (sd != fdescs[0].fd) { if (dup2(sd, fdescs[0].fd) < 0) { - int e = errno; - close(sd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", sd, fdescs[0].fd); } close(sd); @@ -2309,9 +2061,6 @@ static void *make_netlink(const struct factory *factory, struct fdesc fdescs[], nl.nl_family = AF_NETLINK; nl.nl_groups = ugroups; if (bind(sd, (struct sockaddr*)&nl, sizeof(nl)) < 0) { - int e = errno; - close(sd); - errno = e; err(EXIT_FAILURE, "failed in bind(2)"); } @@ -2339,9 +2088,6 @@ static void *make_eventfd(const struct factory *factory _U_, struct fdesc fdescs if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); } close(fd); @@ -2354,19 +2100,12 @@ static void *make_eventfd(const struct factory *factory _U_, struct fdesc fdescs }; if (dup2(fdescs[0].fd, fdescs[1].fd) < 0) { - int e = errno; - close(fdescs[0].fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fdescs[0].fd, fdescs[1].fd); } signal(SIGCHLD, abort_with_child_death_message); *pid = fork(); if (*pid < 0) { - int e = errno; - close(fdescs[0].fd); - close(fdescs[1].fd); - errno = e; err(EXIT_FAILURE, "failed in fork()"); } else if (*pid == 0) { uint64_t v = 1; @@ -2377,7 +2116,6 @@ static void *make_eventfd(const struct factory *factory _U_, struct fdesc fdescs signal(SIGCONT, do_nothing); /* Notify the parent that I'm ready. */ if (write(fdescs[1].fd, &v, sizeof(v)) != sizeof(v)) { - close(fdescs[1].fd); err(EXIT_FAILURE, "failed in write() to notify the readiness to the prent"); } @@ -2395,8 +2133,6 @@ static void *make_eventfd(const struct factory *factory _U_, struct fdesc fdescs /* Wait till the child is ready. */ if (read(fdescs[0].fd, &v, sizeof(uint64_t)) != sizeof(v)) { - free(pid); - close(fdescs[0].fd); err(EXIT_FAILURE, "failed in read() the readiness notification from the child"); } @@ -2529,7 +2265,6 @@ static void *make_mqueue(const struct factory *factory, struct fdesc fdescs[], if (fd != fdescs[0].fd) { if (dup2(fd, fdescs[0].fd) < 0) { int e = errno; - mq_close(fd); mqueue_data_free(mqueue_data); errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd); @@ -2546,7 +2281,6 @@ static void *make_mqueue(const struct factory *factory, struct fdesc fdescs[], fd = mq_open(mqueue_data->path, O_WRONLY, S_IRUSR | S_IWUSR, NULL); if (fd < 0) { int e = errno; - mq_close(fdescs[0].fd); mqueue_data_free(mqueue_data); errno = e; err(EXIT_FAILURE, "failed in mq_open(3) for writing"); @@ -2554,10 +2288,6 @@ static void *make_mqueue(const struct factory *factory, struct fdesc fdescs[], if (fd != fdescs[1].fd) { if (dup2(fd, fdescs[1].fd) < 0) { - int e = errno; - mq_close(fd); - mq_close(fdescs[0].fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[1].fd); } mq_close(fd); @@ -2572,8 +2302,6 @@ static void *make_mqueue(const struct factory *factory, struct fdesc fdescs[], mqueue_data->pid = fork(); if (mqueue_data->pid < 0) { int e = errno; - mq_close(fdescs[0].fd); - mq_close(fdescs[1].fd); mqueue_data_free(mqueue_data); errno = e; err(EXIT_FAILURE, "failed in fork()"); @@ -2602,7 +2330,6 @@ static void *make_mqueue(const struct factory *factory, struct fdesc fdescs[], /* Wait till the child is ready. */ if (mq_receive(fdescs[0].fd, &c, 1, NULL) < 0) { int e = errno; - mq_close(fdescs[0].fd); mqueue_data_free(mqueue_data); errno = e; err(EXIT_FAILURE, @@ -2680,9 +2407,6 @@ static void *make_eventpoll(const struct factory *factory _U_, struct fdesc fdes err(EXIT_FAILURE, "failed in epoll_create(2)"); if (efd != fdescs[0].fd) { if (dup2(efd, fdescs[0].fd) < 0) { - int e = errno; - close(efd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", efd, fdescs[0].fd); } close(efd); @@ -2697,22 +2421,11 @@ static void *make_eventpoll(const struct factory *factory _U_, struct fdesc fdes for (size_t i = 1; i < ARRAY_SIZE(specs); i++) { int fd = open(specs[i].file, specs[i].flag); if (fd < 0) { - int e = errno; - close(efd); - for (size_t j = i - 1; j > 0; j--) - close(fdescs[j].fd); - errno = e; err(EXIT_FAILURE, "failed in open(\"%s\",...)", specs[i].file); } if (fd != fdescs[i].fd) { if (dup2(fd, fdescs[i].fd) < 0) { - int e = errno; - close(efd); - for (size_t j = i - 1; j > 0; j--) - close(fdescs[j].fd); - close(fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[i].fd); } @@ -2728,11 +2441,6 @@ static void *make_eventpoll(const struct factory *factory _U_, struct fdesc fdes .events = specs[i].events, .data = {.ptr = NULL,} }) < 0) { - int e = errno; - close(efd); - for (size_t j = i; j > 0; j--) - close(fdescs[j].fd); - errno = e; err(EXIT_FAILURE, "failed to add fd %d to the eventpoll fd with epoll_ctl", fdescs[i].fd); @@ -2810,17 +2518,11 @@ static void *make_timerfd(const struct factory *factory, struct fdesc fdescs[], tspec.it_interval.tv_nsec = uinterval_frac; if (timerfd_settime(tfd, babstime? TFD_TIMER_ABSTIME: 0, &tspec, NULL) < 0) { - int e = errno; - close(tfd); - errno = e; err(EXIT_FAILURE, "failed in timerfd_settime(2)"); } if (tfd != fdescs[0].fd) { if (dup2(tfd, fdescs[0].fd) < 0) { - int e = errno; - close(tfd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", tfd, fdescs[0].fd); } close(tfd); @@ -2856,9 +2558,6 @@ static void *make_signalfd(const struct factory *factory _U_, struct fdesc fdesc if (sfd != fdescs[0].fd) { if (dup2(sfd, fdescs[0].fd) < 0) { - int e = errno; - close(sfd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", sfd, fdescs[0].fd); } close(sfd); @@ -2890,17 +2589,11 @@ static void *make_cdev_tun(const struct factory *factory _U_, struct fdesc fdesc strcpy(ifr.ifr_name, "mkfds%d"); if (ioctl(tfd, TUNSETIFF, (void *) &ifr) < 0) { - int e = errno; - close(tfd); - errno = e; err(EXIT_FAILURE, "failed in setting \"lo\" to the tun device"); } if (tfd != fdescs[0].fd) { if (dup2(tfd, fdescs[0].fd) < 0) { - int e = errno; - close(tfd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", tfd, fdescs[0].fd); } close(tfd); @@ -2968,9 +2661,6 @@ static void *make_bpf_prog(const struct factory *factory, struct fdesc fdescs[], if (bfd != fdescs[0].fd) { if (dup2(bfd, fdescs[0].fd) < 0) { - int e = errno; - close(bfd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", bfd, fdescs[0].fd); } close(bfd); @@ -3069,9 +2759,6 @@ static void *make_bpf_map(const struct factory *factory, struct fdesc fdescs[], if (bfd != fdescs[0].fd) { if (dup2(bfd, fdescs[0].fd) < 0) { - int e = errno; - close(bfd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", bfd, fdescs[0].fd); } close(bfd); @@ -3097,32 +2784,20 @@ static void *make_pty(const struct factory *factory _U_, struct fdesc fdescs[], err(EXIT_FAILURE, "failed in opening /dev/ptmx"); if (unlockpt(ptmx_fd) < 0) { - int e = errno; - close(ptmx_fd); - errno = e; err(EXIT_FAILURE, "failed in unlockpt()"); } if (ioctl(ptmx_fd, TIOCGPTN, &index) < 0) { - int e = errno; - close(ptmx_fd); - errno = e; err(EXIT_FAILURE, "failed in ioctl(TIOCGPTN)"); } pts = ptsname(ptmx_fd); if (pts == NULL) { - int e = errno; - close(ptmx_fd); - errno = e; err(EXIT_FAILURE, "failed in ptsname()"); } if (ptmx_fd != fdescs[0].fd) { if (dup2(ptmx_fd, fdescs[0].fd) < 0) { - int e = errno; - close(ptmx_fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", ptmx_fd, fdescs[0].fd); } close(ptmx_fd); @@ -3131,18 +2806,11 @@ static void *make_pty(const struct factory *factory _U_, struct fdesc fdescs[], pts_fd = open(pts, O_RDONLY); if (pts_fd < 0) { - int e = errno; - close(ptmx_fd); - errno = e; err(EXIT_FAILURE, "failed in opening %s", pts); } if (pts_fd != fdescs[1].fd) { if (dup2(pts_fd, fdescs[1].fd) < 0) { - int e = errno; - close(pts_fd); - close(ptmx_fd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", pts_fd, fdescs[1].fd); } close(pts_fd); @@ -3197,16 +2865,10 @@ static void *make_mmap(const struct factory *factory, struct fdesc fdescs[] _U_, struct stat sb; if (fstat(fd, &sb) < 0) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed in fstat()"); } char *addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) { - int e = errno; - close(fd); - errno = e; err(EXIT_FAILURE, "failed in mmap()"); } close(fd); @@ -3285,18 +2947,12 @@ static void *make_userns(const struct factory *factory _U_, struct fdesc fdescs[ err(EXIT_FAILURE, "failed to open /proc/self/ns/user for the new user ns"); if (unshare(CLONE_NEWUSER) < 0) { - int e = errno; - close(userns); - errno = e; err((errno == EPERM? EXIT_EPERM: EXIT_FAILURE), "failed in the 2nd unshare(2)"); } if (userns != fdescs[0].fd) { if (dup2(userns, fdescs[0].fd) < 0) { - int e = errno; - close(userns); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", userns, fdescs[0].fd); } close(userns); @@ -3423,9 +3079,6 @@ static void *make_sockdiag(const struct factory *factory, struct fdesc fdescs[], if (diagsd != fdescs[0].fd) { if (dup2(diagsd, fdescs[0].fd) < 0) { - e = errno; - close(diagsd); - errno = e; err(EXIT_FAILURE, "failed to dup %d -> %d", diagsd, fdescs[0].fd); } close(diagsd);