Fix: lttng-ust-fd: remove lttng_ust_common_ctor call
[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 /*
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();
88 }
89
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 */
101 int close(int fd)
102 {
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);
112 }
113
114 /*
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).
128 */
129 int fclose(FILE *stream)
130 {
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);
138 }
139
140 #if defined(__sun__) || defined(__FreeBSD__)
141 /* Solaris and FreeBSD. */
142 void closefrom(int lowfd)
143 {
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);
149 }
150 #elif defined(__NetBSD__) || defined(__OpenBSD__)
151 /* NetBSD and OpenBSD. */
152 int closefrom(int lowfd)
153 {
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);
160 }
161 #else
162 /* As far as we know, this OS does not implement closefrom. */
163 #endif
This page took 0.034673 seconds and 4 git commands to generate.