Commit | Line | Data |
---|---|---|
95e6d268 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
95e6d268 | 3 | * |
c0c0989a | 4 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
95e6d268 MD |
5 | */ |
6 | ||
95e6d268 MD |
7 | #define _LGPL_SOURCE |
8 | #include <limits.h> | |
9 | #include <stdio.h> | |
10 | #include <sys/types.h> | |
11 | #include <unistd.h> | |
95e6d268 | 12 | #include <dlfcn.h> |
5ce8eb6d | 13 | #include <errno.h> |
95e6d268 | 14 | |
fca97dfd MJ |
15 | #include <lttng/ust-common.h> |
16 | ||
9d315d6d | 17 | #include "common/macros.h" |
fca97dfd | 18 | #include "common/ust-fd.h" |
5f748425 | 19 | #include "lib/lttng-ust-common/fd-tracker.h" |
95e6d268 | 20 | |
5ce8eb6d | 21 | #define LTTNG_UST_DLSYM_FAILED_PTR 0x1 |
95e6d268 | 22 | |
5ce8eb6d MJ |
23 | static int (*__lttng_ust_fd_plibc_close)(int fd) = NULL; |
24 | static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream) = NULL; | |
fca97dfd | 25 | |
5ce8eb6d MJ |
26 | /* |
27 | * Use dlsym to find the original libc close() symbol and store it in | |
28 | * __lttng_ust_fd_plibc_close. | |
29 | */ | |
95e6d268 | 30 | static |
5ce8eb6d | 31 | void *_lttng_ust_fd_init_plibc_close(void) |
95e6d268 | 32 | { |
5ce8eb6d | 33 | if (__lttng_ust_fd_plibc_close == NULL) { |
95e6d268 | 34 | __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close"); |
5ce8eb6d MJ |
35 | |
36 | if (__lttng_ust_fd_plibc_close == NULL) { | |
37 | __lttng_ust_fd_plibc_close = (void *) LTTNG_UST_DLSYM_FAILED_PTR; | |
95e6d268 | 38 | fprintf(stderr, "%s\n", dlerror()); |
95e6d268 MD |
39 | } |
40 | } | |
5ce8eb6d MJ |
41 | |
42 | return __lttng_ust_fd_plibc_close; | |
95e6d268 MD |
43 | } |
44 | ||
5ce8eb6d MJ |
45 | /* |
46 | * Use dlsym to find the original libc fclose() symbol and store it in | |
47 | * __lttng_ust_fd_plibc_fclose. | |
48 | */ | |
52a20dc7 | 49 | static |
5ce8eb6d | 50 | void *_lttng_ust_fd_init_plibc_fclose(void) |
52a20dc7 | 51 | { |
5ce8eb6d | 52 | if (__lttng_ust_fd_plibc_fclose == NULL) { |
52a20dc7 | 53 | __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose"); |
5ce8eb6d MJ |
54 | |
55 | if (__lttng_ust_fd_plibc_fclose == NULL) { | |
56 | __lttng_ust_fd_plibc_fclose = (void *) LTTNG_UST_DLSYM_FAILED_PTR; | |
52a20dc7 | 57 | fprintf(stderr, "%s\n", dlerror()); |
52a20dc7 MD |
58 | } |
59 | } | |
5ce8eb6d MJ |
60 | |
61 | return __lttng_ust_fd_plibc_fclose; | |
52a20dc7 MD |
62 | } |
63 | ||
aaa692c8 | 64 | #if !defined(LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS) |
aaa692c8 | 65 | |
5f748425 MD |
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; | |
aaa692c8 MD |
73 | /* |
74 | * Load ust-2.12 in the global symbol namespace. | |
75 | */ | |
5f748425 MD |
76 | lttng_ust_2_12_handle = dlopen("liblttng-ust.so.0", RTLD_GLOBAL | RTLD_NOW); |
77 | if (!lttng_ust_2_12_handle) { | |
aaa692c8 MD |
78 | fprintf(stderr, "liblttng-ust-fd.so.1: Failed to dlopen liblttng-ust.so.0: %s\n", dlerror()); |
79 | abort(); | |
80 | } | |
5f748425 MD |
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(); | |
a1139084 | 88 | (void) lttng_ust_get_fd_mutex_nest_init(); |
5f748425 MD |
89 | } |
90 | #else | |
91 | static | |
92 | void lttng_ust_init_close_chaining(void) { } | |
aaa692c8 MD |
93 | #endif |
94 | ||
5f748425 MD |
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 | ||
5ce8eb6d MJ |
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 | |
5f748425 MD |
123 | * this wrapper could call dlsym() and dlopen() and thus not be |
124 | * async-signal-safe. | |
5ce8eb6d | 125 | */ |
95e6d268 MD |
126 | int close(int fd) |
127 | { | |
5f748425 MD |
128 | lttng_ust_init_close_chaining(); |
129 | ||
5ce8eb6d MJ |
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); | |
95e6d268 MD |
139 | } |
140 | ||
52a20dc7 | 141 | /* |
5ce8eb6d MJ |
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). | |
52a20dc7 MD |
155 | */ |
156 | int fclose(FILE *stream) | |
157 | { | |
5f748425 MD |
158 | lttng_ust_init_close_chaining(); |
159 | ||
5ce8eb6d MJ |
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); | |
52a20dc7 MD |
167 | } |
168 | ||
95e6d268 | 169 | #if defined(__sun__) || defined(__FreeBSD__) |
5f748425 MD |
170 | |
171 | #error "Custom upgrade branch does not support Solaris/FreeBSD closefrom" | |
172 | ||
95e6d268 MD |
173 | /* Solaris and FreeBSD. */ |
174 | void closefrom(int lowfd) | |
175 | { | |
5ce8eb6d MJ |
176 | if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) { |
177 | return; | |
178 | } | |
179 | ||
23366626 | 180 | (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close); |
95e6d268 MD |
181 | } |
182 | #elif defined(__NetBSD__) || defined(__OpenBSD__) | |
5f748425 MD |
183 | |
184 | #error "Custom upgrade branch does not support NetBSD/OpenBSD closefrom" | |
185 | ||
95e6d268 MD |
186 | /* NetBSD and OpenBSD. */ |
187 | int closefrom(int lowfd) | |
188 | { | |
5ce8eb6d MJ |
189 | if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) { |
190 | errno = ENOSYS; | |
191 | return -1; | |
192 | } | |
193 | ||
23366626 | 194 | return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close); |
95e6d268 MD |
195 | } |
196 | #else | |
197 | /* As far as we know, this OS does not implement closefrom. */ | |
198 | #endif |