Hide private usterr-signal-safe.h symbols
[lttng-ust.git] / liblttng-ust-fd / lttng-ust-fd.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
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
15 #include <ust-helper.h>
16 #include "usterr-signal-safe.h"
17
18 static int (*__lttng_ust_fd_plibc_close)(int fd);
19 static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream);
20
21 static __attribute__((constructor))
22 void _lttng_ust_fd_init(void)
23 {
24 ust_err_init();
25 }
26
27 static
28 int _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
40 static
41 int _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
54 int close(int fd)
55 {
56 return _lttng_ust_fd_libc_close(fd);
57 }
58
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 */
64 int fclose(FILE *stream)
65 {
66 return _lttng_ust_fd_libc_fclose(stream);
67 }
68
69 #if defined(__sun__) || defined(__FreeBSD__)
70 /* Solaris and FreeBSD. */
71 void closefrom(int lowfd)
72 {
73 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
74 }
75 #elif defined(__NetBSD__) || defined(__OpenBSD__)
76 /* NetBSD and OpenBSD. */
77 int closefrom(int lowfd)
78 {
79 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
80 }
81 #else
82 /* As far as we know, this OS does not implement closefrom. */
83 #endif
This page took 0.031987 seconds and 4 git commands to generate.