2 * Copyright (C) 2016 - Aravind HT <aravind.ht@gmail.com>
3 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; only
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
30 #include <sys/select.h>
31 #include <sys/resource.h>
35 #include <urcu/compiler.h>
36 #include <urcu/tls-compat.h>
40 #include <lttng/ust-error.h>
41 #include <usterr-signal-safe.h>
43 #include "../liblttng-ust/compat.h"
45 /* Operations on the fd set. */
46 #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
47 #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
48 #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
50 /* Check fd validity before calling these. */
51 #define ADD_FD_TO_SET(fd, fd_sets) \
52 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
53 #define IS_FD_SET(fd, fd_sets) \
54 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
55 #define DEL_FD_FROM_SET(fd, fd_sets) \
56 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
59 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
60 * within the libc dl lock. Therefore, we need to fixup the TLS before
61 * nesting into this lock.
63 static pthread_mutex_t ust_safe_guard_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
65 * Track whether we are within lttng-ust or application, for close
66 * system call override by LD_PRELOAD library.
68 static DEFINE_URCU_TLS(int, thread_fd_tracking
);
70 /* fd_set used to book keep fd being used by lttng-ust. */
71 static fd_set
*lttng_fd_set
;
72 static int lttng_ust_max_fd
;
73 static int num_fd_sets
;
76 * Force a read (imply TLS fixup for dlopen) of TLS variables.
78 void lttng_ust_fixup_fd_tracker_tls(void)
80 asm volatile ("" : : "m" (URCU_TLS(thread_fd_tracking
)));
84 * Allocate the fd set array based on the hard limit set for this
85 * process. This will be called during the constructor execution
86 * and will also be called in the child after fork via lttng_ust_init.
88 void lttng_ust_init_fd_tracker(void)
93 memset(&rlim
, 0, sizeof(rlim
));
94 /* Get the current possible max number of fd for this process. */
95 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0)
98 * FD set array size determined using the hard limit. Even if
99 * the process wishes to increase its limit using setrlimit, it
100 * can only do so with the softlimit which will be less than the
103 lttng_ust_max_fd
= rlim
.rlim_max
;
104 num_fd_sets
= lttng_ust_max_fd
/ FD_SETSIZE
;
105 if (lttng_ust_max_fd
% FD_SETSIZE
)
107 if (lttng_fd_set
!= NULL
) {
111 lttng_fd_set
= malloc(num_fd_sets
* (sizeof(fd_set
)));
114 for (i
= 0; i
< num_fd_sets
; i
++)
115 FD_ZERO((<tng_fd_set
[i
]));
118 void lttng_ust_lock_fd_tracker(void)
120 URCU_TLS(thread_fd_tracking
) = 1;
122 * Ensure the compiler don't move the store after the close()
123 * call in case close() would be marked as leaf.
126 pthread_mutex_lock(&ust_safe_guard_fd_mutex
);
129 void lttng_ust_unlock_fd_tracker(void)
131 pthread_mutex_unlock(&ust_safe_guard_fd_mutex
);
133 * Ensure the compiler don't move the store before the close()
134 * call, in case close() would be marked as leaf.
137 URCU_TLS(thread_fd_tracking
) = 0;
141 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
142 * Has strict checking of fd validity.
144 void lttng_ust_add_fd_to_tracker(int fd
)
146 assert(URCU_TLS(thread_fd_tracking
));
147 /* Trying to add an fd which we can not accommodate. */
148 assert(IS_FD_VALID(fd
));
149 /* Setting an fd thats already set. */
150 assert(!IS_FD_SET(fd
, lttng_fd_set
));
152 ADD_FD_TO_SET(fd
, lttng_fd_set
);
156 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
157 * Has strict checking for fd validity.
159 void lttng_ust_delete_fd_from_tracker(int fd
)
161 assert(URCU_TLS(thread_fd_tracking
));
162 /* Not a valid fd. */
163 assert(IS_FD_VALID(fd
));
164 /* Deleting an fd which was not set. */
165 assert(IS_FD_SET(fd
, lttng_fd_set
));
167 DEL_FD_FROM_SET(fd
, lttng_fd_set
);
171 * Interface allowing applications to close arbitrary file descriptors.
172 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
173 * instead of closing it if it is the case.
175 int lttng_ust_safe_close_fd(int fd
, int (*close_cb
)(int fd
))
179 lttng_ust_fixup_fd_tracker_tls();
182 * If called from lttng-ust, we directly call close without
183 * validating whether the FD is part of the tracked set.
185 if (URCU_TLS(thread_fd_tracking
))
188 lttng_ust_lock_fd_tracker();
189 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
195 lttng_ust_unlock_fd_tracker();
201 * Interface allowing applications to close arbitrary streams.
202 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
203 * instead of closing it if it is the case.
205 int lttng_ust_safe_fclose_stream(FILE *stream
, int (*fclose_cb
)(FILE *stream
))
209 lttng_ust_fixup_fd_tracker_tls();
212 * If called from lttng-ust, we directly call fclose without
213 * validating whether the FD is part of the tracked set.
215 if (URCU_TLS(thread_fd_tracking
))
216 return fclose_cb(stream
);
220 lttng_ust_lock_fd_tracker();
221 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
225 ret
= fclose_cb(stream
);
227 lttng_ust_unlock_fd_tracker();
233 static void set_close_success(int *p
)
237 static int test_close_success(const int *p
)
242 static void set_close_success(int *p
__attribute__((unused
)))
245 static int test_close_success(const int *p
__attribute__((unused
)))
252 * Implement helper for closefrom() override.
254 int lttng_ust_safe_closefrom_fd(int lowfd
, int (*close_cb
)(int fd
))
256 int ret
= 0, close_success
= 0, i
;
258 lttng_ust_fixup_fd_tracker_tls();
262 * NetBSD return EBADF if fd is invalid.
269 * If called from lttng-ust, we directly call close without
270 * validating whether the FD is part of the tracked set.
272 if (URCU_TLS(thread_fd_tracking
)) {
273 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
274 if (close_cb(i
) < 0) {
284 set_close_success(&close_success
);
287 lttng_ust_lock_fd_tracker();
288 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
289 if (IS_FD_VALID(i
) && IS_FD_SET(i
, lttng_fd_set
))
291 if (close_cb(i
) < 0) {
298 lttng_ust_unlock_fd_tracker();
302 set_close_success(&close_success
);
304 lttng_ust_unlock_fd_tracker();
306 if (!test_close_success(&close_success
)) {
308 * OpenBSD return EBADF if fd is greater than all open