2 * Macros to hook functions.
4 * Copyright (C) 2013 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Hook the function by creating a function with the same name. With
24 * LD_PRELOAD our function will be preferred. The original function is stored
25 * in a static variable (real_*). Any function called in these macros must
26 * make sure to restore the errno if it changes it.
28 * "Pseudo code" for the following macros. <name> is the name of the hooked
29 * function, <fd> is either a file descriptor or a FILE pointer.
32 * real_<name> = dlsym_function(<name>);
34 * init_from_environment();
37 * if (tracked_fds_find(<fd>)) {
38 * if (force_write_to_non_tty) {
41 * handle = isatty_noinline(<fd>);
48 * handle_<fd>_pre(<fd>);
50 * <type> result = real_<name>(<args>);
52 * handle_<fd>_post(<fd>);
57 #define _HOOK_PRE(type, name, fd) \
59 if (unlikely(!(real_ ## name ))) { \
60 *(void **) (&(real_ ## name)) = dlsym_function(#name); \
61 /* Initialize our data while we're at it. */ \
62 if (unlikely(!initialized)) { \
63 init_from_environment(); \
66 /* Check if this fd should be handled. */ \
67 if (unlikely(tracked_fds_find(fd))) { \
68 if (unlikely(force_write_to_non_tty)) { \
71 handle = isatty_noinline(fd); \
76 #define _HOOK_PRE_FD(type, name, fd) \
78 _HOOK_PRE_FD_(type, name, fd)
79 #define _HOOK_PRE_FD_(type, name, fd) \
80 _HOOK_PRE(type, name, fd) \
81 if (unlikely(handle)) { \
84 #define _HOOK_PRE_FILE(type, name, file) \
86 _HOOK_PRE(type, name, fileno(file)) \
87 if (unlikely(handle)) { \
88 handle_file_pre(file); \
90 #define _HOOK_POST_FD_(fd) \
91 if (unlikely(handle)) { \
94 #define _HOOK_POST_FD(fd) \
97 #define _HOOK_POST_FILE(file) \
98 if (unlikely(handle)) { \
99 handle_file_post(file); \
104 #define HOOK_FUNC_DEF1(type, name, type1, arg1) \
105 static type (*real_ ## name)(type1); \
106 type name(type1 arg1)
107 #define HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) \
108 static type (*real_ ## name)(type1, type2); \
109 type name(type1 arg1, type2 arg2)
110 #define HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) \
111 static type (*real_ ## name)(type1, type2, type3); \
112 type name(type1 arg1, type2 arg2, type3 arg3)
113 #define HOOK_FUNC_DEF4(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
114 static type (*real_ ## name)(type1, type2, type3, type4); \
115 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4)
117 #define HOOK_FUNC_VAR_DEF2(type, name, type1, arg1, type2, arg2) \
118 static type (*real_ ## name)(type1, type2, ...); \
119 type name(type1 arg1, type2 arg2, ...)
121 #define HOOK_FUNC_SIMPLE3(type, name, type1, arg1, type2, arg2, type3, arg3) \
122 type name(type1 arg1, type2 arg2, type3 arg3)
124 #define HOOK_FUNC_VAR_SIMPLE1(type, name, type1, arg1) \
125 type name(type1 arg1, ...)
126 #define HOOK_FUNC_VAR_SIMPLE2(type, name, type1, arg1, type2, arg2) \
127 type name(type1 arg1, type2 arg2, ...)
128 #define HOOK_FUNC_VAR_SIMPLE3(type, name, type1, arg1, type2, arg2, type3, arg3) \
129 type name(type1 arg1, type2 arg2, type3 arg3, ...)
131 #define HOOK_VOID1(type, name, fd, type1, arg1) \
132 HOOK_FUNC_DEF1(type, name, type1, arg1) { \
133 _HOOK_PRE_FD_(type, name, fd) \
134 real_ ## name(arg1); \
137 #define HOOK_VOID2(type, name, fd, type1, arg1, type2, arg2) \
138 HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) { \
139 _HOOK_PRE_FD_(type, name, fd) \
140 real_ ## name(arg1, arg2); \
143 #define HOOK_VOID3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \
144 HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) { \
145 _HOOK_PRE_FD_(type, name, fd) \
146 real_ ## name(arg1, arg2, arg3); \
150 #define HOOK_VAR_VOID1(type, name, fd, func, type1, arg1) \
151 HOOK_FUNC_VAR_SIMPLE1(type, name, type1, arg1) { \
153 va_start(ap, arg1); \
157 #define HOOK_VAR_VOID2(type, name, fd, func, type1, arg1, type2, arg2) \
158 HOOK_FUNC_VAR_SIMPLE2(type, name, type1, arg1, type2, arg2) { \
160 va_start(ap, arg2); \
161 func(arg1, arg2, ap); \
165 #define HOOK_FD2(type, name, fd, type1, arg1, type2, arg2) \
166 HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) { \
167 _HOOK_PRE_FD(type, name, fd) \
168 result = real_ ## name(arg1, arg2); \
171 #define HOOK_FD3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \
172 HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) { \
173 _HOOK_PRE_FD(type, name, fd) \
174 result = real_ ## name(arg1, arg2, arg3); \
178 #define HOOK_FILE1(type, name, file, type1, arg1) \
179 HOOK_FUNC_DEF1(type, name, type1, arg1) { \
180 _HOOK_PRE_FILE(type, name, file) \
181 result = real_ ## name(arg1); \
182 _HOOK_POST_FILE(file) \
184 #define HOOK_FILE2(type, name, file, type1, arg1, type2, arg2) \
185 HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) { \
186 _HOOK_PRE_FILE(type, name, file) \
187 result = real_ ## name(arg1, arg2); \
188 _HOOK_POST_FILE(file) \
190 #define HOOK_FILE3(type, name, file, type1, arg1, type2, arg2, type3, arg3) \
191 HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) { \
192 _HOOK_PRE_FILE(type, name, file) \
193 result = real_ ## name(arg1, arg2, arg3); \
194 _HOOK_POST_FILE(file) \
196 #define HOOK_FILE4(type, name, file, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
197 HOOK_FUNC_DEF4(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) { \
198 _HOOK_PRE_FILE(type, name, file) \
199 result = real_ ## name(arg1, arg2, arg3, arg4); \
200 _HOOK_POST_FILE(file) \
203 #define HOOK_VAR_FILE1(type, name, file, func, type1, arg1) \
204 HOOK_FUNC_VAR_SIMPLE1(type, name, type1, arg1) { \
206 va_start(ap, arg1); \
207 type result = func(arg1, ap); \
211 #define HOOK_VAR_FILE2(type, name, file, func, type1, arg1, type2, arg2) \
212 HOOK_FUNC_VAR_SIMPLE2(type, name, type1, arg1, type2, arg2) { \
214 va_start(ap, arg2); \
215 type result = func(arg1, arg2, ap); \
219 #define HOOK_VAR_FILE3(type, name, file, func, type1, arg1, type2, arg2, type3, arg3) \
220 HOOK_FUNC_VAR_SIMPLE3(type, name, type1, arg1, type2, arg2, type3, arg3) { \
222 va_start(ap, arg3); \
223 type result = func(arg1, arg2, arg3, ap); \