1 Star 0 Fork 91

孙志刚 / systemd

forked from src-openEuler / systemd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
delete-journal-files-except-system.journal-when-jour.patch 6.67 KB
一键复制 编辑 原始数据 按行查看 历史
From 02d47bd2108d46cf9790500a7568a7523df485f9 Mon Sep 17 00:00:00 2001
From: xujing <xujing125@huawei.com>
Date: Fri, 26 Aug 2022 20:32:37 +0800
Subject: [PATCH] delete journal files except system.journal when journal~
is generated
In the case of time change and system panic, the function of invoking
sd_journal_next to obtain logs may not meet expectations(rsyslog cannot obtain
logs). Therefore, when the journal~ file is generated, delete all journal files
except system.journal, to ensure that the sd_journal_next function meets user
expectations.
---
meson.build | 3 ++-
src/basic/dirent-util.c | 24 +++++++++++++++++
src/basic/dirent-util.h | 2 ++
src/libsystemd/sd-journal/journal-file.c | 34 ++++++++++++++++++++++++
src/libsystemd/sd-journal/sd-journal.c | 22 ---------------
src/test/meson.build | 2 +-
6 files changed, 63 insertions(+), 23 deletions(-)
diff --git a/meson.build b/meson.build
index 0372b17..8b1ce23 100644
--- a/meson.build
+++ b/meson.build
@@ -2001,6 +2001,8 @@ basic_includes = include_directories(
'src/basic',
'src/fundamental',
'src/systemd',
+ 'src/libsystemd/sd-id128',
+ 'src/libsystemd/sd-journal',
'.')
libsystemd_includes = [basic_includes, include_directories(
@@ -1801,7 +1801,7 @@ test_dlopen = executable(
'test-dlopen',
test_dlopen_c,
include_directories : includes,
- link_with : [libbasic],
+ link_with : [libbasic, libsystemd_static],
dependencies : [libdl],
build_by_default : want_tests != 'false')
diff --git a/src/basic/dirent-util.c b/src/basic/dirent-util.c
index 17df6a2..e362554 100644
--- a/src/basic/dirent-util.c
+++ b/src/basic/dirent-util.c
@@ -7,6 +7,8 @@
#include "path-util.h"
#include "stat-util.h"
#include "string-util.h"
+#include "id128-util.h"
+#include "syslog-util.h"
int dirent_ensure_type(int dir_fd, struct dirent *de) {
STRUCT_STATX_DEFINE(sx);
@@ -65,6 +67,28 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
return endswith(de->d_name, suffix);
}
+bool dirent_is_journal_subdir(const struct dirent *de) {
+ const char *e, *n;
+ assert(de);
+
+ /* returns true if the specified directory entry looks like a directory that might contain journal
+ * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
+ * namespace. */
+
+ if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN))
+ return false;
+
+ e = strchr(de->d_name, '.');
+ if (!e)
+ return id128_is_valid(de->d_name); /* No namespace */
+
+ n = strndupa(de->d_name, e - de->d_name);
+ if (!id128_is_valid(n))
+ return false;
+
+ return log_namespace_name_valid(e + 1);
+}
+
struct dirent *readdir_ensure_type(DIR *d) {
int r;
diff --git a/src/basic/dirent-util.h b/src/basic/dirent-util.h
index 0f1fb23..2effce3 100644
--- a/src/basic/dirent-util.h
+++ b/src/basic/dirent-util.h
@@ -12,6 +12,8 @@ bool dirent_is_file(const struct dirent *de) _pure_;
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
int dirent_ensure_type(int dir_fd, struct dirent *de);
+bool dirent_is_journal_subdir(const struct dirent *de);
+
struct dirent *readdir_ensure_type(DIR *d);
struct dirent *readdir_no_dot(DIR *dirp);
diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c
index 9e6bf6e..561a705 100644
--- a/src/libsystemd/sd-journal/journal-file.c
+++ b/src/libsystemd/sd-journal/journal-file.c
@@ -38,6 +38,7 @@
#include "sync-util.h"
#include "user-util.h"
#include "xattr-util.h"
+#include "dirent-util.h"
#define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*sizeof(HashItem))
#define DEFAULT_FIELD_HASH_TABLE_SIZE (333ULL*sizeof(HashItem))
@@ -4069,8 +4070,35 @@ int journal_file_archive(JournalFile *f, char **ret_previous_path) {
return 0;
}
+static void delete_dumped_journal_files(const char *path) {
+ _cleanup_closedir_ DIR *d = NULL;
+
+ d = opendir(path);
+ if (!d)
+ return;
+
+ FOREACH_DIRENT_ALL(de, d, return) {
+ if (IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN) &&
+ (endswith(de->d_name, ".journal") ||
+ endswith(de->d_name, ".journal~")) &&
+ strcmp(de->d_name, "system.journal") != 0)
+ (void) unlinkat_deallocate(dirfd(d), de->d_name, 0);
+
+ if (dirent_is_journal_subdir(de)) {
+ _cleanup_free_ char *sub_path = NULL;
+
+ sub_path = path_join(path, de->d_name);
+ if (!sub_path)
+ continue;
+
+ delete_dumped_journal_files(sub_path);
+ }
+ }
+}
+
int journal_file_dispose(int dir_fd, const char *fname) {
_cleanup_free_ char *p = NULL;
+ dual_timestamp boot_timestamp;
assert(fname);
@@ -4091,6 +4119,12 @@ int journal_file_dispose(int dir_fd, const char *fname) {
if (renameat(dir_fd, fname, dir_fd, p) < 0)
return -errno;
+ dual_timestamp_get(&boot_timestamp);
+ if (boot_timestamp.monotonic < 10*USEC_PER_MINUTE) {
+ delete_dumped_journal_files("/var/log/journal");
+ return 0;
+ }
+
return 0;
}
diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c
index f6090dd..8b83f65 100644
--- a/src/libsystemd/sd-journal/sd-journal.c
+++ b/src/libsystemd/sd-journal/sd-journal.c
@@ -1510,28 +1510,6 @@ static bool dirent_is_journal_file(const struct dirent *de) {
endswith(de->d_name, ".journal~");
}
-static bool dirent_is_journal_subdir(const struct dirent *de) {
- const char *e, *n;
- assert(de);
-
- /* returns true if the specified directory entry looks like a directory that might contain journal
- * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
- * namespace. */
-
- if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN))
- return false;
-
- e = strchr(de->d_name, '.');
- if (!e)
- return id128_is_valid(de->d_name); /* No namespace */
-
- n = strndupa_safe(de->d_name, e - de->d_name);
- if (!id128_is_valid(n))
- return false;
-
- return log_namespace_name_valid(e + 1);
-}
-
static int directory_open(sd_journal *j, const char *path, DIR **ret) {
DIR *d;
--
2.33.0
1
https://gitee.com/zhgsun/systemd.git
git@gitee.com:zhgsun/systemd.git
zhgsun
systemd
systemd
master

搜索帮助