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 | { | |
c668ba47 MJ |
81 | /* |
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. | |
85 | */ | |
86 | (void) _lttng_ust_fd_init_plibc_close(); | |
87 | (void) _lttng_ust_fd_init_plibc_fclose(); | |
52a20dc7 MD |
88 | } |
89 | ||
c668ba47 MJ |
90 | /* |
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. | |
94 | * | |
95 | * If dlsym failed to find the original libc close() symbol, return -1, | |
96 | * errno=ENOSYS. | |
97 | * | |
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. | |
100 | */ | |
95e6d268 MD |
101 | int close(int fd) |
102 | { | |
c668ba47 MJ |
103 | /* |
104 | * We can't retry dlsym here since close is async-signal-safe. | |
105 | */ | |
106 | if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) { | |
107 | errno = ENOSYS; | |
108 | return -1; | |
109 | } | |
110 | ||
111 | return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close); | |
95e6d268 MD |
112 | } |
113 | ||
52a20dc7 | 114 | /* |
c668ba47 MJ |
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. | |
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 | |
123 | * this wrapper could call dlsym() and thus not be async-signal-safe. | |
124 | * | |
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). | |
52a20dc7 MD |
128 | */ |
129 | int fclose(FILE *stream) | |
130 | { | |
c668ba47 MJ |
131 | if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) { |
132 | errno = ENOSYS; | |
133 | return -1; | |
134 | } | |
135 | ||
136 | return lttng_ust_safe_fclose_stream(stream, | |
137 | __lttng_ust_fd_plibc_fclose); | |
52a20dc7 MD |
138 | } |
139 | ||
95e6d268 MD |
140 | #if defined(__sun__) || defined(__FreeBSD__) |
141 | /* Solaris and FreeBSD. */ | |
142 | void closefrom(int lowfd) | |
143 | { | |
c668ba47 MJ |
144 | if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) { |
145 | return; | |
146 | } | |
147 | ||
148 | (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close); | |
95e6d268 MD |
149 | } |
150 | #elif defined(__NetBSD__) || defined(__OpenBSD__) | |
151 | /* NetBSD and OpenBSD. */ | |
152 | int closefrom(int lowfd) | |
153 | { | |
c668ba47 MJ |
154 | if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) { |
155 | errno = ENOSYS; | |
156 | return -1; | |
157 | } | |
158 | ||
159 | return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close); | |
95e6d268 MD |
160 | } |
161 | #else | |
162 | /* As far as we know, this OS does not implement closefrom. */ | |
163 | #endif |