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