9909c0606a45044ab33f37ed09603d87d60dcf3c
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-fd-tracker.c
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>
35 #include <signal.h>
36 #include <stdbool.h>
37 #include <urcu/compiler.h>
38 #include <urcu/tls-compat.h>
39 #include <urcu/system.h>
40
41 #include <ust-fd.h>
42 #include <helper.h>
43 #include <lttng/ust-error.h>
44 #include <usterr-signal-safe.h>
45 #include <lttng/ust-cancelstate.h>
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)
53 #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
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.
67 *
68 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
69 * is also held across fork.
70 */
71 static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
72
73 /*
74 * Track whether we are within lttng-ust or application, for close
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.
78 */
79 static DEFINE_URCU_TLS(int, ust_fd_mutex_nest);
80
81 /* fd_set used to book keep fd being used by lttng-ust. */
82 static fd_set *lttng_fd_set;
83 static int lttng_ust_max_fd;
84 static int num_fd_sets;
85 static int init_done;
86
87 /*
88 * Force a read (imply TLS fixup for dlopen) of TLS variables.
89 */
90 void lttng_ust_fixup_fd_tracker_tls(void)
91 {
92 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest)));
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 */
100 void lttng_ust_init_fd_tracker(void)
101 {
102 struct rlimit rlim;
103 int i;
104
105 if (CMM_LOAD_SHARED(init_done))
106 return;
107
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]));
131 CMM_STORE_SHARED(init_done, 1);
132 }
133
134 void lttng_ust_lock_fd_tracker(void)
135 {
136 sigset_t sig_all_blocked, orig_mask;
137 int ret;
138
139 if (lttng_ust_cancelstate_disable_push()) {
140 ERR("lttng_ust_cancelstate_disable_push");
141 }
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);
154 }
155 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
156 if (ret) {
157 ERR("pthread_sigmask: %s", strerror(ret));
158 }
159 }
160
161 void lttng_ust_unlock_fd_tracker(void)
162 {
163 sigset_t sig_all_blocked, orig_mask;
164 int ret;
165
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 }
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();
176 if (!--URCU_TLS(ust_fd_mutex_nest)) {
177 pthread_mutex_unlock(&ust_safe_guard_fd_mutex);
178 }
179 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
180 if (ret) {
181 ERR("pthread_sigmask: %s", strerror(ret));
182 }
183 if (lttng_ust_cancelstate_disable_pop()) {
184 ERR("lttng_ust_cancelstate_disable_pop");
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.037926 seconds and 3 git commands to generate.