2 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
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.
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
23 #include <sys/types.h>
30 #include "usterr-signal-safe.h"
32 #define LTTNG_UST_DLSYM_FAILED_PTR 0x1
34 static int (*__lttng_ust_fd_plibc_close
)(int fd
) = NULL
;
35 static int (*__lttng_ust_fd_plibc_fclose
)(FILE *stream
) = NULL
;
38 * Use dlsym to find the original libc close() symbol and store it in
39 * __lttng_ust_fd_plibc_close.
42 void *_lttng_ust_fd_init_plibc_close(void)
44 if (__lttng_ust_fd_plibc_close
== NULL
) {
45 __lttng_ust_fd_plibc_close
= dlsym(RTLD_NEXT
, "close");
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());
53 return __lttng_ust_fd_plibc_close
;
57 * Use dlsym to find the original libc fclose() symbol and store it in
58 * __lttng_ust_fd_plibc_fclose.
61 void *_lttng_ust_fd_init_plibc_fclose(void)
63 if (__lttng_ust_fd_plibc_fclose
== NULL
) {
64 __lttng_ust_fd_plibc_fclose
= dlsym(RTLD_NEXT
, "fclose");
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());
72 return __lttng_ust_fd_plibc_fclose
;
76 void _lttng_ust_fd_ctor(void)
77 __attribute__((constructor
));
79 void _lttng_ust_fd_ctor(void)
82 * Initialize the function pointers to the original libc symbols in the
83 * constructor since close() has to stay async-signal-safe and as such,
84 * we can't call dlsym() in the override functions.
86 (void) _lttng_ust_fd_init_plibc_close();
87 (void) _lttng_ust_fd_init_plibc_fclose();
91 * Override the libc close() symbol with our own, allowing applications to
92 * close arbitrary file descriptors. If the fd is owned by lttng-ust, return
93 * -1, errno=EBADF instead of closing it.
95 * If dlsym failed to find the original libc close() symbol, return -1,
98 * There is a short window before the library constructor has executed where
99 * this wrapper could call dlsym() and thus not be async-signal-safe.
104 * We can't retry dlsym here since close is async-signal-safe.
106 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
111 return lttng_ust_safe_close_fd(fd
, __lttng_ust_fd_plibc_close
);
115 * Override the libc fclose() symbol with our own, allowing applications to
116 * close arbitrary streams. If the fd is owned by lttng-ust, return -1,
117 * errno=EBADF instead of closing it.
119 * If dlsym failed to find the original libc close() symbol, return -1,
122 * There is a short window before the library constructor has executed where
123 * this wrapper could call dlsym() and thus not be async-signal-safe.
125 * Note: fcloseall() is not an issue because it closes only the streams it
126 * knows about, which differs from the problems caused by gnulib
127 * close_stdout(), which does an explicit fclose(stdout).
129 int fclose(FILE *stream
)
131 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
136 return lttng_ust_safe_fclose_stream(stream
,
137 __lttng_ust_fd_plibc_fclose
);
140 #if defined(__sun__) || defined(__FreeBSD__)
141 /* Solaris and FreeBSD. */
142 void closefrom(int lowfd
)
144 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
148 (void) lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
150 #elif defined(__NetBSD__) || defined(__OpenBSD__)
151 /* NetBSD and OpenBSD. */
152 int closefrom(int lowfd
)
154 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR
) {
159 return lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
162 /* As far as we know, this OS does not implement closefrom. */