]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/hookmacros.h
Mark hooked functions as visibility(("protected")).
[coloredstderr/coloredstderr.git] / src / hookmacros.h
1 /*
2  * Macros to hook functions.
3  *
4  * Copyright (C) 2013  Simon Ruderich
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #ifndef MACROS_H
21 #define MACROS_H 1
22
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.
27  *
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.
30  *
31  *     if (!real_<name>) {
32  *         real_<name> = dlsym_function(<name>);
33  *         if (!initialized) {
34  *             init_from_environment();
35  *         }
36  *     }
37  *     if (tracked_fds_find(<fd>)) {
38  *         if (force_write_to_non_tty) {
39  *             handle = 1;
40  *         } else {
41  *             handle = isatty_noinline(<fd>);
42  *         }
43  *     } else {
44  *         handle = 0;
45  *     }
46  *
47  *     if (handle) {
48  *         handle_<fd>_pre(<fd>);
49  *     }
50  *     <type> result = real_<name>(<args>);
51  *     if (handle) {
52  *         handle_<fd>_post(<fd>);
53  *     }
54  *     return result;
55  */
56
57 #define _HOOK_PRE(type, name, fd) \
58         int handle; \
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(); \
64             } \
65         } \
66         /* Check if this fd should be handled. */ \
67         if (unlikely(tracked_fds_find(fd))) { \
68             if (unlikely(force_write_to_non_tty)) { \
69                 handle = 1; \
70             } else { \
71                 handle = isatty_noinline(fd); \
72             } \
73         } else { \
74             handle = 0; \
75         }
76 #define _HOOK_PRE_FD(type, name, fd) \
77         type result; \
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)) { \
82             handle_fd_pre(fd); \
83         }
84 #define _HOOK_PRE_FILE(type, name, file) \
85         type result; \
86         _HOOK_PRE(type, name, fileno(file)) \
87         if (unlikely(handle)) { \
88             handle_file_pre(file); \
89         }
90 #define _HOOK_POST_FD_(fd) \
91         if (unlikely(handle)) { \
92             handle_fd_post(fd); \
93         }
94 #define _HOOK_POST_FD(fd) \
95         _HOOK_POST_FD_(fd) \
96         return result;
97 #define _HOOK_POST_FILE(file) \
98         if (unlikely(handle)) { \
99             handle_file_post(file); \
100         } \
101         return result;
102
103
104 #define HOOK_FUNC_DEF1(type, name, type1, arg1) \
105     static type (*real_ ## name)(type1); \
106     type name(type1) visibility_protected; \
107     type name(type1 arg1)
108 #define HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) \
109     static type (*real_ ## name)(type1, type2); \
110     type name(type1, type2) visibility_protected; \
111     type name(type1 arg1, type2 arg2)
112 #define HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) \
113     static type (*real_ ## name)(type1, type2, type3); \
114     type name(type1, type2, type3) visibility_protected; \
115     type name(type1 arg1, type2 arg2, type3 arg3)
116 #define HOOK_FUNC_DEF4(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
117     static type (*real_ ## name)(type1, type2, type3, type4); \
118     type name(type1, type2, type3, type4) visibility_protected; \
119     type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4)
120
121 #define HOOK_FUNC_VAR_DEF2(type, name, type1, arg1, type2, arg2) \
122     static type (*real_ ## name)(type1, type2, ...); \
123     type name(type1, type2, ...) visibility_protected; \
124     type name(type1 arg1, type2 arg2, ...)
125
126 #define HOOK_FUNC_SIMPLE3(type, name, type1, arg1, type2, arg2, type3, arg3) \
127     type name(type1, type2, type3) visibility_protected; \
128     type name(type1 arg1, type2 arg2, type3 arg3)
129
130 #define HOOK_FUNC_VAR_SIMPLE1(type, name, type1, arg1) \
131     type name(type1, ...) visibility_protected; \
132     type name(type1 arg1, ...)
133 #define HOOK_FUNC_VAR_SIMPLE2(type, name, type1, arg1, type2, arg2) \
134     type name(type1, type2, ...) visibility_protected; \
135     type name(type1 arg1, type2 arg2, ...)
136 #define HOOK_FUNC_VAR_SIMPLE3(type, name, type1, arg1, type2, arg2, type3, arg3) \
137     type name(type1, type2, type3, ...) visibility_protected; \
138     type name(type1 arg1, type2 arg2, type3 arg3, ...)
139
140 #define HOOK_VOID1(type, name, fd, type1, arg1) \
141     HOOK_FUNC_DEF1(type, name, type1, arg1) { \
142         _HOOK_PRE_FD_(type, name, fd) \
143         real_ ## name(arg1); \
144         _HOOK_POST_FD_(fd) \
145     }
146 #define HOOK_VOID2(type, name, fd, type1, arg1, type2, arg2) \
147     HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) { \
148         _HOOK_PRE_FD_(type, name, fd) \
149         real_ ## name(arg1, arg2); \
150         _HOOK_POST_FD_(fd) \
151     }
152 #define HOOK_VOID3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \
153     HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) { \
154         _HOOK_PRE_FD_(type, name, fd) \
155         real_ ## name(arg1, arg2, arg3); \
156         _HOOK_POST_FD_(fd) \
157     }
158
159 #define HOOK_VAR_VOID1(type, name, fd, func, type1, arg1) \
160     HOOK_FUNC_VAR_SIMPLE1(type, name, type1, arg1) { \
161         va_list ap; \
162         va_start(ap, arg1); \
163         func(arg1, ap); \
164         va_end(ap); \
165     }
166 #define HOOK_VAR_VOID2(type, name, fd, func, type1, arg1, type2, arg2) \
167     HOOK_FUNC_VAR_SIMPLE2(type, name, type1, arg1, type2, arg2) { \
168         va_list ap; \
169         va_start(ap, arg2); \
170         func(arg1, arg2, ap); \
171         va_end(ap); \
172     }
173
174 #define HOOK_FD2(type, name, fd, type1, arg1, type2, arg2) \
175     HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) { \
176         _HOOK_PRE_FD(type, name, fd) \
177         result = real_ ## name(arg1, arg2); \
178         _HOOK_POST_FD(fd) \
179     }
180 #define HOOK_FD3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \
181     HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) { \
182         _HOOK_PRE_FD(type, name, fd) \
183         result = real_ ## name(arg1, arg2, arg3); \
184         _HOOK_POST_FD(fd) \
185     }
186
187 #define HOOK_FILE1(type, name, file, type1, arg1) \
188     HOOK_FUNC_DEF1(type, name, type1, arg1) { \
189         _HOOK_PRE_FILE(type, name, file) \
190         result = real_ ## name(arg1); \
191         _HOOK_POST_FILE(file) \
192     }
193 #define HOOK_FILE2(type, name, file, type1, arg1, type2, arg2) \
194     HOOK_FUNC_DEF2(type, name, type1, arg1, type2, arg2) { \
195         _HOOK_PRE_FILE(type, name, file) \
196         result = real_ ## name(arg1, arg2); \
197         _HOOK_POST_FILE(file) \
198     }
199 #define HOOK_FILE3(type, name, file, type1, arg1, type2, arg2, type3, arg3) \
200     HOOK_FUNC_DEF3(type, name, type1, arg1, type2, arg2, type3, arg3) { \
201         _HOOK_PRE_FILE(type, name, file) \
202         result = real_ ## name(arg1, arg2, arg3); \
203         _HOOK_POST_FILE(file) \
204     }
205 #define HOOK_FILE4(type, name, file, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
206     HOOK_FUNC_DEF4(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) { \
207         _HOOK_PRE_FILE(type, name, file) \
208         result = real_ ## name(arg1, arg2, arg3, arg4); \
209         _HOOK_POST_FILE(file) \
210     }
211
212 #define HOOK_VAR_FILE1(type, name, file, func, type1, arg1) \
213     HOOK_FUNC_VAR_SIMPLE1(type, name, type1, arg1) { \
214         va_list ap; \
215         va_start(ap, arg1); \
216         type result = func(arg1, ap); \
217         va_end(ap); \
218         return result; \
219     }
220 #define HOOK_VAR_FILE2(type, name, file, func, type1, arg1, type2, arg2) \
221     HOOK_FUNC_VAR_SIMPLE2(type, name, type1, arg1, type2, arg2) { \
222         va_list ap; \
223         va_start(ap, arg2); \
224         type result = func(arg1, arg2, ap); \
225         va_end(ap); \
226         return result; \
227     }
228 #define HOOK_VAR_FILE3(type, name, file, func, type1, arg1, type2, arg2, type3, arg3) \
229     HOOK_FUNC_VAR_SIMPLE3(type, name, type1, arg1, type2, arg2, type3, arg3) { \
230         va_list ap; \
231         va_start(ap, arg3); \
232         type result = func(arg1, arg2, arg3, ap); \
233         va_end(ap); \
234         return result; \
235     }
236
237 #endif