2 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
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.
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
23 #include <sys/types.h>
30 #include "usterr-signal-safe.h"
32 #define LTTNG_UST_DLSYM_FAILED_PTR 0x1
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
;
40 * Use dlsym to find the original libc close() symbol and store it in
41 * __lttng_ust_fd_plibc_close.
44 void *_lttng_ust_fd_init_plibc_close(void)
46 if (__lttng_ust_fd_plibc_close
== NULL
) {
47 __lttng_ust_fd_plibc_close
= dlsym(RTLD_NEXT
, "close");
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());
55 return __lttng_ust_fd_plibc_close
;
59 * Use dlsym to find the original libc fclose() symbol and store it in
60 * __lttng_ust_fd_plibc_fclose.
63 void *_lttng_ust_fd_init_plibc_fclose(void)
65 if (__lttng_ust_fd_plibc_fclose
== NULL
) {
66 __lttng_ust_fd_plibc_fclose
= dlsym(RTLD_NEXT
, "fclose");
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());
74 return __lttng_ust_fd_plibc_fclose
;
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.
83 void *_lttng_ust_fd_init_plibc_close_range(void)
85 if (__lttng_ust_fd_plibc_close_range
== NULL
) {
86 __lttng_ust_fd_plibc_close_range
= dlsym(RTLD_NEXT
, "close_range");
88 if (__lttng_ust_fd_plibc_close_range
== NULL
)
89 __lttng_ust_fd_plibc_close_range
= (void *) LTTNG_UST_DLSYM_FAILED_PTR
;
92 return __lttng_ust_fd_plibc_close_range
;
96 void _lttng_ust_fd_ctor(void)
97 __attribute__((constructor
));
99 void _lttng_ust_fd_ctor(void)
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.
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();
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.
116 * If dlsym failed to find the original libc close() symbol, return -1,
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.
125 * We can't retry dlsym here since close is async-signal-safe.
127 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
132 return lttng_ust_safe_close_fd(fd
, __lttng_ust_fd_plibc_close
);
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.
140 * If dlsym failed to find the original libc close() symbol, return -1,
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.
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).
150 int fclose(FILE *stream
)
152 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
157 return lttng_ust_safe_fclose_stream(stream
,
158 __lttng_ust_fd_plibc_fclose
);
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.
166 * If dlsym failed to find the original libc close_range() symbol,
167 * return -1, errno=ENOSYS.
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.
172 int close_range(unsigned int first
, unsigned int last
, int flags
)
175 * We can't retry dlsym here since close is async-signal-safe.
177 if (_lttng_ust_fd_init_plibc_close_range() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
182 return lttng_ust_safe_close_range_fd(first
, last
, flags
, __lttng_ust_fd_plibc_close_range
);
185 #if defined(__sun__) || defined(__FreeBSD__)
186 /* Solaris and FreeBSD. */
187 void closefrom(int lowfd
)
189 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
193 (void) lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
195 #elif defined(__NetBSD__) || defined(__OpenBSD__)
196 /* NetBSD and OpenBSD. */
197 int closefrom(int lowfd
)
199 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
204 return lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
207 /* As far as we know, this OS does not implement closefrom. */