2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2016 Aravind HT <aravind.ht@gmail.com>
5 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #include <sys/types.h>
17 #include <sys/select.h>
18 #include <sys/resource.h>
24 #include <urcu/compiler.h>
25 #include <urcu/tls-compat.h>
26 #include <urcu/system.h>
28 #include "common/ust-fd.h"
29 #include "common/macros.h"
30 #include <lttng/ust-error.h>
31 #include <lttng/ust-cancelstate.h>
32 #include "common/logging.h"
34 #include "lib/lttng-ust-common/fd-tracker.h"
36 /* Operations on the fd set. */
37 #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
38 #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
39 #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
40 #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
42 /* Check fd validity before calling these. */
43 #define ADD_FD_TO_SET(fd, fd_sets) \
44 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
45 #define IS_FD_SET(fd, fd_sets) \
46 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
47 #define DEL_FD_FROM_SET(fd, fd_sets) \
48 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
51 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
52 * within the libc dl lock. Therefore, we need to allocate the TLS before
53 * nesting into this lock.
55 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
56 * is also held across fork.
58 static pthread_mutex_t ust_safe_guard_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
61 * Track whether we are within lttng-ust or application, for close
62 * system call override by LD_PRELOAD library. This also tracks whether
63 * we are invoking close() from a signal handler nested on an
66 static DEFINE_URCU_TLS(int, ust_fd_mutex_nest
);
68 /* fd_set used to book keep fd being used by lttng-ust. */
69 static fd_set
*lttng_fd_set
;
70 static int lttng_ust_max_fd
;
71 static int num_fd_sets
;
75 * Force a read (imply TLS allocation for dlopen) of TLS variables.
77 void lttng_ust_fd_tracker_alloc_tls(void)
79 __asm__
__volatile__ ("" : : "m" (URCU_TLS(ust_fd_mutex_nest
)));
83 * Allocate the fd set array based on the hard limit set for this
84 * process. This will be called during the constructor execution
85 * and will also be called in the child after fork via lttng_ust_init.
87 void lttng_ust_fd_tracker_init(void)
92 if (CMM_LOAD_SHARED(init_done
))
95 memset(&rlim
, 0, sizeof(rlim
));
96 /* Get the current possible max number of fd for this process. */
97 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0)
100 * FD set array size determined using the hard limit. Even if
101 * the process wishes to increase its limit using setrlimit, it
102 * can only do so with the softlimit which will be less than the
105 lttng_ust_max_fd
= rlim
.rlim_max
;
106 num_fd_sets
= lttng_ust_max_fd
/ FD_SETSIZE
;
107 if (lttng_ust_max_fd
% FD_SETSIZE
)
109 if (lttng_fd_set
!= NULL
) {
113 lttng_fd_set
= malloc(num_fd_sets
* (sizeof(fd_set
)));
116 for (i
= 0; i
< num_fd_sets
; i
++)
117 FD_ZERO((<tng_fd_set
[i
]));
118 CMM_STORE_SHARED(init_done
, 1);
121 void lttng_ust_lock_fd_tracker(void)
123 sigset_t sig_all_blocked
, orig_mask
;
126 if (lttng_ust_cancelstate_disable_push()) {
127 ERR("lttng_ust_cancelstate_disable_push");
129 sigfillset(&sig_all_blocked
);
130 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
132 ERR("pthread_sigmask: %s", strerror(ret
));
134 if (!URCU_TLS(ust_fd_mutex_nest
)++) {
136 * Ensure the compiler don't move the store after the close()
137 * call in case close() would be marked as leaf.
140 pthread_mutex_lock(&ust_safe_guard_fd_mutex
);
142 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
144 ERR("pthread_sigmask: %s", strerror(ret
));
148 void lttng_ust_unlock_fd_tracker(void)
150 sigset_t sig_all_blocked
, orig_mask
;
153 sigfillset(&sig_all_blocked
);
154 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
156 ERR("pthread_sigmask: %s", strerror(ret
));
159 * Ensure the compiler don't move the store before the close()
160 * call, in case close() would be marked as leaf.
163 if (!--URCU_TLS(ust_fd_mutex_nest
)) {
164 pthread_mutex_unlock(&ust_safe_guard_fd_mutex
);
166 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
168 ERR("pthread_sigmask: %s", strerror(ret
));
170 if (lttng_ust_cancelstate_disable_pop()) {
171 ERR("lttng_ust_cancelstate_disable_pop");
175 static int dup_std_fd(int fd
)
178 int fd_to_close
[STDERR_FILENO
+ 1];
179 int fd_to_close_count
= 0;
180 int dup_cmd
= F_DUPFD
; /* Default command */
183 if (!(IS_FD_STD(fd
))) {
184 /* Should not be here */
189 /* Check for FD_CLOEXEC flag */
190 ret
= fcntl(fd
, F_GETFD
);
192 PERROR("fcntl on f_getfd");
197 if (ret
& FD_CLOEXEC
) {
198 dup_cmd
= F_DUPFD_CLOEXEC
;
202 for (i
= 0; i
< STDERR_FILENO
+ 1; i
++) {
203 ret
= fcntl(fd
, dup_cmd
, 0);
205 PERROR("fcntl dup fd");
209 if (!(IS_FD_STD(ret
))) {
210 /* fd is outside of STD range, use it. */
212 /* Close fd received as argument. */
218 fd_to_close
[i
] = ret
;
222 /* Close intermediary fds */
223 for (i
= 0; i
< fd_to_close_count
; i
++) {
224 ret
= close(fd_to_close
[i
]);
226 PERROR("close on temporary fd: %d.", fd_to_close
[i
]);
228 * Not using an abort here would yield a complicated
229 * error handling for the caller. If a failure occurs
230 * here, the system is already in a bad state.
242 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
243 * Has strict checking of fd validity.
245 * If fd <= 2, dup the fd until fd > 2. This enables us to bypass
246 * problems that can be encountered if UST uses stdin, stdout, stderr
247 * fds for internal use (daemon etc.). This can happen if the
248 * application closes either of those file descriptors. Intermediary fds
249 * are closed as needed.
251 * Return -1 on error.
254 int lttng_ust_add_fd_to_tracker(int fd
)
258 * Ensure the tracker is initialized when called from
261 lttng_ust_fd_tracker_init();
262 assert(URCU_TLS(ust_fd_mutex_nest
));
265 ret
= dup_std_fd(fd
);
272 /* Trying to add an fd which we can not accommodate. */
273 assert(IS_FD_VALID(fd
));
274 /* Setting an fd that's already set. */
275 assert(!IS_FD_SET(fd
, lttng_fd_set
));
277 ADD_FD_TO_SET(fd
, lttng_fd_set
);
284 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
285 * Has strict checking for fd validity.
287 void lttng_ust_delete_fd_from_tracker(int fd
)
290 * Ensure the tracker is initialized when called from
293 lttng_ust_fd_tracker_init();
295 assert(URCU_TLS(ust_fd_mutex_nest
));
296 /* Not a valid fd. */
297 assert(IS_FD_VALID(fd
));
298 /* Deleting an fd which was not set. */
299 assert(IS_FD_SET(fd
, lttng_fd_set
));
301 DEL_FD_FROM_SET(fd
, lttng_fd_set
);
305 * Interface allowing applications to close arbitrary file descriptors.
306 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
307 * instead of closing it if it is the case.
309 int lttng_ust_safe_close_fd(int fd
, int (*close_cb
)(int fd
))
313 lttng_ust_fd_tracker_alloc_tls();
316 * Ensure the tracker is initialized when called from
319 lttng_ust_fd_tracker_init();
322 * If called from lttng-ust, we directly call close without
323 * validating whether the FD is part of the tracked set.
325 if (URCU_TLS(ust_fd_mutex_nest
))
328 lttng_ust_lock_fd_tracker();
329 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
335 lttng_ust_unlock_fd_tracker();
341 * Interface allowing applications to close arbitrary streams.
342 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
343 * instead of closing it if it is the case.
345 int lttng_ust_safe_fclose_stream(FILE *stream
, int (*fclose_cb
)(FILE *stream
))
349 lttng_ust_fd_tracker_alloc_tls();
352 * Ensure the tracker is initialized when called from
355 lttng_ust_fd_tracker_init();
358 * If called from lttng-ust, we directly call fclose without
359 * validating whether the FD is part of the tracked set.
361 if (URCU_TLS(ust_fd_mutex_nest
))
362 return fclose_cb(stream
);
366 lttng_ust_lock_fd_tracker();
367 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
371 ret
= fclose_cb(stream
);
373 lttng_ust_unlock_fd_tracker();
379 static void set_close_success(int *p
)
383 static int test_close_success(const int *p
)
388 static void set_close_success(int *p
__attribute__((unused
)))
391 static int test_close_success(const int *p
__attribute__((unused
)))
398 * Implement helper for closefrom() override.
400 int lttng_ust_safe_closefrom_fd(int lowfd
, int (*close_cb
)(int fd
))
402 int ret
= 0, close_success
= 0, i
;
404 lttng_ust_fd_tracker_alloc_tls();
407 * Ensure the tracker is initialized when called from
410 lttng_ust_fd_tracker_init();
414 * NetBSD return EBADF if fd is invalid.
421 * If called from lttng-ust, we directly call close without
422 * validating whether the FD is part of the tracked set.
424 if (URCU_TLS(ust_fd_mutex_nest
)) {
425 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
426 if (close_cb(i
) < 0) {
436 set_close_success(&close_success
);
439 lttng_ust_lock_fd_tracker();
440 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
441 if (IS_FD_VALID(i
) && IS_FD_SET(i
, lttng_fd_set
))
443 if (close_cb(i
) < 0) {
450 lttng_ust_unlock_fd_tracker();
454 set_close_success(&close_success
);
456 lttng_ust_unlock_fd_tracker();
458 if (!test_close_success(&close_success
)) {
460 * OpenBSD return EBADF if fd is greater than all open