Fix: pass private data to context callbacks
[lttng-ust.git] / liblttng-ust-fd / lttng-ust-fd.c
CommitLineData
95e6d268 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
95e6d268 3 *
c0c0989a 4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
95e6d268
MD
5 */
6
95e6d268
MD
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
864a1eda 15#include <ust-helper.h>
95e6d268 16
95e6d268 17static int (*__lttng_ust_fd_plibc_close)(int fd);
52a20dc7 18static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream);
95e6d268
MD
19
20static
21int _lttng_ust_fd_libc_close(int fd)
22{
23 if (!__lttng_ust_fd_plibc_close) {
24 __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close");
25 if (!__lttng_ust_fd_plibc_close) {
26 fprintf(stderr, "%s\n", dlerror());
27 return -1;
28 }
29 }
30 return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close);
31}
32
52a20dc7
MD
33static
34int _lttng_ust_fd_libc_fclose(FILE *stream)
35{
36 if (!__lttng_ust_fd_plibc_fclose) {
37 __lttng_ust_fd_plibc_fclose = dlsym(RTLD_NEXT, "fclose");
38 if (!__lttng_ust_fd_plibc_fclose) {
39 fprintf(stderr, "%s\n", dlerror());
40 return -1;
41 }
42 }
43 return lttng_ust_safe_fclose_stream(stream,
44 __lttng_ust_fd_plibc_fclose);
45}
46
95e6d268
MD
47int close(int fd)
48{
49 return _lttng_ust_fd_libc_close(fd);
50}
51
52a20dc7
MD
52/*
53 * Note: fcloseall() is not an issue because it fcloses only the
54 * streams it knows about, which differs from the problems caused by
55 * gnulib close_stdout(), which does an explicit fclose(stdout).
56 */
57int fclose(FILE *stream)
58{
59 return _lttng_ust_fd_libc_fclose(stream);
60}
61
95e6d268
MD
62#if defined(__sun__) || defined(__FreeBSD__)
63/* Solaris and FreeBSD. */
64void closefrom(int lowfd)
65{
23366626 66 (void) lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
95e6d268
MD
67}
68#elif defined(__NetBSD__) || defined(__OpenBSD__)
69/* NetBSD and OpenBSD. */
70int closefrom(int lowfd)
71{
23366626 72 return lttng_ust_safe_closefrom_fd(lowfd, __lttng_ust_fd_plibc_close);
95e6d268
MD
73}
74#else
75/* As far as we know, this OS does not implement closefrom. */
76#endif
This page took 0.027049 seconds and 4 git commands to generate.