Add close_range wrapper to liblttng-ust-fd.so
[lttng-ust.git] / liblttng-ust-fd / lttng-ust-fd.c
CommitLineData
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
34static int (*__lttng_ust_fd_plibc_close)(int fd) = NULL;
35static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream) = NULL;
26be1417
MD
36static int (*__lttng_ust_fd_plibc_close_range)(unsigned int first,
37 unsigned int last, int flags) = NULL;
c668ba47
MJ
38
39/*
40 * Use dlsym to find the original libc close() symbol and store it in
41 * __lttng_ust_fd_plibc_close.
42 */
95e6d268 43static
c668ba47 44void *_lttng_ust_fd_init_plibc_close(void)
95e6d268 45{
c668ba47 46 if (__lttng_ust_fd_plibc_close == NULL) {
95e6d268 47 __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close");
c668ba47
MJ
48
49 if (__lttng_ust_fd_plibc_close == NULL) {
50 __lttng_ust_fd_plibc_close = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
95e6d268 51 fprintf(stderr, "%s\n", dlerror());
95e6d268
MD
52 }
53 }
c668ba47
MJ
54
55 return __lttng_ust_fd_plibc_close;
95e6d268
MD
56}
57
c668ba47
MJ
58/*
59 * Use dlsym to find the original libc fclose() symbol and store it in
60 * __lttng_ust_fd_plibc_fclose.
61 */
52a20dc7 62static
c668ba47 63void *_lttng_ust_fd_init_plibc_fclose(void)
52a20dc7 64{
c668ba47 65 if (__lttng_ust_fd_plibc_fclose == NULL) {
52a20dc7 66 __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose");
c668ba47
MJ
67
68 if (__lttng_ust_fd_plibc_fclose == NULL) {
69 __lttng_ust_fd_plibc_fclose = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
52a20dc7 70 fprintf(stderr, "%s\n", dlerror());
52a20dc7
MD
71 }
72 }
c668ba47
MJ
73
74 return __lttng_ust_fd_plibc_fclose;
75}
76
26be1417
MD
77/*
78 * Use dlsym to find the original libc close_range() symbol and store it
79 * in __lttng_ust_fd_plibc_close_range. The close_range symbol only
80 * appears in glibc 2.34, so it is considered optional.
81 */
82static
83void *_lttng_ust_fd_init_plibc_close_range(void)
84{
85 if (__lttng_ust_fd_plibc_close_range == NULL) {
86 __lttng_ust_fd_plibc_close_range = dlsym(RTLD_NEXT, "close_range");
87
88 if (__lttng_ust_fd_plibc_close_range == NULL)
89 __lttng_ust_fd_plibc_close_range = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
90 }
91
92 return __lttng_ust_fd_plibc_close_range;
93}
94
c668ba47
MJ
95static
96void _lttng_ust_fd_ctor(void)
97 __attribute__((constructor));
98static
99void _lttng_ust_fd_ctor(void)
100{
c668ba47
MJ
101 /*
102 * Initialize the function pointers to the original libc symbols in the
103 * constructor since close() has to stay async-signal-safe and as such,
104 * we can't call dlsym() in the override functions.
105 */
106 (void) _lttng_ust_fd_init_plibc_close();
107 (void) _lttng_ust_fd_init_plibc_fclose();
26be1417 108 (void) _lttng_ust_fd_init_plibc_close_range();
52a20dc7
MD
109}
110
c668ba47
MJ
111/*
112 * Override the libc close() symbol with our own, allowing applications to
113 * close arbitrary file descriptors. If the fd is owned by lttng-ust, return
114 * -1, errno=EBADF instead of closing it.
115 *
116 * If dlsym failed to find the original libc close() symbol, return -1,
117 * errno=ENOSYS.
118 *
119 * There is a short window before the library constructor has executed where
120 * this wrapper could call dlsym() and thus not be async-signal-safe.
121 */
95e6d268
MD
122int close(int fd)
123{
c668ba47
MJ
124 /*
125 * We can't retry dlsym here since close is async-signal-safe.
126 */
127 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
128 errno = ENOSYS;
129 return -1;
130 }
131
132 return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close);
95e6d268
MD
133}
134
52a20dc7 135/*
c668ba47
MJ
136 * Override the libc fclose() symbol with our own, allowing applications to
137 * close arbitrary streams. If the fd is owned by lttng-ust, return -1,
138 * errno=EBADF instead of closing it.
139 *
140 * If dlsym failed to find the original libc close() symbol, return -1,
141 * errno=ENOSYS.
142 *
143 * There is a short window before the library constructor has executed where
144 * this wrapper could call dlsym() and thus not be async-signal-safe.
145 *
146 * Note: fcloseall() is not an issue because it closes only the streams it
147 * knows about, which differs from the problems caused by gnulib
148 * close_stdout(), which does an explicit fclose(stdout).
52a20dc7
MD
149 */
150int fclose(FILE *stream)
151{
c668ba47
MJ
152 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
153 errno = ENOSYS;
154 return -1;
155 }
156
157 return lttng_ust_safe_fclose_stream(stream,
158 __lttng_ust_fd_plibc_fclose);
52a20dc7
MD
159}
160
26be1417
MD
161/*
162 * Override the libc close_range() symbol with our own, allowing
163 * applications to close arbitrary file descriptors. If the fd is owned
164 * by lttng-ust, return -1, errno=EBADF instead of closing it.
165 *
166 * If dlsym failed to find the original libc close_range() symbol,
167 * return -1, errno=ENOSYS.
168 *
169 * There is a short window before the library constructor has executed where
170 * this wrapper could call dlsym() and thus not be async-signal-safe.
171 */
172int close_range(unsigned int first, unsigned int last, int flags)
173{
174 /*
175 * We can't retry dlsym here since close is async-signal-safe.
176 */
177 if (_lttng_ust_fd_init_plibc_close_range() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
178 errno = ENOSYS;
179 return -1;
180 }
181
182 return lttng_ust_safe_close_range_fd(first, last, flags, __lttng_ust_fd_plibc_close_range);
183}
184
95e6d268
MD
185#if defined(__sun__) || defined(__FreeBSD__)
186/* Solaris and FreeBSD. */
187void closefrom(int lowfd)
188{
c668ba47
MJ
189 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
190 return;
191 }
192
193 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
95e6d268
MD
194}
195#elif defined(__NetBSD__) || defined(__OpenBSD__)
196/* NetBSD and OpenBSD. */
197int closefrom(int lowfd)
198{
c668ba47
MJ
199 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
200 errno = ENOSYS;
201 return -1;
202 }
203
204 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
95e6d268
MD
205}
206#else
207/* As far as we know, this OS does not implement closefrom. */
208#endif
This page took 0.033174 seconds and 4 git commands to generate.