Commit | Line | Data |
---|---|---|
95e6d268 MD |
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> | |
c668ba47 | 27 | #include <errno.h> |
95e6d268 MD |
28 | |
29 | #include <helper.h> | |
30 | #include "usterr-signal-safe.h" | |
31 | ||
c668ba47 | 32 | #define LTTNG_UST_DLSYM_FAILED_PTR 0x1 |
95e6d268 | 33 | |
c668ba47 MJ |
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 | */ | |
95e6d268 | 41 | static |
c668ba47 | 42 | void *_lttng_ust_fd_init_plibc_close(void) |
95e6d268 | 43 | { |
c668ba47 | 44 | if (__lttng_ust_fd_plibc_close == NULL) { |
95e6d268 | 45 | __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close"); |
c668ba47 MJ |
46 | |
47 | if (__lttng_ust_fd_plibc_close == NULL) { | |
48 | __lttng_ust_fd_plibc_close = (void *) LTTNG_UST_DLSYM_FAILED_PTR; | |
95e6d268 | 49 | fprintf(stderr, "%s\n", dlerror()); |
95e6d268 MD |
50 | } |
51 | } | |
c668ba47 MJ |
52 | |
53 | return __lttng_ust_fd_plibc_close; | |
95e6d268 MD |
54 | } |
55 | ||
c668ba47 MJ |
56 | /* |
57 | * Use dlsym to find the original libc fclose() symbol and store it in | |
58 | * __lttng_ust_fd_plibc_fclose. | |
59 | */ | |
52a20dc7 | 60 | static |
c668ba47 | 61 | void *_lttng_ust_fd_init_plibc_fclose(void) |
52a20dc7 | 62 | { |
c668ba47 | 63 | if (__lttng_ust_fd_plibc_fclose == NULL) { |
52a20dc7 | 64 | __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose"); |
c668ba47 MJ |
65 | |
66 | if (__lttng_ust_fd_plibc_fclose == NULL) { | |
67 | __lttng_ust_fd_plibc_fclose = (void *) LTTNG_UST_DLSYM_FAILED_PTR; | |
52a20dc7 | 68 | fprintf(stderr, "%s\n", dlerror()); |
52a20dc7 MD |
69 | } |
70 | } | |
c668ba47 MJ |
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(); | |
52a20dc7 MD |
90 | } |
91 | ||
c668ba47 MJ |
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 | */ | |
95e6d268 MD |
103 | int close(int fd) |
104 | { | |
c668ba47 MJ |
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); | |
95e6d268 MD |
114 | } |
115 | ||
52a20dc7 | 116 | /* |
c668ba47 MJ |
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). | |
52a20dc7 MD |
130 | */ |
131 | int fclose(FILE *stream) | |
132 | { | |
c668ba47 MJ |
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); | |
52a20dc7 MD |
140 | } |
141 | ||
95e6d268 MD |
142 | #if defined(__sun__) || defined(__FreeBSD__) |
143 | /* Solaris and FreeBSD. */ | |
144 | void closefrom(int lowfd) | |
145 | { | |
c668ba47 MJ |
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); | |
95e6d268 MD |
151 | } |
152 | #elif defined(__NetBSD__) || defined(__OpenBSD__) | |
153 | /* NetBSD and OpenBSD. */ | |
154 | int closefrom(int lowfd) | |
155 | { | |
c668ba47 MJ |
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); | |
95e6d268 MD |
162 | } |
163 | #else | |
164 | /* As far as we know, this OS does not implement closefrom. */ | |
165 | #endif |