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