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>
35 #include <urcu/compiler.h>
36 #include <urcu/tls-compat.h>
37 #include <urcu/system.h>
41 #include <lttng/ust-error.h>
42 #include <usterr-signal-safe.h>
44 #include "../liblttng-ust/compat.h"
46 /* Operations on the fd set. */
47 #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
48 #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
49 #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
50 #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
52 /* Check fd validity before calling these. */
53 #define ADD_FD_TO_SET(fd, fd_sets) \
54 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
55 #define IS_FD_SET(fd, fd_sets) \
56 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
57 #define DEL_FD_FROM_SET(fd, fd_sets) \
58 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
61 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
62 * within the libc dl lock. Therefore, we need to fixup the TLS before
63 * nesting into this lock.
65 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
66 * is also held across fork.
68 static pthread_mutex_t ust_safe_guard_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
71 * Cancel state when grabbing the ust_safe_guard_fd_mutex. Saved when
72 * locking, restored on unlock. Protected by ust_safe_guard_fd_mutex.
74 static int ust_safe_guard_saved_cancelstate
;
77 * Track whether we are within lttng-ust or application, for close
78 * system call override by LD_PRELOAD library.
80 static DEFINE_URCU_TLS(int, thread_fd_tracking
);
82 /* fd_set used to book keep fd being used by lttng-ust. */
83 static fd_set
*lttng_fd_set
;
84 static int lttng_ust_max_fd
;
85 static int num_fd_sets
;
89 * Force a read (imply TLS fixup for dlopen) of TLS variables.
91 void lttng_ust_fixup_fd_tracker_tls(void)
93 asm volatile ("" : : "m" (URCU_TLS(thread_fd_tracking
)));
97 * Allocate the fd set array based on the hard limit set for this
98 * process. This will be called during the constructor execution
99 * and will also be called in the child after fork via lttng_ust_init.
101 void lttng_ust_init_fd_tracker(void)
106 if (CMM_LOAD_SHARED(init_done
))
109 memset(&rlim
, 0, sizeof(rlim
));
110 /* Get the current possible max number of fd for this process. */
111 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0)
114 * FD set array size determined using the hard limit. Even if
115 * the process wishes to increase its limit using setrlimit, it
116 * can only do so with the softlimit which will be less than the
119 lttng_ust_max_fd
= rlim
.rlim_max
;
120 num_fd_sets
= lttng_ust_max_fd
/ FD_SETSIZE
;
121 if (lttng_ust_max_fd
% FD_SETSIZE
)
123 if (lttng_fd_set
!= NULL
) {
127 lttng_fd_set
= malloc(num_fd_sets
* (sizeof(fd_set
)));
130 for (i
= 0; i
< num_fd_sets
; i
++)
131 FD_ZERO((<tng_fd_set
[i
]));
132 CMM_STORE_SHARED(init_done
, 1);
135 void lttng_ust_lock_fd_tracker(void)
139 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
141 ERR("pthread_setcancelstate: %s", strerror(ret
));
143 URCU_TLS(thread_fd_tracking
) = 1;
145 * Ensure the compiler don't move the store after the close()
146 * call in case close() would be marked as leaf.
149 pthread_mutex_lock(&ust_safe_guard_fd_mutex
);
150 ust_safe_guard_saved_cancelstate
= oldstate
;
153 void lttng_ust_unlock_fd_tracker(void)
155 int ret
, newstate
, oldstate
;
157 newstate
= ust_safe_guard_saved_cancelstate
;
158 pthread_mutex_unlock(&ust_safe_guard_fd_mutex
);
160 * Ensure the compiler don't move the store before the close()
161 * call, in case close() would be marked as leaf.
164 URCU_TLS(thread_fd_tracking
) = 0;
165 ret
= pthread_setcancelstate(newstate
, &oldstate
);
167 ERR("pthread_setcancelstate: %s", strerror(ret
));
171 static int dup_std_fd(int fd
)
174 int fd_to_close
[STDERR_FILENO
+ 1];
175 int fd_to_close_count
= 0;
176 int dup_cmd
= F_DUPFD
; /* Default command */
179 if (!(IS_FD_STD(fd
))) {
180 /* Should not be here */
185 /* Check for FD_CLOEXEC flag */
186 ret
= fcntl(fd
, F_GETFD
);
188 PERROR("fcntl on f_getfd");
193 if (ret
& FD_CLOEXEC
) {
194 dup_cmd
= F_DUPFD_CLOEXEC
;
198 for (i
= 0; i
< STDERR_FILENO
+ 1; i
++) {
199 ret
= fcntl(fd
, dup_cmd
, 0);
201 PERROR("fcntl dup fd");
205 if (!(IS_FD_STD(ret
))) {
206 /* fd is outside of STD range, use it. */
208 /* Close fd received as argument. */
214 fd_to_close
[i
] = ret
;
218 /* Close intermediary fds */
219 for (i
= 0; i
< fd_to_close_count
; i
++) {
220 ret
= close(fd_to_close
[i
]);
222 PERROR("close on temporary fd: %d.", fd_to_close
[i
]);
224 * Not using an abort here would yield a complicated
225 * error handling for the caller. If a failure occurs
226 * here, the system is already in a bad state.
238 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
239 * Has strict checking of fd validity.
241 * If fd <= 2, dup the fd until fd > 2. This enables us to bypass
242 * problems that can be encountered if UST uses stdin, stdout, stderr
243 * fds for internal use (daemon etc.). This can happen if the
244 * application closes either of those file descriptors. Intermediary fds
245 * are closed as needed.
247 * Return -1 on error.
250 int lttng_ust_add_fd_to_tracker(int fd
)
254 * Ensure the tracker is initialized when called from
257 lttng_ust_init_fd_tracker();
258 assert(URCU_TLS(thread_fd_tracking
));
261 ret
= dup_std_fd(fd
);
268 /* Trying to add an fd which we can not accommodate. */
269 assert(IS_FD_VALID(fd
));
270 /* Setting an fd thats already set. */
271 assert(!IS_FD_SET(fd
, lttng_fd_set
));
273 ADD_FD_TO_SET(fd
, lttng_fd_set
);
280 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
281 * Has strict checking for fd validity.
283 void lttng_ust_delete_fd_from_tracker(int fd
)
286 * Ensure the tracker is initialized when called from
289 lttng_ust_init_fd_tracker();
291 assert(URCU_TLS(thread_fd_tracking
));
292 /* Not a valid fd. */
293 assert(IS_FD_VALID(fd
));
294 /* Deleting an fd which was not set. */
295 assert(IS_FD_SET(fd
, lttng_fd_set
));
297 DEL_FD_FROM_SET(fd
, lttng_fd_set
);
301 * Interface allowing applications to close arbitrary file descriptors.
302 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
303 * instead of closing it if it is the case.
305 int lttng_ust_safe_close_fd(int fd
, int (*close_cb
)(int fd
))
309 lttng_ust_fixup_fd_tracker_tls();
312 * Ensure the tracker is initialized when called from
315 lttng_ust_init_fd_tracker();
318 * If called from lttng-ust, we directly call close without
319 * validating whether the FD is part of the tracked set.
321 if (URCU_TLS(thread_fd_tracking
))
324 lttng_ust_lock_fd_tracker();
325 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
331 lttng_ust_unlock_fd_tracker();
337 * Interface allowing applications to close arbitrary streams.
338 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
339 * instead of closing it if it is the case.
341 int lttng_ust_safe_fclose_stream(FILE *stream
, int (*fclose_cb
)(FILE *stream
))
345 lttng_ust_fixup_fd_tracker_tls();
348 * Ensure the tracker is initialized when called from
351 lttng_ust_init_fd_tracker();
354 * If called from lttng-ust, we directly call fclose without
355 * validating whether the FD is part of the tracked set.
357 if (URCU_TLS(thread_fd_tracking
))
358 return fclose_cb(stream
);
362 lttng_ust_lock_fd_tracker();
363 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
367 ret
= fclose_cb(stream
);
369 lttng_ust_unlock_fd_tracker();
375 static void set_close_success(int *p
)
379 static int test_close_success(const int *p
)
384 static void set_close_success(int *p
__attribute__((unused
)))
387 static int test_close_success(const int *p
__attribute__((unused
)))
394 * Implement helper for closefrom() override.
396 int lttng_ust_safe_closefrom_fd(int lowfd
, int (*close_cb
)(int fd
))
398 int ret
= 0, close_success
= 0, i
;
400 lttng_ust_fixup_fd_tracker_tls();
403 * Ensure the tracker is initialized when called from
406 lttng_ust_init_fd_tracker();
410 * NetBSD return EBADF if fd is invalid.
417 * If called from lttng-ust, we directly call close without
418 * validating whether the FD is part of the tracked set.
420 if (URCU_TLS(thread_fd_tracking
)) {
421 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
422 if (close_cb(i
) < 0) {
432 set_close_success(&close_success
);
435 lttng_ust_lock_fd_tracker();
436 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
437 if (IS_FD_VALID(i
) && IS_FD_SET(i
, lttng_fd_set
))
439 if (close_cb(i
) < 0) {
446 lttng_ust_unlock_fd_tracker();
450 set_close_success(&close_success
);
452 lttng_ust_unlock_fd_tracker();
454 if (!test_close_success(&close_success
)) {
456 * OpenBSD return EBADF if fd is greater than all open