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