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