From dfd317650f05542db49d7bc37649a232f28efa3f Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Mon, 10 Jun 2013 05:24:43 +0200 Subject: [PATCH] Remove errno save/restore where not necessary. --- src/coloredstderr.c | 34 +++++++++++++++++++++++----------- src/debug.h | 11 +++++++++++ src/hookmacros.h | 13 ++----------- src/ldpreload.h | 2 ++ src/trackfds.h | 10 ++++++++++ 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/coloredstderr.c b/src/coloredstderr.c index c36c2f7..8775541 100644 --- a/src/coloredstderr.c +++ b/src/coloredstderr.c @@ -75,11 +75,15 @@ static int check_handle_fd(int fd) { /* Never touch anything not going to a terminal - unless we are explicitly * asked to do so. */ - if (!force_write_to_non_tty && !isatty(fd)) { - return 0; + if (force_write_to_non_tty) { + return 1; } - return 1; + int saved_errno = errno; + int result = isatty(fd); + errno = saved_errno; + + return result; } static void dup_fd(int oldfd, int newfd) { @@ -141,29 +145,45 @@ inline static void init_pre_post_string(void) { } static void handle_fd_pre(int fd) { + int saved_errno = errno; + if (!pre_string || !post_string) { init_pre_post_string(); } DLSYM_FUNCTION(real_write, "write"); real_write(fd, pre_string, pre_string_size); + + errno = saved_errno; } static void handle_fd_post(int fd) { + int saved_errno = errno; + /* write() already loaded above in handle_fd_pre(). */ real_write(fd, post_string, post_string_size); + + errno = saved_errno; } static void handle_file_pre(FILE *stream) { + int saved_errno = errno; + if (!pre_string || !post_string) { init_pre_post_string(); } DLSYM_FUNCTION(real_fwrite, "fwrite"); real_fwrite(pre_string, pre_string_size, 1, stream); + + errno = saved_errno; } static void handle_file_post(FILE *stream) { + int saved_errno = errno; + /* fwrite() already loaded above in handle_file_pre(). */ real_fwrite(post_string, post_string_size, 1, stream); + + errno = saved_errno; } @@ -303,9 +323,7 @@ int dup(int oldfd) { newfd = real_dup(oldfd); if (newfd != -1) { - int saved_errno = errno; dup_fd(oldfd, newfd); - errno = saved_errno; } return newfd; @@ -315,9 +333,7 @@ int dup2(int oldfd, int newfd) { newfd = real_dup2(oldfd, newfd); if (newfd != -1) { - int saved_errno = errno; dup_fd(oldfd, newfd); - errno = saved_errno; } return newfd; @@ -327,9 +343,7 @@ int dup3(int oldfd, int newfd, int flags) { newfd = real_dup3(oldfd, newfd, flags); if (newfd != -1) { - int saved_errno = errno; dup_fd(oldfd, newfd); - errno = saved_errno; } return newfd; @@ -361,9 +375,7 @@ int fcntl(int fd, int cmd, ...) { /* We only care about duping fds. */ if (cmd == F_DUPFD && result != -1) { - int saved_errno = errno; dup_fd(fd, result); - errno = saved_errno; } return result; diff --git a/src/debug.h b/src/debug.h index aebd88c..7425ce4 100644 --- a/src/debug.h +++ b/src/debug.h @@ -44,10 +44,13 @@ static void debug_write(int fd, int first_call, char const *format, va_list ap) static void debug(char const *format, ...) { va_list ap; + int saved_errno = errno; + /* If the file doesn't exist, do nothing. Prevents writing log files in * unexpected places. The user must create the file manually. */ int fd = open(DEBUG_FILE, O_WRONLY | O_APPEND); if (fd == -1) { + errno = saved_errno; return; } @@ -57,13 +60,18 @@ static void debug(char const *format, ...) { va_start(ap, format); debug_write(fd, call_count == 1, format, ap); va_end(ap); + + errno = saved_errno; } static void warning(char const *format, ...) { va_list ap; + int saved_errno = errno; + char const *home = getenv("HOME"); if (!home) { + errno = saved_errno; return; } @@ -75,6 +83,7 @@ static void warning(char const *format, ...) { /* Create the warning file if it doesn't exist yet. */ int fd = open(path, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); if (fd == -1) { + errno = saved_errno; return; } @@ -84,6 +93,8 @@ static void warning(char const *format, ...) { va_start(ap, format); debug_write(fd, call_count == 1, format, ap); va_end(ap); + + errno = saved_errno; } #endif diff --git a/src/hookmacros.h b/src/hookmacros.h index 4698869..f2a1fe8 100644 --- a/src/hookmacros.h +++ b/src/hookmacros.h @@ -26,7 +26,6 @@ #define _HOOK_PRE(type, name) \ int handle; \ - int saved_errno = errno; \ DLSYM_FUNCTION(real_ ## name, #name); #define _HOOK_PRE_FD(type, name, fd) \ type result; \ @@ -36,32 +35,24 @@ handle = check_handle_fd(fd); \ if (handle) { \ handle_fd_pre(fd); \ - } \ - errno = saved_errno; + } #define _HOOK_PRE_FILE(type, name, file) \ type result; \ _HOOK_PRE(type, name) \ handle = check_handle_fd(fileno(file)); \ if (handle) { \ handle_file_pre(file); \ - } \ - errno = saved_errno; -/* Save and restore the errno to make sure we return the errno of the original - * function call. */ + } #define _HOOK_POST_FD_(fd) \ if (handle) { \ - saved_errno = errno; \ handle_fd_post(fd); \ - errno = saved_errno; \ } #define _HOOK_POST_FD(fd) \ _HOOK_POST_FD_(fd) \ return result; #define _HOOK_POST_FILE(file) \ if (handle) { \ - saved_errno = errno; \ handle_file_post(file); \ - errno = saved_errno; \ } \ return result; diff --git a/src/ldpreload.h b/src/ldpreload.h index 33a2e75..a3d646e 100644 --- a/src/ldpreload.h +++ b/src/ldpreload.h @@ -34,6 +34,7 @@ * Terminate program on failure. */ #define DLSYM_FUNCTION(pointer, name) \ if (NULL == (pointer)) { \ + int saved_errnox = errno; \ char *error; \ dlerror(); /* Clear possibly existing error. */ \ \ @@ -44,6 +45,7 @@ * failed to load too. */ \ abort(); \ } \ + errno = saved_errnox; \ } #endif diff --git a/src/trackfds.h b/src/trackfds.h index 8237781..9b2aaa6 100644 --- a/src/trackfds.h +++ b/src/trackfds.h @@ -78,6 +78,8 @@ static void init_from_environment(void) { #endif char const *env; + int saved_errno = errno; + initialized = 1; tracked_fds_list_count = 0; @@ -90,6 +92,7 @@ static void init_from_environment(void) { env = getenv(ENV_NAME_FDS); if (!env) { + errno = saved_errno; return; } /* Environment is read-only. */ @@ -153,6 +156,8 @@ next: #ifdef DEBUG tracked_fds_debug(); #endif + + errno = saved_errno; } static char *update_environment_buffer_entry(char *x, int fd) { @@ -231,6 +236,8 @@ static void tracked_fds_add(int fd) { } if (tracked_fds_list_count >= tracked_fds_list_space) { + int saved_errno = errno; + size_t new_space = tracked_fds_list_space + TRACKFDS_REALLOC_STEP; int *tmp = realloc(tracked_fds_list, sizeof(*tracked_fds_list) * new_space); @@ -242,8 +249,11 @@ static void tracked_fds_add(int fd) { warning("realloc(tracked_fds_list, %zu) failed! [%d]\n", sizeof(*tracked_fds_list) * new_space, getpid()); #endif + errno = saved_errno; return; } + errno = saved_errno; + tracked_fds_list = tmp; tracked_fds_list_space = new_space; } -- 2.45.2