diff options
author | Todd C. Miller <Todd.Miller@courtesan.com> | 2014-01-15 06:00:59 -0700 |
---|---|---|
committer | Todd C. Miller <Todd.Miller@courtesan.com> | 2014-01-15 06:00:59 -0700 |
commit | 5156a336e50a3a1fde7df8b1e6260eabe66738de (patch) | |
tree | 6603c7349bf5f12febd992b09885c5f008b55c4a | |
parent | 209f751ef61b479afbf4d94912642173297c2b1c (diff) | |
download | sudo-5156a336e50a3a1fde7df8b1e6260eabe66738de.tar.gz |
When relocating fds, update the debug fd if it is set so we are
guaranteed to get debugging output.
-rw-r--r-- | common/sudo_debug.c | 16 | ||||
-rw-r--r-- | include/sudo_debug.h | 1 | ||||
-rw-r--r-- | src/preserve_fds.c | 8 |
3 files changed, 24 insertions, 1 deletions
diff --git a/common/sudo_debug.c b/common/sudo_debug.c index 2f542a352..ed842bc62 100644 --- a/common/sudo_debug.c +++ b/common/sudo_debug.c @@ -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; +} diff --git a/include/sudo_debug.h b/include/sudo_debug.h index f781dc0b3..31be22d09 100644 --- a/include/sudo_debug.h +++ b/include/sudo_debug.h @@ -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); diff --git a/src/preserve_fds.c b/src/preserve_fds.c index 981ac2066..d5c1fce7d 100644 --- a/src/preserve_fds.c +++ b/src/preserve_fds.c @@ -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); } } |