X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=liblttng-ust-comm%2Flttng-ust-fd-tracker.c;h=d56288e370470e1deb94a25dc5603048c75f03aa;hb=52a20dc78a372394471e3394a8d335ef00cebad3;hp=04ae2391f034f7f8a23e92aef97d3e4ace8518a0;hpb=553bbf7f38652084ed7966c7817b8ccb372b14e1;p=lttng-ust.git diff --git a/liblttng-ust-comm/lttng-ust-fd-tracker.c b/liblttng-ust-comm/lttng-ust-fd-tracker.c index 04ae2391..d56288e3 100644 --- a/liblttng-ust-comm/lttng-ust-fd-tracker.c +++ b/liblttng-ust-comm/lttng-ust-fd-tracker.c @@ -65,7 +65,7 @@ static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER; * Track whether we are within lttng-ust or application, for close * system call override by LD_PRELOAD library. */ -static DEFINE_URCU_TLS_IE(int, thread_fd_tracking); +static DEFINE_URCU_TLS(int, thread_fd_tracking); /* fd_set used to book keep fd being used by lttng-ust. */ static fd_set *lttng_fd_set; @@ -197,6 +197,38 @@ int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int fd)) return ret; } +/* + * Interface allowing applications to close arbitrary streams. + * We check if it is owned by lttng-ust, and return -1, errno=EBADF + * instead of closing it if it is the case. + */ +int lttng_ust_safe_fclose_stream(FILE *stream, int (*fclose_cb)(FILE *stream)) +{ + int ret = 0, fd; + + lttng_ust_fixup_fd_tracker_tls(); + + /* + * If called from lttng-ust, we directly call fclose without + * validating whether the FD is part of the tracked set. + */ + if (URCU_TLS(thread_fd_tracking)) + return fclose_cb(stream); + + fd = fileno(stream); + + lttng_ust_lock_fd_tracker(); + if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) { + ret = -1; + errno = EBADF; + } else { + ret = fclose_cb(stream); + } + lttng_ust_unlock_fd_tracker(); + + return ret; +} + #ifdef __OpenBSD__ static void set_close_success(int *p) {