56efb457269c2f7e9f85478a65b2daab61b6d7ff
[lttng-ust.git] / src / lib / lttng-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 <dlfcn.h>
13
14 #include <lttng/ust-common.h>
15
16 #include "common/macros.h"
17 #include "common/ust-fd.h"
18
19 static int (*__lttng_ust_fd_plibc_close)(int fd);
20 static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream);
21
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
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
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
58 int close(int fd)
59 {
60 return _lttng_ust_fd_libc_close(fd);
61 }
62
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
73 #if defined(__sun__) || defined(__FreeBSD__)
74 /* Solaris and FreeBSD. */
75 void closefrom(int lowfd)
76 {
77 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
78 }
79 #elif defined(__NetBSD__) || defined(__OpenBSD__)
80 /* NetBSD and OpenBSD. */
81 int closefrom(int lowfd)
82 {
83 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
84 }
85 #else
86 /* As far as we know, this OS does not implement closefrom. */
87 #endif
This page took 0.037609 seconds and 3 git commands to generate.