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();
88 (void) lttng_ust_get_fd_mutex_nest_init();
92 void lttng_ust_init_close_chaining(void) { }
96 void _lttng_ust_fd_ctor(void)
97 __attribute__((constructor
));
99 void _lttng_ust_fd_ctor(void)
101 lttng_ust_init_close_chaining();
103 lttng_ust_common_ctor();
106 * Initialize the function pointers to the original libc symbols in the
107 * constructor since close() has to stay async-signal-safe and as such,
108 * we can't call dlsym() in the override functions.
110 (void) _lttng_ust_fd_init_plibc_close();
111 (void) _lttng_ust_fd_init_plibc_fclose();
115 * Override the libc close() symbol with our own, allowing applications to
116 * close arbitrary file descriptors. If the fd is owned by lttng-ust, return
117 * -1, errno=EBADF instead of closing it.
119 * If dlsym failed to find the original libc close() symbol, return -1,
122 * There is a short window before the library constructor has executed where
123 * this wrapper could call dlsym() and dlopen() and thus not be
128 lttng_ust_init_close_chaining();
131 * We can't retry dlsym here since close is async-signal-safe.
133 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
138 return lttng_ust_safe_close_fd(fd
, __lttng_ust_fd_plibc_close
);
142 * Override the libc fclose() symbol with our own, allowing applications to
143 * close arbitrary streams. If the fd is owned by lttng-ust, return -1,
144 * errno=EBADF instead of closing it.
146 * If dlsym failed to find the original libc close() symbol, return -1,
149 * There is a short window before the library constructor has executed where
150 * this wrapper could call dlsym() and thus not be async-signal-safe.
152 * Note: fcloseall() is not an issue because it closes only the streams it
153 * knows about, which differs from the problems caused by gnulib
154 * close_stdout(), which does an explicit fclose(stdout).
156 int fclose(FILE *stream
)
158 lttng_ust_init_close_chaining();
160 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
165 return lttng_ust_safe_fclose_stream(stream
,
166 __lttng_ust_fd_plibc_fclose
);
169 #if defined(__sun__) || defined(__FreeBSD__)
171 #error "Custom upgrade branch does not support Solaris/FreeBSD closefrom"
173 /* Solaris and FreeBSD. */
174 void closefrom(int lowfd
)
176 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
180 (void) lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
182 #elif defined(__NetBSD__) || defined(__OpenBSD__)
184 #error "Custom upgrade branch does not support NetBSD/OpenBSD closefrom"
186 /* NetBSD and OpenBSD. */
187 int closefrom(int lowfd
)
189 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
194 return lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
197 /* As far as we know, this OS does not implement closefrom. */