]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
mdmon: fix segfault
authorMateusz Kusiak <mateusz.kusiak@intel.com>
Mon, 2 Jan 2023 08:46:21 +0000 (09:46 +0100)
committerJes Sorensen <jes@trained-monkey.org>
Wed, 4 Jan 2023 15:25:56 +0000 (10:25 -0500)
Mdmon crashes if stat2devnm returns null.
Use open_mddev to check if device is mddevice and get name using
fd2devnm.
Refactor container name handling.

Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
Makefile
mdmon.c

index ec1f99ed5d83d638cb46932495135f74f554555c..5eac1a4e96907dc902e01b3f7df8a320562323e1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -160,7 +160,7 @@ SRCS =  $(patsubst %.o,%.c,$(OBJS))
 
 INCL = mdadm.h part.h bitmap.h
 
-MON_OBJS = mdmon.o monitor.o managemon.o uuid.o util.o maps.o mdstat.o sysfs.o \
+MON_OBJS = mdmon.o monitor.o managemon.o uuid.o util.o maps.o mdstat.o sysfs.o config.o mapfile.o mdopen.o\
        policy.o lib.o \
        Kill.o sg_io.o dlink.o ReadMe.o super-intel.o \
        super-mbr.o super-gpt.o \
diff --git a/mdmon.c b/mdmon.c
index e9d035ebb5bb1095eefc9f5174fbf69eb5df7979..ecf52dc854de4736770bf1c4c89c05358989faf7 100644 (file)
--- a/mdmon.c
+++ b/mdmon.c
@@ -363,14 +363,14 @@ int main(int argc, char *argv[])
        }
 
        if (all == 0 && container_name == NULL) {
-               if (argv[optind])
-                       container_name = argv[optind];
+               if (argv[optind]) {
+                       container_name = get_md_name(argv[optind]);
+                       if (!container_name)
+                               container_name = argv[optind];
+               }
        }
 
-       if (container_name == NULL)
-               usage();
-
-       if (argc - optind > 1)
+       if (container_name == NULL || argc - optind > 1)
                usage();
 
        if (strcmp(container_name, "/proc/mdstat") == 0)
@@ -402,21 +402,19 @@ int main(int argc, char *argv[])
                free_mdstat(mdstat);
 
                return status;
-       } else if (strncmp(container_name, "md", 2) == 0) {
-               int id = devnm2devid(container_name);
-               if (id)
-                       devnm = container_name;
        } else {
-               struct stat st;
+               int mdfd = open_mddev(container_name, 1);
 
-               if (stat(container_name, &st) == 0)
-                       devnm = xstrdup(stat2devnm(&st));
+               if (mdfd < 0)
+                       return 1;
+               devnm = fd2devnm(mdfd);
+               close(mdfd);
        }
 
        if (!devnm) {
                pr_err("%s is not a valid md device name\n",
                        container_name);
-               exit(1);
+               return 1;
        }
        return mdmon(devnm, dofork && do_fork(), takeover);
 }