Add ust fd mutex nesting getter
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-fd-tracker.c
CommitLineData
6548fca4
MD
1/*
2 * Copyright (C) 2016 - Aravind HT <aravind.ht@gmail.com>
3 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
9 *
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.
14 *
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
18 */
19
20#define _GNU_SOURCE
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <assert.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <sys/select.h>
31#include <sys/resource.h>
32#include <sys/time.h>
33#include <fcntl.h>
34#include <pthread.h>
96a6162e
MD
35#include <signal.h>
36#include <stdbool.h>
6548fca4
MD
37#include <urcu/compiler.h>
38#include <urcu/tls-compat.h>
7d34f27d 39#include <urcu/system.h>
6548fca4
MD
40
41#include <ust-fd.h>
42#include <helper.h>
43#include <lttng/ust-error.h>
44#include <usterr-signal-safe.h>
595c1577 45#include <lttng/ust-cancelstate.h>
6548fca4
MD
46
47#include "../liblttng-ust/compat.h"
48
49/* Operations on the fd set. */
50#define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
51#define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
52#define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
f5c453e9 53#define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
6548fca4
MD
54
55/* Check fd validity before calling these. */
56#define ADD_FD_TO_SET(fd, fd_sets) \
57 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
58#define IS_FD_SET(fd, fd_sets) \
59 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
60#define DEL_FD_FROM_SET(fd, fd_sets) \
61 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
62
63/*
64 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
65 * within the libc dl lock. Therefore, we need to fixup the TLS before
66 * nesting into this lock.
c1be081a
MD
67 *
68 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
69 * is also held across fork.
6548fca4
MD
70 */
71static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
283f4bec 72
6548fca4
MD
73/*
74 * Track whether we are within lttng-ust or application, for close
793d29c9
MD
75 * system call override by LD_PRELOAD library. This also tracks whether
76 * we are invoking close() from a signal handler nested on an
77 * application thread.
6548fca4 78 */
96a6162e 79static DEFINE_URCU_TLS(int, ust_fd_mutex_nest);
6548fca4 80
3531136c
MD
81int lttng_ust_get_fd_mutex_nest(void);
82
83int lttng_ust_get_fd_mutex_nest(void)
84{
85 return ust_fd_mutex_nest;
86}
87
6548fca4
MD
88/* fd_set used to book keep fd being used by lttng-ust. */
89static fd_set *lttng_fd_set;
90static int lttng_ust_max_fd;
91static int num_fd_sets;
7d34f27d 92static int init_done;
6548fca4
MD
93
94/*
95 * Force a read (imply TLS fixup for dlopen) of TLS variables.
96 */
97void lttng_ust_fixup_fd_tracker_tls(void)
98{
96a6162e 99 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest)));
6548fca4
MD
100}
101
102/*
103 * Allocate the fd set array based on the hard limit set for this
104 * process. This will be called during the constructor execution
105 * and will also be called in the child after fork via lttng_ust_init.
106 */
107void lttng_ust_init_fd_tracker(void)
108{
109 struct rlimit rlim;
110 int i;
111
7d34f27d
MD
112 if (CMM_LOAD_SHARED(init_done))
113 return;
114
6548fca4
MD
115 memset(&rlim, 0, sizeof(rlim));
116 /* Get the current possible max number of fd for this process. */
117 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
118 abort();
119 /*
120 * FD set array size determined using the hard limit. Even if
121 * the process wishes to increase its limit using setrlimit, it
122 * can only do so with the softlimit which will be less than the
123 * hard limit.
124 */
125 lttng_ust_max_fd = rlim.rlim_max;
126 num_fd_sets = lttng_ust_max_fd / FD_SETSIZE;
127 if (lttng_ust_max_fd % FD_SETSIZE)
128 ++num_fd_sets;
129 if (lttng_fd_set != NULL) {
130 free(lttng_fd_set);
131 lttng_fd_set = NULL;
132 }
133 lttng_fd_set = malloc(num_fd_sets * (sizeof(fd_set)));
134 if (!lttng_fd_set)
135 abort();
136 for (i = 0; i < num_fd_sets; i++)
137 FD_ZERO((&lttng_fd_set[i]));
7d34f27d 138 CMM_STORE_SHARED(init_done, 1);
6548fca4
MD
139}
140
141void lttng_ust_lock_fd_tracker(void)
142{
96a6162e 143 sigset_t sig_all_blocked, orig_mask;
595c1577 144 int ret;
283f4bec 145
595c1577
MD
146 if (lttng_ust_cancelstate_disable_push()) {
147 ERR("lttng_ust_cancelstate_disable_push");
283f4bec 148 }
96a6162e
MD
149 sigfillset(&sig_all_blocked);
150 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
151 if (ret) {
152 ERR("pthread_sigmask: %s", strerror(ret));
153 }
154 if (!URCU_TLS(ust_fd_mutex_nest)++) {
155 /*
156 * Ensure the compiler don't move the store after the close()
157 * call in case close() would be marked as leaf.
158 */
159 cmm_barrier();
160 pthread_mutex_lock(&ust_safe_guard_fd_mutex);
96a6162e
MD
161 }
162 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
163 if (ret) {
164 ERR("pthread_sigmask: %s", strerror(ret));
165 }
6548fca4
MD
166}
167
168void lttng_ust_unlock_fd_tracker(void)
169{
96a6162e 170 sigset_t sig_all_blocked, orig_mask;
595c1577 171 int ret;
283f4bec 172
96a6162e
MD
173 sigfillset(&sig_all_blocked);
174 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
175 if (ret) {
176 ERR("pthread_sigmask: %s", strerror(ret));
177 }
6548fca4
MD
178 /*
179 * Ensure the compiler don't move the store before the close()
180 * call, in case close() would be marked as leaf.
181 */
182 cmm_barrier();
96a6162e 183 if (!--URCU_TLS(ust_fd_mutex_nest)) {
96a6162e
MD
184 pthread_mutex_unlock(&ust_safe_guard_fd_mutex);
185 }
186 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
283f4bec 187 if (ret) {
96a6162e
MD
188 ERR("pthread_sigmask: %s", strerror(ret));
189 }
595c1577
MD
190 if (lttng_ust_cancelstate_disable_pop()) {
191 ERR("lttng_ust_cancelstate_disable_pop");
283f4bec 192 }
6548fca4
MD
193}
194
f5c453e9
JR
195static int dup_std_fd(int fd)
196{
5a4d96d1 197 int ret, i;
f5c453e9
JR
198 int fd_to_close[STDERR_FILENO + 1];
199 int fd_to_close_count = 0;
200 int dup_cmd = F_DUPFD; /* Default command */
201 int fd_valid = -1;
202
203 if (!(IS_FD_STD(fd))) {
204 /* Should not be here */
205 ret = -1;
206 goto error;
207 }
208
209 /* Check for FD_CLOEXEC flag */
210 ret = fcntl(fd, F_GETFD);
211 if (ret < 0) {
212 PERROR("fcntl on f_getfd");
213 ret = -1;
214 goto error;
215 }
216
217 if (ret & FD_CLOEXEC) {
218 dup_cmd = F_DUPFD_CLOEXEC;
219 }
220
221 /* Perform dup */
5a4d96d1 222 for (i = 0; i < STDERR_FILENO + 1; i++) {
f5c453e9
JR
223 ret = fcntl(fd, dup_cmd, 0);
224 if (ret < 0) {
225 PERROR("fcntl dup fd");
226 goto error;
227 }
228
229 if (!(IS_FD_STD(ret))) {
230 /* fd is outside of STD range, use it. */
231 fd_valid = ret;
232 /* Close fd received as argument. */
233 fd_to_close[i] = fd;
234 fd_to_close_count++;
235 break;
236 }
237
238 fd_to_close[i] = ret;
239 fd_to_close_count++;
240 }
241
242 /* Close intermediary fds */
5a4d96d1 243 for (i = 0; i < fd_to_close_count; i++) {
f5c453e9
JR
244 ret = close(fd_to_close[i]);
245 if (ret) {
246 PERROR("close on temporary fd: %d.", fd_to_close[i]);
247 /*
248 * Not using an abort here would yield a complicated
249 * error handling for the caller. If a failure occurs
250 * here, the system is already in a bad state.
251 */
252 abort();
253 }
254 }
255
256 ret = fd_valid;
257error:
258 return ret;
259}
260
6548fca4
MD
261/*
262 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
263 * Has strict checking of fd validity.
f5c453e9
JR
264 *
265 * If fd <= 2, dup the fd until fd > 2. This enables us to bypass
266 * problems that can be encountered if UST uses stdin, stdout, stderr
267 * fds for internal use (daemon etc.). This can happen if the
268 * application closes either of those file descriptors. Intermediary fds
269 * are closed as needed.
270 *
271 * Return -1 on error.
272 *
6548fca4 273 */
f5c453e9 274int lttng_ust_add_fd_to_tracker(int fd)
6548fca4 275{
f5c453e9 276 int ret;
7d34f27d
MD
277 /*
278 * Ensure the tracker is initialized when called from
279 * constructors.
280 */
281 lttng_ust_init_fd_tracker();
96a6162e 282 assert(URCU_TLS(ust_fd_mutex_nest));
f5c453e9
JR
283
284 if (IS_FD_STD(fd)) {
285 ret = dup_std_fd(fd);
286 if (ret < 0) {
287 goto error;
288 }
289 fd = ret;
290 }
291
6548fca4
MD
292 /* Trying to add an fd which we can not accommodate. */
293 assert(IS_FD_VALID(fd));
294 /* Setting an fd thats already set. */
295 assert(!IS_FD_SET(fd, lttng_fd_set));
296
297 ADD_FD_TO_SET(fd, lttng_fd_set);
f5c453e9
JR
298 return fd;
299error:
300 return ret;
6548fca4
MD
301}
302
303/*
304 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
305 * Has strict checking for fd validity.
306 */
307void lttng_ust_delete_fd_from_tracker(int fd)
308{
7d34f27d
MD
309 /*
310 * Ensure the tracker is initialized when called from
311 * constructors.
312 */
313 lttng_ust_init_fd_tracker();
314
96a6162e 315 assert(URCU_TLS(ust_fd_mutex_nest));
6548fca4
MD
316 /* Not a valid fd. */
317 assert(IS_FD_VALID(fd));
318 /* Deleting an fd which was not set. */
319 assert(IS_FD_SET(fd, lttng_fd_set));
320
321 DEL_FD_FROM_SET(fd, lttng_fd_set);
322}
323
324/*
325 * Interface allowing applications to close arbitrary file descriptors.
326 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
327 * instead of closing it if it is the case.
328 */
329int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int fd))
330{
331 int ret = 0;
332
333 lttng_ust_fixup_fd_tracker_tls();
334
7d34f27d
MD
335 /*
336 * Ensure the tracker is initialized when called from
337 * constructors.
338 */
339 lttng_ust_init_fd_tracker();
340
6548fca4
MD
341 /*
342 * If called from lttng-ust, we directly call close without
343 * validating whether the FD is part of the tracked set.
344 */
793d29c9 345 if (URCU_TLS(ust_fd_mutex_nest))
6548fca4
MD
346 return close_cb(fd);
347
348 lttng_ust_lock_fd_tracker();
349 if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) {
350 ret = -1;
351 errno = EBADF;
352 } else {
353 ret = close_cb(fd);
354 }
355 lttng_ust_unlock_fd_tracker();
356
357 return ret;
358}
359
52a20dc7
MD
360/*
361 * Interface allowing applications to close arbitrary streams.
362 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
363 * instead of closing it if it is the case.
364 */
365int lttng_ust_safe_fclose_stream(FILE *stream, int (*fclose_cb)(FILE *stream))
366{
367 int ret = 0, fd;
368
369 lttng_ust_fixup_fd_tracker_tls();
370
7d34f27d
MD
371 /*
372 * Ensure the tracker is initialized when called from
373 * constructors.
374 */
375 lttng_ust_init_fd_tracker();
376
52a20dc7
MD
377 /*
378 * If called from lttng-ust, we directly call fclose without
379 * validating whether the FD is part of the tracked set.
380 */
793d29c9 381 if (URCU_TLS(ust_fd_mutex_nest))
52a20dc7
MD
382 return fclose_cb(stream);
383
384 fd = fileno(stream);
385
386 lttng_ust_lock_fd_tracker();
387 if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) {
388 ret = -1;
389 errno = EBADF;
390 } else {
391 ret = fclose_cb(stream);
392 }
393 lttng_ust_unlock_fd_tracker();
394
395 return ret;
396}
397
6548fca4
MD
398#ifdef __OpenBSD__
399static void set_close_success(int *p)
400{
401 *p = 1;
402}
403static int test_close_success(const int *p)
404{
405 return *p;
406}
407#else
408static void set_close_success(int *p __attribute__((unused)))
409{
410}
411static int test_close_success(const int *p __attribute__((unused)))
412{
413 return 1;
414}
415#endif
416
417/*
418 * Implement helper for closefrom() override.
419 */
420int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int fd))
421{
422 int ret = 0, close_success = 0, i;
423
424 lttng_ust_fixup_fd_tracker_tls();
425
7d34f27d
MD
426 /*
427 * Ensure the tracker is initialized when called from
428 * constructors.
429 */
430 lttng_ust_init_fd_tracker();
431
6548fca4
MD
432 if (lowfd < 0) {
433 /*
434 * NetBSD return EBADF if fd is invalid.
435 */
436 errno = EBADF;
437 ret = -1;
438 goto end;
439 }
440 /*
441 * If called from lttng-ust, we directly call close without
442 * validating whether the FD is part of the tracked set.
443 */
793d29c9 444 if (URCU_TLS(ust_fd_mutex_nest)) {
6548fca4
MD
445 for (i = lowfd; i < lttng_ust_max_fd; i++) {
446 if (close_cb(i) < 0) {
447 switch (errno) {
448 case EBADF:
449 continue;
450 case EINTR:
451 default:
452 ret = -1;
453 goto end;
454 }
455 }
456 set_close_success(&close_success);
457 }
458 } else {
459 lttng_ust_lock_fd_tracker();
460 for (i = lowfd; i < lttng_ust_max_fd; i++) {
461 if (IS_FD_VALID(i) && IS_FD_SET(i, lttng_fd_set))
462 continue;
463 if (close_cb(i) < 0) {
464 switch (errno) {
465 case EBADF:
466 continue;
467 case EINTR:
468 default:
469 ret = -1;
470 lttng_ust_unlock_fd_tracker();
471 goto end;
472 }
473 }
474 set_close_success(&close_success);
475 }
476 lttng_ust_unlock_fd_tracker();
477 }
478 if (!test_close_success(&close_success)) {
479 /*
480 * OpenBSD return EBADF if fd is greater than all open
481 * file descriptors.
482 */
483 ret = -1;
484 errno = EBADF;
485 }
486end:
487 return ret;
488}
This page took 0.045843 seconds and 5 git commands to generate.