af208a9852dbf03e9442140d6089c9f18472d110
[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 }
89 #else
90 static
91 void lttng_ust_init_close_chaining(void) { }
92 #endif
93
94 static
95 void _lttng_ust_fd_ctor(void)
96 __attribute__((constructor));
97 static
98 void _lttng_ust_fd_ctor(void)
99 {
100 lttng_ust_init_close_chaining();
101
102 lttng_ust_common_ctor();
103
104 /*
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.
108 */
109 (void) _lttng_ust_fd_init_plibc_close();
110 (void) _lttng_ust_fd_init_plibc_fclose();
111 }
112
113 /*
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.
117 *
118 * If dlsym failed to find the original libc close() symbol, return -1,
119 * errno=ENOSYS.
120 *
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
123 * async-signal-safe.
124 */
125 int close(int fd)
126 {
127 lttng_ust_init_close_chaining();
128
129 /*
130 * We can't retry dlsym here since close is async-signal-safe.
131 */
132 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
133 errno = ENOSYS;
134 return -1;
135 }
136
137 return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close);
138 }
139
140 /*
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.
144 *
145 * If dlsym failed to find the original libc close() symbol, return -1,
146 * errno=ENOSYS.
147 *
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.
150 *
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).
154 */
155 int fclose(FILE *stream)
156 {
157 lttng_ust_init_close_chaining();
158
159 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
160 errno = ENOSYS;
161 return -1;
162 }
163
164 return lttng_ust_safe_fclose_stream(stream,
165 __lttng_ust_fd_plibc_fclose);
166 }
167
168 #if defined(__sun__) || defined(__FreeBSD__)
169
170 #error "Custom upgrade branch does not support Solaris/FreeBSD closefrom"
171
172 /* Solaris and FreeBSD. */
173 void closefrom(int lowfd)
174 {
175 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
176 return;
177 }
178
179 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
180 }
181 #elif defined(__NetBSD__) || defined(__OpenBSD__)
182
183 #error "Custom upgrade branch does not support NetBSD/OpenBSD closefrom"
184
185 /* NetBSD and OpenBSD. */
186 int closefrom(int lowfd)
187 {
188 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
189 errno = ENOSYS;
190 return -1;
191 }
192
193 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
194 }
195 #else
196 /* As far as we know, this OS does not implement closefrom. */
197 #endif
This page took 0.037569 seconds and 4 git commands to generate.