When relocating fds, update the debug fd if it is set so we are

guaranteed to get debugging output.
This commit is contained in:
Todd C. Miller
2014-01-15 06:00:59 -07:00
parent 640a5ddb48
commit 8ca711ee15
3 changed files with 24 additions and 1 deletions

View File

@@ -99,11 +99,13 @@ add_preserved_fd(struct preserved_fd_list *pfds, int fd)
void
closefrom_except(int startfd, struct preserved_fd_list *pfds)
{
int fd, lastfd = -1;
int debug_fd, fd, lastfd = -1;
struct preserved_fd *pfd, *pfd_next;
fd_set *fdsp;
debug_decl(closefrom_except, SUDO_DEBUG_UTIL)
debug_fd = sudo_debug_fd_get();
/* First, relocate preserved fds to be as contiguous as possible. */
TAILQ_FOREACH_REVERSE_SAFE(pfd, pfds, preserved_fd_list, entries, pfd_next) {
if (pfd->highfd < startfd)
@@ -117,6 +119,8 @@ closefrom_except(int startfd, struct preserved_fd_list *pfds)
}
pfd->lowfd = fd;
fd = pfd->highfd;
if (fd == debug_fd)
debug_fd = sudo_debug_fd_set(pfd->lowfd);
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"dup %d -> %d", pfd->highfd, pfd->lowfd);
}
@@ -182,6 +186,8 @@ closefrom_except(int startfd, struct preserved_fd_list *pfds)
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"fcntl(%d, F_SETFD, %d)", pfd->highfd, pfd->flags);
}
if (pfd->lowfd == debug_fd)
debug_fd = sudo_debug_fd_set(pfd->highfd);
(void) close(pfd->lowfd);
}
}