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 /* Operations on the fd set. */
34 #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
35 #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
36 #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
37 #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
39 /* Check fd validity before calling these. */
40 #define ADD_FD_TO_SET(fd, fd_sets) \
41 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
42 #define IS_FD_SET(fd, fd_sets) \
43 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
44 #define DEL_FD_FROM_SET(fd, fd_sets) \
45 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
48 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
49 * within the libc dl lock. Therefore, we need to fixup the TLS before
50 * nesting into this lock.
52 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
53 * is also held across fork.
55 static pthread_mutex_t ust_safe_guard_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
58 * Cancel state when grabbing the ust_safe_guard_fd_mutex. Saved when
59 * locking, restored on unlock. Protected by ust_safe_guard_fd_mutex.
61 static int ust_safe_guard_saved_cancelstate
;
64 * Track whether we are within lttng-ust or application, for close
65 * system call override by LD_PRELOAD library. This also tracks whether
66 * we are invoking close() from a signal handler nested on an
69 static DEFINE_URCU_TLS(int, ust_fd_mutex_nest
);
71 /* fd_set used to book keep fd being used by lttng-ust. */
72 static fd_set
*lttng_fd_set
;
73 static int lttng_ust_max_fd
;
74 static int num_fd_sets
;
78 * Force a read (imply TLS fixup for dlopen) of TLS variables.
80 void lttng_ust_fixup_fd_tracker_tls(void)
82 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest
)));
86 * Allocate the fd set array based on the hard limit set for this
87 * process. This will be called during the constructor execution
88 * and will also be called in the child after fork via lttng_ust_init.
90 void lttng_ust_init_fd_tracker(void)
95 if (CMM_LOAD_SHARED(init_done
))
98 memset(&rlim
, 0, sizeof(rlim
));
99 /* Get the current possible max number of fd for this process. */
100 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0)
103 * FD set array size determined using the hard limit. Even if
104 * the process wishes to increase its limit using setrlimit, it
105 * can only do so with the softlimit which will be less than the
108 lttng_ust_max_fd
= rlim
.rlim_max
;
109 num_fd_sets
= lttng_ust_max_fd
/ FD_SETSIZE
;
110 if (lttng_ust_max_fd
% FD_SETSIZE
)
112 if (lttng_fd_set
!= NULL
) {
116 lttng_fd_set
= malloc(num_fd_sets
* (sizeof(fd_set
)));
119 for (i
= 0; i
< num_fd_sets
; i
++)
120 FD_ZERO((<tng_fd_set
[i
]));
121 CMM_STORE_SHARED(init_done
, 1);
124 void lttng_ust_lock_fd_tracker(void)
126 sigset_t sig_all_blocked
, orig_mask
;
129 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
131 ERR("pthread_setcancelstate: %s", strerror(ret
));
133 sigfillset(&sig_all_blocked
);
134 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
136 ERR("pthread_sigmask: %s", strerror(ret
));
138 if (!URCU_TLS(ust_fd_mutex_nest
)++) {
140 * Ensure the compiler don't move the store after the close()
141 * call in case close() would be marked as leaf.
144 pthread_mutex_lock(&ust_safe_guard_fd_mutex
);
145 ust_safe_guard_saved_cancelstate
= oldstate
;
147 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
149 ERR("pthread_sigmask: %s", strerror(ret
));
153 void lttng_ust_unlock_fd_tracker(void)
155 sigset_t sig_all_blocked
, orig_mask
;
156 int ret
, newstate
, oldstate
;
157 bool restore_cancel
= false;
159 sigfillset(&sig_all_blocked
);
160 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
162 ERR("pthread_sigmask: %s", strerror(ret
));
165 * Ensure the compiler don't move the store before the close()
166 * call, in case close() would be marked as leaf.
169 if (!--URCU_TLS(ust_fd_mutex_nest
)) {
170 newstate
= ust_safe_guard_saved_cancelstate
;
171 restore_cancel
= true;
172 pthread_mutex_unlock(&ust_safe_guard_fd_mutex
);
174 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
176 ERR("pthread_sigmask: %s", strerror(ret
));
178 if (restore_cancel
) {
179 ret
= pthread_setcancelstate(newstate
, &oldstate
);
181 ERR("pthread_setcancelstate: %s", strerror(ret
));
186 static int dup_std_fd(int fd
)
189 int fd_to_close
[STDERR_FILENO
+ 1];
190 int fd_to_close_count
= 0;
191 int dup_cmd
= F_DUPFD
; /* Default command */
194 if (!(IS_FD_STD(fd
))) {
195 /* Should not be here */
200 /* Check for FD_CLOEXEC flag */
201 ret
= fcntl(fd
, F_GETFD
);
203 PERROR("fcntl on f_getfd");
208 if (ret
& FD_CLOEXEC
) {
209 dup_cmd
= F_DUPFD_CLOEXEC
;
213 for (i
= 0; i
< STDERR_FILENO
+ 1; i
++) {
214 ret
= fcntl(fd
, dup_cmd
, 0);
216 PERROR("fcntl dup fd");
220 if (!(IS_FD_STD(ret
))) {
221 /* fd is outside of STD range, use it. */
223 /* Close fd received as argument. */
229 fd_to_close
[i
] = ret
;
233 /* Close intermediary fds */
234 for (i
= 0; i
< fd_to_close_count
; i
++) {
235 ret
= close(fd_to_close
[i
]);
237 PERROR("close on temporary fd: %d.", fd_to_close
[i
]);
239 * Not using an abort here would yield a complicated
240 * error handling for the caller. If a failure occurs
241 * here, the system is already in a bad state.
253 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
254 * Has strict checking of fd validity.
256 * If fd <= 2, dup the fd until fd > 2. This enables us to bypass
257 * problems that can be encountered if UST uses stdin, stdout, stderr
258 * fds for internal use (daemon etc.). This can happen if the
259 * application closes either of those file descriptors. Intermediary fds
260 * are closed as needed.
262 * Return -1 on error.
265 int lttng_ust_add_fd_to_tracker(int fd
)
269 * Ensure the tracker is initialized when called from
272 lttng_ust_init_fd_tracker();
273 assert(URCU_TLS(ust_fd_mutex_nest
));
276 ret
= dup_std_fd(fd
);
283 /* Trying to add an fd which we can not accommodate. */
284 assert(IS_FD_VALID(fd
));
285 /* Setting an fd thats already set. */
286 assert(!IS_FD_SET(fd
, lttng_fd_set
));
288 ADD_FD_TO_SET(fd
, lttng_fd_set
);
295 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
296 * Has strict checking for fd validity.
298 void lttng_ust_delete_fd_from_tracker(int fd
)
301 * Ensure the tracker is initialized when called from
304 lttng_ust_init_fd_tracker();
306 assert(URCU_TLS(ust_fd_mutex_nest
));
307 /* Not a valid fd. */
308 assert(IS_FD_VALID(fd
));
309 /* Deleting an fd which was not set. */
310 assert(IS_FD_SET(fd
, lttng_fd_set
));
312 DEL_FD_FROM_SET(fd
, lttng_fd_set
);
316 * Interface allowing applications to close arbitrary file descriptors.
317 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
318 * instead of closing it if it is the case.
320 int lttng_ust_safe_close_fd(int fd
, int (*close_cb
)(int fd
))
324 lttng_ust_fixup_fd_tracker_tls();
327 * Ensure the tracker is initialized when called from
330 lttng_ust_init_fd_tracker();
333 * If called from lttng-ust, we directly call close without
334 * validating whether the FD is part of the tracked set.
336 if (URCU_TLS(ust_fd_mutex_nest
))
339 lttng_ust_lock_fd_tracker();
340 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
346 lttng_ust_unlock_fd_tracker();
352 * Interface allowing applications to close arbitrary streams.
353 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
354 * instead of closing it if it is the case.
356 int lttng_ust_safe_fclose_stream(FILE *stream
, int (*fclose_cb
)(FILE *stream
))
360 lttng_ust_fixup_fd_tracker_tls();
363 * Ensure the tracker is initialized when called from
366 lttng_ust_init_fd_tracker();
369 * If called from lttng-ust, we directly call fclose without
370 * validating whether the FD is part of the tracked set.
372 if (URCU_TLS(ust_fd_mutex_nest
))
373 return fclose_cb(stream
);
377 lttng_ust_lock_fd_tracker();
378 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
382 ret
= fclose_cb(stream
);
384 lttng_ust_unlock_fd_tracker();
390 static void set_close_success(int *p
)
394 static int test_close_success(const int *p
)
399 static void set_close_success(int *p
__attribute__((unused
)))
402 static int test_close_success(const int *p
__attribute__((unused
)))
409 * Implement helper for closefrom() override.
411 int lttng_ust_safe_closefrom_fd(int lowfd
, int (*close_cb
)(int fd
))
413 int ret
= 0, close_success
= 0, i
;
415 lttng_ust_fixup_fd_tracker_tls();
418 * Ensure the tracker is initialized when called from
421 lttng_ust_init_fd_tracker();
425 * NetBSD return EBADF if fd is invalid.
432 * If called from lttng-ust, we directly call close without
433 * validating whether the FD is part of the tracked set.
435 if (URCU_TLS(ust_fd_mutex_nest
)) {
436 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
437 if (close_cb(i
) < 0) {
447 set_close_success(&close_success
);
450 lttng_ust_lock_fd_tracker();
451 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
452 if (IS_FD_VALID(i
) && IS_FD_SET(i
, lttng_fd_set
))
454 if (close_cb(i
) < 0) {
461 lttng_ust_unlock_fd_tracker();
465 set_close_success(&close_success
);
467 lttng_ust_unlock_fd_tracker();
469 if (!test_close_success(&close_success
)) {
471 * OpenBSD return EBADF if fd is greater than all open