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 "common/logging.h"
33 #include "lib/lttng-ust-common/fd-tracker.h"
35 /* Operations on the fd set. */
36 #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
37 #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
38 #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
39 #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
41 /* Check fd validity before calling these. */
42 #define ADD_FD_TO_SET(fd, fd_sets) \
43 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
44 #define IS_FD_SET(fd, fd_sets) \
45 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
46 #define DEL_FD_FROM_SET(fd, fd_sets) \
47 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
50 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
51 * within the libc dl lock. Therefore, we need to allocate the TLS before
52 * nesting into this lock.
54 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
55 * is also held across fork.
57 static pthread_mutex_t ust_safe_guard_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
60 * Cancel state when grabbing the ust_safe_guard_fd_mutex. Saved when
61 * locking, restored on unlock. Protected by ust_safe_guard_fd_mutex.
63 static int ust_safe_guard_saved_cancelstate
;
66 * Track whether we are within lttng-ust or application, for close
67 * system call override by LD_PRELOAD library. This also tracks whether
68 * we are invoking close() from a signal handler nested on an
71 static DEFINE_URCU_TLS(int, ust_fd_mutex_nest
);
73 /* fd_set used to book keep fd being used by lttng-ust. */
74 static fd_set
*lttng_fd_set
;
75 static int lttng_ust_max_fd
;
76 static int num_fd_sets
;
80 * Force a read (imply TLS allocation for dlopen) of TLS variables.
82 void lttng_ust_fd_tracker_alloc_tls(void)
84 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest
)));
88 * Allocate the fd set array based on the hard limit set for this
89 * process. This will be called during the constructor execution
90 * and will also be called in the child after fork via lttng_ust_init.
92 void lttng_ust_fd_tracker_init(void)
97 if (CMM_LOAD_SHARED(init_done
))
100 memset(&rlim
, 0, sizeof(rlim
));
101 /* Get the current possible max number of fd for this process. */
102 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0)
105 * FD set array size determined using the hard limit. Even if
106 * the process wishes to increase its limit using setrlimit, it
107 * can only do so with the softlimit which will be less than the
110 lttng_ust_max_fd
= rlim
.rlim_max
;
111 num_fd_sets
= lttng_ust_max_fd
/ FD_SETSIZE
;
112 if (lttng_ust_max_fd
% FD_SETSIZE
)
114 if (lttng_fd_set
!= NULL
) {
118 lttng_fd_set
= malloc(num_fd_sets
* (sizeof(fd_set
)));
121 for (i
= 0; i
< num_fd_sets
; i
++)
122 FD_ZERO((<tng_fd_set
[i
]));
123 CMM_STORE_SHARED(init_done
, 1);
126 void lttng_ust_lock_fd_tracker(void)
128 sigset_t sig_all_blocked
, orig_mask
;
131 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
133 ERR("pthread_setcancelstate: %s", strerror(ret
));
135 sigfillset(&sig_all_blocked
);
136 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
138 ERR("pthread_sigmask: %s", strerror(ret
));
140 if (!URCU_TLS(ust_fd_mutex_nest
)++) {
142 * Ensure the compiler don't move the store after the close()
143 * call in case close() would be marked as leaf.
146 pthread_mutex_lock(&ust_safe_guard_fd_mutex
);
147 ust_safe_guard_saved_cancelstate
= oldstate
;
149 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
151 ERR("pthread_sigmask: %s", strerror(ret
));
155 void lttng_ust_unlock_fd_tracker(void)
157 sigset_t sig_all_blocked
, orig_mask
;
158 int ret
, newstate
, oldstate
;
159 bool restore_cancel
= false;
161 sigfillset(&sig_all_blocked
);
162 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
164 ERR("pthread_sigmask: %s", strerror(ret
));
167 * Ensure the compiler don't move the store before the close()
168 * call, in case close() would be marked as leaf.
171 if (!--URCU_TLS(ust_fd_mutex_nest
)) {
172 newstate
= ust_safe_guard_saved_cancelstate
;
173 restore_cancel
= true;
174 pthread_mutex_unlock(&ust_safe_guard_fd_mutex
);
176 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
178 ERR("pthread_sigmask: %s", strerror(ret
));
180 if (restore_cancel
) {
181 ret
= pthread_setcancelstate(newstate
, &oldstate
);
183 ERR("pthread_setcancelstate: %s", strerror(ret
));
188 static int dup_std_fd(int fd
)
191 int fd_to_close
[STDERR_FILENO
+ 1];
192 int fd_to_close_count
= 0;
193 int dup_cmd
= F_DUPFD
; /* Default command */
196 if (!(IS_FD_STD(fd
))) {
197 /* Should not be here */
202 /* Check for FD_CLOEXEC flag */
203 ret
= fcntl(fd
, F_GETFD
);
205 PERROR("fcntl on f_getfd");
210 if (ret
& FD_CLOEXEC
) {
211 dup_cmd
= F_DUPFD_CLOEXEC
;
215 for (i
= 0; i
< STDERR_FILENO
+ 1; i
++) {
216 ret
= fcntl(fd
, dup_cmd
, 0);
218 PERROR("fcntl dup fd");
222 if (!(IS_FD_STD(ret
))) {
223 /* fd is outside of STD range, use it. */
225 /* Close fd received as argument. */
231 fd_to_close
[i
] = ret
;
235 /* Close intermediary fds */
236 for (i
= 0; i
< fd_to_close_count
; i
++) {
237 ret
= close(fd_to_close
[i
]);
239 PERROR("close on temporary fd: %d.", fd_to_close
[i
]);
241 * Not using an abort here would yield a complicated
242 * error handling for the caller. If a failure occurs
243 * here, the system is already in a bad state.
255 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
256 * Has strict checking of fd validity.
258 * If fd <= 2, dup the fd until fd > 2. This enables us to bypass
259 * problems that can be encountered if UST uses stdin, stdout, stderr
260 * fds for internal use (daemon etc.). This can happen if the
261 * application closes either of those file descriptors. Intermediary fds
262 * are closed as needed.
264 * Return -1 on error.
267 int lttng_ust_add_fd_to_tracker(int fd
)
271 * Ensure the tracker is initialized when called from
274 lttng_ust_fd_tracker_init();
275 assert(URCU_TLS(ust_fd_mutex_nest
));
278 ret
= dup_std_fd(fd
);
285 /* Trying to add an fd which we can not accommodate. */
286 assert(IS_FD_VALID(fd
));
287 /* Setting an fd that's already set. */
288 assert(!IS_FD_SET(fd
, lttng_fd_set
));
290 ADD_FD_TO_SET(fd
, lttng_fd_set
);
297 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
298 * Has strict checking for fd validity.
300 void lttng_ust_delete_fd_from_tracker(int fd
)
303 * Ensure the tracker is initialized when called from
306 lttng_ust_fd_tracker_init();
308 assert(URCU_TLS(ust_fd_mutex_nest
));
309 /* Not a valid fd. */
310 assert(IS_FD_VALID(fd
));
311 /* Deleting an fd which was not set. */
312 assert(IS_FD_SET(fd
, lttng_fd_set
));
314 DEL_FD_FROM_SET(fd
, lttng_fd_set
);
318 * Interface allowing applications to close arbitrary file descriptors.
319 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
320 * instead of closing it if it is the case.
322 int lttng_ust_safe_close_fd(int fd
, int (*close_cb
)(int fd
))
326 lttng_ust_fd_tracker_alloc_tls();
329 * Ensure the tracker is initialized when called from
332 lttng_ust_fd_tracker_init();
335 * If called from lttng-ust, we directly call close without
336 * validating whether the FD is part of the tracked set.
338 if (URCU_TLS(ust_fd_mutex_nest
))
341 lttng_ust_lock_fd_tracker();
342 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
348 lttng_ust_unlock_fd_tracker();
354 * Interface allowing applications to close arbitrary streams.
355 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
356 * instead of closing it if it is the case.
358 int lttng_ust_safe_fclose_stream(FILE *stream
, int (*fclose_cb
)(FILE *stream
))
362 lttng_ust_fd_tracker_alloc_tls();
365 * Ensure the tracker is initialized when called from
368 lttng_ust_fd_tracker_init();
371 * If called from lttng-ust, we directly call fclose without
372 * validating whether the FD is part of the tracked set.
374 if (URCU_TLS(ust_fd_mutex_nest
))
375 return fclose_cb(stream
);
379 lttng_ust_lock_fd_tracker();
380 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
384 ret
= fclose_cb(stream
);
386 lttng_ust_unlock_fd_tracker();
392 static void set_close_success(int *p
)
396 static int test_close_success(const int *p
)
401 static void set_close_success(int *p
__attribute__((unused
)))
404 static int test_close_success(const int *p
__attribute__((unused
)))
411 * Implement helper for closefrom() override.
413 int lttng_ust_safe_closefrom_fd(int lowfd
, int (*close_cb
)(int fd
))
415 int ret
= 0, close_success
= 0, i
;
417 lttng_ust_fd_tracker_alloc_tls();
420 * Ensure the tracker is initialized when called from
423 lttng_ust_fd_tracker_init();
427 * NetBSD return EBADF if fd is invalid.
434 * If called from lttng-ust, we directly call close without
435 * validating whether the FD is part of the tracked set.
437 if (URCU_TLS(ust_fd_mutex_nest
)) {
438 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
439 if (close_cb(i
) < 0) {
449 set_close_success(&close_success
);
452 lttng_ust_lock_fd_tracker();
453 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
454 if (IS_FD_VALID(i
) && IS_FD_SET(i
, lttng_fd_set
))
456 if (close_cb(i
) < 0) {
463 lttng_ust_unlock_fd_tracker();
467 set_close_success(&close_success
);
469 lttng_ust_unlock_fd_tracker();
471 if (!test_close_success(&close_success
)) {
473 * OpenBSD return EBADF if fd is greater than all open