ffa72903900ef7b7f675bd983016b395e0b9c0dc
[lttng-ust.git] / liblttng-ust-fd / lttng-ust-fd.c
1 /*
2 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; version 2.1 of
7 * the License.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #define _GNU_SOURCE
20 #define _LGPL_SOURCE
21 #include <limits.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <ust-fd.h>
26 #include <dlfcn.h>
27 #include <errno.h>
28
29 #include <helper.h>
30 #include "usterr-signal-safe.h"
31
32 #define LTTNG_UST_DLSYM_FAILED_PTR 0x1
33
34 static int (*__lttng_ust_fd_plibc_close)(int fd) = NULL;
35 static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream) = NULL;
36 static int (*__lttng_ust_fd_plibc_close_range)(unsigned int first,
37 unsigned int last, int flags) = NULL;
38
39 /*
40 * Use dlsym to find the original libc close() symbol and store it in
41 * __lttng_ust_fd_plibc_close.
42 */
43 static
44 void *_lttng_ust_fd_init_plibc_close(void)
45 {
46 if (__lttng_ust_fd_plibc_close == NULL) {
47 __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close");
48
49 if (__lttng_ust_fd_plibc_close == NULL) {
50 __lttng_ust_fd_plibc_close = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
51 fprintf(stderr, "%s\n", dlerror());
52 }
53 }
54
55 return __lttng_ust_fd_plibc_close;
56 }
57
58 /*
59 * Use dlsym to find the original libc fclose() symbol and store it in
60 * __lttng_ust_fd_plibc_fclose.
61 */
62 static
63 void *_lttng_ust_fd_init_plibc_fclose(void)
64 {
65 if (__lttng_ust_fd_plibc_fclose == NULL) {
66 __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose");
67
68 if (__lttng_ust_fd_plibc_fclose == NULL) {
69 __lttng_ust_fd_plibc_fclose = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
70 fprintf(stderr, "%s\n", dlerror());
71 }
72 }
73
74 return __lttng_ust_fd_plibc_fclose;
75 }
76
77 /*
78 * Use dlsym to find the original libc close_range() symbol and store it
79 * in __lttng_ust_fd_plibc_close_range. The close_range symbol only
80 * appears in glibc 2.34, so it is considered optional.
81 */
82 static
83 void *_lttng_ust_fd_init_plibc_close_range(void)
84 {
85 if (__lttng_ust_fd_plibc_close_range == NULL) {
86 __lttng_ust_fd_plibc_close_range = dlsym(RTLD_NEXT, "close_range");
87
88 if (__lttng_ust_fd_plibc_close_range == NULL)
89 __lttng_ust_fd_plibc_close_range = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
90 }
91
92 return __lttng_ust_fd_plibc_close_range;
93 }
94
95 static
96 void _lttng_ust_fd_ctor(void)
97 __attribute__((constructor));
98 static
99 void _lttng_ust_fd_ctor(void)
100 {
101 /*
102 * Initialize the function pointers to the original libc symbols in the
103 * constructor since close() has to stay async-signal-safe and as such,
104 * we can't call dlsym() in the override functions.
105 */
106 (void) _lttng_ust_fd_init_plibc_close();
107 (void) _lttng_ust_fd_init_plibc_fclose();
108 (void) _lttng_ust_fd_init_plibc_close_range();
109 }
110
111 /*
112 * Override the libc close() symbol with our own, allowing applications to
113 * close arbitrary file descriptors. If the fd is owned by lttng-ust, return
114 * -1, errno=EBADF instead of closing it.
115 *
116 * If dlsym failed to find the original libc close() symbol, return -1,
117 * errno=ENOSYS.
118 *
119 * There is a short window before the library constructor has executed where
120 * this wrapper could call dlsym() and thus not be async-signal-safe.
121 */
122 int close(int fd)
123 {
124 /*
125 * We can't retry dlsym here since close is async-signal-safe.
126 */
127 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
128 errno = ENOSYS;
129 return -1;
130 }
131
132 return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close);
133 }
134
135 /*
136 * Override the libc fclose() symbol with our own, allowing applications to
137 * close arbitrary streams. If the fd is owned by lttng-ust, return -1,
138 * errno=EBADF instead of closing it.
139 *
140 * If dlsym failed to find the original libc close() symbol, return -1,
141 * errno=ENOSYS.
142 *
143 * There is a short window before the library constructor has executed where
144 * this wrapper could call dlsym() and thus not be async-signal-safe.
145 *
146 * Note: fcloseall() is not an issue because it closes only the streams it
147 * knows about, which differs from the problems caused by gnulib
148 * close_stdout(), which does an explicit fclose(stdout).
149 */
150 int fclose(FILE *stream)
151 {
152 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
153 errno = ENOSYS;
154 return -1;
155 }
156
157 return lttng_ust_safe_fclose_stream(stream,
158 __lttng_ust_fd_plibc_fclose);
159 }
160
161 /*
162 * Override the libc close_range() symbol with our own, allowing
163 * applications to close arbitrary file descriptors. If the fd is owned
164 * by lttng-ust, return -1, errno=EBADF instead of closing it.
165 *
166 * If dlsym failed to find the original libc close_range() symbol,
167 * return -1, errno=ENOSYS.
168 *
169 * There is a short window before the library constructor has executed where
170 * this wrapper could call dlsym() and thus not be async-signal-safe.
171 */
172 int close_range(unsigned int first, unsigned int last, int flags)
173 {
174 /*
175 * We can't retry dlsym here since close is async-signal-safe.
176 */
177 if (_lttng_ust_fd_init_plibc_close_range() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
178 errno = ENOSYS;
179 return -1;
180 }
181
182 return lttng_ust_safe_close_range_fd(first, last, flags, __lttng_ust_fd_plibc_close_range);
183 }
184
185 #if defined(__sun__) || defined(__FreeBSD__)
186 /* Solaris and FreeBSD. */
187 void closefrom(int lowfd)
188 {
189 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
190 return;
191 }
192
193 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
194 }
195 #elif defined(__NetBSD__) || defined(__OpenBSD__)
196 /* NetBSD and OpenBSD. */
197 int closefrom(int lowfd)
198 {
199 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
200 errno = ENOSYS;
201 return -1;
202 }
203
204 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
205 }
206 #else
207 /* As far as we know, this OS does not implement closefrom. */
208 #endif
This page took 0.03308 seconds and 3 git commands to generate.