2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <sys/types.h>
15 #include <ust-helper.h>
16 #include "usterr-signal-safe.h"
18 static int (*__lttng_ust_fd_plibc_close
)(int fd
);
19 static int (*__lttng_ust_fd_plibc_fclose
)(FILE *stream
);
21 static __attribute__((constructor
))
22 void _lttng_ust_fd_init(void)
28 int _lttng_ust_fd_libc_close(int fd
)
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());
37 return lttng_ust_safe_close_fd(fd
, __lttng_ust_fd_plibc_close
);
41 int _lttng_ust_fd_libc_fclose(FILE *stream
)
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());
50 return lttng_ust_safe_fclose_stream(stream
,
51 __lttng_ust_fd_plibc_fclose
);
56 return _lttng_ust_fd_libc_close(fd
);
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).
64 int fclose(FILE *stream
)
66 return _lttng_ust_fd_libc_fclose(stream
);
69 #if defined(__sun__) || defined(__FreeBSD__)
70 /* Solaris and FreeBSD. */
71 void closefrom(int lowfd
)
73 (void) lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
75 #elif defined(__NetBSD__) || defined(__OpenBSD__)
76 /* NetBSD and OpenBSD. */
77 int closefrom(int lowfd
)
79 return lttng_ust_safe_closefrom_fd(lowfd
, __lttng_ust_fd_plibc_close
);
82 /* As far as we know, this OS does not implement closefrom. */