Fix: Use 2.12 custom mutex nest getter
[deliverable/lttng-ust.git] / src / lib / lttng-ust-common / fd-tracker.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2016 Aravind HT <aravind.ht@gmail.com>
5 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
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>
22 #include <signal.h>
23 #include <stdbool.h>
24 #include <dlfcn.h>
25 #include <urcu/compiler.h>
26 #include <urcu/tls-compat.h>
27 #include <urcu/system.h>
28
29 #include "common/ust-fd.h"
30 #include "common/macros.h"
31 #include <lttng/ust-error.h>
32 #include <lttng/ust-cancelstate.h>
33 #include "common/logging.h"
34
35 #include "lib/lttng-ust-common/fd-tracker.h"
36
37 #define LTTNG_UST_DLSYM_FAILED_PTR 0x1
38
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)
43 #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
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
55 * within the libc dl lock. Therefore, we need to allocate the TLS before
56 * nesting into this lock.
57 *
58 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
59 * is also held across fork.
60 */
61 static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
62
63 /*
64 * Track whether we are within lttng-ust or application, for close
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.
68 */
69 static DEFINE_URCU_TLS(int, ust_fd_mutex_nest);
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;
75 static int init_done;
76
77 /*
78 * Force a read (imply TLS allocation for dlopen) of TLS variables.
79 */
80 void lttng_ust_fd_tracker_alloc_tls(void)
81 {
82 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest)));
83 }
84
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
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 */
160 void lttng_ust_fd_tracker_init(void)
161 {
162 struct rlimit rlim;
163 int i;
164
165 if (CMM_LOAD_SHARED(init_done))
166 return;
167
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((&lttng_fd_set[i]));
191 CMM_STORE_SHARED(init_done, 1);
192 }
193
194 static void lttng_ust_lock_fd_tracker_orig(void)
195 {
196 sigset_t sig_all_blocked, orig_mask;
197 int ret;
198
199 if (lttng_ust_cancelstate_disable_push()) {
200 ERR("lttng_ust_cancelstate_disable_push");
201 }
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);
214 }
215 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
216 if (ret) {
217 ERR("pthread_sigmask: %s", strerror(ret));
218 }
219 }
220
221 static void lttng_ust_unlock_fd_tracker_orig(void)
222 {
223 sigset_t sig_all_blocked, orig_mask;
224 int ret;
225
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 }
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();
236 if (!--URCU_TLS(ust_fd_mutex_nest)) {
237 pthread_mutex_unlock(&ust_safe_guard_fd_mutex);
238 }
239 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
240 if (ret) {
241 ERR("pthread_sigmask: %s", strerror(ret));
242 }
243 if (lttng_ust_cancelstate_disable_pop()) {
244 ERR("lttng_ust_cancelstate_disable_pop");
245 }
246 }
247
248 static int dup_std_fd(int fd)
249 {
250 int ret, i;
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 */
275 for (i = 0; i < STDERR_FILENO + 1; i++) {
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 */
296 for (i = 0; i < fd_to_close_count; i++) {
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
314 /*
315 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
316 * Has strict checking of fd validity.
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 *
326 */
327 static int lttng_ust_add_fd_to_tracker_orig(int fd)
328 {
329 int ret;
330 /*
331 * Ensure the tracker is initialized when called from
332 * constructors.
333 */
334 lttng_ust_fd_tracker_init();
335 assert(URCU_TLS(ust_fd_mutex_nest));
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
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);
351 return fd;
352 error:
353 return ret;
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 */
360 static void lttng_ust_delete_fd_from_tracker_orig(int fd)
361 {
362 /*
363 * Ensure the tracker is initialized when called from
364 * constructors.
365 */
366 lttng_ust_fd_tracker_init();
367
368 assert(URCU_TLS(ust_fd_mutex_nest));
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
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
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 */
414 static int lttng_ust_safe_close_fd_orig(int fd, int (*close_cb)(int fd))
415 {
416 int ret = 0;
417
418 lttng_ust_fd_tracker_alloc_tls();
419
420 /*
421 * Ensure the tracker is initialized when called from
422 * constructors.
423 */
424 lttng_ust_fd_tracker_init();
425
426 /*
427 * If called from lttng-ust, we directly call close without
428 * validating whether the FD is part of the tracked set.
429 */
430 if (URCU_TLS(ust_fd_mutex_nest) || lttng_ust_get_fd_mutex_nest_chain())
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 {
438 ret = lttng_ust_safe_close_fd_chain(fd, close_cb);
439 }
440 lttng_ust_unlock_fd_tracker();
441
442 return ret;
443 }
444
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 */
450 static int lttng_ust_safe_fclose_stream_orig(FILE *stream, int (*fclose_cb)(FILE *stream))
451 {
452 int ret = 0, fd;
453
454 lttng_ust_fd_tracker_alloc_tls();
455
456 /*
457 * Ensure the tracker is initialized when called from
458 * constructors.
459 */
460 lttng_ust_fd_tracker_init();
461
462 /*
463 * If called from lttng-ust, we directly call fclose without
464 * validating whether the FD is part of the tracked set.
465 */
466 if (URCU_TLS(ust_fd_mutex_nest) || lttng_ust_get_fd_mutex_nest_chain())
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 {
476 ret = lttng_ust_safe_fclose_stream_chain(stream, fclose_cb);
477 }
478 lttng_ust_unlock_fd_tracker();
479
480 return ret;
481 }
482
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 */
505 static int lttng_ust_safe_closefrom_fd_orig(int lowfd, int (*close_cb)(int fd))
506 {
507 int ret = 0, close_success = 0, i;
508
509 lttng_ust_fd_tracker_alloc_tls();
510
511 /*
512 * Ensure the tracker is initialized when called from
513 * constructors.
514 */
515 lttng_ust_fd_tracker_init();
516
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 */
529 if (URCU_TLS(ust_fd_mutex_nest) || lttng_ust_get_fd_mutex_nest_chain()) {
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 }
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
This page took 0.077132 seconds and 5 git commands to generate.