|
Packit Service |
97d2fb |
/* Test program for libdwfl file decriptors leakage.
|
|
Packit Service |
97d2fb |
Copyright (C) 2007, 2008 Red Hat, Inc.
|
|
Packit Service |
97d2fb |
This file is part of elfutils.
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
This file is free software; you can redistribute it and/or modify
|
|
Packit Service |
97d2fb |
it under the terms of the GNU General Public License as published by
|
|
Packit Service |
97d2fb |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
97d2fb |
(at your option) any later version.
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
elfutils is distributed in the hope that it will be useful, but
|
|
Packit Service |
97d2fb |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
97d2fb |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
97d2fb |
GNU General Public License for more details.
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
You should have received a copy of the GNU General Public License
|
|
Packit Service |
97d2fb |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
#include <config.h>
|
|
Packit Service |
97d2fb |
#include <assert.h>
|
|
Packit Service |
97d2fb |
#include <inttypes.h>
|
|
Packit Service |
97d2fb |
#include <stdio.h>
|
|
Packit Service |
97d2fb |
#include <stdio_ext.h>
|
|
Packit Service |
97d2fb |
#include <locale.h>
|
|
Packit Service |
97d2fb |
#include <dirent.h>
|
|
Packit Service |
97d2fb |
#include <stdlib.h>
|
|
Packit Service |
97d2fb |
#include <errno.h>
|
|
Packit Service |
97d2fb |
#include <unistd.h>
|
|
Packit Service |
97d2fb |
#include <dwarf.h>
|
|
Packit Service |
97d2fb |
#include "system.h"
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
#ifndef __linux__
|
|
Packit Service |
97d2fb |
int
|
|
Packit Service |
97d2fb |
main (void)
|
|
Packit Service |
97d2fb |
{
|
|
Packit Service |
97d2fb |
return 77; /* dwfl_linux_proc_report is linux specific. */
|
|
Packit Service |
97d2fb |
}
|
|
Packit Service |
97d2fb |
#else
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
#include <sys/resource.h>
|
|
Packit Service |
97d2fb |
#include ELFUTILS_HEADER(dwfl)
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
static Dwfl *
|
|
Packit Service |
97d2fb |
elfutils_open (pid_t pid, Dwarf_Addr address)
|
|
Packit Service |
97d2fb |
{
|
|
Packit Service |
97d2fb |
static char *debuginfo_path;
|
|
Packit Service |
97d2fb |
static const Dwfl_Callbacks proc_callbacks =
|
|
Packit Service |
97d2fb |
{
|
|
Packit Service |
97d2fb |
.find_debuginfo = dwfl_standard_find_debuginfo,
|
|
Packit Service |
97d2fb |
.debuginfo_path = &debuginfo_path,
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
.find_elf = dwfl_linux_proc_find_elf,
|
|
Packit Service |
97d2fb |
};
|
|
Packit Service |
97d2fb |
Dwfl *dwfl = dwfl_begin (&proc_callbacks);
|
|
Packit Service |
97d2fb |
if (dwfl == NULL)
|
|
Packit Service |
97d2fb |
error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
int result = dwfl_linux_proc_report (dwfl, pid);
|
|
Packit Service |
97d2fb |
if (result < 0)
|
|
Packit Service |
97d2fb |
error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
|
|
Packit Service |
97d2fb |
else if (result > 0)
|
|
Packit Service |
97d2fb |
error (2, result, "dwfl_linux_proc_report");
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
if (dwfl_report_end (dwfl, NULL, NULL) != 0)
|
|
Packit Service |
97d2fb |
error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
Dwarf_Addr bias;
|
|
Packit Service |
97d2fb |
Dwarf *dbg = dwfl_addrdwarf (dwfl, address, &bias);
|
|
Packit Service |
97d2fb |
if (dbg != NULL)
|
|
Packit Service |
97d2fb |
{
|
|
Packit Service |
97d2fb |
Elf *elf = dwarf_getelf (dbg);
|
|
Packit Service |
97d2fb |
if (elf == NULL)
|
|
Packit Service |
97d2fb |
error (2, 0, "dwarf_getelf: %s", dwarf_errmsg (-1));
|
|
Packit Service |
97d2fb |
}
|
|
Packit Service |
97d2fb |
else
|
|
Packit Service |
97d2fb |
{
|
|
Packit Service |
97d2fb |
Dwfl_Module *module = dwfl_addrmodule (dwfl, address);
|
|
Packit Service |
97d2fb |
if (module == NULL)
|
|
Packit Service |
97d2fb |
error (2, 0, "dwfl_addrmodule: no module available for 0x%" PRIx64 "",
|
|
Packit Service |
97d2fb |
address);
|
|
Packit Service |
97d2fb |
Elf *elf = dwfl_module_getelf (module, &bias);
|
|
Packit Service |
97d2fb |
if (elf == NULL)
|
|
Packit Service |
97d2fb |
error (2, 0, "dwfl_module_getelf: %s", dwfl_errmsg (-1));
|
|
Packit Service |
97d2fb |
}
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
return dwfl;
|
|
Packit Service |
97d2fb |
}
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
static void
|
|
Packit Service |
97d2fb |
elfutils_close (Dwfl *dwfl)
|
|
Packit Service |
97d2fb |
{
|
|
Packit Service |
97d2fb |
dwfl_end (dwfl);
|
|
Packit Service |
97d2fb |
}
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
int
|
|
Packit Service |
97d2fb |
main (void)
|
|
Packit Service |
97d2fb |
{
|
|
Packit Service |
97d2fb |
/* We use no threads here which can interfere with handling a stream. */
|
|
Packit Service |
97d2fb |
(void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
/* Set locale. */
|
|
Packit Service |
97d2fb |
(void) setlocale (LC_ALL, "");
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
/* Get both the soft and hard limits first. The soft limit is what
|
|
Packit Service |
97d2fb |
will be enforced against the process. The hard limit is the max
|
|
Packit Service |
97d2fb |
that can be set for the soft limit. We don't want to lower it
|
|
Packit Service |
97d2fb |
because we might not be able to (under valgrind). */
|
|
Packit Service |
97d2fb |
struct rlimit fd_limit;
|
|
Packit Service |
97d2fb |
if (getrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
|
|
Packit Service |
97d2fb |
error (2, errno, "getrlimit");
|
|
Packit Service |
97d2fb |
fd_limit.rlim_cur = 32;
|
|
Packit Service |
97d2fb |
if (setrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
|
|
Packit Service |
97d2fb |
error (2, errno, "setrlimit");
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
for (int i = 0; i < 5000; ++i)
|
|
Packit Service |
97d2fb |
{
|
|
Packit Service |
97d2fb |
Dwfl *dwfl = elfutils_open (getpid (), (Dwarf_Addr) (uintptr_t) &main);
|
|
Packit Service |
97d2fb |
elfutils_close (dwfl);
|
|
Packit Service |
97d2fb |
}
|
|
Packit Service |
97d2fb |
|
|
Packit Service |
97d2fb |
return 0;
|
|
Packit Service |
97d2fb |
}
|
|
Packit Service |
97d2fb |
#endif
|