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