mirror of
https://github.com/alsa-project/alsa-utils
synced 2024-12-22 19:46:31 +01:00
alsactl: lock - use alarm signal and F_SETLKW rather polling
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
parent
ccb6d4612b
commit
64fa37f09b
1 changed files with 35 additions and 8 deletions
|
@ -27,15 +27,26 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include "alsactl.h"
|
#include "alsactl.h"
|
||||||
|
|
||||||
|
static int alarm_flag;
|
||||||
|
|
||||||
|
static void signal_handler_alarm(int sig)
|
||||||
|
{
|
||||||
|
alarm_flag = 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int state_lock_(int lock, int timeout, int _fd)
|
static int state_lock_(int lock, int timeout, int _fd)
|
||||||
{
|
{
|
||||||
int fd = -1, err = 0;
|
int fd = -1, err = 0;
|
||||||
struct flock lck;
|
struct flock lck;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char lcktxt[14];
|
char lcktxt[14];
|
||||||
|
struct sigaction sig_alarm, sig_alarm_orig;
|
||||||
|
struct itimerval itv;
|
||||||
char *nfile = lockfile;
|
char *nfile = lockfile;
|
||||||
|
|
||||||
if (do_lock <= 0)
|
if (do_lock <= 0)
|
||||||
|
@ -93,15 +104,31 @@ static int state_lock_(int lock, int timeout, int _fd)
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (timeout > 0) {
|
alarm_flag = 0;
|
||||||
if (fcntl(fd, F_SETLK, &lck) < 0) {
|
memset(&sig_alarm, 0, sizeof(sig_alarm));
|
||||||
sleep(1);
|
sigemptyset(&sig_alarm.sa_mask);
|
||||||
timeout--;
|
sig_alarm.sa_handler = signal_handler_alarm;
|
||||||
} else {
|
if (sigaction(SIGALRM, &sig_alarm, &sig_alarm_orig) < 0) {
|
||||||
break;
|
err = -ENXIO;
|
||||||
}
|
goto out;
|
||||||
}
|
}
|
||||||
if (timeout <= 0) {
|
memset(&itv, 0, sizeof(itv));
|
||||||
|
itv.it_value.tv_sec = timeout;
|
||||||
|
if (setitimer(ITIMER_REAL, &itv, NULL) < 0) {
|
||||||
|
err = -ENXIO;
|
||||||
|
sigaction(SIGALRM, &sig_alarm_orig, NULL);
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
while (alarm_flag == 0) {
|
||||||
|
if (fcntl(fd, F_SETLKW, &lck) == 0)
|
||||||
|
break;
|
||||||
|
if (errno == EAGAIN || errno == ERESTART)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
memset(&itv, 0, sizeof(itv));
|
||||||
|
setitimer(ITIMER_REAL, &itv, NULL);
|
||||||
|
sigaction(SIGALRM, &sig_alarm_orig, NULL);
|
||||||
|
if (alarm_flag) {
|
||||||
err = -EBUSY;
|
err = -EBUSY;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue