X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=src%2Fcoloredstderr.c;h=c36e7d70d3688a4d5c234dd7b0a33c5d22e0e14c;hb=f54cd101d750cb31f54081a41d6cb880a9199dba;hp=586b0240d3df19b3a70a3f4a1d0692e81a36345f;hpb=0d7f3068981f2b08e583cec21d9069e97c73addd;p=coloredstderr%2Fcoloredstderr.git diff --git a/src/coloredstderr.c b/src/coloredstderr.c index 586b024..c36e7d7 100644 --- a/src/coloredstderr.c +++ b/src/coloredstderr.c @@ -2,7 +2,7 @@ * Hook output functions (like printf(3)) with LD_PRELOAD to color stderr (or * other file descriptors). * - * Copyright (C) 2013 Simon Ruderich + * Copyright (C) 2013-2014 Simon Ruderich * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -81,6 +81,12 @@ static int force_write_to_non_tty; /* Was ENV_NAME_FDS found and used when init_from_environment() was called? * This is not true if the process set it manually after initialization. */ static int used_fds_set_by_user; +/* Was any of our handle_*_pre()/handle_*_post() functions called recursively? + * If so don't print the pre/post string for the recursive calls. This is + * necessary on some systems (e.g. FreeBSD 9.1) which call multiple hooked + * functions while printing a string (e.g. a FILE * and a fd hook function is + * called). */ +static int handle_recursive; #include "constants.h" @@ -181,9 +187,13 @@ static void handle_file_pre(FILE *stream) noinline; static void handle_file_post(FILE *stream) noinline; static void handle_fd_pre(int fd) { + if (handle_recursive++ > 0) { + return; + } + int saved_errno = errno; - if (unlikely(!pre_string || !post_string)) { + if (unlikely(!pre_string)) { init_pre_post_string(); } @@ -193,6 +203,10 @@ static void handle_fd_pre(int fd) { errno = saved_errno; } static void handle_fd_post(int fd) { + if (--handle_recursive > 0) { + return; + } + int saved_errno = errno; /* write() already loaded above in handle_fd_pre(). */ @@ -202,9 +216,13 @@ static void handle_fd_post(int fd) { } static void handle_file_pre(FILE *stream) { + if (handle_recursive++ > 0) { + return; + } + int saved_errno = errno; - if (unlikely(!pre_string || !post_string)) { + if (unlikely(!pre_string)) { init_pre_post_string(); } @@ -214,6 +232,10 @@ static void handle_file_pre(FILE *stream) { errno = saved_errno; } static void handle_file_post(FILE *stream) { + if (--handle_recursive > 0) { + return; + } + int saved_errno = errno; /* fwrite() already loaded above in handle_file_pre(). */ @@ -238,6 +260,14 @@ HOOK_FILE2(int, fputc, stream, int, c, FILE *, stream) HOOK_FILE2(int, putc, stream, int, c, FILE *, stream) +/* The glibc uses a macro for putc() which expands to _IO_putc(). However + * sometimes the raw putc() is used as well, not sure why. Make sure to hook + * it too. */ +#ifdef putc +# undef putc +HOOK_FILE2(int, putc, stream, + int, c, FILE *, stream) +#endif HOOK_FILE1(int, putchar, stdout, int, c) HOOK_FILE1(int, puts, stdout, @@ -341,7 +371,7 @@ static void error_vararg(int status, int errnum, if (error_one_per_line && filename != NULL && linenum != 0 && filename == last_filename && linenum == last_linenum) { - return; + goto out; } last_filename = filename; last_linenum = linenum; @@ -374,6 +404,7 @@ static void error_vararg(int status, int errnum, fprintf(stderr, "\n"); +out: if (status != 0) { exit(status); } @@ -406,7 +437,7 @@ HOOK_FUNC_DEF1(int, dup, int, oldfd) { DLSYM_FUNCTION(real_dup, "dup"); newfd = real_dup(oldfd); - if (newfd != -1) { + if (newfd > -1) { dup_fd(oldfd, newfd); } @@ -417,7 +448,7 @@ HOOK_FUNC_DEF2(int, dup2, int, oldfd, int, newfd) { DLSYM_FUNCTION(real_dup2, "dup2"); newfd = real_dup2(oldfd, newfd); - if (newfd != -1) { + if (newfd > -1) { dup_fd(oldfd, newfd); } @@ -428,7 +459,7 @@ HOOK_FUNC_DEF3(int, dup3, int, oldfd, int, newfd, int, flags) { DLSYM_FUNCTION(real_dup3, "dup3"); newfd = real_dup3(oldfd, newfd, flags); - if (newfd != -1) { + if (newfd > -1) { dup_fd(oldfd, newfd); } @@ -460,7 +491,7 @@ HOOK_FUNC_VAR_DEF2(int, fcntl, int, fd, int, cmd /*, ... */) { va_end(ap); /* We only care about duping fds. */ - if (cmd == F_DUPFD && result != -1) { + if (cmd == F_DUPFD && result > -1) { dup_fd(fd, result); } @@ -646,10 +677,16 @@ HOOK_FUNC_DEF2(int, execvp, char const *, file, char * const *, argv) { #ifdef HAVE_EXECVPE extern char **environ; int execvpe(char const *file, char * const argv[], char * const envp[]) { + int result; + char **old_environ = environ; + /* Fake the environment so we can reuse execvp(). */ environ = (char **)envp; /* execvp() updates the environment. */ - return execvp(file, argv); + result = execvp(file, argv); + + environ = old_environ; + return result; } #endif