55 lines
2.2 KiB
Diff
55 lines
2.2 KiB
Diff
From 4f8cec1924bf00532f5350d9a4d7af8e853241fe Mon Sep 17 00:00:00 2001
|
|
From: huangkaibin <huangkaibin@huawei.com>
|
|
Date: Thu, 28 Jun 2018 20:23:45 +0800
|
|
Subject: [PATCH] systemd-journald: Fix journal file descriptors leak problems.
|
|
|
|
Journal files opened and then be removed by external programs(for example, the journal rotation
|
|
of systemd-journald will removed jounal files) before journal directory notify watching is added
|
|
will not be closed properly. This patch fix this problem by removing and closing these deleted journal files
|
|
after notify watching is added.
|
|
---
|
|
src/journal/sd-journal.c | 20 ++++++++++++++++++++
|
|
1 file changed, 20 insertions(+)
|
|
|
|
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
|
|
index 004fe64..8be5481 100644
|
|
--- a/src/journal/sd-journal.c
|
|
+++ b/src/journal/sd-journal.c
|
|
@@ -1436,6 +1436,18 @@ fail:
|
|
log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path);
|
|
}
|
|
|
|
+static void remove_nonexistent_journal_files(sd_journal *j) {
|
|
+ Iterator i;
|
|
+ JournalFile *f = NULL;
|
|
+ ORDERED_HASHMAP_FOREACH(f, j->files, i) {
|
|
+ if(f->path && access(f->path, F_OK) < 0) {
|
|
+ log_debug("Remove not-existed file from the journal map: %s", f->path);
|
|
+ /*Its OK to remove entry from the hashmap although we are iterating on it.*/
|
|
+ remove_file_real(j, f);
|
|
+ }
|
|
+ }
|
|
+}
|
|
+
|
|
static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) {
|
|
int r;
|
|
|
|
@@ -1464,6 +1476,14 @@ static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask)
|
|
(void) inotify_rm_watch(j->inotify_fd, m->wd);
|
|
m->wd = -1;
|
|
}
|
|
+
|
|
+ /*
|
|
+ * Before event watching, there were some files opened and if some of these opened files were
|
|
+ * deleted due to the journal rotation of systemd-jounald, they will become leaking files and will
|
|
+ * never be closed until the process exited.
|
|
+ * So here we remove these deleted files from the journal after event watching.
|
|
+ */
|
|
+ remove_nonexistent_journal_files(j);
|
|
}
|
|
|
|
static int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
|
|
--
|
|
1.8.3.1
|
|
|