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)
51 /* Check fd validity before calling these. */
52 #define ADD_FD_TO_SET(fd, fd_sets) \
53 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
54 #define IS_FD_SET(fd, fd_sets) \
55 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
56 #define DEL_FD_FROM_SET(fd, fd_sets) \
57 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
60 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
61 * within the libc dl lock. Therefore, we need to fixup the TLS before
62 * nesting into this lock.
64 static pthread_mutex_t ust_safe_guard_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
66 * Track whether we are within lttng-ust or application, for close
67 * system call override by LD_PRELOAD library.
69 static DEFINE_URCU_TLS(int, thread_fd_tracking
);
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(thread_fd_tracking
)));
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 URCU_TLS(thread_fd_tracking
) = 1;
128 * Ensure the compiler don't move the store after the close()
129 * call in case close() would be marked as leaf.
132 pthread_mutex_lock(&ust_safe_guard_fd_mutex
);
135 void lttng_ust_unlock_fd_tracker(void)
137 pthread_mutex_unlock(&ust_safe_guard_fd_mutex
);
139 * Ensure the compiler don't move the store before the close()
140 * call, in case close() would be marked as leaf.
143 URCU_TLS(thread_fd_tracking
) = 0;
147 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
148 * Has strict checking of fd validity.
150 void lttng_ust_add_fd_to_tracker(int fd
)
153 * Ensure the tracker is initialized when called from
156 lttng_ust_init_fd_tracker();
158 assert(URCU_TLS(thread_fd_tracking
));
159 /* Trying to add an fd which we can not accommodate. */
160 assert(IS_FD_VALID(fd
));
161 /* Setting an fd thats already set. */
162 assert(!IS_FD_SET(fd
, lttng_fd_set
));
164 ADD_FD_TO_SET(fd
, lttng_fd_set
);
168 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
169 * Has strict checking for fd validity.
171 void lttng_ust_delete_fd_from_tracker(int fd
)
174 * Ensure the tracker is initialized when called from
177 lttng_ust_init_fd_tracker();
179 assert(URCU_TLS(thread_fd_tracking
));
180 /* Not a valid fd. */
181 assert(IS_FD_VALID(fd
));
182 /* Deleting an fd which was not set. */
183 assert(IS_FD_SET(fd
, lttng_fd_set
));
185 DEL_FD_FROM_SET(fd
, lttng_fd_set
);
189 * Interface allowing applications to close arbitrary file descriptors.
190 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
191 * instead of closing it if it is the case.
193 int lttng_ust_safe_close_fd(int fd
, int (*close_cb
)(int fd
))
197 lttng_ust_fixup_fd_tracker_tls();
200 * Ensure the tracker is initialized when called from
203 lttng_ust_init_fd_tracker();
206 * If called from lttng-ust, we directly call close without
207 * validating whether the FD is part of the tracked set.
209 if (URCU_TLS(thread_fd_tracking
))
212 lttng_ust_lock_fd_tracker();
213 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
219 lttng_ust_unlock_fd_tracker();
225 * Interface allowing applications to close arbitrary streams.
226 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
227 * instead of closing it if it is the case.
229 int lttng_ust_safe_fclose_stream(FILE *stream
, int (*fclose_cb
)(FILE *stream
))
233 lttng_ust_fixup_fd_tracker_tls();
236 * Ensure the tracker is initialized when called from
239 lttng_ust_init_fd_tracker();
242 * If called from lttng-ust, we directly call fclose without
243 * validating whether the FD is part of the tracked set.
245 if (URCU_TLS(thread_fd_tracking
))
246 return fclose_cb(stream
);
250 lttng_ust_lock_fd_tracker();
251 if (IS_FD_VALID(fd
) && IS_FD_SET(fd
, lttng_fd_set
)) {
255 ret
= fclose_cb(stream
);
257 lttng_ust_unlock_fd_tracker();
263 static void set_close_success(int *p
)
267 static int test_close_success(const int *p
)
272 static void set_close_success(int *p
__attribute__((unused
)))
275 static int test_close_success(const int *p
__attribute__((unused
)))
282 * Implement helper for closefrom() override.
284 int lttng_ust_safe_closefrom_fd(int lowfd
, int (*close_cb
)(int fd
))
286 int ret
= 0, close_success
= 0, i
;
288 lttng_ust_fixup_fd_tracker_tls();
291 * Ensure the tracker is initialized when called from
294 lttng_ust_init_fd_tracker();
298 * NetBSD return EBADF if fd is invalid.
305 * If called from lttng-ust, we directly call close without
306 * validating whether the FD is part of the tracked set.
308 if (URCU_TLS(thread_fd_tracking
)) {
309 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
310 if (close_cb(i
) < 0) {
320 set_close_success(&close_success
);
323 lttng_ust_lock_fd_tracker();
324 for (i
= lowfd
; i
< lttng_ust_max_fd
; i
++) {
325 if (IS_FD_VALID(i
) && IS_FD_SET(i
, lttng_fd_set
))
327 if (close_cb(i
) < 0) {
334 lttng_ust_unlock_fd_tracker();
338 set_close_success(&close_success
);
340 lttng_ust_unlock_fd_tracker();
342 if (!test_close_success(&close_success
)) {
344 * OpenBSD return EBADF if fd is greater than all open