]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/coloredstderr.c
Fix signature of fcntl() hook.
[coloredstderr/coloredstderr.git] / src / coloredstderr.c
1 /*
2  * Hook output functions (like printf(3)) with LD_PRELOAD to color stderr (or
3  * other file descriptors).
4  *
5  * Copyright (C) 2013  Simon Ruderich
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "config.h"
22
23 /* Must be loaded before the following headers. */
24 #include "ldpreload.h"
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 /* Conflicting declaration in glibc. */
34 #undef fwrite_unlocked
35
36
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 *);
41
42
43 #include "constants.h"
44 #ifdef DEBUG
45 # include "debug.h"
46 #endif
47
48 #include "hookmacros.h"
49 #include "trackfds.h"
50
51
52
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. */
56     if (!isatty(fd)) {
57         return 0;
58     }
59
60     /* Load state from environment. Only necessary once per process. */
61     if (!tracked_fds) {
62         init_from_environment();
63     }
64
65     if (tracked_fds_count == 0) {
66         return 0;
67     }
68     return tracked_fds_find(fd);
69 }
70
71 static void dup_fd(int oldfd, int newfd) {
72 #ifdef DEBUG
73     debug("%d -> %d\t\t\t[%d]\n", oldfd, newfd, getpid());
74 #endif
75
76     if (!tracked_fds) {
77         init_from_environment();
78     }
79     if (tracked_fds_count == 0) {
80         return;
81     }
82
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);
88             update_environment();
89         }
90     /* We are not tracking this file descriptor, remove newfd from the list
91      * (if present). */
92     } else {
93         if (tracked_fds_remove(newfd)) {
94             update_environment();
95         }
96     }
97 }
98
99 static void close_fd(int fd) {
100 #ifdef DEBUG
101     debug("%d -> .\t\t\t[%d]\n", fd, getpid());
102 #endif
103
104     if (!tracked_fds) {
105         init_from_environment();
106     }
107
108     if (tracked_fds_count == 0) {
109         return;
110     }
111     tracked_fds_remove(fd);
112 }
113
114
115 /* "Action" handlers called when a file descriptor is matched. */
116
117 static char *pre_string;
118 static size_t pre_string_size;
119 static char *post_string;
120 static size_t post_string_size;
121
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);
126     if (!pre_string) {
127         pre_string = DEFAULT_PRE_STRING;
128     }
129     pre_string_size = strlen(pre_string);
130
131     post_string = getenv(ENV_NAME_POST_STRING);
132     if (!post_string) {
133         post_string = DEFAULT_POST_STRING;
134     }
135     post_string_size = strlen(post_string);
136 }
137
138 static void handle_fd_pre(int fd, int action) {
139     (void)action;
140
141     if (!pre_string || !post_string) {
142         init_pre_post_string();
143     }
144
145     DLSYM_FUNCTION(real_write, "write");
146     real_write(fd, pre_string, pre_string_size);
147 }
148 static void handle_fd_post(int fd, int action) {
149     (void)action;
150
151     /* write() already loaded above in handle_fd_pre(). */
152     real_write(fd, post_string, post_string_size);
153 }
154
155 static void handle_file_pre(FILE *stream, int action) {
156     (void)action;
157
158     if (!pre_string || !post_string) {
159         init_pre_post_string();
160     }
161
162     DLSYM_FUNCTION(real_fwrite, "fwrite");
163     real_fwrite(pre_string, pre_string_size, 1, stream);
164 }
165 static void handle_file_post(FILE *stream, int action) {
166     (void)action;
167
168     /* fwrite() already loaded above in handle_file_pre(). */
169     real_fwrite(post_string, post_string_size, 1, stream);
170 }
171
172
173
174 /* Hook all important output functions to manipulate their output. */
175
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)
180
181 /* puts(3) */
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,
189            int, c)
190 HOOK_FILE1(int, puts, stdout,
191            const char *, s)
192
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)
211
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,
222            int, c)
223 HOOK_FILE1(int, puts_unlocked, stdout,
224            const char *, s)
225
226
227 /* Hook functions which duplicate file descriptors to track them. */
228
229 static int (*real_dup)(int);
230 static int (*real_dup2)(int, int);
231 static int (*real_dup3)(int, int, int);
232 int dup(int oldfd) {
233     int newfd;
234
235     DLSYM_FUNCTION(real_dup, "dup");
236
237     newfd = real_dup(oldfd);
238     if (newfd != -1) {
239         int saved_errno = errno;
240         dup_fd(oldfd, newfd);
241         errno = saved_errno;
242     }
243
244     return newfd;
245 }
246 int dup2(int oldfd, int newfd) {
247     DLSYM_FUNCTION(real_dup2, "dup2");
248
249     newfd = real_dup2(oldfd, newfd);
250     if (newfd != -1) {
251         int saved_errno = errno;
252         dup_fd(oldfd, newfd);
253         errno = saved_errno;
254     }
255
256     return newfd;
257 }
258 int dup3(int oldfd, int newfd, int flags) {
259     DLSYM_FUNCTION(real_dup3, "dup3");
260
261     newfd = real_dup3(oldfd, newfd, flags);
262     if (newfd != -1) {
263         int saved_errno = errno;
264         dup_fd(oldfd, newfd);
265         errno = saved_errno;
266     }
267
268     return newfd;
269 }
270
271 static int (*real_fcntl)(int, int, ...);
272 int fcntl(int fd, int cmd, ...) {
273     int result;
274     va_list ap;
275
276     DLSYM_FUNCTION(real_fcntl, "fcntl");
277
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
281      * is error prone.
282      *
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
289      * work fine.
290      */
291     va_start(ap, cmd);
292     result = real_fcntl(fd, cmd, va_arg(ap, void *));
293     va_end(ap);
294     /* We only care about duping fds. */
295     if (cmd == F_DUPFD && result != -1) {
296         int saved_errno = errno;
297         dup_fd(fd, result);
298         errno = saved_errno;
299     }
300
301     return result;
302 }
303
304 static int (*real_close)(int);
305 int close(int fd) {
306     DLSYM_FUNCTION(real_close, "close");
307
308     close_fd(fd);
309     return real_close(fd);
310 }
311 static int (*real_fclose)(FILE *);
312 int fclose(FILE *fp) {
313     DLSYM_FUNCTION(real_fclose, "fclose");
314
315     close_fd(fileno(fp));
316     return real_fclose(fp);
317 }