]> git.alsa-project.org Git - alsa-utils.git/commitdiff
alsactl: Fix race at creating a lock file
authorTakashi Iwai <tiwai@suse.de>
Fri, 11 Dec 2020 22:46:23 +0000 (23:46 +0100)
committerTakashi Iwai <tiwai@suse.de>
Fri, 11 Dec 2020 22:46:23 +0000 (23:46 +0100)
A race at creating a lock file in state_lock() was discovered
recently: namely, between the first open(O_RDWR) and the second
open(O_RDWR|O_CREAT|O_EXCL) calls, another alsactl invocation may
already create a lock file, then the second open() will return EEXIST,
which isn't handled properly and treated as a fatal error.

In this patch, we check EEXIST case and try again open() with O_RDWR.
This must succeed usually, and if it fails, handle finally as the
fatal error.

BugLink: https://bugzilla.opensuse.org/show_bug.cgi?id=1179904
Signed-off-by: Takashi Iwai <tiwai@suse.de>
alsactl/lock.c

index 05f6e4d2a1023a4ab69ad0bed33b4a54322a79f7..5b47462319963e226d73f381bc10dcd54397c5da 100644 (file)
@@ -63,10 +63,15 @@ static int state_lock_(const char *file, int lock, int timeout, int _fd)
                        if (fd < 0) {
                                if (errno == EBUSY || errno == EAGAIN) {
                                        sleep(1);
-                               } else {
-                                       err = -errno;
-                                       goto out;
+                                       continue;
                                }
+                               if (errno == EEXIST) {
+                                       fd = open(nfile, O_RDWR);
+                                       if (fd >= 0)
+                                               break;
+                               }
+                               err = -errno;
+                               goto out;
                        }
                }
        }