SHA256
1
0
forked from pool/mdadm
mdadm/0088-Mdmonitor-Refactor-check_one_sharer-for-better-error.patch
Coly Li 90d4e6869d Accepting request 1137473 from home:colyli:branches:Base:System
- Update mdadm to latest upstream state upto commit
  582945c2d3bb. (jsc#PED-7542)
  1) The testing changes are not included here.
  2) Code clean up, and more helper routines added for Manage.c,
     Monitor.c, mdadm.c, ReadMe.c, super-intel.c, super0.c, super1.c,
     Create.c, Incremental.c and so on.
  3) Man page update for mdadm.8.in.
  4) Several memory leak and double free fixes.
  5) Check /etc/initrd-release for whether systemd running on an initrd.
  - Manage: Block unsafe member failing
    0062-Manage-Block-unsafe-member-failing.patch
  - Mdmonitor: Split alert() into separate functions
    0063-Mdmonitor-Split-alert-into-separate-functions.patch
  - Monitor: block if monitor modes are combined.
    0064-Monitor-block-if-monitor-modes-are-combined.patch
  - Update mdadm Monitor manual.
    0065-Update-mdadm-Monitor-manual.patch
  - mdadm: create ident_init()
    0066-mdadm-create-ident_init.patch
  - mdadm: Add option validation for --update-subarray
    0067-mdadm-Add-option-validation-for-update-subarray.patch
  - Fix --update-subarray on active volume
    0068-Fix-update-subarray-on-active-volume.patch
  - Add code specific update options to enum.
    0069-Add-code-specific-update-options-to-enum.patch
  - super-ddf: Remove update_super_ddf.
    0070-super-ddf-Remove-update_super_ddf.patch
  - super0: refactor the code for enum
    0071-super0-refactor-the-code-for-enum.patch
  - super1: refactor the code for enum

OBS-URL: https://build.opensuse.org/request/show/1137473
OBS-URL: https://build.opensuse.org/package/show/Base:System/mdadm?expand=0&rev=231
2024-01-11 00:49:23 +00:00

140 lines
3.4 KiB
Diff

From 0a07dea8d3b78a22a59f4604a5e8da15690f28e3 Mon Sep 17 00:00:00 2001
From: Mateusz Grzonka <mateusz.grzonka@intel.com>
Date: Thu, 2 Feb 2023 12:27:04 +0100
Subject: [PATCH] Mdmonitor: Refactor check_one_sharer() for better error
handling
Also check if autorebuild.pid is a symlink, which we shouldn't accept.
Signed-off-by: Mateusz Grzonka <mateusz.grzonka@intel.com>
Acked-by: Coly Li <colyli@suse.de>
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
---
Monitor.c | 89 ++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 62 insertions(+), 27 deletions(-)
diff --git a/Monitor.c b/Monitor.c
index 14a2dfe..4491818 100644
--- a/Monitor.c
+++ b/Monitor.c
@@ -32,6 +32,7 @@
#include <libudev.h>
#endif
+#define TASK_COMM_LEN 16
#define EVENT_NAME_MAX 32
#define AUTOREBUILD_PID_PATH MDMON_DIR "/autorebuild.pid"
@@ -224,7 +225,7 @@ int Monitor(struct mddev_dev *devlist,
info.hostname[sizeof(info.hostname) - 1] = '\0';
if (share){
- if (check_one_sharer(c->scan))
+ if (check_one_sharer(c->scan) == 2)
return 1;
}
@@ -406,39 +407,73 @@ static int make_daemon(char *pidfile)
return -1;
}
+/*
+ * check_one_sharer() - Checks for other mdmon processes running.
+ *
+ * Return:
+ * 0 - no other processes running,
+ * 1 - warning,
+ * 2 - error, or when scan mode is enabled, and one mdmon process already exists
+ */
static int check_one_sharer(int scan)
{
int pid;
- FILE *comm_fp;
- FILE *fp;
+ FILE *fp, *comm_fp;
char comm_path[PATH_MAX];
- char path[PATH_MAX];
- char comm[20];
-
- sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
- fp = fopen(path, "r");
- if (fp) {
- if (fscanf(fp, "%d", &pid) != 1)
- pid = -1;
- snprintf(comm_path, sizeof(comm_path),
- "/proc/%d/comm", pid);
- comm_fp = fopen(comm_path, "r");
- if (comm_fp) {
- if (fscanf(comm_fp, "%19s", comm) &&
- strncmp(basename(comm), Name, strlen(Name)) == 0) {
- if (scan) {
- pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
- fclose(comm_fp);
- fclose(fp);
- return 1;
- } else {
- pr_err("Warning: One autorebuild process already running.\n");
- }
- }
+ char comm[TASK_COMM_LEN];
+
+ if (!is_directory(MDMON_DIR)) {
+ pr_err("%s is not a regular directory.\n", MDMON_DIR);
+ return 2;
+ }
+
+ if (access(AUTOREBUILD_PID_PATH, F_OK) != 0)
+ return 0;
+
+ if (!is_file(AUTOREBUILD_PID_PATH)) {
+ pr_err("%s is not a regular file.\n", AUTOREBUILD_PID_PATH);
+ return 2;
+ }
+
+ fp = fopen(AUTOREBUILD_PID_PATH, "r");
+ if (!fp) {
+ pr_err("Cannot open %s file.\n", AUTOREBUILD_PID_PATH);
+ return 2;
+ }
+
+ if (fscanf(fp, "%d", &pid) != 1) {
+ pr_err("Cannot read pid from %s file.\n", AUTOREBUILD_PID_PATH);
+ fclose(fp);
+ return 2;
+ }
+
+ snprintf(comm_path, sizeof(comm_path), "/proc/%d/comm", pid);
+
+ comm_fp = fopen(comm_path, "r");
+ if (!comm_fp) {
+ dprintf("Warning: Cannot open %s, continuing\n", comm_path);
+ fclose(fp);
+ return 1;
+ }
+
+ if (fscanf(comm_fp, "%15s", comm) == 0) {
+ dprintf("Warning: Cannot read comm from %s, continuing\n", comm_path);
+ fclose(comm_fp);
+ fclose(fp);
+ return 1;
+ }
+
+ if (strncmp(basename(comm), Name, strlen(Name)) == 0) {
+ if (scan) {
+ pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
fclose(comm_fp);
+ fclose(fp);
+ return 2;
}
- fclose(fp);
+ pr_err("Warning: One autorebuild process already running.\n");
}
+ fclose(comm_fp);
+ fclose(fp);
return 0;
}
--
2.35.3