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