Commit | Line | Data |
---|---|---|
6548fca4 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
6548fca4 | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2016 Aravind HT <aravind.ht@gmail.com> |
5 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
6548fca4 MD |
6 | */ |
7 | ||
6548fca4 MD |
8 | #include <limits.h> |
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
12 | #include <sys/types.h> | |
13 | #include <unistd.h> | |
14 | #include <assert.h> | |
15 | #include <errno.h> | |
16 | #include <fcntl.h> | |
17 | #include <sys/select.h> | |
18 | #include <sys/resource.h> | |
19 | #include <sys/time.h> | |
20 | #include <fcntl.h> | |
21 | #include <pthread.h> | |
96a6162e MD |
22 | #include <signal.h> |
23 | #include <stdbool.h> | |
aaa692c8 | 24 | #include <dlfcn.h> |
6548fca4 MD |
25 | #include <urcu/compiler.h> |
26 | #include <urcu/tls-compat.h> | |
7d34f27d | 27 | #include <urcu/system.h> |
6548fca4 | 28 | |
9d315d6d MJ |
29 | #include "common/ust-fd.h" |
30 | #include "common/macros.h" | |
6548fca4 | 31 | #include <lttng/ust-error.h> |
59e57035 | 32 | #include <lttng/ust-cancelstate.h> |
9d315d6d | 33 | #include "common/logging.h" |
6548fca4 | 34 | |
fca97dfd MJ |
35 | #include "lib/lttng-ust-common/fd-tracker.h" |
36 | ||
5f748425 MD |
37 | #define LTTNG_UST_DLSYM_FAILED_PTR 0x1 |
38 | ||
6548fca4 MD |
39 | /* Operations on the fd set. */ |
40 | #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd) | |
41 | #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE])) | |
42 | #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE) | |
f5c453e9 | 43 | #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO) |
6548fca4 MD |
44 | |
45 | /* Check fd validity before calling these. */ | |
46 | #define ADD_FD_TO_SET(fd, fd_sets) \ | |
47 | FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets)) | |
48 | #define IS_FD_SET(fd, fd_sets) \ | |
49 | FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets)) | |
50 | #define DEL_FD_FROM_SET(fd, fd_sets) \ | |
51 | FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets)) | |
52 | ||
53 | /* | |
54 | * Protect the lttng_fd_set. Nests within the ust_lock, and therefore | |
a9fd951a | 55 | * within the libc dl lock. Therefore, we need to allocate the TLS before |
6548fca4 | 56 | * nesting into this lock. |
c1be081a MD |
57 | * |
58 | * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex | |
59 | * is also held across fork. | |
6548fca4 MD |
60 | */ |
61 | static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER; | |
283f4bec | 62 | |
6548fca4 MD |
63 | /* |
64 | * Track whether we are within lttng-ust or application, for close | |
793d29c9 MD |
65 | * system call override by LD_PRELOAD library. This also tracks whether |
66 | * we are invoking close() from a signal handler nested on an | |
67 | * application thread. | |
6548fca4 | 68 | */ |
96a6162e | 69 | static DEFINE_URCU_TLS(int, ust_fd_mutex_nest); |
6548fca4 MD |
70 | |
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; | |
7d34f27d | 75 | static int init_done; |
6548fca4 MD |
76 | |
77 | /* | |
a9fd951a | 78 | * Force a read (imply TLS allocation for dlopen) of TLS variables. |
6548fca4 | 79 | */ |
a9fd951a | 80 | void lttng_ust_fd_tracker_alloc_tls(void) |
6548fca4 | 81 | { |
96a6162e | 82 | asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest))); |
6548fca4 MD |
83 | } |
84 | ||
5f748425 MD |
85 | #if !defined(LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS) |
86 | static int (*__lttng_ust_safe_close_fd)(int fd, int (*close_cb)(int fd)) = NULL; | |
87 | ||
88 | void *lttng_ust_safe_close_fd_init(void) | |
89 | { | |
90 | if (__lttng_ust_safe_close_fd == NULL) { | |
91 | __lttng_ust_safe_close_fd = dlsym(RTLD_DEFAULT, "lttng_ust_safe_close_fd"); | |
92 | ||
93 | if (__lttng_ust_safe_close_fd == NULL) { | |
94 | __lttng_ust_safe_close_fd = (void *) LTTNG_UST_DLSYM_FAILED_PTR; | |
95 | fprintf(stderr, "%s\n", dlerror()); | |
96 | } | |
97 | } | |
98 | ||
99 | return __lttng_ust_safe_close_fd; | |
100 | } | |
101 | ||
102 | static int lttng_ust_safe_close_fd_chain(int fd, int (*close_cb)(int fd)) | |
103 | { | |
104 | assert(__lttng_ust_safe_close_fd != NULL); | |
105 | if (__lttng_ust_safe_close_fd != (void *) LTTNG_UST_DLSYM_FAILED_PTR) { | |
106 | /* Chain on ust-2.12 preload */ | |
107 | return __lttng_ust_safe_close_fd(fd, close_cb); | |
108 | } else { | |
109 | /* Fallback to libc symbol */ | |
110 | return close_cb(fd); | |
111 | } | |
112 | } | |
113 | #else | |
114 | static int lttng_ust_safe_close_fd_chain(int fd, int (*close_cb)(int fd)) | |
115 | { | |
116 | return close_cb(fd); | |
117 | } | |
118 | #endif | |
119 | ||
120 | #if !defined(LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS) | |
121 | static int (*__lttng_ust_safe_fclose_stream)(FILE *stream, int (*fclose_cb)(FILE *stream)) = NULL; | |
122 | ||
123 | void *lttng_ust_safe_fclose_stream_init(void) | |
124 | { | |
125 | if (__lttng_ust_safe_fclose_stream == NULL) { | |
126 | __lttng_ust_safe_fclose_stream = dlsym(RTLD_DEFAULT, "lttng_ust_safe_fclose_stream"); | |
127 | ||
128 | if (__lttng_ust_safe_fclose_stream == NULL) { | |
129 | __lttng_ust_safe_fclose_stream = (void *) LTTNG_UST_DLSYM_FAILED_PTR; | |
130 | fprintf(stderr, "%s\n", dlerror()); | |
131 | } | |
132 | } | |
133 | ||
134 | return __lttng_ust_safe_fclose_stream; | |
135 | } | |
136 | ||
137 | static int lttng_ust_safe_fclose_stream_chain(FILE *stream, int (*fclose_cb)(FILE *stream)) | |
138 | { | |
139 | assert(__lttng_ust_safe_fclose_stream != NULL); | |
140 | if (__lttng_ust_safe_fclose_stream != (void *) LTTNG_UST_DLSYM_FAILED_PTR) { | |
141 | /* Chain on ust-2.12 preload */ | |
142 | return __lttng_ust_safe_fclose_stream(stream, fclose_cb); | |
143 | } else { | |
144 | /* Fallback to libc symbol */ | |
145 | return fclose_cb(stream); | |
146 | } | |
147 | } | |
148 | #else | |
149 | static int lttng_ust_safe_fclose_stream_chain(FILE *stream, int (*fclose_cb)(FILE *stream)) | |
150 | { | |
151 | return fclose_cb(stream); | |
152 | } | |
153 | #endif | |
154 | ||
6548fca4 MD |
155 | /* |
156 | * Allocate the fd set array based on the hard limit set for this | |
157 | * process. This will be called during the constructor execution | |
158 | * and will also be called in the child after fork via lttng_ust_init. | |
159 | */ | |
fca97dfd | 160 | void lttng_ust_fd_tracker_init(void) |
6548fca4 MD |
161 | { |
162 | struct rlimit rlim; | |
163 | int i; | |
164 | ||
7d34f27d MD |
165 | if (CMM_LOAD_SHARED(init_done)) |
166 | return; | |
167 | ||
6548fca4 MD |
168 | memset(&rlim, 0, sizeof(rlim)); |
169 | /* Get the current possible max number of fd for this process. */ | |
170 | if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) | |
171 | abort(); | |
172 | /* | |
173 | * FD set array size determined using the hard limit. Even if | |
174 | * the process wishes to increase its limit using setrlimit, it | |
175 | * can only do so with the softlimit which will be less than the | |
176 | * hard limit. | |
177 | */ | |
178 | lttng_ust_max_fd = rlim.rlim_max; | |
179 | num_fd_sets = lttng_ust_max_fd / FD_SETSIZE; | |
180 | if (lttng_ust_max_fd % FD_SETSIZE) | |
181 | ++num_fd_sets; | |
182 | if (lttng_fd_set != NULL) { | |
183 | free(lttng_fd_set); | |
184 | lttng_fd_set = NULL; | |
185 | } | |
186 | lttng_fd_set = malloc(num_fd_sets * (sizeof(fd_set))); | |
187 | if (!lttng_fd_set) | |
188 | abort(); | |
189 | for (i = 0; i < num_fd_sets; i++) | |
190 | FD_ZERO((<tng_fd_set[i])); | |
7d34f27d | 191 | CMM_STORE_SHARED(init_done, 1); |
6548fca4 MD |
192 | } |
193 | ||
f799b0b7 | 194 | static void lttng_ust_lock_fd_tracker_orig(void) |
6548fca4 | 195 | { |
96a6162e | 196 | sigset_t sig_all_blocked, orig_mask; |
59e57035 | 197 | int ret; |
283f4bec | 198 | |
59e57035 MD |
199 | if (lttng_ust_cancelstate_disable_push()) { |
200 | ERR("lttng_ust_cancelstate_disable_push"); | |
283f4bec | 201 | } |
96a6162e MD |
202 | sigfillset(&sig_all_blocked); |
203 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask); | |
204 | if (ret) { | |
205 | ERR("pthread_sigmask: %s", strerror(ret)); | |
206 | } | |
207 | if (!URCU_TLS(ust_fd_mutex_nest)++) { | |
208 | /* | |
209 | * Ensure the compiler don't move the store after the close() | |
210 | * call in case close() would be marked as leaf. | |
211 | */ | |
212 | cmm_barrier(); | |
213 | pthread_mutex_lock(&ust_safe_guard_fd_mutex); | |
96a6162e MD |
214 | } |
215 | ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL); | |
216 | if (ret) { | |
217 | ERR("pthread_sigmask: %s", strerror(ret)); | |
218 | } | |
6548fca4 MD |
219 | } |
220 | ||
f799b0b7 | 221 | static void lttng_ust_unlock_fd_tracker_orig(void) |
6548fca4 | 222 | { |
96a6162e | 223 | sigset_t sig_all_blocked, orig_mask; |
59e57035 | 224 | int ret; |
283f4bec | 225 | |
96a6162e MD |
226 | sigfillset(&sig_all_blocked); |
227 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask); | |
228 | if (ret) { | |
229 | ERR("pthread_sigmask: %s", strerror(ret)); | |
230 | } | |
6548fca4 MD |
231 | /* |
232 | * Ensure the compiler don't move the store before the close() | |
233 | * call, in case close() would be marked as leaf. | |
234 | */ | |
235 | cmm_barrier(); | |
96a6162e | 236 | if (!--URCU_TLS(ust_fd_mutex_nest)) { |
96a6162e MD |
237 | pthread_mutex_unlock(&ust_safe_guard_fd_mutex); |
238 | } | |
239 | ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL); | |
283f4bec | 240 | if (ret) { |
96a6162e MD |
241 | ERR("pthread_sigmask: %s", strerror(ret)); |
242 | } | |
59e57035 MD |
243 | if (lttng_ust_cancelstate_disable_pop()) { |
244 | ERR("lttng_ust_cancelstate_disable_pop"); | |
283f4bec | 245 | } |
6548fca4 MD |
246 | } |
247 | ||
f5c453e9 JR |
248 | static int dup_std_fd(int fd) |
249 | { | |
5a4d96d1 | 250 | int ret, i; |
f5c453e9 JR |
251 | int fd_to_close[STDERR_FILENO + 1]; |
252 | int fd_to_close_count = 0; | |
253 | int dup_cmd = F_DUPFD; /* Default command */ | |
254 | int fd_valid = -1; | |
255 | ||
256 | if (!(IS_FD_STD(fd))) { | |
257 | /* Should not be here */ | |
258 | ret = -1; | |
259 | goto error; | |
260 | } | |
261 | ||
262 | /* Check for FD_CLOEXEC flag */ | |
263 | ret = fcntl(fd, F_GETFD); | |
264 | if (ret < 0) { | |
265 | PERROR("fcntl on f_getfd"); | |
266 | ret = -1; | |
267 | goto error; | |
268 | } | |
269 | ||
270 | if (ret & FD_CLOEXEC) { | |
271 | dup_cmd = F_DUPFD_CLOEXEC; | |
272 | } | |
273 | ||
274 | /* Perform dup */ | |
5a4d96d1 | 275 | for (i = 0; i < STDERR_FILENO + 1; i++) { |
f5c453e9 JR |
276 | ret = fcntl(fd, dup_cmd, 0); |
277 | if (ret < 0) { | |
278 | PERROR("fcntl dup fd"); | |
279 | goto error; | |
280 | } | |
281 | ||
282 | if (!(IS_FD_STD(ret))) { | |
283 | /* fd is outside of STD range, use it. */ | |
284 | fd_valid = ret; | |
285 | /* Close fd received as argument. */ | |
286 | fd_to_close[i] = fd; | |
287 | fd_to_close_count++; | |
288 | break; | |
289 | } | |
290 | ||
291 | fd_to_close[i] = ret; | |
292 | fd_to_close_count++; | |
293 | } | |
294 | ||
295 | /* Close intermediary fds */ | |
5a4d96d1 | 296 | for (i = 0; i < fd_to_close_count; i++) { |
f5c453e9 JR |
297 | ret = close(fd_to_close[i]); |
298 | if (ret) { | |
299 | PERROR("close on temporary fd: %d.", fd_to_close[i]); | |
300 | /* | |
301 | * Not using an abort here would yield a complicated | |
302 | * error handling for the caller. If a failure occurs | |
303 | * here, the system is already in a bad state. | |
304 | */ | |
305 | abort(); | |
306 | } | |
307 | } | |
308 | ||
309 | ret = fd_valid; | |
310 | error: | |
311 | return ret; | |
312 | } | |
313 | ||
6548fca4 MD |
314 | /* |
315 | * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd. | |
316 | * Has strict checking of fd validity. | |
f5c453e9 JR |
317 | * |
318 | * If fd <= 2, dup the fd until fd > 2. This enables us to bypass | |
319 | * problems that can be encountered if UST uses stdin, stdout, stderr | |
320 | * fds for internal use (daemon etc.). This can happen if the | |
321 | * application closes either of those file descriptors. Intermediary fds | |
322 | * are closed as needed. | |
323 | * | |
324 | * Return -1 on error. | |
325 | * | |
6548fca4 | 326 | */ |
f799b0b7 | 327 | static int lttng_ust_add_fd_to_tracker_orig(int fd) |
6548fca4 | 328 | { |
f5c453e9 | 329 | int ret; |
7d34f27d MD |
330 | /* |
331 | * Ensure the tracker is initialized when called from | |
332 | * constructors. | |
333 | */ | |
fca97dfd | 334 | lttng_ust_fd_tracker_init(); |
96a6162e | 335 | assert(URCU_TLS(ust_fd_mutex_nest)); |
f5c453e9 JR |
336 | |
337 | if (IS_FD_STD(fd)) { | |
338 | ret = dup_std_fd(fd); | |
339 | if (ret < 0) { | |
340 | goto error; | |
341 | } | |
342 | fd = ret; | |
343 | } | |
344 | ||
6548fca4 MD |
345 | /* Trying to add an fd which we can not accommodate. */ |
346 | assert(IS_FD_VALID(fd)); | |
347 | /* Setting an fd thats already set. */ | |
348 | assert(!IS_FD_SET(fd, lttng_fd_set)); | |
349 | ||
350 | ADD_FD_TO_SET(fd, lttng_fd_set); | |
f5c453e9 JR |
351 | return fd; |
352 | error: | |
353 | return ret; | |
6548fca4 MD |
354 | } |
355 | ||
356 | /* | |
357 | * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd. | |
358 | * Has strict checking for fd validity. | |
359 | */ | |
f799b0b7 | 360 | static void lttng_ust_delete_fd_from_tracker_orig(int fd) |
6548fca4 | 361 | { |
7d34f27d MD |
362 | /* |
363 | * Ensure the tracker is initialized when called from | |
364 | * constructors. | |
365 | */ | |
fca97dfd | 366 | lttng_ust_fd_tracker_init(); |
7d34f27d | 367 | |
96a6162e | 368 | assert(URCU_TLS(ust_fd_mutex_nest)); |
6548fca4 MD |
369 | /* Not a valid fd. */ |
370 | assert(IS_FD_VALID(fd)); | |
371 | /* Deleting an fd which was not set. */ | |
372 | assert(IS_FD_SET(fd, lttng_fd_set)); | |
373 | ||
374 | DEL_FD_FROM_SET(fd, lttng_fd_set); | |
375 | } | |
376 | ||
a1139084 MD |
377 | #if !defined(LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS) |
378 | /* lttng-ust 2.12 (custom branch) getter */ | |
379 | static int (*__lttng_ust_get_fd_mutex_nest)(void) = NULL; | |
380 | ||
381 | void *lttng_ust_get_fd_mutex_nest_init(void) | |
382 | { | |
383 | if (__lttng_ust_get_fd_mutex_nest == NULL) { | |
384 | __lttng_ust_get_fd_mutex_nest = dlsym(RTLD_DEFAULT, "lttng_ust_get_fd_mutex_nest"); | |
385 | ||
386 | if (__lttng_ust_get_fd_mutex_nest == NULL) { | |
387 | __lttng_ust_get_fd_mutex_nest = (void *) LTTNG_UST_DLSYM_FAILED_PTR; | |
388 | fprintf(stderr, "%s\n", dlerror()); | |
389 | } | |
390 | } | |
391 | ||
392 | return __lttng_ust_get_fd_mutex_nest; | |
393 | } | |
394 | ||
395 | static int lttng_ust_get_fd_mutex_nest_chain(void) | |
396 | { | |
397 | assert(__lttng_ust_get_fd_mutex_nest != NULL); | |
398 | if (__lttng_ust_get_fd_mutex_nest != (void *) LTTNG_UST_DLSYM_FAILED_PTR) | |
399 | return __lttng_ust_get_fd_mutex_nest(); | |
400 | return 0; | |
401 | } | |
402 | #else | |
403 | static int lttng_ust_get_fd_mutex_nest_chain(void) | |
404 | { | |
405 | return 0; | |
406 | } | |
407 | #endif | |
408 | ||
6548fca4 MD |
409 | /* |
410 | * Interface allowing applications to close arbitrary file descriptors. | |
411 | * We check if it is owned by lttng-ust, and return -1, errno=EBADF | |
412 | * instead of closing it if it is the case. | |
413 | */ | |
f799b0b7 | 414 | static int lttng_ust_safe_close_fd_orig(int fd, int (*close_cb)(int fd)) |
6548fca4 MD |
415 | { |
416 | int ret = 0; | |
417 | ||
a9fd951a | 418 | lttng_ust_fd_tracker_alloc_tls(); |
6548fca4 | 419 | |
7d34f27d MD |
420 | /* |
421 | * Ensure the tracker is initialized when called from | |
422 | * constructors. | |
423 | */ | |
fca97dfd | 424 | lttng_ust_fd_tracker_init(); |
7d34f27d | 425 | |
6548fca4 MD |
426 | /* |
427 | * If called from lttng-ust, we directly call close without | |
428 | * validating whether the FD is part of the tracked set. | |
429 | */ | |
a1139084 | 430 | if (URCU_TLS(ust_fd_mutex_nest) || lttng_ust_get_fd_mutex_nest_chain()) |
6548fca4 MD |
431 | return close_cb(fd); |
432 | ||
433 | lttng_ust_lock_fd_tracker(); | |
434 | if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) { | |
435 | ret = -1; | |
436 | errno = EBADF; | |
437 | } else { | |
aaa692c8 | 438 | ret = lttng_ust_safe_close_fd_chain(fd, close_cb); |
6548fca4 MD |
439 | } |
440 | lttng_ust_unlock_fd_tracker(); | |
441 | ||
442 | return ret; | |
443 | } | |
444 | ||
52a20dc7 MD |
445 | /* |
446 | * Interface allowing applications to close arbitrary streams. | |
447 | * We check if it is owned by lttng-ust, and return -1, errno=EBADF | |
448 | * instead of closing it if it is the case. | |
449 | */ | |
f799b0b7 | 450 | static int lttng_ust_safe_fclose_stream_orig(FILE *stream, int (*fclose_cb)(FILE *stream)) |
52a20dc7 MD |
451 | { |
452 | int ret = 0, fd; | |
453 | ||
a9fd951a | 454 | lttng_ust_fd_tracker_alloc_tls(); |
52a20dc7 | 455 | |
7d34f27d MD |
456 | /* |
457 | * Ensure the tracker is initialized when called from | |
458 | * constructors. | |
459 | */ | |
fca97dfd | 460 | lttng_ust_fd_tracker_init(); |
7d34f27d | 461 | |
52a20dc7 MD |
462 | /* |
463 | * If called from lttng-ust, we directly call fclose without | |
464 | * validating whether the FD is part of the tracked set. | |
465 | */ | |
a1139084 | 466 | if (URCU_TLS(ust_fd_mutex_nest) || lttng_ust_get_fd_mutex_nest_chain()) |
52a20dc7 MD |
467 | return fclose_cb(stream); |
468 | ||
469 | fd = fileno(stream); | |
470 | ||
471 | lttng_ust_lock_fd_tracker(); | |
472 | if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) { | |
473 | ret = -1; | |
474 | errno = EBADF; | |
475 | } else { | |
aaa692c8 | 476 | ret = lttng_ust_safe_fclose_stream_chain(stream, fclose_cb); |
52a20dc7 MD |
477 | } |
478 | lttng_ust_unlock_fd_tracker(); | |
479 | ||
480 | return ret; | |
481 | } | |
482 | ||
6548fca4 MD |
483 | #ifdef __OpenBSD__ |
484 | static void set_close_success(int *p) | |
485 | { | |
486 | *p = 1; | |
487 | } | |
488 | static int test_close_success(const int *p) | |
489 | { | |
490 | return *p; | |
491 | } | |
492 | #else | |
493 | static void set_close_success(int *p __attribute__((unused))) | |
494 | { | |
495 | } | |
496 | static int test_close_success(const int *p __attribute__((unused))) | |
497 | { | |
498 | return 1; | |
499 | } | |
500 | #endif | |
501 | ||
502 | /* | |
503 | * Implement helper for closefrom() override. | |
504 | */ | |
f799b0b7 | 505 | static int lttng_ust_safe_closefrom_fd_orig(int lowfd, int (*close_cb)(int fd)) |
6548fca4 MD |
506 | { |
507 | int ret = 0, close_success = 0, i; | |
508 | ||
a9fd951a | 509 | lttng_ust_fd_tracker_alloc_tls(); |
6548fca4 | 510 | |
7d34f27d MD |
511 | /* |
512 | * Ensure the tracker is initialized when called from | |
513 | * constructors. | |
514 | */ | |
fca97dfd | 515 | lttng_ust_fd_tracker_init(); |
7d34f27d | 516 | |
6548fca4 MD |
517 | if (lowfd < 0) { |
518 | /* | |
519 | * NetBSD return EBADF if fd is invalid. | |
520 | */ | |
521 | errno = EBADF; | |
522 | ret = -1; | |
523 | goto end; | |
524 | } | |
525 | /* | |
526 | * If called from lttng-ust, we directly call close without | |
527 | * validating whether the FD is part of the tracked set. | |
528 | */ | |
a1139084 | 529 | if (URCU_TLS(ust_fd_mutex_nest) || lttng_ust_get_fd_mutex_nest_chain()) { |
6548fca4 MD |
530 | for (i = lowfd; i < lttng_ust_max_fd; i++) { |
531 | if (close_cb(i) < 0) { | |
532 | switch (errno) { | |
533 | case EBADF: | |
534 | continue; | |
535 | case EINTR: | |
536 | default: | |
537 | ret = -1; | |
538 | goto end; | |
539 | } | |
540 | } | |
541 | set_close_success(&close_success); | |
542 | } | |
543 | } else { | |
544 | lttng_ust_lock_fd_tracker(); | |
545 | for (i = lowfd; i < lttng_ust_max_fd; i++) { | |
546 | if (IS_FD_VALID(i) && IS_FD_SET(i, lttng_fd_set)) | |
547 | continue; | |
548 | if (close_cb(i) < 0) { | |
549 | switch (errno) { | |
550 | case EBADF: | |
551 | continue; | |
552 | case EINTR: | |
553 | default: | |
554 | ret = -1; | |
555 | lttng_ust_unlock_fd_tracker(); | |
556 | goto end; | |
557 | } | |
558 | } | |
559 | set_close_success(&close_success); | |
560 | } | |
561 | lttng_ust_unlock_fd_tracker(); | |
562 | } | |
563 | if (!test_close_success(&close_success)) { | |
564 | /* | |
565 | * OpenBSD return EBADF if fd is greater than all open | |
566 | * file descriptors. | |
567 | */ | |
568 | ret = -1; | |
569 | errno = EBADF; | |
570 | } | |
571 | end: | |
572 | return ret; | |
573 | } | |
f799b0b7 MD |
574 | |
575 | /* Custom upgrade 2.12 to 2.13 */ | |
576 | ||
577 | #undef lttng_ust_add_fd_to_tracker | |
578 | #undef lttng_ust_delete_fd_from_tracker | |
579 | #undef lttng_ust_lock_fd_tracker | |
580 | #undef lttng_ust_unlock_fd_tracker | |
581 | #undef lttng_ust_safe_close_fd | |
582 | #undef lttng_ust_safe_fclose_stream | |
583 | #undef lttng_ust_safe_closefrom_fd | |
584 | ||
585 | int lttng_ust_add_fd_to_tracker1(int fd) | |
586 | __attribute__ ((alias ("lttng_ust_add_fd_to_tracker_orig"))); | |
587 | void lttng_ust_delete_fd_from_tracker1(int fd) | |
588 | __attribute__ ((alias ("lttng_ust_delete_fd_from_tracker_orig"))); | |
589 | void lttng_ust_lock_fd_tracker1(void) | |
590 | __attribute__ ((alias ("lttng_ust_lock_fd_tracker_orig"))); | |
591 | void lttng_ust_unlock_fd_tracker1(void) | |
592 | __attribute__ ((alias ("lttng_ust_unlock_fd_tracker_orig"))); | |
593 | int lttng_ust_safe_close_fd1(int fd, int (*close_cb)(int)) | |
594 | __attribute__ ((alias ("lttng_ust_safe_close_fd_orig"))); | |
595 | int lttng_ust_safe_fclose_stream1(FILE *stream, int (*fclose_cb)(FILE *stream)) | |
596 | __attribute__ ((alias ("lttng_ust_safe_fclose_stream_orig"))); | |
597 | int lttng_ust_safe_closefrom_fd1(int lowfd, int (*close_cb)(int)) | |
598 | __attribute__ ((alias ("lttng_ust_safe_closefrom_fd_orig"))); | |
599 | ||
600 | #ifdef LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS | |
601 | int lttng_ust_add_fd_to_tracker(int fd) | |
602 | __attribute__ ((alias ("lttng_ust_add_fd_to_tracker_orig"))); | |
603 | void lttng_ust_delete_fd_from_tracker(int fd) | |
604 | __attribute__ ((alias ("lttng_ust_delete_fd_from_tracker_orig"))); | |
605 | void lttng_ust_lock_fd_tracker(void) | |
606 | __attribute__ ((alias ("lttng_ust_lock_fd_tracker_orig"))); | |
607 | void lttng_ust_unlock_fd_tracker(void) | |
608 | __attribute__ ((alias ("lttng_ust_unlock_fd_tracker_orig"))); | |
609 | int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int)) | |
610 | __attribute__ ((alias ("lttng_ust_safe_close_fd_orig"))); | |
611 | int lttng_ust_safe_fclose_stream(FILE *stream, int (*fclose_cb)(FILE *stream)) | |
612 | __attribute__ ((alias ("lttng_ust_safe_fclose_stream_orig"))); | |
613 | int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int)) | |
614 | __attribute__ ((alias ("lttng_ust_safe_closefrom_fd_orig"))); | |
615 | #endif |