2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <sys/types.h>
15 #include <lttng/ust-common.h>
17 #include "common/macros.h"
18 #include "common/ust-fd.h"
19 #include "lib/lttng-ust-common/fd-tracker.h"
21 #define LTTNG_UST_DLSYM_FAILED_PTR 0x1
23 static int (*__lttng_ust_fd_plibc_close
)(int fd
) = NULL
;
24 static int (*__lttng_ust_fd_plibc_fclose
)(FILE *stream
) = NULL
;
27 * Use dlsym to find the original libc close() symbol and store it in
28 * __lttng_ust_fd_plibc_close.
31 void *_lttng_ust_fd_init_plibc_close(void)
33 if (__lttng_ust_fd_plibc_close
== NULL
) {
34 __lttng_ust_fd_plibc_close
= dlsym(RTLD_NEXT
, "close");
36 if (__lttng_ust_fd_plibc_close
== NULL
) {
37 __lttng_ust_fd_plibc_close
= (void *) LTTNG_UST_DLSYM_FAILED_PTR
;
38 fprintf(stderr
, "%s\n", dlerror());
42 return __lttng_ust_fd_plibc_close
;
46 * Use dlsym to find the original libc fclose() symbol and store it in
47 * __lttng_ust_fd_plibc_fclose.
50 void *_lttng_ust_fd_init_plibc_fclose(void)
52 if (__lttng_ust_fd_plibc_fclose
== NULL
) {
53 __lttng_ust_fd_plibc_fclose
= dlsym(RTLD_NEXT
, "fclose");
55 if (__lttng_ust_fd_plibc_fclose
== NULL
) {
56 __lttng_ust_fd_plibc_fclose
= (void *) LTTNG_UST_DLSYM_FAILED_PTR
;
57 fprintf(stderr
, "%s\n", dlerror());
61 return __lttng_ust_fd_plibc_fclose
;
64 #if !defined(LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS)
66 static void *lttng_ust_2_12_handle
;
69 void lttng_ust_init_close_chaining(void)
71 if (lttng_ust_2_12_handle
)
74 * Load ust-2.12 in the global symbol namespace.
76 lttng_ust_2_12_handle
= dlopen("liblttng-ust.so.0", RTLD_GLOBAL
| RTLD_NOW
);
77 if (!lttng_ust_2_12_handle
) {
78 fprintf(stderr
, "liblttng-ust-fd.so.1: Failed to dlopen liblttng-ust.so.0: %s\n", dlerror());
82 * Initialize the function pointers to the ust 2.12 symbols in
83 * the constructor since close() has to stay async-signal-safe
84 * and as such, we can't call dlsym() in the override functions.
86 (void) lttng_ust_safe_close_fd_init();
87 (void) lttng_ust_safe_fclose_stream_init();
91 void lttng_ust_init_close_chaining(void) { }
95 void _lttng_ust_fd_ctor(void)
96 __attribute__((constructor
));
98 void _lttng_ust_fd_ctor(void)
100 lttng_ust_init_close_chaining();
102 lttng_ust_common_ctor();
105 * Initialize the function pointers to the original libc symbols in the
106 * constructor since close() has to stay async-signal-safe and as such,
107 * we can't call dlsym() in the override functions.
109 (void) _lttng_ust_fd_init_plibc_close();
110 (void) _lttng_ust_fd_init_plibc_fclose();
114 * Override the libc close() symbol with our own, allowing applications to
115 * close arbitrary file descriptors. If the fd is owned by lttng-ust, return
116 * -1, errno=EBADF instead of closing it.
118 * If dlsym failed to find the original libc close() symbol, return -1,
121 * There is a short window before the library constructor has executed where
122 * this wrapper could call dlsym() and dlopen() and thus not be
127 lttng_ust_init_close_chaining();
130 * We can't retry dlsym here since close is async-signal-safe.
132 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
137 return lttng_ust_safe_close_fd(fd
, __lttng_ust_fd_plibc_close
);
141 * Override the libc fclose() symbol with our own, allowing applications to
142 * close arbitrary streams. If the fd is owned by lttng-ust, return -1,
143 * errno=EBADF instead of closing it.
145 * If dlsym failed to find the original libc close() symbol, return -1,
148 * There is a short window before the library constructor has executed where
149 * this wrapper could call dlsym() and thus not be async-signal-safe.
151 * Note: fcloseall() is not an issue because it closes only the streams it
152 * knows about, which differs from the problems caused by gnulib
153 * close_stdout(), which does an explicit fclose(stdout).
155 int fclose(FILE *stream
)
157 lttng_ust_init_close_chaining();
159 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
164 return lttng_ust_safe_fclose_stream(stream
,
165 __lttng_ust_fd_plibc_fclose
);
168 #if defined(__sun__) || defined(__FreeBSD__)
170 #error "Custom upgrade branch does not support Solaris/FreeBSD closefrom"
172 /* Solaris and FreeBSD. */
173 void closefrom(int lowfd
)
175 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
179 (void) lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
181 #elif defined(__NetBSD__) || defined(__OpenBSD__)
183 #error "Custom upgrade branch does not support NetBSD/OpenBSD closefrom"
185 /* NetBSD and OpenBSD. */
186 int closefrom(int lowfd
)
188 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
193 return lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
196 /* As far as we know, this OS does not implement closefrom. */