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