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 *);
42 /* Did we already (try to) parse the environment and setup the necessary
44 static int initialized;
45 /* Force hooked writes even when not writing to a tty. Used for tests. */
46 static int force_write_to_non_tty;
49 #include "constants.h"
54 #include "hookmacros.h"
59 /* Should the "action" handler be invoked for this file descriptor? */
60 static int check_handle_fd(int fd) {
61 /* Load state from environment. Only necessary once per process. */
63 init_from_environment();
66 /* Never touch anything not going to a terminal - unless we are explicitly
68 if (!force_write_to_non_tty && !isatty(fd)) {
72 if (tracked_fds_count == 0) {
75 return tracked_fds_find(fd);
78 static void dup_fd(int oldfd, int newfd) {
80 debug("%3d -> %3d\t\t\t[%d]\n", oldfd, newfd, getpid());
84 init_from_environment();
86 if (tracked_fds_count == 0) {
90 /* We are already tracking this file descriptor, add newfd to the list as
91 * it will reference the same descriptor. */
92 if (tracked_fds_find(oldfd)) {
93 if (!tracked_fds_find(newfd)) {
94 tracked_fds_add(newfd);
97 /* We are not tracking this file descriptor, remove newfd from the list
100 if (tracked_fds_remove(newfd)) {
101 update_environment();
106 static void close_fd(int fd) {
108 debug("%3d -> .\t\t\t[%d]\n", fd, getpid());
112 init_from_environment();
115 if (tracked_fds_count == 0) {
118 tracked_fds_remove(fd);
122 /* "Action" handlers called when a file descriptor is matched. */
124 static char *pre_string;
125 static size_t pre_string_size;
126 static char *post_string;
127 static size_t post_string_size;
129 /* Load alternative pre/post strings from the environment if available, fall
130 * back to default values. */
131 inline static void init_pre_post_string() {
132 pre_string = getenv(ENV_NAME_PRE_STRING);
134 pre_string = DEFAULT_PRE_STRING;
136 pre_string_size = strlen(pre_string);
138 post_string = getenv(ENV_NAME_POST_STRING);
140 post_string = DEFAULT_POST_STRING;
142 post_string_size = strlen(post_string);
145 static void handle_fd_pre(int fd, int action) {
148 if (!pre_string || !post_string) {
149 init_pre_post_string();
152 DLSYM_FUNCTION(real_write, "write");
153 real_write(fd, pre_string, pre_string_size);
155 static void handle_fd_post(int fd, int action) {
158 /* write() already loaded above in handle_fd_pre(). */
159 real_write(fd, post_string, post_string_size);
162 static void handle_file_pre(FILE *stream, int action) {
165 if (!pre_string || !post_string) {
166 init_pre_post_string();
169 DLSYM_FUNCTION(real_fwrite, "fwrite");
170 real_fwrite(pre_string, pre_string_size, 1, stream);
172 static void handle_file_post(FILE *stream, int action) {
175 /* fwrite() already loaded above in handle_file_pre(). */
176 real_fwrite(post_string, post_string_size, 1, stream);
181 /* Hook all important output functions to manipulate their output. */
183 HOOK_FD3(ssize_t, write, fd,
184 int, fd, const void *, buf, size_t, count)
185 HOOK_FILE4(size_t, fwrite, stream,
186 const void *, ptr, size_t, size, size_t, nmemb, FILE *, stream)
189 HOOK_FILE2(int, fputs, stream,
190 const char *, s, FILE *, stream)
191 HOOK_FILE2(int, fputc, stream,
192 int, c, FILE *, stream)
193 HOOK_FILE2(int, putc, stream,
194 int, c, FILE *, stream)
195 HOOK_FILE1(int, putchar, stdout,
197 HOOK_FILE1(int, puts, stdout,
200 /* printf(3), excluding all s*() and vs*() functions (no output) */
201 HOOK_VAR_FILE1(int, printf, stdout, vprintf,
202 const char *, format)
203 HOOK_VAR_FILE2(int, fprintf, stream, vfprintf,
204 FILE *, stream, const char *, format)
205 HOOK_FILE2(int, vprintf, stdout,
206 const char *, format, va_list, ap)
207 HOOK_FILE3(int, vfprintf, stream,
208 FILE *, stream, const char *, format, va_list, ap)
209 /* Hardening functions (-D_FORTIFY_SOURCE=2). */
210 HOOK_VAR_FILE2(int, __printf_chk, stdout, __vprintf_chk,
211 int, flag, const char *, format)
212 HOOK_VAR_FILE3(int, __fprintf_chk, fp, __vfprintf_chk,
213 FILE *, fp, int, flag, const char *, format)
214 HOOK_FILE3(int, __vprintf_chk, stdout,
215 int, flag, const char *, format, va_list, ap)
216 HOOK_FILE4(int, __vfprintf_chk, stream,
217 FILE *, stream, int, flag, const char *, format, va_list, ap)
219 /* unlocked_stdio(3), only functions from above are hooked */
220 HOOK_FILE4(size_t, fwrite_unlocked, stream,
221 const void *, ptr, size_t, size, size_t, nmemb, FILE *, stream)
222 HOOK_FILE2(int, fputs_unlocked, stream,
223 const char *, s, FILE *, stream)
224 HOOK_FILE2(int, fputc_unlocked, stream,
225 int, c, FILE *, stream)
226 HOOK_FILE2(int, putc_unlocked, stream,
227 int, c, FILE *, stream)
228 HOOK_FILE1(int, putchar_unlocked, stdout,
230 HOOK_FILE1(int, puts_unlocked, stdout,
234 HOOK_VOID1(void, perror, STDERR_FILENO,
238 /* Hook functions which duplicate file descriptors to track them. */
240 static int (*real_dup)(int);
241 static int (*real_dup2)(int, int);
242 static int (*real_dup3)(int, int, int);
246 DLSYM_FUNCTION(real_dup, "dup");
248 newfd = real_dup(oldfd);
250 int saved_errno = errno;
251 dup_fd(oldfd, newfd);
257 int dup2(int oldfd, int newfd) {
258 DLSYM_FUNCTION(real_dup2, "dup2");
260 newfd = real_dup2(oldfd, newfd);
262 int saved_errno = errno;
263 dup_fd(oldfd, newfd);
269 int dup3(int oldfd, int newfd, int flags) {
270 DLSYM_FUNCTION(real_dup3, "dup3");
272 newfd = real_dup3(oldfd, newfd, flags);
274 int saved_errno = errno;
275 dup_fd(oldfd, newfd);
282 static int (*real_fcntl)(int, int, ...);
283 int fcntl(int fd, int cmd, ...) {
287 DLSYM_FUNCTION(real_fcntl, "fcntl");
289 /* fcntl() takes different types of arguments depending on the cmd type
290 * (int, void and pointers are used at the moment). Handling these
291 * arguments for different systems and with possible changes in the future
294 * Therefore always retrieve a void-pointer from our arguments (even if it
295 * wasn't there) and pass it to real_fcntl(). This shouldn't cause any
296 * problems because a void-pointer is most-likely bigger than an int
297 * (something which is not true in reverse) and shouldn't cause
298 * truncation. For register based calling conventions an invalid register
299 * content is passed, but ignored by real_fcntl(). Not perfect, but should
303 result = real_fcntl(fd, cmd, va_arg(ap, void *));
305 /* We only care about duping fds. */
306 if (cmd == F_DUPFD && result != -1) {
307 int saved_errno = errno;
315 static int (*real_close)(int);
317 DLSYM_FUNCTION(real_close, "close");
320 return real_close(fd);
322 static int (*real_fclose)(FILE *);
323 int fclose(FILE *fp) {
324 DLSYM_FUNCTION(real_fclose, "fclose");
326 close_fd(fileno(fp));
327 return real_fclose(fp);
331 /* Hook functions which are necessary for correct tracking. */
333 #if defined(HAVE_VFORK) && defined(HAVE_FORK)
335 /* vfork() is similar to fork() but the address space is shared between
336 * father and child. It's designed for fork()/exec() usage because it's
337 * faster than fork(). However according to the POSIX standard the "child"
338 * isn't allowed to perform any memory-modifications before the exec()
339 * (except the pid_t result variable of vfork()).
341 * As some programs don't adhere to the standard (e.g. the "child" closes
342 * or dups a descriptor before the exec()) and this breaks our tracking of
343 * file descriptors (e.g. it gets closed in the parent as well), we just
344 * fork() instead. This is in compliance with the POSIX standard and as
345 * most systems use copy-on-write anyway not a performance issue. */