]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/hookmacros.h
Rename macros.h to hookmacros.h.
[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_*). */
26
27 #define _HOOK_PRE(type, name) \
28         int handle; \
29         type result; \
30         DLSYM_FUNCTION(real_ ## name, #name);
31 #define _HOOK_PRE_FD(type, name, fd) \
32         _HOOK_PRE(type, name) \
33         handle = check_handle_fd(fd); \
34         if (handle) { \
35             handle_fd_pre(fd, handle); \
36         }
37 #define _HOOK_PRE_FILE(type, name, file) \
38         _HOOK_PRE(type, name) \
39         handle = check_handle_fd(fileno(file)); \
40         if (handle) { \
41             handle_file_pre(file, handle); \
42         }
43 /* Save and restore the errno to make sure we return the errno of the original
44  * function call. */
45 #define _HOOK_POST_FD(fd) \
46         if (handle) { \
47             int saved_errno = errno; \
48             handle_fd_post(fd, handle); \
49             errno = saved_errno; \
50         } \
51         return result;
52 #define _HOOK_POST_FILE(file) \
53         if (handle) { \
54             int saved_errno = errno; \
55             handle_file_post(file, handle); \
56             errno = saved_errno; \
57         } \
58         return result;
59
60
61 #define HOOK_FD3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \
62     static type (*real_ ## name)(type1, type2, type3); \
63     type name(type1 arg1, type2 arg2, type3 arg3) { \
64         _HOOK_PRE_FD(type, name, fd) \
65         result = real_ ## name(arg1, arg2, arg3); \
66         _HOOK_POST_FD(fd) \
67     }
68
69 #define HOOK_FILE1(type, name, file, type1, arg1) \
70     static type (*real_ ## name)(type1); \
71     type name(type1 arg1) { \
72         _HOOK_PRE_FILE(type, name, file) \
73         result = real_ ## name(arg1); \
74         _HOOK_POST_FILE(file) \
75     }
76 #define HOOK_FILE2(type, name, file, type1, arg1, type2, arg2) \
77     static type (*real_ ## name)(type1, type2); \
78     type name(type1 arg1, type2 arg2) { \
79         _HOOK_PRE_FILE(type, name, file) \
80         result = real_ ## name(arg1, arg2); \
81         _HOOK_POST_FILE(file) \
82     }
83 #define HOOK_FILE3(type, name, file, type1, arg1, type2, arg2, type3, arg3) \
84     static type (*real_ ## name)(type1, type2, type3); \
85     type name(type1 arg1, type2 arg2, type3 arg3) { \
86         _HOOK_PRE_FILE(type, name, file) \
87         result = real_ ## name(arg1, arg2, arg3); \
88         _HOOK_POST_FILE(file) \
89     }
90 #define HOOK_FILE4(type, name, file, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
91     static type (*real_ ## name)(type1, type2, type3, type4); \
92     type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
93         _HOOK_PRE_FILE(type, name, file) \
94         result = real_ ## name(arg1, arg2, arg3, arg4); \
95         _HOOK_POST_FILE(file) \
96     }
97
98 #define HOOK_VAR_FILE1(type, name, file, func, type1, arg1) \
99     static type (*real_ ## func)(type1, va_list); \
100     type name(type1 arg1, ...) { \
101         va_list ap; \
102         _HOOK_PRE_FILE(type, func, file) \
103         va_start(ap, arg1); \
104         result = real_ ## func(arg1, ap); \
105         va_end(ap); \
106         _HOOK_POST_FILE(file) \
107     }
108 #define HOOK_VAR_FILE2(type, name, file, func, type1, arg1, type2, arg2) \
109     static type (*real_ ## func)(type1, type2, va_list); \
110     type name(type1 arg1, type2 arg2, ...) { \
111         va_list ap; \
112         _HOOK_PRE_FILE(type, func, file) \
113         va_start(ap, arg2); \
114         result = real_ ## func(arg1, arg2, ap); \
115         va_end(ap); \
116         _HOOK_POST_FILE(file) \
117     }
118 #define HOOK_VAR_FILE3(type, name, file, func, type1, arg1, type2, arg2, type3, arg3) \
119     static type (*real_ ## func)(type1, type2, type3, va_list); \
120     type name(type1 arg1, type2 arg2, type3 arg3, ...) \
121     { \
122         va_list ap; \
123         _HOOK_PRE_FILE(type, func, file) \
124         va_start(ap, arg3); \
125         result = real_ ## func(arg1, arg2, arg3, ap); \
126         va_end(ap); \
127         _HOOK_POST_FILE(file) \
128     }
129
130 #endif