X-Git-Url: https://ruderich.org/simon/gitweb/?p=coloredstderr%2Fcoloredstderr.git;a=blobdiff_plain;f=src%2Fhookmacros.h;h=ebd5f7a80cdf4127da5a241fad99c6f466f5a8ea;hp=ffbc6b6de457a7b45aa2a31c285fb03fdff3d873;hb=217e8c8bc5fa8c22221514a320d6edeb1c2a101f;hpb=6d15f6c9902c1ac879ce0dee201ff8c6c66afaf1 diff --git a/src/hookmacros.h b/src/hookmacros.h index ffbc6b6..ebd5f7a 100644 --- a/src/hookmacros.h +++ b/src/hookmacros.h @@ -22,42 +22,114 @@ /* Hook the function by creating a function with the same name. With * LD_PRELOAD our function will be preferred. The original function is stored - * in a static variable (real_*). */ + * in a static variable (real_*). Any function called in these macros must + * make sure to restore the errno if it changes it. + * + * "Pseudo code" for the following macros. is the name of the hooked + * function, is either a file descriptor or a FILE pointer. + * + * if (!real_) { + * real_ = dlsym_function(); + * if (!initialized) { + * init_from_environment(); + * } + * } + * if (tracked_fds_find()) { + * if (force_write_to_non_tty) { + * handle = 1; + * } else { + * handle = isatty_noinline(); + * } + * } else { + * handle = 0; + * } + * + * if (handle) { + * handle__pre(); + * } + * result = real_(); + * if (handle) { + * handle__post(); + * } + * return result; + */ -#define _HOOK_PRE(type, name) \ +#define _HOOK_PRE(type, name, fd) \ int handle; \ - type result; \ - DLSYM_FUNCTION(real_ ## name, #name); + if (unlikely(!(real_ ## name ))) { \ + *(void **) (&(real_ ## name)) = dlsym_function(#name); \ + /* Initialize our data while we're at it. */ \ + if (unlikely(!initialized)) { \ + init_from_environment(); \ + } \ + } \ + /* Check if this fd should be handled. */ \ + if (unlikely(tracked_fds_find(fd))) { \ + if (unlikely(force_write_to_non_tty)) { \ + handle = 1; \ + } else { \ + handle = isatty_noinline(fd); \ + } \ + } else { \ + handle = 0; \ + } #define _HOOK_PRE_FD(type, name, fd) \ - _HOOK_PRE(type, name) \ - handle = check_handle_fd(fd); \ - if (handle) { \ - handle_fd_pre(fd, handle); \ + type result; \ + _HOOK_PRE_FD_(type, name, fd) +#define _HOOK_PRE_FD_(type, name, fd) \ + _HOOK_PRE(type, name, fd) \ + if (unlikely(handle)) { \ + handle_fd_pre(fd); \ } #define _HOOK_PRE_FILE(type, name, file) \ - _HOOK_PRE(type, name) \ - handle = check_handle_fd(fileno(file)); \ - if (handle) { \ - handle_file_pre(file, handle); \ + type result; \ + _HOOK_PRE(type, name, fileno(file)) \ + if (unlikely(handle)) { \ + handle_file_pre(file); \ + } +#define _HOOK_POST_FD_(fd) \ + if (unlikely(handle)) { \ + handle_fd_post(fd); \ } -/* Save and restore the errno to make sure we return the errno of the original - * function call. */ #define _HOOK_POST_FD(fd) \ - if (handle) { \ - int saved_errno = errno; \ - handle_fd_post(fd, handle); \ - errno = saved_errno; \ - } \ + _HOOK_POST_FD_(fd) \ return result; #define _HOOK_POST_FILE(file) \ - if (handle) { \ - int saved_errno = errno; \ - handle_file_post(file, handle); \ - errno = saved_errno; \ + if (unlikely(handle)) { \ + handle_file_post(file); \ } \ return result; +#define HOOK_FUNC_DEF1(type, name, type1, arg1) \ + static type (*real_ ## name)(type1); \ + type name(type1 arg1) +#define HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) \ + static type (*real_ ## name)(type1, type2); \ + type name(type1 arg1, type2 arg2) +#define HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) \ + static type (*real_ ## name)(type1, type2, type3); \ + type name(type1 arg1, type2 arg2, type3 arg3) + +#define HOOK_FUNC_VAR_DEF2(type, name, type1, arg1, type2, arg2) \ + static type (*real_ ## name)(type1, type2, ...); \ + type name(type1 arg1, type2 arg2, ...) + +#define HOOK_VOID1(type, name, fd, type1, arg1) \ + static type (*real_ ## name)(type1); \ + type name(type1 arg1) { \ + _HOOK_PRE_FD_(type, name, fd) \ + real_ ## name(arg1); \ + _HOOK_POST_FD_(fd) \ + } + +#define HOOK_FD2(type, name, fd, type1, arg1, type2, arg2) \ + static type (*real_ ## name)(type1, type2); \ + type name(type1 arg1, type2 arg2) { \ + _HOOK_PRE_FD(type, name, fd) \ + result = real_ ## name(arg1, arg2); \ + _HOOK_POST_FD(fd) \ + } #define HOOK_FD3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \ static type (*real_ ## name)(type1, type2, type3); \ type name(type1 arg1, type2 arg2, type3 arg3) { \ @@ -117,8 +189,7 @@ } #define HOOK_VAR_FILE3(type, name, file, func, type1, arg1, type2, arg2, type3, arg3) \ static type (*real_ ## func)(type1, type2, type3, va_list); \ - type name(type1 arg1, type2 arg2, type3 arg3, ...) \ - { \ + type name(type1 arg1, type2 arg2, type3 arg3, ...) { \ va_list ap; \ _HOOK_PRE_FILE(type, func, file) \ va_start(ap, arg3); \