1 /* Test program for libdwfl file decriptors leakage. 2 Copyright (C) 2007, 2008 Red Hat, Inc. 3 This file is part of Red Hat elfutils. 4 5 Red Hat elfutils is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by the 7 Free Software Foundation; version 2 of the License. 8 9 Red Hat elfutils is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 General Public License for more details. 13 14 You should have received a copy of the GNU General Public License along 15 with Red Hat elfutils; if not, write to the Free Software Foundation, 16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 17 18 Red Hat elfutils is an included package of the Open Invention Network. 19 An included package of the Open Invention Network is a package for which 20 Open Invention Network licensees cross-license their patents. No patent 21 license is granted, either expressly or impliedly, by designation as an 22 included package. Should you wish to participate in the Open Invention 23 Network licensing program, please visit www.openinventionnetwork.com 24 <http://www.openinventionnetwork.com>. */ 25 26 #include <config.h> 27 #include <assert.h> 28 #include <inttypes.h> 29 #include <stdio.h> 30 #include <stdio_ext.h> 31 #include <locale.h> 32 #include <dirent.h> 33 #include <stdlib.h> 34 #include <errno.h> 35 #include <error.h> 36 #include <unistd.h> 37 #include <dwarf.h> 38 #include <sys/resource.h> 39 #include ELFUTILS_HEADER(dwfl) 40 41 42 static Dwfl * 43 elfutils_open (pid_t pid, Dwarf_Addr address) 44 { 45 static char *debuginfo_path; 46 static const Dwfl_Callbacks proc_callbacks = 47 { 48 .find_debuginfo = dwfl_standard_find_debuginfo, 49 .debuginfo_path = &debuginfo_path, 50 51 .find_elf = dwfl_linux_proc_find_elf, 52 }; 53 Dwfl *dwfl = dwfl_begin (&proc_callbacks); 54 if (dwfl == NULL) 55 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1)); 56 57 int result = dwfl_linux_proc_report (dwfl, pid); 58 if (result < 0) 59 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1)); 60 else if (result > 0) 61 error (2, result, "dwfl_linux_proc_report"); 62 63 if (dwfl_report_end (dwfl, NULL, NULL) != 0) 64 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1)); 65 66 Dwarf_Addr bias; 67 Dwarf *dbg = dwfl_addrdwarf (dwfl, address, &bias); 68 if (dbg != NULL) 69 { 70 Elf *elf = dwarf_getelf (dbg); 71 if (elf == NULL) 72 error (2, 0, "dwarf_getelf: %s", dwarf_errmsg (-1)); 73 } 74 else 75 { 76 Elf *elf = dwfl_module_getelf (dwfl_addrmodule (dwfl, address), &bias); 77 if (elf == NULL) 78 error (2, 0, "dwfl_module_getelf: %s", dwfl_errmsg (-1)); 79 } 80 81 return dwfl; 82 } 83 84 static void 85 elfutils_close (Dwfl *dwfl) 86 { 87 dwfl_end (dwfl); 88 } 89 90 int 91 main (void) 92 { 93 /* We use no threads here which can interfere with handling a stream. */ 94 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); 95 96 /* Set locale. */ 97 (void) setlocale (LC_ALL, ""); 98 99 struct rlimit fd_limit = { .rlim_cur = 32, .rlim_max = 32 }; 100 if (setrlimit (RLIMIT_NOFILE, &fd_limit) < 0) 101 error (2, errno, "setrlimit"); 102 103 for (int i = 0; i < 5000; ++i) 104 { 105 Dwfl *dwfl = elfutils_open (getpid (), (Dwarf_Addr) (uintptr_t) &main); 106 elfutils_close (dwfl); 107 } 108 109 return 0; 110 } 111