]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
mdadm: Fix socket connection failure when mdmon runs in foreground mode.
authorShminderjit Singh <shminderjit.singh@oracle.com>
Tue, 4 Jun 2024 07:46:03 +0000 (07:46 +0000)
committerMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Tue, 18 Jun 2024 10:51:38 +0000 (12:51 +0200)
While creating an IMSM RAID, mdadm will wait for the mdmon main process
to finish if mdmon runs in forking mode. This is because with
"Type=forking" in the mdmon service unit file, "systemctl start service"
will block until the main process of mdmon exits. At that moment, mdmon
has already created the socket, so the subsequent socket connect from
mdadm will succeed.

However, when mdmon runs in foreground mode (without "Type=forking" in
the service unit file), "systemctl start service" will return once the
mdmon process starts. This causes mdadm and mdmon to run in parallel,
which may lead to a socket connection failure since mdmon has not yet
initialized the socket when mdadm tries to connect. If the next
instruction/command is to access this device and try to write to it, a
permission error will occur since mdmon has not yet set the array to RW
mode.

Signed-off-by: Shminderjit Singh <shminderjit.singh@oracle.com>
msg.c

diff --git a/msg.c b/msg.c
index ba0e25be906d5b80de7a9f55ffd504e23accc876..d17f679d608dc52bb9a9d04b8e8fbda240906886 100644 (file)
--- a/msg.c
+++ b/msg.c
@@ -151,6 +151,7 @@ int connect_monitor(char *devname)
        struct sockaddr_un addr;
        int pos;
        char *c;
+       int rv, retry_count = 0;
 
        pos = sprintf(path, "%s/", MDMON_DIR);
        if (is_subarray(devname)) {
@@ -170,7 +171,24 @@ int connect_monitor(char *devname)
 
        addr.sun_family = PF_LOCAL;
        strcpy(addr.sun_path, path);
-       if (connect(sfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
+
+       /* In foreground mode, when mdadm is trying to connect to control
+        * socket it is possible that the mdmon has not created it yet.
+        * Give some time to mdmon to create socket.
+        */
+       for (retry_count = 0; retry_count < 10; retry_count++) {
+               rv = connect(sfd, (struct sockaddr*)&addr, sizeof(addr));
+
+               if (rv < 0) {
+                       sleep_for(0, MSEC_TO_NSEC(200), true);
+                       continue;
+               }
+               break;
+       }
+
+       if (rv < 0) {
+               pr_err("Failed to connect to control socket. (%s!!)\n",
+                               strerror(errno));
                close(sfd);
                return -1;
        }