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"
20 #define LTTNG_UST_DLSYM_FAILED_PTR 0x1
22 static int (*__lttng_ust_fd_plibc_close
)(int fd
) = NULL
;
23 static int (*__lttng_ust_fd_plibc_fclose
)(FILE *stream
) = NULL
;
26 * Use dlsym to find the original libc close() symbol and store it in
27 * __lttng_ust_fd_plibc_close.
30 void *_lttng_ust_fd_init_plibc_close(void)
32 if (__lttng_ust_fd_plibc_close
== NULL
) {
33 __lttng_ust_fd_plibc_close
= dlsym(RTLD_NEXT
, "close");
35 if (__lttng_ust_fd_plibc_close
== NULL
) {
36 __lttng_ust_fd_plibc_close
= (void *) LTTNG_UST_DLSYM_FAILED_PTR
;
37 fprintf(stderr
, "%s\n", dlerror());
41 return __lttng_ust_fd_plibc_close
;
45 * Use dlsym to find the original libc fclose() symbol and store it in
46 * __lttng_ust_fd_plibc_fclose.
49 void *_lttng_ust_fd_init_plibc_fclose(void)
51 if (__lttng_ust_fd_plibc_fclose
== NULL
) {
52 __lttng_ust_fd_plibc_fclose
= dlsym(RTLD_NEXT
, "fclose");
54 if (__lttng_ust_fd_plibc_fclose
== NULL
) {
55 __lttng_ust_fd_plibc_fclose
= (void *) LTTNG_UST_DLSYM_FAILED_PTR
;
56 fprintf(stderr
, "%s\n", dlerror());
60 return __lttng_ust_fd_plibc_fclose
;
64 void _lttng_ust_fd_ctor(void)
65 __attribute__((constructor
));
67 void _lttng_ust_fd_ctor(void)
69 #if !defined(LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS)
73 * Load ust-2.12 in the global symbol namespace.
75 handle
= dlopen("liblttng-ust.so.0", RTLD_GLOBAL
| RTLD_NOW
);
77 fprintf(stderr
, "liblttng-ust-fd.so.1: Failed to dlopen liblttng-ust.so.0: %s\n", dlerror());
82 lttng_ust_common_ctor();
85 * Initialize the function pointers to the original libc symbols in the
86 * constructor since close() has to stay async-signal-safe and as such,
87 * we can't call dlsym() in the override functions.
89 (void) _lttng_ust_fd_init_plibc_close();
90 (void) _lttng_ust_fd_init_plibc_fclose();
94 * Override the libc close() symbol with our own, allowing applications to
95 * close arbitrary file descriptors. If the fd is owned by lttng-ust, return
96 * -1, errno=EBADF instead of closing it.
98 * If dlsym failed to find the original libc close() symbol, return -1,
101 * There is a short window before the library constructor has executed where
102 * this wrapper could call dlsym() and thus not be async-signal-safe.
107 * We can't retry dlsym here since close is async-signal-safe.
109 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
114 return lttng_ust_safe_close_fd(fd
, __lttng_ust_fd_plibc_close
);
118 * Override the libc fclose() symbol with our own, allowing applications to
119 * close arbitrary streams. If the fd is owned by lttng-ust, return -1,
120 * errno=EBADF instead of closing it.
122 * If dlsym failed to find the original libc close() symbol, return -1,
125 * There is a short window before the library constructor has executed where
126 * this wrapper could call dlsym() and thus not be async-signal-safe.
128 * Note: fcloseall() is not an issue because it closes only the streams it
129 * knows about, which differs from the problems caused by gnulib
130 * close_stdout(), which does an explicit fclose(stdout).
132 int fclose(FILE *stream
)
134 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
139 return lttng_ust_safe_fclose_stream(stream
,
140 __lttng_ust_fd_plibc_fclose
);
143 #if defined(__sun__) || defined(__FreeBSD__)
144 /* Solaris and FreeBSD. */
145 void closefrom(int lowfd
)
147 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
151 (void) lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
153 #elif defined(__NetBSD__) || defined(__OpenBSD__)
154 /* NetBSD and OpenBSD. */
155 int closefrom(int lowfd
)
157 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
162 return lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
165 /* As far as we know, this OS does not implement closefrom. */