d56288e370470e1deb94a25dc5603048c75f03aa
[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 <urcu/compiler.h>
36 #include <urcu/tls-compat.h>
37
38 #include <ust-fd.h>
39 #include <helper.h>
40 #include <lttng/ust-error.h>
41 #include <usterr-signal-safe.h>
42
43 #include "../liblttng-ust/compat.h"
44
45 /* Operations on the fd set. */
46 #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
47 #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
48 #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
49
50 /* Check fd validity before calling these. */
51 #define ADD_FD_TO_SET(fd, fd_sets) \
52 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
53 #define IS_FD_SET(fd, fd_sets) \
54 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
55 #define DEL_FD_FROM_SET(fd, fd_sets) \
56 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
57
58 /*
59 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
60 * within the libc dl lock. Therefore, we need to fixup the TLS before
61 * nesting into this lock.
62 */
63 static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
64 /*
65 * Track whether we are within lttng-ust or application, for close
66 * system call override by LD_PRELOAD library.
67 */
68 static DEFINE_URCU_TLS(int, thread_fd_tracking);
69
70 /* fd_set used to book keep fd being used by lttng-ust. */
71 static fd_set *lttng_fd_set;
72 static int lttng_ust_max_fd;
73 static int num_fd_sets;
74
75 /*
76 * Force a read (imply TLS fixup for dlopen) of TLS variables.
77 */
78 void lttng_ust_fixup_fd_tracker_tls(void)
79 {
80 asm volatile ("" : : "m" (URCU_TLS(thread_fd_tracking)));
81 }
82
83 /*
84 * Allocate the fd set array based on the hard limit set for this
85 * process. This will be called during the constructor execution
86 * and will also be called in the child after fork via lttng_ust_init.
87 */
88 void lttng_ust_init_fd_tracker(void)
89 {
90 struct rlimit rlim;
91 int i;
92
93 memset(&rlim, 0, sizeof(rlim));
94 /* Get the current possible max number of fd for this process. */
95 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
96 abort();
97 /*
98 * FD set array size determined using the hard limit. Even if
99 * the process wishes to increase its limit using setrlimit, it
100 * can only do so with the softlimit which will be less than the
101 * hard limit.
102 */
103 lttng_ust_max_fd = rlim.rlim_max;
104 num_fd_sets = lttng_ust_max_fd / FD_SETSIZE;
105 if (lttng_ust_max_fd % FD_SETSIZE)
106 ++num_fd_sets;
107 if (lttng_fd_set != NULL) {
108 free(lttng_fd_set);
109 lttng_fd_set = NULL;
110 }
111 lttng_fd_set = malloc(num_fd_sets * (sizeof(fd_set)));
112 if (!lttng_fd_set)
113 abort();
114 for (i = 0; i < num_fd_sets; i++)
115 FD_ZERO((&lttng_fd_set[i]));
116 }
117
118 void lttng_ust_lock_fd_tracker(void)
119 {
120 URCU_TLS(thread_fd_tracking) = 1;
121 /*
122 * Ensure the compiler don't move the store after the close()
123 * call in case close() would be marked as leaf.
124 */
125 cmm_barrier();
126 pthread_mutex_lock(&ust_safe_guard_fd_mutex);
127 }
128
129 void lttng_ust_unlock_fd_tracker(void)
130 {
131 pthread_mutex_unlock(&ust_safe_guard_fd_mutex);
132 /*
133 * Ensure the compiler don't move the store before the close()
134 * call, in case close() would be marked as leaf.
135 */
136 cmm_barrier();
137 URCU_TLS(thread_fd_tracking) = 0;
138 }
139
140 /*
141 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
142 * Has strict checking of fd validity.
143 */
144 void lttng_ust_add_fd_to_tracker(int fd)
145 {
146 assert(URCU_TLS(thread_fd_tracking));
147 /* Trying to add an fd which we can not accommodate. */
148 assert(IS_FD_VALID(fd));
149 /* Setting an fd thats already set. */
150 assert(!IS_FD_SET(fd, lttng_fd_set));
151
152 ADD_FD_TO_SET(fd, lttng_fd_set);
153 }
154
155 /*
156 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
157 * Has strict checking for fd validity.
158 */
159 void lttng_ust_delete_fd_from_tracker(int fd)
160 {
161 assert(URCU_TLS(thread_fd_tracking));
162 /* Not a valid fd. */
163 assert(IS_FD_VALID(fd));
164 /* Deleting an fd which was not set. */
165 assert(IS_FD_SET(fd, lttng_fd_set));
166
167 DEL_FD_FROM_SET(fd, lttng_fd_set);
168 }
169
170 /*
171 * Interface allowing applications to close arbitrary file descriptors.
172 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
173 * instead of closing it if it is the case.
174 */
175 int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int fd))
176 {
177 int ret = 0;
178
179 lttng_ust_fixup_fd_tracker_tls();
180
181 /*
182 * If called from lttng-ust, we directly call close without
183 * validating whether the FD is part of the tracked set.
184 */
185 if (URCU_TLS(thread_fd_tracking))
186 return close_cb(fd);
187
188 lttng_ust_lock_fd_tracker();
189 if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) {
190 ret = -1;
191 errno = EBADF;
192 } else {
193 ret = close_cb(fd);
194 }
195 lttng_ust_unlock_fd_tracker();
196
197 return ret;
198 }
199
200 /*
201 * Interface allowing applications to close arbitrary streams.
202 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
203 * instead of closing it if it is the case.
204 */
205 int lttng_ust_safe_fclose_stream(FILE *stream, int (*fclose_cb)(FILE *stream))
206 {
207 int ret = 0, fd;
208
209 lttng_ust_fixup_fd_tracker_tls();
210
211 /*
212 * If called from lttng-ust, we directly call fclose without
213 * validating whether the FD is part of the tracked set.
214 */
215 if (URCU_TLS(thread_fd_tracking))
216 return fclose_cb(stream);
217
218 fd = fileno(stream);
219
220 lttng_ust_lock_fd_tracker();
221 if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) {
222 ret = -1;
223 errno = EBADF;
224 } else {
225 ret = fclose_cb(stream);
226 }
227 lttng_ust_unlock_fd_tracker();
228
229 return ret;
230 }
231
232 #ifdef __OpenBSD__
233 static void set_close_success(int *p)
234 {
235 *p = 1;
236 }
237 static int test_close_success(const int *p)
238 {
239 return *p;
240 }
241 #else
242 static void set_close_success(int *p __attribute__((unused)))
243 {
244 }
245 static int test_close_success(const int *p __attribute__((unused)))
246 {
247 return 1;
248 }
249 #endif
250
251 /*
252 * Implement helper for closefrom() override.
253 */
254 int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int fd))
255 {
256 int ret = 0, close_success = 0, i;
257
258 lttng_ust_fixup_fd_tracker_tls();
259
260 if (lowfd < 0) {
261 /*
262 * NetBSD return EBADF if fd is invalid.
263 */
264 errno = EBADF;
265 ret = -1;
266 goto end;
267 }
268 /*
269 * If called from lttng-ust, we directly call close without
270 * validating whether the FD is part of the tracked set.
271 */
272 if (URCU_TLS(thread_fd_tracking)) {
273 for (i = lowfd; i < lttng_ust_max_fd; i++) {
274 if (close_cb(i) < 0) {
275 switch (errno) {
276 case EBADF:
277 continue;
278 case EINTR:
279 default:
280 ret = -1;
281 goto end;
282 }
283 }
284 set_close_success(&close_success);
285 }
286 } else {
287 lttng_ust_lock_fd_tracker();
288 for (i = lowfd; i < lttng_ust_max_fd; i++) {
289 if (IS_FD_VALID(i) && IS_FD_SET(i, lttng_fd_set))
290 continue;
291 if (close_cb(i) < 0) {
292 switch (errno) {
293 case EBADF:
294 continue;
295 case EINTR:
296 default:
297 ret = -1;
298 lttng_ust_unlock_fd_tracker();
299 goto end;
300 }
301 }
302 set_close_success(&close_success);
303 }
304 lttng_ust_unlock_fd_tracker();
305 }
306 if (!test_close_success(&close_success)) {
307 /*
308 * OpenBSD return EBADF if fd is greater than all open
309 * file descriptors.
310 */
311 ret = -1;
312 errno = EBADF;
313 }
314 end:
315 return ret;
316 }
This page took 0.034544 seconds and 3 git commands to generate.