alsactl: Fix double decrease of lock timeout
PATCH 1/2 alsactl: Fix double decrease of lock timeout
The state_lock() has a loop to wait for the lock file creation, and the timeout value gets decremented twice mistakenly, which leads to a half timeout (5 seconds) than expected 10 seconds. Fix it.
--- alsactl/lock.c | 1 - 1 file changed, 1 deletion(-)
alsactl/lock.c +0 -1
@@ -63,7 +63,6 @@ static int state_lock_(const char *file, int lock, int timeout, int _fd)if (fd <0 ) {if (errno ==EBUSY || errno ==EAGAIN ) {sleep (1 ); - timeout--; }else { err = -errno; goto out;
Signature · 1 line
2.26.2
PATCH 2/2 alsactl: Fix race at creating a lock file
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.
--- alsactl/lock.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
alsactl/lock.c +8 -3
@@ -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; } } }
Signature · 1 line
2.26.2