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