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