]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/hookmacros.h
hookmacros.h: Minor style fix.
[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) \
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 #define _HOOK_PRE_FD(type, name, fd) \
67         type result; \
68         _HOOK_PRE_FD_(type, name, fd)
69 #define _HOOK_PRE_FD_(type, name, fd) \
70         _HOOK_PRE(type, name) \
71         if (unlikely(tracked_fds_find(fd))) { \
72             if (unlikely(force_write_to_non_tty)) { \
73                 handle = 1; \
74             } else { \
75                 handle = isatty_noinline(fd); \
76             } \
77         } else { \
78             handle = 0; \
79         } \
80         if (unlikely(handle)) { \
81             handle_fd_pre(fd); \
82         }
83 #define _HOOK_PRE_FILE(type, name, file) \
84         type result; \
85         _HOOK_PRE(type, name) \
86         if (unlikely(tracked_fds_find(fileno(file)))) { \
87             if (unlikely(force_write_to_non_tty)) { \
88                 handle = 1; \
89             } else { \
90                 handle = isatty_noinline(fileno(file)); \
91             } \
92         } else { \
93             handle = 0; \
94         } \
95         if (unlikely(handle)) { \
96             handle_file_pre(file); \
97         }
98 #define _HOOK_POST_FD_(fd) \
99         if (unlikely(handle)) { \
100             handle_fd_post(fd); \
101         }
102 #define _HOOK_POST_FD(fd) \
103         _HOOK_POST_FD_(fd) \
104         return result;
105 #define _HOOK_POST_FILE(file) \
106         if (unlikely(handle)) { \
107             handle_file_post(file); \
108         } \
109         return result;
110
111
112 #define HOOK_VOID1(type, name, fd, type1, arg1) \
113     static type (*real_ ## name)(type1); \
114     type name(type1 arg1) { \
115         _HOOK_PRE_FD_(type, name, fd) \
116         real_ ## name(arg1); \
117         _HOOK_POST_FD_(fd) \
118     }
119
120 #define HOOK_FD3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \
121     static type (*real_ ## name)(type1, type2, type3); \
122     type name(type1 arg1, type2 arg2, type3 arg3) { \
123         _HOOK_PRE_FD(type, name, fd) \
124         result = real_ ## name(arg1, arg2, arg3); \
125         _HOOK_POST_FD(fd) \
126     }
127
128 #define HOOK_FILE1(type, name, file, type1, arg1) \
129     static type (*real_ ## name)(type1); \
130     type name(type1 arg1) { \
131         _HOOK_PRE_FILE(type, name, file) \
132         result = real_ ## name(arg1); \
133         _HOOK_POST_FILE(file) \
134     }
135 #define HOOK_FILE2(type, name, file, type1, arg1, type2, arg2) \
136     static type (*real_ ## name)(type1, type2); \
137     type name(type1 arg1, type2 arg2) { \
138         _HOOK_PRE_FILE(type, name, file) \
139         result = real_ ## name(arg1, arg2); \
140         _HOOK_POST_FILE(file) \
141     }
142 #define HOOK_FILE3(type, name, file, type1, arg1, type2, arg2, type3, arg3) \
143     static type (*real_ ## name)(type1, type2, type3); \
144     type name(type1 arg1, type2 arg2, type3 arg3) { \
145         _HOOK_PRE_FILE(type, name, file) \
146         result = real_ ## name(arg1, arg2, arg3); \
147         _HOOK_POST_FILE(file) \
148     }
149 #define HOOK_FILE4(type, name, file, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
150     static type (*real_ ## name)(type1, type2, type3, type4); \
151     type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
152         _HOOK_PRE_FILE(type, name, file) \
153         result = real_ ## name(arg1, arg2, arg3, arg4); \
154         _HOOK_POST_FILE(file) \
155     }
156
157 #define HOOK_VAR_FILE1(type, name, file, func, type1, arg1) \
158     static type (*real_ ## func)(type1, va_list); \
159     type name(type1 arg1, ...) { \
160         va_list ap; \
161         _HOOK_PRE_FILE(type, func, file) \
162         va_start(ap, arg1); \
163         result = real_ ## func(arg1, ap); \
164         va_end(ap); \
165         _HOOK_POST_FILE(file) \
166     }
167 #define HOOK_VAR_FILE2(type, name, file, func, type1, arg1, type2, arg2) \
168     static type (*real_ ## func)(type1, type2, va_list); \
169     type name(type1 arg1, type2 arg2, ...) { \
170         va_list ap; \
171         _HOOK_PRE_FILE(type, func, file) \
172         va_start(ap, arg2); \
173         result = real_ ## func(arg1, arg2, ap); \
174         va_end(ap); \
175         _HOOK_POST_FILE(file) \
176     }
177 #define HOOK_VAR_FILE3(type, name, file, func, type1, arg1, type2, arg2, type3, arg3) \
178     static type (*real_ ## func)(type1, type2, type3, va_list); \
179     type name(type1 arg1, type2 arg2, type3 arg3, ...) { \
180         va_list ap; \
181         _HOOK_PRE_FILE(type, func, file) \
182         va_start(ap, arg3); \
183         result = real_ ## func(arg1, arg2, arg3, ap); \
184         va_end(ap); \
185         _HOOK_POST_FILE(file) \
186     }
187
188 #endif