Files
usrbinjava/src/filedesc.c
Athenas Jimenez 94bb9a8dc5 Initial commit
Signed-off-by: Athenas Jimenez <athenas.jimenez.gonzalez@intel.com>
2019-10-30 18:41:22 +00:00

76 lines
1.9 KiB
C

/*
* Software Updater - client side
*
* Copyright © 2012-2015 Intel Corporation.
*
* 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, version 2 or later of the License.
*
* 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 <http://www.gnu.org/licenses/>.
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
void dump_file_descriptor_leaks(void)
{
DIR *dir;
struct dirent *entry;
dir = opendir("/proc/self/fd");
if (!dir)
return;
while (1) {
char *filename;
char buffer[PATH_MAX + 1];
entry = readdir(dir);
size_t size;
if (!entry)
break;
if (strcmp(entry->d_name, ".") == 0)
continue;
if (strcmp(entry->d_name, "..") == 0)
continue;
/* skip stdin/out/err */
if (strcmp(entry->d_name, "0") == 0)
continue;
if (strcmp(entry->d_name, "1") == 0)
continue;
if (strcmp(entry->d_name, "2") == 0)
continue;
/* we hold an fd open, the one from opendir above */
sprintf(buffer, "%i", dirfd(dir));
if (strcmp(entry->d_name, buffer) == 0)
continue;
if (asprintf(&filename, "/proc/self/fd/%s", entry->d_name) <= 0)
abort();
memset(&buffer, 0, sizeof(buffer));
size = readlink(filename, buffer, PATH_MAX);
if (size)
printf("Possible filedescriptor leak : %s (%s)", entry->d_name, buffer);
free(filename);
}
closedir(dir);
}