1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/* fdleak.c -- detect file descriptor leaks
Copyright (C) 2010-2021 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* config.h must be included first. */
#include <config.h>
/* system headers. */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#if HAVE_GETRLIMIT
# include <sys/resource.h>
#endif
#include <unistd.h>
/* gnulib headers. */
#include "cloexec.h"
#include "dirent--.h"
#include "error.h"
#include "fcntl--.h"
/* find headers. */
#include "system.h"
#include "extendbuf.h"
#include "fdleak.h"
#include "safe-atoi.h"
/* In order to detect FD leaks, we take a snapshot of the open
* file descriptors which are not FD_CLOEXEC when the program starts.
* When the program exits, we discover if there are any new
* file descriptors which aren't FD_CLOEXEC.
*/
static int *non_cloexec_fds;
static size_t num_cloexec_fds;
/* Determine the value of the largest open fd, on systems that
* offer /proc/self/fd. */
static int
get_proc_max_fd (void)
{
const char *path = "/proc/self/fd";
int maxfd = -1;
/* We don't use readdir_r, because we cannot trust pathconf
* to tell us the maximum possible length of a path in
* a given directory (the manpage for readdir_r claims this
* is the approved method, but the manpage for pathconf indicates
* that _PC_NAME_MAX is not an upper limit). */
DIR *dir = opendir (path);
if (dir)
{
int good = 0;
struct dirent *dent;
while (1)
{
errno = 0;
dent = readdir (dir);
if (NULL == dent)
{
if (errno)
{
error (0, errno, "%s", quotearg_n_style (0, locale_quoting_style, path));
good = 0;
}
break;
}
if (dent->d_name[0] != '.'
|| (dent->d_name[1] != 0
&& (dent->d_name[1] != '.' || dent->d_name[2] != 0)))
{
const int fd = safe_atoi (dent->d_name, literal_quoting_style);
if (fd > maxfd)
maxfd = fd;
good = 1;
}
}
closedir (dir);
if (good)
return maxfd;
}
return -1;
}
/* Estimate the value of the largest possible file descriptor */
static int
get_max_fd (void)
{
long open_max;
open_max = get_proc_max_fd ();
if (open_max >= 0)
return open_max;
open_max = sysconf (_SC_OPEN_MAX);
if (open_max == -1)
open_max = _POSIX_OPEN_MAX; /* underestimate */
/* We assume if RLIMIT_NOFILE is defined, all the related macros are, too. */
#if defined HAVE_GETRLIMIT && defined RLIMIT_NOFILE
{
struct rlimit fd_limit;
if (0 == getrlimit (RLIMIT_NOFILE, &fd_limit))
{
if (fd_limit.rlim_cur == RLIM_INFINITY)
return open_max;
else
return (int) fd_limit.rlim_cur;
}
}
#endif
/* cannot determine the limit's value */
return open_max;
}
static int
visit_open_fds (int fd_min, int fd_max,
int (*callback)(int, void*), void *cb_context)
{
enum { MAX_POLL = 64 };
struct pollfd pf[MAX_POLL];
int rv = 0;
while (fd_min < fd_max)
{
int i;
int limit = fd_max - fd_min;
if (limit > MAX_POLL)
limit = MAX_POLL;
for (i=0; i<limit; i++)
{
pf[i].events = POLLIN|POLLOUT;
pf[i].revents = 0;
pf[i].fd = fd_min + i;
}
rv = poll (pf, limit, 0);
if (-1 == rv)
{
return -1;
}
else
{
int j;
for (j=0; j<limit; j++)
{
if (pf[j].revents != POLLNVAL)
{
if (0 != (rv = callback (pf[j].fd, cb_context)))
return rv;
}
}
}
fd_min += limit;
}
return 0;
}
static int
fd_is_cloexec (int fd)
{
const int flags = fcntl (fd, F_GETFD);
return flags & FD_CLOEXEC;
}
/* Faking closures in C is a bit of a pain. */
struct remember_fd_context
{
int *buf;
size_t used;
size_t allocated;
};
/* Record FD is it's not FD_CLOEXEC. */
static int
remember_fd_if_non_cloexec (int fd, void *context)
{
if (fd_is_cloexec (fd))
{
return 0;
}
else
{
struct remember_fd_context * const p = context;
void *newbuf = extendbuf (p->buf,
sizeof (p->buf[0])*(p->used+1),
&(p->allocated));
if (newbuf)
{
p->buf = newbuf;
p->buf[p->used] = fd;
++p->used;
return 0;
}
else
{
return -1;
}
}
}
void
remember_non_cloexec_fds (void)
{
int max_fd = get_max_fd ();
struct remember_fd_context cb_data;
cb_data.buf = NULL;
cb_data.used = cb_data.allocated = 0;
if (max_fd < INT_MAX)
++max_fd;
visit_open_fds (0, max_fd, remember_fd_if_non_cloexec, &cb_data);
non_cloexec_fds = cb_data.buf;
num_cloexec_fds = cb_data.used;
}
struct fd_leak_context
{
const int *prev_buf;
size_t used;
size_t lookup_pos;
int leaked_fd;
};
/* FD is open and not close-on-exec.
* If it's not in the list of non-cloexec file descriptors we saw before, it's a leak.
*/
static int
find_first_leak_callback (int fd, void *context)
{
if (!fd_is_cloexec (fd))
{
struct fd_leak_context *p = context;
while (p->lookup_pos < p->used)
{
if (p->prev_buf[p->lookup_pos] < fd)
{
++p->lookup_pos;
}
else if (p->prev_buf[p->lookup_pos] == fd)
{
/* FD was open and still is, it's not a leak. */
return 0;
}
else
{
break;
}
}
/* We come here if p->prev_buf[p->lookup_pos] > fd, or
if we ran out of items in the lookup table.
Either way, this is a leak. */
p->leaked_fd = fd;
return -1; /* No more callbacks needed. */
}
return 0;
}
static int
find_first_leaked_fd (const int* prev_non_cloexec_fds, size_t n)
{
struct fd_leak_context context;
int max_fd = get_max_fd ();
if (max_fd < INT_MAX)
++max_fd;
context.prev_buf = prev_non_cloexec_fds;
context.used = n;
context.lookup_pos = 0;
context.leaked_fd = -1;
visit_open_fds (0, max_fd, find_first_leak_callback, &context);
return context.leaked_fd;
}
/* Determine if O_CLOEXEC actually works (Savannah bug #29435:
fd_is_cloexec () does not work on Fedora buildhosts).
*/
static bool
o_cloexec_works (void)
{
bool result = false;
int fd = open ("/", O_RDONLY|O_CLOEXEC);
if (fd >= 0)
{
result = fd_is_cloexec (fd);
close (fd);
}
return result;
}
int
open_cloexec (const char *path, int flags, ...)
{
int fd;
mode_t mode = 0;
static bool cloexec_works = false;
static bool cloexec_status_known = false;
if (flags & O_CREAT)
{
/* this code is copied from gnulib's open-safer.c. */
va_list ap;
va_start (ap, flags);
/* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
creates crashing code when 'mode_t' is smaller than 'int'. */
mode = va_arg (ap, PROMOTED_MODE_T);
va_end (ap);
}
/* Kernels usually ignore open flags they don't recognise, so it
* is possible this program was built against a library which
* defines O_CLOEXEC, but is running on a kernel that (silently)
* does not recognise it. We figure this out by just trying it,
* once.
*/
if (!cloexec_status_known)
{
cloexec_works = o_cloexec_works ();
cloexec_status_known = true;
}
fd = open (path, flags|O_CLOEXEC, mode);
if ((fd >= 0) && !(O_CLOEXEC && cloexec_works))
{
set_cloexec_flag (fd, true);
}
return fd;
}
void
forget_non_cloexec_fds (void)
{
free (non_cloexec_fds);
non_cloexec_fds = NULL;
num_cloexec_fds = 0;
}
/* Return nonzero if file descriptor leak-checking is enabled.
*/
bool
fd_leak_check_is_enabled (void)
{
if (getenv ("GNU_FINDUTILS_FD_LEAK_CHECK"))
return true;
else
return false;
}
void
complain_about_leaky_fds (void)
{
int no_leaks = 1;
const int leaking_fd = find_first_leaked_fd (non_cloexec_fds, num_cloexec_fds);
if (leaking_fd >= 0)
{
no_leaks = 0;
error (0, 0,
_("File descriptor %d will leak; please report this as a bug, "
"remembering to include a detailed description of the simplest "
"way to reproduce this problem."),
leaking_fd);
}
assert (no_leaks);
}
|