Commit | Line | Data |
---|---|---|
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 MD |
12 | #include <dlfcn.h> |
13 | ||
fca97dfd MJ |
14 | #include <lttng/ust-common.h> |
15 | ||
9d315d6d | 16 | #include "common/macros.h" |
fca97dfd | 17 | #include "common/ust-fd.h" |
95e6d268 | 18 | |
95e6d268 | 19 | static int (*__lttng_ust_fd_plibc_close)(int fd); |
52a20dc7 | 20 | static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream); |
95e6d268 | 21 | |
fca97dfd MJ |
22 | static |
23 | void _lttng_ust_fd_ctor(void) | |
24 | __attribute__((constructor)); | |
25 | static | |
26 | void _lttng_ust_fd_ctor(void) | |
27 | { | |
28 | lttng_ust_common_ctor(); | |
29 | } | |
30 | ||
95e6d268 MD |
31 | static |
32 | int _lttng_ust_fd_libc_close(int fd) | |
33 | { | |
34 | if (!__lttng_ust_fd_plibc_close) { | |
35 | __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close"); | |
36 | if (!__lttng_ust_fd_plibc_close) { | |
37 | fprintf(stderr, "%s\n", dlerror()); | |
38 | return -1; | |
39 | } | |
40 | } | |
41 | return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close); | |
42 | } | |
43 | ||
52a20dc7 MD |
44 | static |
45 | int _lttng_ust_fd_libc_fclose(FILE *stream) | |
46 | { | |
47 | if (!__lttng_ust_fd_plibc_fclose) { | |
48 | __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose"); | |
49 | if (!__lttng_ust_fd_plibc_fclose) { | |
50 | fprintf(stderr, "%s\n", dlerror()); | |
51 | return -1; | |
52 | } | |
53 | } | |
54 | return lttng_ust_safe_fclose_stream(stream, | |
55 | __lttng_ust_fd_plibc_fclose); | |
56 | } | |
57 | ||
95e6d268 MD |
58 | int close(int fd) |
59 | { | |
60 | return _lttng_ust_fd_libc_close(fd); | |
61 | } | |
62 | ||
52a20dc7 MD |
63 | /* |
64 | * Note: fcloseall() is not an issue because it fcloses only the | |
65 | * streams it knows about, which differs from the problems caused by | |
66 | * gnulib close_stdout(), which does an explicit fclose(stdout). | |
67 | */ | |
68 | int fclose(FILE *stream) | |
69 | { | |
70 | return _lttng_ust_fd_libc_fclose(stream); | |
71 | } | |
72 | ||
95e6d268 MD |
73 | #if defined(__sun__) || defined(__FreeBSD__) |
74 | /* Solaris and FreeBSD. */ | |
75 | void closefrom(int lowfd) | |
76 | { | |
23366626 | 77 | (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close); |
95e6d268 MD |
78 | } |
79 | #elif defined(__NetBSD__) || defined(__OpenBSD__) | |
80 | /* NetBSD and OpenBSD. */ | |
81 | int closefrom(int lowfd) | |
82 | { | |
23366626 | 83 | return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close); |
95e6d268 MD |
84 | } |
85 | #else | |
86 | /* As far as we know, this OS does not implement closefrom. */ | |
87 | #endif |