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