2 * Copyright (C) 2016 - Aravind HT <aravind.ht@gmail.com>
3 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; only
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
30 #include <sys/select.h>
31 #include <sys/resource.h>
37 #include <urcu/compiler.h>
38 #include <urcu/tls-compat.h>
39 #include <urcu/system.h>
43 #include <lttng/ust-error.h>
44 #include <usterr-signal-safe.h>
45 #include <lttng/ust-cancelstate.h>
47 #include "../liblttng-ust/compat.h"
49 /* Operations on the fd set. */
50 #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
51 #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
52 #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
53 #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
55 /* Check fd validity before calling these. */
56 #define ADD_FD_TO_SET(fd, fd_sets) \
57 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
58 #define IS_FD_SET(fd, fd_sets) \
59 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
60 #define DEL_FD_FROM_SET(fd, fd_sets) \
61 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
64 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
65 * within the libc dl lock. Therefore, we need to fixup the TLS before
66 * nesting into this lock.
68 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
69 * is also held across fork.
71 static pthread_mutex_t ust_safe_guard_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
74 * Track whether we are within lttng-ust or application, for close
75 * system call override by LD_PRELOAD library. This also tracks whether
76 * we are invoking close() from a signal handler nested on an
79 static DEFINE_URCU_TLS(int, ust_fd_mutex_nest
);
81 /* fd_set used to book keep fd being used by lttng-ust. */
82 static fd_set
*lttng_fd_set
;
83 static int lttng_ust_max_fd
;
84 static int num_fd_sets
;
88 * Force a read (imply TLS fixup for dlopen) of TLS variables.
90 void lttng_ust_fixup_fd_tracker_tls(void)
92 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest
)));
96 * Allocate the fd set array based on the hard limit set for this
97 * process. This will be called during the constructor execution
98 * and will also be called in the child after fork via lttng_ust_init.
100 void lttng_ust_init_fd_tracker(void)
105 if (CMM_LOAD_SHARED(init_done
))
108 memset(&rlim
, 0, sizeof(rlim
));
109 /* Get the current possible max number of fd for this process. */
110 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0)
113 * FD set array size determined using the hard limit. Even if
114 * the process wishes to increase its limit using setrlimit, it
115 * can only do so with the softlimit which will be less than the
118 lttng_ust_max_fd
= rlim
.rlim_max
;
119 num_fd_sets
= lttng_ust_max_fd
/ FD_SETSIZE
;
120 if (lttng_ust_max_fd
% FD_SETSIZE
)
122 if (lttng_fd_set
!= NULL
) {
126 lttng_fd_set
= malloc(num_fd_sets
* (sizeof(fd_set
)));
129 for (i
= 0; i
< num_fd_sets
; i
++)
130 FD_ZERO((<tng_fd_set
[i
]));
131 CMM_STORE_SHARED(init_done
, 1);
134 void lttng_ust_lock_fd_tracker(void)
136 sigset_t sig_all_blocked
, orig_mask
;
139 if (lttng_ust_cancelstate_disable_push()) {
140 ERR("lttng_ust_cancelstate_disable_push");
142 sigfillset(&sig_all_blocked
);
143 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
145 ERR("pthread_sigmask: %s", strerror(ret
));
147 if (!URCU_TLS(ust_fd_mutex_nest
)++) {
149 * Ensure the compiler don't move the store after the close()
150 * call in case close() would be marked as leaf.
153 pthread_mutex_lock(&ust_safe_guard_fd_mutex
);
155 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
157 ERR("pthread_sigmask: %s", strerror(ret
));
161 void lttng_ust_unlock_fd_tracker(void)
163 sigset_t sig_all_blocked
, orig_mask
;
166 sigfillset(&sig_all_blocked
);
167 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
169 ERR("pthread_sigmask: %s", strerror(ret
));
172 * Ensure the compiler don't move the store before the close()
173 * call, in case close() would be marked as leaf.
176 if (!--URCU_TLS(ust_fd_mutex_nest
)) {
177 pthread_mutex_unlock(&ust_safe_guard_fd_mutex
);
179 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
181 ERR("pthread_sigmask: %s", strerror(ret
));
183 if (lttng_ust_cancelstate_disable_pop()) {
184 ERR("lttng_ust_cancelstate_disable_pop");
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_init_fd_tracker();
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 thats 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_init_fd_tracker();
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_fixup_fd_tracker_tls();
329 * Ensure the tracker is initialized when called from
332 lttng_ust_init_fd_tracker();
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_fixup_fd_tracker_tls();
365 * Ensure the tracker is initialized when called from
368 lttng_ust_init_fd_tracker();
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_fixup_fd_tracker_tls();
420 * Ensure the tracker is initialized when called from
423 lttng_ust_init_fd_tracker();
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