alsa-utils/0008-alsactl-Fix-the-file-locking-routines-for-the-state-.patch
Takashi Iwai a8785cdb8c Accepting request 257836 from home:tiwai:branches:multimedia:libs
- Backport upstream fixes: a few speaker-test fixes, minor alsactl
  fixes and a revert of a wrong fix for aplay.
  0007-speaker-test-Add-missing-draining-at-the-end.patch
  0008-alsactl-Fix-the-file-locking-routines-for-the-state-.patch
  0009-alsactl-aded-missing-monitor-prototype.patch
  0010-speaker-text-fix-simple-signess-assignment-warning.patch
  0011-monitor-fix-clang-warning-Declared-variable-length-a.patch
  0012-alsactl-coverity-missing_va_end-va_end-was-not-calle.patch
  0013-Revert-aplay-fix-pcm_read-return-value.patch

OBS-URL: https://build.opensuse.org/request/show/257836
OBS-URL: https://build.opensuse.org/package/show/multimedia:libs/alsa-utils?expand=0&rev=108
2014-10-21 07:49:07 +00:00

188 lines
5.1 KiB
Diff

From 1363f852e92c1c913474fc015f9da12d1e93d750 Mon Sep 17 00:00:00 2001
From: Jaroslav Kysela <perex@perex.cz>
Date: Wed, 24 Sep 2014 10:35:53 +0200
Subject: [PATCH] alsactl: Fix the file locking routines (for the state
management)
---
alsactl/alsactl.h | 3 ++-
alsactl/lock.c | 41 +++++++++++++++++++++++++++++++----------
alsactl/state.c | 22 +++++++++++++---------
3 files changed, 46 insertions(+), 20 deletions(-)
diff --git a/alsactl/alsactl.h b/alsactl/alsactl.h
index 76e8c36100f9..bd9bf23e74b3 100644
--- a/alsactl/alsactl.h
+++ b/alsactl/alsactl.h
@@ -25,7 +25,8 @@ void dbg_(const char *fcn, long line, const char *fmt, ...);
#endif
int init(const char *file, const char *cardname);
-int state_lock(const char *file, int lock, int timeout);
+int state_lock(const char *file, int timeout);
+int state_unlock(int fd, const char *file);
int save_state(const char *file, const char *cardname);
int load_state(const char *file, const char *initfile, const char *cardname,
int do_init);
diff --git a/alsactl/lock.c b/alsactl/lock.c
index 6f85f202576d..9d7efccd7ce6 100644
--- a/alsactl/lock.c
+++ b/alsactl/lock.c
@@ -30,7 +30,7 @@
#include <sys/stat.h>
#include "alsactl.h"
-static int state_lock_(const char *file, int lock, int timeout)
+static int state_lock_(const char *file, int lock, int timeout, int _fd)
{
int fd = -1, err = 0;
struct flock lck;
@@ -50,9 +50,14 @@ static int state_lock_(const char *file, int lock, int timeout)
snprintf(lcktxt, sizeof(lcktxt), "%10li\n", (long)getpid());
} else {
snprintf(lcktxt, sizeof(lcktxt), "%10s\n", "");
+ fd = _fd;
}
while (fd < 0 && timeout-- > 0) {
fd = open(nfile, O_RDWR);
+ if (!lock && fd < 0) {
+ err = -EIO;
+ goto out;
+ }
if (fd < 0) {
fd = open(nfile, O_RDWR|O_CREAT|O_EXCL, 0644);
if (fd < 0) {
@@ -74,12 +79,12 @@ static int state_lock_(const char *file, int lock, int timeout)
err = -errno;
goto out;
}
- if (st.st_size != 11) {
+ if (st.st_size != 11 || !lock) {
if (write(fd, lcktxt, 11) != 11) {
err = -EIO;
goto out;
}
- if (lseek(fd, 0, SEEK_SET)) {
+ if (lock && lseek(fd, 0, SEEK_SET)) {
err = -errno;
goto out;
}
@@ -96,21 +101,37 @@ static int state_lock_(const char *file, int lock, int timeout)
err = -EBUSY;
goto out;
}
- if (write(fd, lcktxt, 11) != 11) {
- err = -EIO;
- goto out;
+ if (lock) {
+ if (write(fd, lcktxt, 11) != 11) {
+ err = -EIO;
+ goto out;
+ }
+ return fd;
}
+ err = 0;
+
out:
+ if (fd >= 0)
+ close(fd);
+ return err;
+}
+
+int state_lock(const char *file, int timeout)
+{
+ int err;
+
+ err = state_lock_(file, 1, timeout, -1);
+ if (err < 0)
+ error("file %s lock error: %s", file, strerror(-err));
return err;
}
-int state_lock(const char *file, int lock, int timeout)
+int state_unlock(int _fd, const char *file)
{
int err;
- err = state_lock_(file, lock, timeout);
+ err = state_lock_(file, 0, 10, _fd);
if (err < 0)
- error("file %s %slock error: %s", file,
- lock ? "" : "un", strerror(-err));
+ error("file %s unlock error: %s", file, strerror(-err));
return err;
}
diff --git a/alsactl/state.c b/alsactl/state.c
index e0c6f2e02e9b..0c897034057c 100644
--- a/alsactl/state.c
+++ b/alsactl/state.c
@@ -1544,6 +1544,7 @@ int save_state(const char *file, const char *cardname)
snd_output_t *out;
int stdio;
char *nfile = NULL;
+ int lock_fd = -EINVAL;
err = snd_config_top(&config);
if (err < 0) {
@@ -1555,12 +1556,16 @@ int save_state(const char *file, const char *cardname)
nfile = malloc(strlen(file) + 5);
if (nfile == NULL) {
error("No enough memory...");
+ err = -ENOMEM;
goto out;
}
strcpy(nfile, file);
strcat(nfile, ".new");
- if (state_lock(file, 1, 10) != 0)
+ lock_fd = state_lock(file, 10);
+ if (lock_fd < 0) {
+ err = lock_fd;
goto out;
+ }
}
if (!stdio && (err = snd_input_stdio_open(&in, file, "r")) >= 0) {
err = snd_config_load(config, in);
@@ -1632,8 +1637,8 @@ int save_state(const char *file, const char *cardname)
error("rename failed: %s (%s)", strerror(-err), file);
}
out:
- if (!stdio)
- state_lock(file, 0, 10);
+ if (!stdio && lock_fd >= 0)
+ state_unlock(lock_fd, file);
free(nfile);
snd_config_delete(config);
snd_config_update_free_global();
@@ -1646,7 +1651,7 @@ int load_state(const char *file, const char *initfile, const char *cardname,
int err, finalerr = 0;
snd_config_t *config;
snd_input_t *in;
- int stdio, locked = 0;
+ int stdio, lock_fd = -EINVAL;
err = snd_config_top(&config);
if (err < 0) {
@@ -1657,15 +1662,14 @@ int load_state(const char *file, const char *initfile, const char *cardname,
if (stdio) {
err = snd_input_stdio_attach(&in, stdin, 0);
} else {
- err = state_lock(file, 1, 10);
- locked = err >= 0;
- err = err >= 0 ? snd_input_stdio_open(&in, file, "r") : err;
+ lock_fd = state_lock(file, 10);
+ err = lock_fd >= 0 ? snd_input_stdio_open(&in, file, "r") : lock_fd;
}
if (err >= 0) {
err = snd_config_load(config, in);
snd_input_close(in);
- if (locked)
- state_lock(file, 0, 10);
+ if (lock_fd >= 0)
+ state_unlock(lock_fd, file);
if (err < 0) {
error("snd_config_load error: %s", snd_strerror(err));
goto out;
--
2.1.2