fix: liblttng-ust-fd async-signal-safe close()
[lttng-ust.git] / liblttng-ust-fd / lttng-ust-fd.c
1 /*
2 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; version 2.1 of
7 * the License.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #define _GNU_SOURCE
20 #define _LGPL_SOURCE
21 #include <limits.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <ust-fd.h>
26 #include <dlfcn.h>
27 #include <errno.h>
28
29 #include <helper.h>
30 #include "usterr-signal-safe.h"
31
32 #define LTTNG_UST_DLSYM_FAILED_PTR 0x1
33
34 static int (*__lttng_ust_fd_plibc_close)(int fd) = NULL;
35 static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream) = NULL;
36
37 /*
38 * Use dlsym to find the original libc close() symbol and store it in
39 * __lttng_ust_fd_plibc_close.
40 */
41 static
42 void *_lttng_ust_fd_init_plibc_close(void)
43 {
44 if (__lttng_ust_fd_plibc_close == NULL) {
45 __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close");
46
47 if (__lttng_ust_fd_plibc_close == NULL) {
48 __lttng_ust_fd_plibc_close = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
49 fprintf(stderr, "%s\n", dlerror());
50 }
51 }
52
53 return __lttng_ust_fd_plibc_close;
54 }
55
56 /*
57 * Use dlsym to find the original libc fclose() symbol and store it in
58 * __lttng_ust_fd_plibc_fclose.
59 */
60 static
61 void *_lttng_ust_fd_init_plibc_fclose(void)
62 {
63 if (__lttng_ust_fd_plibc_fclose == NULL) {
64 __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose");
65
66 if (__lttng_ust_fd_plibc_fclose == NULL) {
67 __lttng_ust_fd_plibc_fclose = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
68 fprintf(stderr, "%s\n", dlerror());
69 }
70 }
71
72 return __lttng_ust_fd_plibc_fclose;
73 }
74
75 static
76 void _lttng_ust_fd_ctor(void)
77 __attribute__((constructor));
78 static
79 void _lttng_ust_fd_ctor(void)
80 {
81 lttng_ust_common_ctor();
82
83 /*
84 * Initialize the function pointers to the original libc symbols in the
85 * constructor since close() has to stay async-signal-safe and as such,
86 * we can't call dlsym() in the override functions.
87 */
88 (void) _lttng_ust_fd_init_plibc_close();
89 (void) _lttng_ust_fd_init_plibc_fclose();
90 }
91
92 /*
93 * Override the libc close() symbol with our own, allowing applications to
94 * close arbitrary file descriptors. If the fd is owned by lttng-ust, return
95 * -1, errno=EBADF instead of closing it.
96 *
97 * If dlsym failed to find the original libc close() symbol, return -1,
98 * errno=ENOSYS.
99 *
100 * There is a short window before the library constructor has executed where
101 * this wrapper could call dlsym() and thus not be async-signal-safe.
102 */
103 int close(int fd)
104 {
105 /*
106 * We can't retry dlsym here since close is async-signal-safe.
107 */
108 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
109 errno = ENOSYS;
110 return -1;
111 }
112
113 return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close);
114 }
115
116 /*
117 * Override the libc fclose() symbol with our own, allowing applications to
118 * close arbitrary streams. If the fd is owned by lttng-ust, return -1,
119 * errno=EBADF instead of closing it.
120 *
121 * If dlsym failed to find the original libc close() symbol, return -1,
122 * errno=ENOSYS.
123 *
124 * There is a short window before the library constructor has executed where
125 * this wrapper could call dlsym() and thus not be async-signal-safe.
126 *
127 * Note: fcloseall() is not an issue because it closes only the streams it
128 * knows about, which differs from the problems caused by gnulib
129 * close_stdout(), which does an explicit fclose(stdout).
130 */
131 int fclose(FILE *stream)
132 {
133 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
134 errno = ENOSYS;
135 return -1;
136 }
137
138 return lttng_ust_safe_fclose_stream(stream,
139 __lttng_ust_fd_plibc_fclose);
140 }
141
142 #if defined(__sun__) || defined(__FreeBSD__)
143 /* Solaris and FreeBSD. */
144 void closefrom(int lowfd)
145 {
146 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
147 return;
148 }
149
150 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
151 }
152 #elif defined(__NetBSD__) || defined(__OpenBSD__)
153 /* NetBSD and OpenBSD. */
154 int closefrom(int lowfd)
155 {
156 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
157 errno = ENOSYS;
158 return -1;
159 }
160
161 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
162 }
163 #else
164 /* As far as we know, this OS does not implement closefrom. */
165 #endif
This page took 0.03208 seconds and 4 git commands to generate.