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