From b7961bdb2f235945446bdcde0e1c7109c6787e80 Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Fri, 1 Aug 2008 19:51:27 +0000 Subject: [PATCH] Improve file descriptor leak detection to display likely culprit and filename. --- WHATS_NEW | 1 + lib/datastruct/lvm-types.h | 1 + tools/lvmcmdline.c | 85 ++++++++++++++++++++++++++++++++------ 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/WHATS_NEW b/WHATS_NEW index 6f0f9f3ba..97ef4b8b3 100644 --- a/WHATS_NEW +++ b/WHATS_NEW @@ -1,5 +1,6 @@ Version 2.02.40 - ================================ + Improve file descriptor leak detection to display likely culprit and filename. Change clustered mirror kernel module name from cmirror to dm-log-clustered. Avoid looping forever in _pv_analyze_mda_raw used by pvck. Change lvchange exit status to indicate if any part of the operation failed. diff --git a/lib/datastruct/lvm-types.h b/lib/datastruct/lvm-types.h index 8ed6bd1e2..38ec90b10 100644 --- a/lib/datastruct/lvm-types.h +++ b/lib/datastruct/lvm-types.h @@ -24,6 +24,7 @@ /* Define some portable printing types */ #define PRIsize_t "zu" #define PRIptrdiff_t "td" +#define PRIpid_t PRId32 struct str_list { struct list list; diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c index 9fd2fe6fd..e97cf65db 100644 --- a/tools/lvmcmdline.c +++ b/tools/lvmcmdline.c @@ -1001,11 +1001,76 @@ static void _init_rand(void) srand((unsigned) time(NULL) + (unsigned) getpid()); } -static void _close_stray_fds(void) +static const char *_get_cmdline(pid_t pid) +{ + static char _proc_cmdline[32]; + char buf[256]; + int fd; + + snprintf(buf, sizeof(buf), DEFAULT_PROC_DIR "/%u/cmdline", pid); + if ((fd = open(buf, O_RDONLY)) > 0) { + read(fd, _proc_cmdline, sizeof(_proc_cmdline) - 1); + _proc_cmdline[sizeof(_proc_cmdline) - 1] = '\0'; + close(fd); + } else + _proc_cmdline[0] = '\0'; + + return _proc_cmdline; +} + +static const char *_get_filename(int fd) +{ + static char filename[PATH_MAX]; + char buf[32]; /* Assumes short DEFAULT_PROC_DIR */ + int size; + + snprintf(buf, sizeof(buf), DEFAULT_PROC_DIR "/self/fd/%u", fd); + + if ((size = readlink(buf, filename, sizeof(filename) - 1)) == -1) + filename[0] = '\0'; + else + filename[size] = '\0'; + + return filename; +} + +static void _close_descriptor(int fd, unsigned suppress_warnings, + const char *command, pid_t ppid, + const char *parent_cmdline) +{ + int r; + const char *filename; + + /* Ignore bad file descriptors */ + if (fcntl(fd, F_GETFD) == -1 && errno == EBADF) + return; + + if (!suppress_warnings) + filename = _get_filename(fd); + + r = close(fd); + if (suppress_warnings) + return; + + if (!r) + fprintf(stderr, "File descriptor %d (%s) leaked on " + "%s invocation.", fd, filename, command); + else if (errno == EBADF) + return; + else + fprintf(stderr, "Close failed on stray file descriptor " + "%d (%s): %s", fd, filename, strerror(errno)); + + fprintf(stderr, " Parent PID %" PRIpid_t ": %s\n", ppid, parent_cmdline); +} + +static void _close_stray_fds(const char *command) { struct rlimit rlim; int fd; - int suppress_warnings = 0; + unsigned suppress_warnings = 0; + pid_t ppid = getppid(); + const char *parent_cmdline = _get_cmdline(ppid); if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { fprintf(stderr, "getrlimit(RLIMIT_NOFILE) failed: %s\n", @@ -1016,15 +1081,9 @@ static void _close_stray_fds(void) if (getenv("LVM_SUPPRESS_FD_WARNINGS")) suppress_warnings = 1; - for (fd = 3; fd < rlim.rlim_cur; fd++) { - if (suppress_warnings) - close(fd); - else if (!close(fd)) - fprintf(stderr, "File descriptor %d left open\n", fd); - else if (errno != EBADF) - fprintf(stderr, "Close failed on stray file " - "descriptor %d: %s\n", fd, strerror(errno)); - } + for (fd = 3; fd < rlim.rlim_cur; fd++) + _close_descriptor(fd, suppress_warnings, command, ppid, + parent_cmdline); } struct cmd_context *init_lvm(unsigned is_static) @@ -1162,13 +1221,13 @@ int lvm2_main(int argc, char **argv, unsigned is_static) int ret, alias = 0; struct cmd_context *cmd; - _close_stray_fds(); - base = last_path_component(argv[0]); if (strcmp(base, "lvm") && strcmp(base, "lvm.static") && strcmp(base, "initrd-lvm")) alias = 1; + _close_stray_fds(base); + if (is_static && strcmp(base, "lvm.static") && path_exists(LVM_SHARED_PATH) && !getenv("LVM_DID_EXEC")) {