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>
29 #include <ust-helper.h>
30 #include <lttng/ust-error.h>
31 #include <usterr-signal-safe.h>
33 #include "../liblttng-ust/compat.h"
34 #include "../liblttng-ust/lttng-tracer-core.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 fixup 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 * Cancel state when grabbing the ust_safe_guard_fd_mutex. Saved when
62 * locking, restored on unlock. Protected by ust_safe_guard_fd_mutex.
64 static int ust_safe_guard_saved_cancelstate
;
67 * Track whether we are within lttng-ust or application, for close
68 * system call override by LD_PRELOAD library. This also tracks whether
69 * we are invoking close() from a signal handler nested on an
72 static DEFINE_URCU_TLS(int, ust_fd_mutex_nest
);
74 /* fd_set used to book keep fd being used by lttng-ust. */
75 static fd_set
*lttng_fd_set
;
76 static int lttng_ust_max_fd
;
77 static int num_fd_sets
;
81 * Force a read (imply TLS fixup for dlopen) of TLS variables.
83 void lttng_ust_fixup_fd_tracker_tls(void)
85 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest
)));
89 * Allocate the fd set array based on the hard limit set for this
90 * process. This will be called during the constructor execution
91 * and will also be called in the child after fork via lttng_ust_init.
93 void lttng_ust_init_fd_tracker(void)
98 if (CMM_LOAD_SHARED(init_done
))
101 memset(&rlim
, 0, sizeof(rlim
));
102 /* Get the current possible max number of fd for this process. */
103 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0)
106 * FD set array size determined using the hard limit. Even if
107 * the process wishes to increase its limit using setrlimit, it
108 * can only do so with the softlimit which will be less than the
111 lttng_ust_max_fd
= rlim
.rlim_max
;
112 num_fd_sets
= lttng_ust_max_fd
/ FD_SETSIZE
;
113 if (lttng_ust_max_fd
% FD_SETSIZE
)
115 if (lttng_fd_set
!= NULL
) {
119 lttng_fd_set
= malloc(num_fd_sets
* (sizeof(fd_set
)));
122 for (i
= 0; i
< num_fd_sets
; i
++)
123 FD_ZERO((<tng_fd_set
[i
]));
124 CMM_STORE_SHARED(init_done
, 1);
127 void lttng_ust_lock_fd_tracker(void)
129 sigset_t sig_all_blocked
, orig_mask
;
132 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
134 ERR("pthread_setcancelstate: %s", strerror(ret
));
136 sigfillset(&sig_all_blocked
);
137 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
139 ERR("pthread_sigmask: %s", strerror(ret
));
141 if (!URCU_TLS(ust_fd_mutex_nest
)++) {
143 * Ensure the compiler don't move the store after the close()
144 * call in case close() would be marked as leaf.
147 pthread_mutex_lock(&ust_safe_guard_fd_mutex
);
148 ust_safe_guard_saved_cancelstate
= oldstate
;
150 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
152 ERR("pthread_sigmask: %s", strerror(ret
));
156 void lttng_ust_unlock_fd_tracker(void)
158 sigset_t sig_all_blocked
, orig_mask
;
159 int ret
, newstate
, oldstate
;
160 bool restore_cancel
= false;
162 sigfillset(&sig_all_blocked
);
163 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
165 ERR("pthread_sigmask: %s", strerror(ret
));
168 * Ensure the compiler don't move the store before the close()
169 * call, in case close() would be marked as leaf.
172 if (!--URCU_TLS(ust_fd_mutex_nest
)) {
173 newstate
= ust_safe_guard_saved_cancelstate
;
174 restore_cancel
= true;
175 pthread_mutex_unlock(&ust_safe_guard_fd_mutex
);
177 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
179 ERR("pthread_sigmask: %s", strerror(ret
));
181 if (restore_cancel
) {
182 ret
= pthread_setcancelstate(newstate
, &oldstate
);
184 ERR("pthread_setcancelstate: %s", strerror(ret
));
189 static int dup_std_fd(int fd
)
192 int fd_to_close
[STDERR_FILENO
+ 1];
193 int fd_to_close_count
= 0;
194 int dup_cmd
= F_DUPFD
; /* Default command */
197 if (!(IS_FD_STD(fd
))) {
198 /* Should not be here */
203 /* Check for FD_CLOEXEC flag */
204 ret
= fcntl(fd
, F_GETFD
);
206 PERROR("fcntl on f_getfd");
211 if (ret
& FD_CLOEXEC
) {
212 dup_cmd
= F_DUPFD_CLOEXEC
;
216 for (i
= 0; i
< STDERR_FILENO
+ 1; i
++) {
217 ret
= fcntl(fd
, dup_cmd
, 0);
219 PERROR("fcntl dup fd");
223 if (!(IS_FD_STD(ret
))) {
224 /* fd is outside of STD range, use it. */
226 /* Close fd received as argument. */
232 fd_to_close
[i
] = ret
;
236 /* Close intermediary fds */
237 for (i
= 0; i
< fd_to_close_count
; i
++) {
238 ret
= close(fd_to_close
[i
]);
240 PERROR("close on temporary fd: %d.", fd_to_close
[i
]);
242 * Not using an abort here would yield a complicated
243 * error handling for the caller. If a failure occurs
244 * here, the system is already in a bad state.
256 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
257 * Has strict checking of fd validity.
259 * If fd <= 2, dup the fd until fd > 2. This enables us to bypass
260 * problems that can be encountered if UST uses stdin, stdout, stderr
261 * fds for internal use (daemon etc.). This can happen if the
262 * application closes either of those file descriptors. Intermediary fds
263 * are closed as needed.
265 * Return -1 on error.
268 int lttng_ust_add_fd_to_tracker(int fd
)
272 * Ensure the tracker is initialized when called from
275 lttng_ust_init_fd_tracker();
276 assert(URCU_TLS(ust_fd_mutex_nest
));
279 ret
= dup_std_fd(fd
);
286 /* Trying to add an fd which we can not accommodate. */
287 assert(IS_FD_VALID(fd
));
288 /* Setting an fd thats already set. */
289 assert(!IS_FD_SET(fd
, lttng_fd_set
));
291 ADD_FD_TO_SET(fd
, lttng_fd_set
);
298 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
299 * Has strict checking for fd validity.
301 void lttng_ust_delete_fd_from_tracker(int fd
)
304 * Ensure the tracker is initialized when called from
307 lttng_ust_init_fd_tracker();
309 assert(URCU_TLS(ust_fd_mutex_nest
));
310 /* Not a valid fd. */
311 assert(IS_FD_VALID(fd
));
312 /* Deleting an fd which was not set. */
313 assert(IS_FD_SET(fd
, lttng_fd_set
));
315 DEL_FD_FROM_SET(fd
, lttng_fd_set
);
319 * Interface allowing applications to close arbitrary file descriptors.
320 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
321 * instead of closing it if it is the case.
323 int lttng_ust_safe_close_fd(int fd
, int (*close_cb
)(int fd
))
327 lttng_ust_fixup_fd_tracker_tls();
330 * Ensure the tracker is initialized when called from
333 lttng_ust_init_fd_tracker();
336 * If called from lttng-ust, we directly call close without
337 * validating whether the FD is part of the tracked set.
339 if (URCU_TLS(ust_fd_mutex_nest
))
342 lttng_ust_lock_fd_tracker();
343 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
349 lttng_ust_unlock_fd_tracker();
355 * Interface allowing applications to close arbitrary streams.
356 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
357 * instead of closing it if it is the case.
359 int lttng_ust_safe_fclose_stream(FILE *stream
, int (*fclose_cb
)(FILE *stream
))
363 lttng_ust_fixup_fd_tracker_tls();
366 * Ensure the tracker is initialized when called from
369 lttng_ust_init_fd_tracker();
372 * If called from lttng-ust, we directly call fclose without
373 * validating whether the FD is part of the tracked set.
375 if (URCU_TLS(ust_fd_mutex_nest
))
376 return fclose_cb(stream
);
380 lttng_ust_lock_fd_tracker();
381 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
385 ret
= fclose_cb(stream
);
387 lttng_ust_unlock_fd_tracker();
393 static void set_close_success(int *p
)
397 static int test_close_success(const int *p
)
402 static void set_close_success(int *p
__attribute__((unused
)))
405 static int test_close_success(const int *p
__attribute__((unused
)))
412 * Implement helper for closefrom() override.
414 int lttng_ust_safe_closefrom_fd(int lowfd
, int (*close_cb
)(int fd
))
416 int ret
= 0, close_success
= 0, i
;
418 lttng_ust_fixup_fd_tracker_tls();
421 * Ensure the tracker is initialized when called from
424 lttng_ust_init_fd_tracker();
428 * NetBSD return EBADF if fd is invalid.
435 * If called from lttng-ust, we directly call close without
436 * validating whether the FD is part of the tracked set.
438 if (URCU_TLS(ust_fd_mutex_nest
)) {
439 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
440 if (close_cb(i
) < 0) {
450 set_close_success(&close_success
);
453 lttng_ust_lock_fd_tracker();
454 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
455 if (IS_FD_VALID(i
) && IS_FD_SET(i
, lttng_fd_set
))
457 if (close_cb(i
) < 0) {
464 lttng_ust_unlock_fd_tracker();
468 set_close_success(&close_success
);
470 lttng_ust_unlock_fd_tracker();
472 if (!test_close_success(&close_success
)) {
474 * OpenBSD return EBADF if fd is greater than all open