| 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 | |
| 28 | #include <helper.h> |
| 29 | #include "usterr-signal-safe.h" |
| 30 | |
| 31 | volatile enum ust_loglevel ust_loglevel; |
| 32 | |
| 33 | static int (*__lttng_ust_fd_plibc_close)(int fd); |
| 34 | static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream); |
| 35 | |
| 36 | static |
| 37 | int _lttng_ust_fd_libc_close(int fd) |
| 38 | { |
| 39 | if (!__lttng_ust_fd_plibc_close) { |
| 40 | __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close"); |
| 41 | if (!__lttng_ust_fd_plibc_close) { |
| 42 | fprintf(stderr, "%s\n", dlerror()); |
| 43 | return -1; |
| 44 | } |
| 45 | } |
| 46 | return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close); |
| 47 | } |
| 48 | |
| 49 | static |
| 50 | int _lttng_ust_fd_libc_fclose(FILE *stream) |
| 51 | { |
| 52 | if (!__lttng_ust_fd_plibc_fclose) { |
| 53 | __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose"); |
| 54 | if (!__lttng_ust_fd_plibc_fclose) { |
| 55 | fprintf(stderr, "%s\n", dlerror()); |
| 56 | return -1; |
| 57 | } |
| 58 | } |
| 59 | return lttng_ust_safe_fclose_stream(stream, |
| 60 | __lttng_ust_fd_plibc_fclose); |
| 61 | } |
| 62 | |
| 63 | int close(int fd) |
| 64 | { |
| 65 | return _lttng_ust_fd_libc_close(fd); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Note: fcloseall() is not an issue because it fcloses only the |
| 70 | * streams it knows about, which differs from the problems caused by |
| 71 | * gnulib close_stdout(), which does an explicit fclose(stdout). |
| 72 | */ |
| 73 | int fclose(FILE *stream) |
| 74 | { |
| 75 | return _lttng_ust_fd_libc_fclose(stream); |
| 76 | } |
| 77 | |
| 78 | #if defined(__sun__) || defined(__FreeBSD__) |
| 79 | /* Solaris and FreeBSD. */ |
| 80 | void closefrom(int lowfd) |
| 81 | { |
| 82 | (void) lttng_ust_safe_closefrom(lowfd, __lttng_ust_fd_plibc_close); |
| 83 | } |
| 84 | #elif defined(__NetBSD__) || defined(__OpenBSD__) |
| 85 | /* NetBSD and OpenBSD. */ |
| 86 | int closefrom(int lowfd) |
| 87 | { |
| 88 | return lttng_ust_safe_closefrom(lowfd, __lttng_ust_fd_plibc_close); |
| 89 | } |
| 90 | #else |
| 91 | /* As far as we know, this OS does not implement closefrom. */ |
| 92 | #endif |