Custom upgrade: disable ABI conflict tests
[lttng-ust.git] / src / lib / lttng-ust-fd / lttng-ust-fd.c
CommitLineData
95e6d268 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
95e6d268 3 *
c0c0989a 4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
95e6d268
MD
5 */
6
95e6d268
MD
7#define _LGPL_SOURCE
8#include <limits.h>
9#include <stdio.h>
10#include <sys/types.h>
11#include <unistd.h>
95e6d268 12#include <dlfcn.h>
5ce8eb6d 13#include <errno.h>
95e6d268 14
fca97dfd
MJ
15#include <lttng/ust-common.h>
16
9d315d6d 17#include "common/macros.h"
fca97dfd 18#include "common/ust-fd.h"
95e6d268 19
5ce8eb6d 20#define LTTNG_UST_DLSYM_FAILED_PTR 0x1
95e6d268 21
5ce8eb6d
MJ
22static int (*__lttng_ust_fd_plibc_close)(int fd) = NULL;
23static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream) = NULL;
fca97dfd 24
5ce8eb6d
MJ
25/*
26 * Use dlsym to find the original libc close() symbol and store it in
27 * __lttng_ust_fd_plibc_close.
28 */
95e6d268 29static
5ce8eb6d 30void *_lttng_ust_fd_init_plibc_close(void)
95e6d268 31{
5ce8eb6d 32 if (__lttng_ust_fd_plibc_close == NULL) {
95e6d268 33 __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close");
5ce8eb6d
MJ
34
35 if (__lttng_ust_fd_plibc_close == NULL) {
36 __lttng_ust_fd_plibc_close = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
95e6d268 37 fprintf(stderr, "%s\n", dlerror());
95e6d268
MD
38 }
39 }
5ce8eb6d
MJ
40
41 return __lttng_ust_fd_plibc_close;
95e6d268
MD
42}
43
5ce8eb6d
MJ
44/*
45 * Use dlsym to find the original libc fclose() symbol and store it in
46 * __lttng_ust_fd_plibc_fclose.
47 */
52a20dc7 48static
5ce8eb6d 49void *_lttng_ust_fd_init_plibc_fclose(void)
52a20dc7 50{
5ce8eb6d 51 if (__lttng_ust_fd_plibc_fclose == NULL) {
52a20dc7 52 __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose");
5ce8eb6d
MJ
53
54 if (__lttng_ust_fd_plibc_fclose == NULL) {
55 __lttng_ust_fd_plibc_fclose = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
52a20dc7 56 fprintf(stderr, "%s\n", dlerror());
52a20dc7
MD
57 }
58 }
5ce8eb6d
MJ
59
60 return __lttng_ust_fd_plibc_fclose;
52a20dc7
MD
61}
62
5ce8eb6d
MJ
63static
64void _lttng_ust_fd_ctor(void)
65 __attribute__((constructor));
66static
67void _lttng_ust_fd_ctor(void)
68{
aaa692c8
MD
69#if !defined(LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS)
70 void *handle = NULL;
71
72 /*
73 * Load ust-2.12 in the global symbol namespace.
74 */
75 handle = dlopen("liblttng-ust.so.0", RTLD_GLOBAL | RTLD_NOW);
76 if (!handle) {
77 fprintf(stderr, "liblttng-ust-fd.so.1: Failed to dlopen liblttng-ust.so.0: %s\n", dlerror());
78 abort();
79 }
80#endif
81
5ce8eb6d
MJ
82 lttng_ust_common_ctor();
83
84 /*
85 * Initialize the function pointers to the original libc symbols in the
86 * constructor since close() has to stay async-signal-safe and as such,
87 * we can't call dlsym() in the override functions.
88 */
89 (void) _lttng_ust_fd_init_plibc_close();
90 (void) _lttng_ust_fd_init_plibc_fclose();
91}
92
93/*
94 * Override the libc close() symbol with our own, allowing applications to
95 * close arbitrary file descriptors. If the fd is owned by lttng-ust, return
96 * -1, errno=EBADF instead of closing it.
97 *
98 * If dlsym failed to find the original libc close() symbol, return -1,
99 * errno=ENOSYS.
100 *
101 * There is a short window before the library constructor has executed where
102 * this wrapper could call dlsym() and thus not be async-signal-safe.
103 */
95e6d268
MD
104int close(int fd)
105{
5ce8eb6d
MJ
106 /*
107 * We can't retry dlsym here since close is async-signal-safe.
108 */
109 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
110 errno = ENOSYS;
111 return -1;
112 }
113
114 return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close);
95e6d268
MD
115}
116
52a20dc7 117/*
5ce8eb6d
MJ
118 * Override the libc fclose() symbol with our own, allowing applications to
119 * close arbitrary streams. If the fd is owned by lttng-ust, return -1,
120 * errno=EBADF instead of closing it.
121 *
122 * If dlsym failed to find the original libc close() symbol, return -1,
123 * errno=ENOSYS.
124 *
125 * There is a short window before the library constructor has executed where
126 * this wrapper could call dlsym() and thus not be async-signal-safe.
127 *
128 * Note: fcloseall() is not an issue because it closes only the streams it
129 * knows about, which differs from the problems caused by gnulib
130 * close_stdout(), which does an explicit fclose(stdout).
52a20dc7
MD
131 */
132int fclose(FILE *stream)
133{
5ce8eb6d
MJ
134 if (_lttng_ust_fd_init_plibc_fclose() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
135 errno = ENOSYS;
136 return -1;
137 }
138
139 return lttng_ust_safe_fclose_stream(stream,
140 __lttng_ust_fd_plibc_fclose);
52a20dc7
MD
141}
142
95e6d268
MD
143#if defined(__sun__) || defined(__FreeBSD__)
144/* Solaris and FreeBSD. */
145void closefrom(int lowfd)
146{
5ce8eb6d
MJ
147 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
148 return;
149 }
150
23366626 151 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
95e6d268
MD
152}
153#elif defined(__NetBSD__) || defined(__OpenBSD__)
154/* NetBSD and OpenBSD. */
155int closefrom(int lowfd)
156{
5ce8eb6d
MJ
157 if (_lttng_ust_fd_init_plibc_close() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
158 errno = ENOSYS;
159 return -1;
160 }
161
23366626 162 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
95e6d268
MD
163}
164#else
165/* As far as we know, this OS does not implement closefrom. */
166#endif
This page took 0.051135 seconds and 5 git commands to generate.