2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <sys/types.h>
14 #include <lttng/ust-common.h>
16 #include "common/macros.h"
17 #include "common/ust-fd.h"
19 static int (*__lttng_ust_fd_plibc_close
)(int fd
);
20 static int (*__lttng_ust_fd_plibc_fclose
)(FILE *stream
);
23 void _lttng_ust_fd_ctor(void)
24 __attribute__((constructor
));
26 void _lttng_ust_fd_ctor(void)
28 lttng_ust_common_ctor();
32 int _lttng_ust_fd_libc_close(int fd
)
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());
41 return lttng_ust_safe_close_fd(fd
, __lttng_ust_fd_plibc_close
);
45 int _lttng_ust_fd_libc_fclose(FILE *stream
)
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());
54 return lttng_ust_safe_fclose_stream(stream
,
55 __lttng_ust_fd_plibc_fclose
);
60 return _lttng_ust_fd_libc_close(fd
);
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).
68 int fclose(FILE *stream
)
70 return _lttng_ust_fd_libc_fclose(stream
);
73 #if defined(__sun__) || defined(__FreeBSD__)
74 /* Solaris and FreeBSD. */
75 void closefrom(int lowfd
)
77 (void) lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
79 #elif defined(__NetBSD__) || defined(__OpenBSD__)
80 /* NetBSD and OpenBSD. */
81 int closefrom(int lowfd
)
83 return lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
86 /* As far as we know, this OS does not implement closefrom. */