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