From d4b2ad7e7b4dbf6360f1b6e50d5083d0a3ead8e7 Mon Sep 17 00:00:00 2001 From: baiguo Date: Mon, 1 Apr 2024 11:05:09 +0800 Subject: [PATCH 1/3] fix parsing of proc self stat --- 0018-fix-parsing-of-proc-self-stat.patch | 125 +++++++++++++++++++++++ lvm2.spec | 6 +- 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 0018-fix-parsing-of-proc-self-stat.patch diff --git a/0018-fix-parsing-of-proc-self-stat.patch b/0018-fix-parsing-of-proc-self-stat.patch new file mode 100644 index 0000000..a9376a3 --- /dev/null +++ b/0018-fix-parsing-of-proc-self-stat.patch @@ -0,0 +1,125 @@ +From 232062e03b94df77374013b115bdf974817a0cf1 Mon Sep 17 00:00:00 2001 +From: Zdenek Kabelac +Date: Mon, 1 Apr 2024 11:02:16 +0800 +Subject: [PATCH] Fix parsing of /proc/self/stat + +--- + lib/log/log.c | 79 +++++++++++++++++++++++++++++++++------------------ + 1 file changed, 52 insertions(+), 27 deletions(-) + +diff --git a/lib/log/log.c b/lib/log/log.c +index 86a3034..dc332bd 100644 +--- a/lib/log/log.c ++++ b/lib/log/log.c +@@ -218,6 +218,45 @@ void init_log_fn(lvm2_log_fn_t log_fn) + _lvm2_log_fn = log_fn; + } + ++/* Read /proc/self/stat to extract pid and starttime */ ++static int _get_pid_starttime(int *pid, unsigned long long *starttime) ++{ ++ static const char statfile[] = "/proc/self/stat"; ++ char buf[1024]; ++ char *p; ++ int fd; ++ int e; ++ ++ if ((fd = open(statfile, O_RDONLY)) == -1) { ++ log_sys_debug("open", statfile); ++ return 0; ++ } ++ ++ if ((e = read(fd, buf, sizeof(buf) - 1)) <= 0) ++ log_sys_debug("read", statfile); ++ ++ if (!close(fd)) ++ log_sys_debug("close", statfile); ++ ++ if (e <= 0) ++ return 0; ++ ++ buf[e] = '\0'; ++ if ((sscanf(buf, "%d ", pid) == 1) && ++ /* Jump past COMM, don't use scanf with '%s' since COMM may contain a space. */ ++ (p = strrchr(buf, ')')) && ++ (scanf(++p, " %*c %*d %*d %*d %*d " /* tty_nr */ ++ "%*d %*u %*u %*u %*u " /* mjflt */ ++ "%*u %*u %*u %*d %*d " /* cstim */ ++ "%*d %*d %*d %*d " /* itrealvalue */ ++ "%llu", &starttime) == 1)) ++ return 1; ++ ++ log_debug("Cannot parse content of %s.", statfile); ++ ++ return 0; ++} ++ + /* + * Support envvar LVM_LOG_FILE_EPOCH and allow to attach + * extra keyword (consist of upto 32 alpha chars) to +@@ -229,11 +268,9 @@ void init_log_fn(lvm2_log_fn_t log_fn) + */ + void init_log_file(const char *log_file, int append) + { +- static const char statfile[] = "/proc/self/stat"; +- const char *env; + int pid; + unsigned long long starttime; +- FILE *st; ++ const char *env; + int i = 0; + + _log_file_path[0] = '\0'; +@@ -245,29 +282,18 @@ void init_log_file(const char *log_file, int append) + log_warn("WARNING: Ignoring invalid LVM_LOG_FILE_EPOCH envvar \"%s\".", env); + goto no_epoch; + } +- +- if (!(st = fopen(statfile, "r"))) +- log_sys_error("fopen", statfile); +- else if (fscanf(st, "%d %*s %*c %*d %*d %*d %*d " /* tty_nr */ +- "%*d %*u %*u %*u %*u " /* mjflt */ +- "%*u %*u %*u %*d %*d " /* cstim */ +- "%*d %*d %*d %*d " /* itrealvalue */ +- "%llu", &pid, &starttime) != 2) { +- log_warn("WARNING: Cannot parse content of %s.", statfile); +- } else { +- if (dm_snprintf(_log_file_path, sizeof(_log_file_path), +- "%s_%s_%d_%llu", log_file, env, pid, starttime) < 0) { +- log_warn("WARNING: Debug log file path is too long for epoch."); +- _log_file_path[0] = '\0'; +- } else { +- log_file = _log_file_path; +- append = 1; /* force */ +- } +- } +- +- if (st && fclose(st)) +- log_sys_debug("fclose", statfile); +- ++ if (!_get_pid_starttime(&pid, &starttime)) ++ log_debug("Failed to obtain pid and starttime."); ++ ++ if (dm_snprintf(_log_file_path, sizeof(_log_file_path), ++ "%s_%s_%d_%llu", log_file, env, pid, starttime) < 0) { ++ log_warn("WARNING: Debug log file path is too long for epoch."); ++ _log_file_path[0] = '\0'; ++ } else { ++ log_file = _log_file_path; ++ append = 1; /* force */ ++ } ++ + if ((env = getenv("LVM_LOG_FILE_MAX_LINES"))) { + if (sscanf(env, FMTu64, &_log_file_max_lines) != 1) { + log_warn("WARNING: Ignoring invalid LVM_LOG_MAX_LINES envvar \"%s\".", env); +@@ -285,7 +311,6 @@ no_epoch: + + _log_to_file = 1; + } +- + /* + * Unlink the log file depeding on command's return value + * +-- +2.27.0 + diff --git a/lvm2.spec b/lvm2.spec index 0afcfd8..fa84e79 100644 --- a/lvm2.spec +++ b/lvm2.spec @@ -43,7 +43,7 @@ Name: lvm2 Version: 2.03.21 -Release: 7 +Release: 8 Epoch: 8 Summary: Tools for logical volume management License: GPLv2+ and LGPLv2.1 and BSD @@ -66,6 +66,7 @@ Patch14: 0014-use-sync-io-read-bcache-by-defaults.patch Patch15: 0015-vgchange-acquire-an-exclusive-VG-lock-for-refresh.patch Patch16: 0016-dm-event-release-buffer-on-dm_event_get_version.patch Patch17: 0017-clean-up-group-struct-in-_stats_create_group-error-path.patch +Patch18: 0018-fix-parsing-of-proc-self-stat.patch BuildRequires: gcc BuildRequires: gcc-c++ @@ -492,6 +493,9 @@ fi %changelog +* Mon Apr 1 2024 baiguo - 8:2.03.21-8 +- fix parsing of /proc/self/stat + * Thu Mar 21 2024 wangzhiqiang - 8:2.03.21-7 - backport upstream patch -- Gitee From f7be662fa135763b6edd46fea52312f92ca1e16e Mon Sep 17 00:00:00 2001 From: baiguo Date: Thu, 9 May 2024 10:45:29 +0800 Subject: [PATCH 2/3] fix parsing of proc self stat --- ...oc-self-stat.patch => 0019-fix-parsing-of-proc-self-stat.patch | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 0018-fix-parsing-of-proc-self-stat.patch => 0019-fix-parsing-of-proc-self-stat.patch (100%) diff --git a/0018-fix-parsing-of-proc-self-stat.patch b/0019-fix-parsing-of-proc-self-stat.patch similarity index 100% rename from 0018-fix-parsing-of-proc-self-stat.patch rename to 0019-fix-parsing-of-proc-self-stat.patch -- Gitee From fd53881011b34602c8401ab8fe882ce77f35b84a Mon Sep 17 00:00:00 2001 From: baiguo Date: Thu, 9 May 2024 13:58:29 +0800 Subject: [PATCH 3/3] fix parsing of proc self stat --- lvm2.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lvm2.spec b/lvm2.spec index 3cb9865..b25be70 100644 --- a/lvm2.spec +++ b/lvm2.spec @@ -494,7 +494,7 @@ fi %changelog -* Thu May 8 2024 baiguo - 8:2.03.21-9 +* Thu May 9 2024 baiguo - 8:2.03.21-9 - fix parsing of /proc/self/stat * Tue May 7 2024 yanshuai - 8:2.03.21-8 -- Gitee