2 * Hook output functions (like printf(3)) with LD_PRELOAD to color stderr (or
3 * other file descriptors).
5 * Copyright (C) 2013 Simon Ruderich
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Must be loaded before the following headers. */
24 #include "ldpreload.h"
33 /* Conflicting declaration in glibc. */
34 #undef fwrite_unlocked
37 /* Used by various functions, including debug(). */
38 static ssize_t (*real_write)(int, const void *, size_t);
39 static int (*real_close)(int);
40 static size_t (*real_fwrite)(const void *, size_t, size_t, FILE *);
43 #include "constants.h"
48 #include "hookmacros.h"
53 /* Should the "action" handler be invoked for this file descriptor? */
54 static int check_handle_fd(int fd) {
55 /* Never touch anything not going to a terminal. */
60 /* Load state from environment. Only necessary once per process. */
62 init_from_environment();
65 if (tracked_fds_count == 0) {
68 return tracked_fds_find(fd);
71 static void dup_fd(int oldfd, int newfd) {
73 debug("%d -> %d\t\t\t[%d]\n", oldfd, newfd, getpid());
77 init_from_environment();
79 if (tracked_fds_count == 0) {
83 /* We are already tracking this file descriptor, add newfd to the list as
84 * it will reference the same descriptor. */
85 if (tracked_fds_find(oldfd)) {
86 if (!tracked_fds_find(newfd)) {
87 tracked_fds_add(newfd);
90 /* We are not tracking this file descriptor, remove newfd from the list
93 if (tracked_fds_remove(newfd)) {
99 static void close_fd(int fd) {
101 debug("%d -> .\t\t\t[%d]\n", fd, getpid());
105 init_from_environment();
108 if (tracked_fds_count == 0) {
111 tracked_fds_remove(fd);
115 /* "Action" handlers called when a file descriptor is matched. */
117 static char *pre_string;
118 static size_t pre_string_size;
119 static char *post_string;
120 static size_t post_string_size;
122 /* Load alternative pre/post strings from the environment if available, fall
123 * back to default values. */
124 inline static void init_pre_post_string() {
125 pre_string = getenv(ENV_NAME_PRE_STRING);
127 pre_string = DEFAULT_PRE_STRING;
129 pre_string_size = strlen(pre_string);
131 post_string = getenv(ENV_NAME_POST_STRING);
133 post_string = DEFAULT_POST_STRING;
135 post_string_size = strlen(post_string);
138 static void handle_fd_pre(int fd, int action) {
141 if (!pre_string || !post_string) {
142 init_pre_post_string();
145 DLSYM_FUNCTION(real_write, "write");
146 real_write(fd, pre_string, pre_string_size);
148 static void handle_fd_post(int fd, int action) {
151 /* write() already loaded above in handle_fd_pre(). */
152 real_write(fd, post_string, post_string_size);
155 static void handle_file_pre(FILE *stream, int action) {
158 if (!pre_string || !post_string) {
159 init_pre_post_string();
162 DLSYM_FUNCTION(real_fwrite, "fwrite");
163 real_fwrite(pre_string, pre_string_size, 1, stream);
165 static void handle_file_post(FILE *stream, int action) {
168 /* fwrite() already loaded above in handle_file_pre(). */
169 real_fwrite(post_string, post_string_size, 1, stream);
174 /* Hook all important output functions to manipulate their output. */
176 HOOK_FD3(ssize_t, write, fd,
177 int, fd, const void *, buf, size_t, count)
178 HOOK_FILE4(size_t, fwrite, stream,
179 const void *, ptr, size_t, size, size_t, nmemb, FILE *, stream)
182 HOOK_FILE2(int, fputs, stream,
183 const char *, s, FILE *, stream)
184 HOOK_FILE2(int, fputc, stream,
185 int, c, FILE *, stream)
186 HOOK_FILE2(int, putc, stream,
187 int, c, FILE *, stream)
188 HOOK_FILE1(int, putchar, stdout,
190 HOOK_FILE1(int, puts, stdout,
193 /* printf(3), excluding all s*() and vs*() functions (no output) */
194 HOOK_VAR_FILE1(int, printf, stdout, vprintf,
195 const char *, format)
196 HOOK_VAR_FILE2(int, fprintf, stream, vfprintf,
197 FILE *, stream, const char *, format)
198 HOOK_FILE2(int, vprintf, stdout,
199 const char *, format, va_list, ap)
200 HOOK_FILE3(int, vfprintf, stream,
201 FILE *, stream, const char *, format, va_list, ap)
202 /* Hardening functions (-D_FORTIFY_SOURCE=2). */
203 HOOK_VAR_FILE2(int, __printf_chk, stdout, __vprintf_chk,
204 int, flag, const char *, format)
205 HOOK_VAR_FILE3(int, __fprintf_chk, fp, __vfprintf_chk,
206 FILE *, fp, int, flag, const char *, format)
207 HOOK_FILE3(int, __vprintf_chk, stdout,
208 int, flag, const char *, format, va_list, ap)
209 HOOK_FILE4(int, __vfprintf_chk, stream,
210 FILE *, stream, int, flag, const char *, format, va_list, ap)
212 /* unlocked_stdio(3), only functions from above are hooked */
213 HOOK_FILE4(size_t, fwrite_unlocked, stream,
214 const void *, ptr, size_t, size, size_t, nmemb, FILE *, stream)
215 HOOK_FILE2(int, fputs_unlocked, stream,
216 const char *, s, FILE *, stream)
217 HOOK_FILE2(int, fputc_unlocked, stream,
218 int, c, FILE *, stream)
219 HOOK_FILE2(int, putc_unlocked, stream,
220 int, c, FILE *, stream)
221 HOOK_FILE1(int, putchar_unlocked, stdout,
223 HOOK_FILE1(int, puts_unlocked, stdout,
227 /* Hook functions which duplicate file descriptors to track them. */
229 static int (*real_dup)(int);
230 static int (*real_dup2)(int, int);
231 static int (*real_dup3)(int, int, int);
235 DLSYM_FUNCTION(real_dup, "dup");
237 newfd = real_dup(oldfd);
239 int saved_errno = errno;
240 dup_fd(oldfd, newfd);
246 int dup2(int oldfd, int newfd) {
247 DLSYM_FUNCTION(real_dup2, "dup2");
249 newfd = real_dup2(oldfd, newfd);
251 int saved_errno = errno;
252 dup_fd(oldfd, newfd);
258 int dup3(int oldfd, int newfd, int flags) {
259 DLSYM_FUNCTION(real_dup3, "dup3");
261 newfd = real_dup3(oldfd, newfd, flags);
263 int saved_errno = errno;
264 dup_fd(oldfd, newfd);
271 static int (*real_fcntl)(int, int, ...);
272 int fcntl(int fd, int cmd, ...) {
276 DLSYM_FUNCTION(real_fcntl, "fcntl");
278 /* fcntl() takes different types of arguments depending on the cmd type
279 * (int, void and pointers are used at the moment). Handling these
280 * arguments for different systems and with possible changes in the future
283 * Therefore always retrieve a void-pointer from our arguments (even if it
284 * wasn't there) and pass it to real_fcntl(). This shouldn't cause any
285 * problems because a void-pointer is most-likely bigger than an int
286 * (something which is not true in reverse) and shouldn't cause
287 * truncation. For register based calling conventions an invalid register
288 * content is passed, but ignored by real_fcntl(). Not perfect, but should
292 result = real_fcntl(fd, cmd, va_arg(ap, void *));
294 /* We only care about duping fds. */
295 if (cmd == F_DUPFD && result != -1) {
296 int saved_errno = errno;
304 static int (*real_close)(int);
306 DLSYM_FUNCTION(real_close, "close");
309 return real_close(fd);
311 static int (*real_fclose)(FILE *);
312 int fclose(FILE *fp) {
313 DLSYM_FUNCTION(real_fclose, "fclose");
315 close_fd(fileno(fp));
316 return real_fclose(fp);