Fix: Use 2.12 custom mutex nest getter
[lttng-ust.git] / src / lib / lttng-ust-fd / lttng-ust-fd.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #define _LGPL_SOURCE
8 #include <limits.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <dlfcn.h>
13 #include <errno.h>
14
15 #include <lttng/ust-common.h>
16
17 #include "common/macros.h"
18 #include "common/ust-fd.h"
19 #include "lib/lttng-ust-common/fd-tracker.h"
20
21 #define LTTNG_UST_DLSYM_FAILED_PTR 0x1
22
23 static int (*__lttng_ust_fd_plibc_close)(int fd) = NULL;
24 static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream) = NULL;
25
26 /*
27 * Use dlsym to find the original libc close() symbol and store it in
28 * __lttng_ust_fd_plibc_close.
29 */
30 static
31 void *_lttng_ust_fd_init_plibc_close(void)
32 {
33 if (__lttng_ust_fd_plibc_close == NULL) {
34 __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close");
35
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());
39 }
40 }
41
42 return __lttng_ust_fd_plibc_close;
43 }
44
45 /*
46 * Use dlsym to find the original libc fclose() symbol and store it in
47 * __lttng_ust_fd_plibc_fclose.
48 */
49 static
50 void *_lttng_ust_fd_init_plibc_fclose(void)
51 {
52 if (__lttng_ust_fd_plibc_fclose == NULL) {
53 __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose");
54
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());
58 }
59 }
60
61 return __lttng_ust_fd_plibc_fclose;
62 }
63
64 #if !defined(LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS)
65
66 static void *lttng_ust_2_12_handle;
67
68 static
69 void lttng_ust_init_close_chaining(void)
70 {
71 if (lttng_ust_2_12_handle)
72 return;
73 /*
74 * Load ust-2.12 in the global symbol namespace.
75 */
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());
79 abort();
80 }
81 /*
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.
85 */
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();
89 }
90 #else
91 static
92 void lttng_ust_init_close_chaining(void) { }
93 #endif
94
95 static
96 void _lttng_ust_fd_ctor(void)
97 __attribute__((constructor));
98 static
99 void _lttng_ust_fd_ctor(void)
100 {
101 lttng_ust_init_close_chaining();
102
103 lttng_ust_common_ctor();
104
105 /*
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.
109 */
110 (void) _lttng_ust_fd_init_plibc_close();
111 (void) _lttng_ust_fd_init_plibc_fclose();
112 }
113
114 /*
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.
118 *
119 * If dlsym failed to find the original libc close() symbol, return -1,
120 * errno=ENOSYS.
121 *
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
124 * async-signal-safe.
125 */
126 int close(int fd)
127 {
128 lttng_ust_init_close_chaining();
129
130 /*
131 * We can't retry dlsym here since close is async-signal-safe.
132 */
133 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
134 errno = ENOSYS;
135 return -1;
136 }
137
138 return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close);
139 }
140
141 /*
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.
145 *
146 * If dlsym failed to find the original libc close() symbol, return -1,
147 * errno=ENOSYS.
148 *
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.
151 *
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).
155 */
156 int fclose(FILE *stream)
157 {
158 lttng_ust_init_close_chaining();
159
160 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
161 errno = ENOSYS;
162 return -1;
163 }
164
165 return lttng_ust_safe_fclose_stream(stream,
166 __lttng_ust_fd_plibc_fclose);
167 }
168
169 #if defined(__sun__) || defined(__FreeBSD__)
170
171 #error "Custom upgrade branch does not support Solaris/FreeBSD closefrom"
172
173 /* Solaris and FreeBSD. */
174 void closefrom(int lowfd)
175 {
176 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
177 return;
178 }
179
180 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
181 }
182 #elif defined(__NetBSD__) || defined(__OpenBSD__)
183
184 #error "Custom upgrade branch does not support NetBSD/OpenBSD closefrom"
185
186 /* NetBSD and OpenBSD. */
187 int closefrom(int lowfd)
188 {
189 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
190 errno = ENOSYS;
191 return -1;
192 }
193
194 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
195 }
196 #else
197 /* As far as we know, this OS does not implement closefrom. */
198 #endif
This page took 0.034604 seconds and 5 git commands to generate.