Commit | Line | Data |
---|---|---|
6548fca4 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
6548fca4 | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2016 Aravind HT <aravind.ht@gmail.com> |
5 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
6548fca4 MD |
6 | */ |
7 | ||
6548fca4 MD |
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> | |
96a6162e MD |
22 | #include <signal.h> |
23 | #include <stdbool.h> | |
6548fca4 MD |
24 | #include <urcu/compiler.h> |
25 | #include <urcu/tls-compat.h> | |
7d34f27d | 26 | #include <urcu/system.h> |
6548fca4 | 27 | |
9d315d6d MJ |
28 | #include "common/ust-fd.h" |
29 | #include "common/macros.h" | |
6548fca4 | 30 | #include <lttng/ust-error.h> |
59e57035 | 31 | #include <lttng/ust-cancelstate.h> |
9d315d6d | 32 | #include "common/logging.h" |
6548fca4 | 33 | |
fca97dfd MJ |
34 | #include "lib/lttng-ust-common/fd-tracker.h" |
35 | ||
6548fca4 MD |
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) | |
f5c453e9 | 40 | #define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO) |
6548fca4 MD |
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 | |
a9fd951a | 52 | * within the libc dl lock. Therefore, we need to allocate the TLS before |
6548fca4 | 53 | * nesting into this lock. |
c1be081a MD |
54 | * |
55 | * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex | |
56 | * is also held across fork. | |
6548fca4 MD |
57 | */ |
58 | static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER; | |
283f4bec | 59 | |
6548fca4 MD |
60 | /* |
61 | * Track whether we are within lttng-ust or application, for close | |
793d29c9 MD |
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. | |
6548fca4 | 65 | */ |
96a6162e | 66 | static DEFINE_URCU_TLS(int, ust_fd_mutex_nest); |
6548fca4 MD |
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; | |
7d34f27d | 72 | static int init_done; |
6548fca4 MD |
73 | |
74 | /* | |
a9fd951a | 75 | * Force a read (imply TLS allocation for dlopen) of TLS variables. |
6548fca4 | 76 | */ |
a9fd951a | 77 | void lttng_ust_fd_tracker_alloc_tls(void) |
6548fca4 | 78 | { |
96a6162e | 79 | asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest))); |
6548fca4 MD |
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 | */ | |
fca97dfd | 87 | void lttng_ust_fd_tracker_init(void) |
6548fca4 MD |
88 | { |
89 | struct rlimit rlim; | |
90 | int i; | |
91 | ||
7d34f27d MD |
92 | if (CMM_LOAD_SHARED(init_done)) |
93 | return; | |
94 | ||
6548fca4 MD |
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((<tng_fd_set[i])); | |
7d34f27d | 118 | CMM_STORE_SHARED(init_done, 1); |
6548fca4 MD |
119 | } |
120 | ||
121 | void lttng_ust_lock_fd_tracker(void) | |
122 | { | |
96a6162e | 123 | sigset_t sig_all_blocked, orig_mask; |
59e57035 | 124 | int ret; |
283f4bec | 125 | |
59e57035 MD |
126 | if (lttng_ust_cancelstate_disable_push()) { |
127 | ERR("lttng_ust_cancelstate_disable_push"); | |
283f4bec | 128 | } |
96a6162e MD |
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); | |
96a6162e MD |
141 | } |
142 | ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL); | |
143 | if (ret) { | |
144 | ERR("pthread_sigmask: %s", strerror(ret)); | |
145 | } | |
6548fca4 MD |
146 | } |
147 | ||
148 | void lttng_ust_unlock_fd_tracker(void) | |
149 | { | |
96a6162e | 150 | sigset_t sig_all_blocked, orig_mask; |
59e57035 | 151 | int ret; |
283f4bec | 152 | |
96a6162e MD |
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 | } | |
6548fca4 MD |
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(); | |
96a6162e | 163 | if (!--URCU_TLS(ust_fd_mutex_nest)) { |
96a6162e MD |
164 | pthread_mutex_unlock(&ust_safe_guard_fd_mutex); |
165 | } | |
166 | ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL); | |
283f4bec | 167 | if (ret) { |
96a6162e MD |
168 | ERR("pthread_sigmask: %s", strerror(ret)); |
169 | } | |
59e57035 MD |
170 | if (lttng_ust_cancelstate_disable_pop()) { |
171 | ERR("lttng_ust_cancelstate_disable_pop"); | |
283f4bec | 172 | } |
6548fca4 MD |
173 | } |
174 | ||
f5c453e9 JR |
175 | static int dup_std_fd(int fd) |
176 | { | |
5a4d96d1 | 177 | int ret, i; |
f5c453e9 JR |
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 */ | |
5a4d96d1 | 202 | for (i = 0; i < STDERR_FILENO + 1; i++) { |
f5c453e9 JR |
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 */ | |
5a4d96d1 | 223 | for (i = 0; i < fd_to_close_count; i++) { |
f5c453e9 JR |
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 | ||
6548fca4 MD |
241 | /* |
242 | * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd. | |
243 | * Has strict checking of fd validity. | |
f5c453e9 JR |
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 | * | |
6548fca4 | 253 | */ |
f5c453e9 | 254 | int lttng_ust_add_fd_to_tracker(int fd) |
6548fca4 | 255 | { |
f5c453e9 | 256 | int ret; |
7d34f27d MD |
257 | /* |
258 | * Ensure the tracker is initialized when called from | |
259 | * constructors. | |
260 | */ | |
fca97dfd | 261 | lttng_ust_fd_tracker_init(); |
96a6162e | 262 | assert(URCU_TLS(ust_fd_mutex_nest)); |
f5c453e9 JR |
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 | ||
6548fca4 MD |
272 | /* Trying to add an fd which we can not accommodate. */ |
273 | assert(IS_FD_VALID(fd)); | |
274 | /* Setting an fd thats already set. */ | |
275 | assert(!IS_FD_SET(fd, lttng_fd_set)); | |
276 | ||
277 | ADD_FD_TO_SET(fd, lttng_fd_set); | |
f5c453e9 JR |
278 | return fd; |
279 | error: | |
280 | return ret; | |
6548fca4 MD |
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 | { | |
7d34f27d MD |
289 | /* |
290 | * Ensure the tracker is initialized when called from | |
291 | * constructors. | |
292 | */ | |
fca97dfd | 293 | lttng_ust_fd_tracker_init(); |
7d34f27d | 294 | |
96a6162e | 295 | assert(URCU_TLS(ust_fd_mutex_nest)); |
6548fca4 MD |
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 | ||
a9fd951a | 313 | lttng_ust_fd_tracker_alloc_tls(); |
6548fca4 | 314 | |
7d34f27d MD |
315 | /* |
316 | * Ensure the tracker is initialized when called from | |
317 | * constructors. | |
318 | */ | |
fca97dfd | 319 | lttng_ust_fd_tracker_init(); |
7d34f27d | 320 | |
6548fca4 MD |
321 | /* |
322 | * If called from lttng-ust, we directly call close without | |
323 | * validating whether the FD is part of the tracked set. | |
324 | */ | |
793d29c9 | 325 | if (URCU_TLS(ust_fd_mutex_nest)) |
6548fca4 MD |
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 | ||
52a20dc7 MD |
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 | ||
a9fd951a | 349 | lttng_ust_fd_tracker_alloc_tls(); |
52a20dc7 | 350 | |
7d34f27d MD |
351 | /* |
352 | * Ensure the tracker is initialized when called from | |
353 | * constructors. | |
354 | */ | |
fca97dfd | 355 | lttng_ust_fd_tracker_init(); |
7d34f27d | 356 | |
52a20dc7 MD |
357 | /* |
358 | * If called from lttng-ust, we directly call fclose without | |
359 | * validating whether the FD is part of the tracked set. | |
360 | */ | |
793d29c9 | 361 | if (URCU_TLS(ust_fd_mutex_nest)) |
52a20dc7 MD |
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 | ||
6548fca4 MD |
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 | ||
a9fd951a | 404 | lttng_ust_fd_tracker_alloc_tls(); |
6548fca4 | 405 | |
7d34f27d MD |
406 | /* |
407 | * Ensure the tracker is initialized when called from | |
408 | * constructors. | |
409 | */ | |
fca97dfd | 410 | lttng_ust_fd_tracker_init(); |
7d34f27d | 411 | |
6548fca4 MD |
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 | */ | |
793d29c9 | 424 | if (URCU_TLS(ust_fd_mutex_nest)) { |
6548fca4 MD |
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 | } |