Fix: nestable pthread cancelstate
[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
MD
80
81/* fd_set used to book keep fd being used by lttng-ust. */
82static fd_set *lttng_fd_set;
83static int lttng_ust_max_fd;
84static int num_fd_sets;
7d34f27d 85static int init_done;
6548fca4
MD
86
87/*
88 * Force a read (imply TLS fixup for dlopen) of TLS variables.
89 */
90void lttng_ust_fixup_fd_tracker_tls(void)
91{
96a6162e 92 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest)));
6548fca4
MD
93}
94
95/*
96 * Allocate the fd set array based on the hard limit set for this
97 * process. This will be called during the constructor execution
98 * and will also be called in the child after fork via lttng_ust_init.
99 */
100void lttng_ust_init_fd_tracker(void)
101{
102 struct rlimit rlim;
103 int i;
104
7d34f27d
MD
105 if (CMM_LOAD_SHARED(init_done))
106 return;
107
6548fca4
MD
108 memset(&rlim, 0, sizeof(rlim));
109 /* Get the current possible max number of fd for this process. */
110 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
111 abort();
112 /*
113 * FD set array size determined using the hard limit. Even if
114 * the process wishes to increase its limit using setrlimit, it
115 * can only do so with the softlimit which will be less than the
116 * hard limit.
117 */
118 lttng_ust_max_fd = rlim.rlim_max;
119 num_fd_sets = lttng_ust_max_fd / FD_SETSIZE;
120 if (lttng_ust_max_fd % FD_SETSIZE)
121 ++num_fd_sets;
122 if (lttng_fd_set != NULL) {
123 free(lttng_fd_set);
124 lttng_fd_set = NULL;
125 }
126 lttng_fd_set = malloc(num_fd_sets * (sizeof(fd_set)));
127 if (!lttng_fd_set)
128 abort();
129 for (i = 0; i < num_fd_sets; i++)
130 FD_ZERO((&lttng_fd_set[i]));
7d34f27d 131 CMM_STORE_SHARED(init_done, 1);
6548fca4
MD
132}
133
134void lttng_ust_lock_fd_tracker(void)
135{
96a6162e 136 sigset_t sig_all_blocked, orig_mask;
595c1577 137 int ret;
283f4bec 138
595c1577
MD
139 if (lttng_ust_cancelstate_disable_push()) {
140 ERR("lttng_ust_cancelstate_disable_push");
283f4bec 141 }
96a6162e
MD
142 sigfillset(&sig_all_blocked);
143 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
144 if (ret) {
145 ERR("pthread_sigmask: %s", strerror(ret));
146 }
147 if (!URCU_TLS(ust_fd_mutex_nest)++) {
148 /*
149 * Ensure the compiler don't move the store after the close()
150 * call in case close() would be marked as leaf.
151 */
152 cmm_barrier();
153 pthread_mutex_lock(&ust_safe_guard_fd_mutex);
96a6162e
MD
154 }
155 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
156 if (ret) {
157 ERR("pthread_sigmask: %s", strerror(ret));
158 }
6548fca4
MD
159}
160
161void lttng_ust_unlock_fd_tracker(void)
162{
96a6162e 163 sigset_t sig_all_blocked, orig_mask;
595c1577 164 int ret;
283f4bec 165
96a6162e
MD
166 sigfillset(&sig_all_blocked);
167 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
168 if (ret) {
169 ERR("pthread_sigmask: %s", strerror(ret));
170 }
6548fca4
MD
171 /*
172 * Ensure the compiler don't move the store before the close()
173 * call, in case close() would be marked as leaf.
174 */
175 cmm_barrier();
96a6162e 176 if (!--URCU_TLS(ust_fd_mutex_nest)) {
96a6162e
MD
177 pthread_mutex_unlock(&ust_safe_guard_fd_mutex);
178 }
179 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
283f4bec 180 if (ret) {
96a6162e
MD
181 ERR("pthread_sigmask: %s", strerror(ret));
182 }
595c1577
MD
183 if (lttng_ust_cancelstate_disable_pop()) {
184 ERR("lttng_ust_cancelstate_disable_pop");
283f4bec 185 }
6548fca4
MD
186}
187
f5c453e9
JR
188static int dup_std_fd(int fd)
189{
5a4d96d1 190 int ret, i;
f5c453e9
JR
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 */
5a4d96d1 215 for (i = 0; i < STDERR_FILENO + 1; i++) {
f5c453e9
JR
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 */
5a4d96d1 236 for (i = 0; i < fd_to_close_count; i++) {
f5c453e9
JR
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;
250error:
251 return ret;
252}
253
6548fca4
MD
254/*
255 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
256 * Has strict checking of fd validity.
f5c453e9
JR
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 *
6548fca4 266 */
f5c453e9 267int lttng_ust_add_fd_to_tracker(int fd)
6548fca4 268{
f5c453e9 269 int ret;
7d34f27d
MD
270 /*
271 * Ensure the tracker is initialized when called from
272 * constructors.
273 */
274 lttng_ust_init_fd_tracker();
96a6162e 275 assert(URCU_TLS(ust_fd_mutex_nest));
f5c453e9
JR
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
6548fca4
MD
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);
f5c453e9
JR
291 return fd;
292error:
293 return ret;
6548fca4
MD
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 */
300void lttng_ust_delete_fd_from_tracker(int fd)
301{
7d34f27d
MD
302 /*
303 * Ensure the tracker is initialized when called from
304 * constructors.
305 */
306 lttng_ust_init_fd_tracker();
307
96a6162e 308 assert(URCU_TLS(ust_fd_mutex_nest));
6548fca4
MD
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 */
322int 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
7d34f27d
MD
328 /*
329 * Ensure the tracker is initialized when called from
330 * constructors.
331 */
332 lttng_ust_init_fd_tracker();
333
6548fca4
MD
334 /*
335 * If called from lttng-ust, we directly call close without
336 * validating whether the FD is part of the tracked set.
337 */
793d29c9 338 if (URCU_TLS(ust_fd_mutex_nest))
6548fca4
MD
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
52a20dc7
MD
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 */
358int 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
7d34f27d
MD
364 /*
365 * Ensure the tracker is initialized when called from
366 * constructors.
367 */
368 lttng_ust_init_fd_tracker();
369
52a20dc7
MD
370 /*
371 * If called from lttng-ust, we directly call fclose without
372 * validating whether the FD is part of the tracked set.
373 */
793d29c9 374 if (URCU_TLS(ust_fd_mutex_nest))
52a20dc7
MD
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
6548fca4
MD
391#ifdef __OpenBSD__
392static void set_close_success(int *p)
393{
394 *p = 1;
395}
396static int test_close_success(const int *p)
397{
398 return *p;
399}
400#else
401static void set_close_success(int *p __attribute__((unused)))
402{
403}
404static 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 */
413int 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
7d34f27d
MD
419 /*
420 * Ensure the tracker is initialized when called from
421 * constructors.
422 */
423 lttng_ust_init_fd_tracker();
424
6548fca4
MD
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 */
793d29c9 437 if (URCU_TLS(ust_fd_mutex_nest)) {
6548fca4
MD
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 }
479end:
480 return ret;
481}
This page took 0.046712 seconds and 5 git commands to generate.