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

@@ -570,3 +570,19 @@ sudo_debug_fd_get(void)
{
return sudo_debug_fd;
}
/*
* Setter for the debug descriptor.
*/
int
sudo_debug_fd_set(int fd)
{
if (sudo_debug_fd != -1 && fd != sudo_debug_fd) {
if (dup2(sudo_debug_fd, fd) == -1)
return -1;
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
close(sudo_debug_fd);
sudo_debug_fd = fd;
}
return sudo_debug_fd;
}

View File

@@ -219,6 +219,7 @@ void sudo_debug_exit_str(const char *func, const char *file, int line, int subsy
void sudo_debug_exit_str_masked(const char *func, const char *file, int line, int subsys, const char *rval);
void sudo_debug_exit_ptr(const char *func, const char *file, int line, int subsys, const void *rval);
int sudo_debug_fd_get(void);
int sudo_debug_fd_set(int fd);
int sudo_debug_init(const char *debugfile, const char *settings);
void sudo_debug_printf_nvm(int pri, const char *fmt, ...) __printf0like(2, 3);
void sudo_debug_printf2(const char *func, const char *file, int line, int level, const char *fmt, ...) __printf0like(5, 6);

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);
}
}