2013-04-05 11:51:51 +02:00
|
|
|
/*
|
|
|
|
* Advanced Linux Sound Architecture Control Program
|
|
|
|
* Copyright (c) by Jaroslav Kysela <perex@perex.cz>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2017-11-14 14:28:51 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2013-04-05 11:51:51 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "aconfig.h"
|
|
|
|
#include "version.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2022-02-01 20:45:27 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
2013-04-05 11:51:51 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "alsactl.h"
|
2023-02-25 10:29:01 +01:00
|
|
|
#include "os_compat.h"
|
2013-04-05 11:51:51 +02:00
|
|
|
|
2022-02-02 15:31:33 +01:00
|
|
|
#define PATH_SIZE 512
|
|
|
|
|
2022-02-01 20:45:27 +01:00
|
|
|
static int alarm_flag;
|
|
|
|
|
2023-08-30 12:35:23 +02:00
|
|
|
static void signal_handler_alarm(int sig ATTRIBUTE_UNUSED)
|
2022-02-01 20:45:27 +01:00
|
|
|
{
|
|
|
|
alarm_flag = 1;
|
|
|
|
}
|
|
|
|
|
2022-02-02 15:31:33 +01:00
|
|
|
static int state_lock_(const char *lock_file, int lock, int timeout, int _fd)
|
2013-04-05 11:51:51 +02:00
|
|
|
{
|
|
|
|
int fd = -1, err = 0;
|
|
|
|
struct flock lck;
|
|
|
|
struct stat st;
|
2018-10-14 17:01:38 +02:00
|
|
|
char lcktxt[14];
|
2022-02-01 20:45:27 +01:00
|
|
|
struct sigaction sig_alarm, sig_alarm_orig;
|
|
|
|
struct itimerval itv;
|
2013-04-05 11:51:51 +02:00
|
|
|
|
2014-05-07 11:13:41 +02:00
|
|
|
if (do_lock <= 0)
|
2013-04-05 11:51:51 +02:00
|
|
|
return 0;
|
2014-05-06 21:32:19 +02:00
|
|
|
|
2013-04-05 11:51:51 +02:00
|
|
|
lck.l_type = lock ? F_WRLCK : F_UNLCK;
|
|
|
|
lck.l_whence = SEEK_SET;
|
|
|
|
lck.l_start = 0;
|
|
|
|
lck.l_len = 11;
|
|
|
|
lck.l_pid = 0;
|
|
|
|
if (lock) {
|
2013-05-01 18:30:26 +02:00
|
|
|
snprintf(lcktxt, sizeof(lcktxt), "%10li\n", (long)getpid());
|
2013-04-05 11:51:51 +02:00
|
|
|
} else {
|
2013-05-01 18:30:26 +02:00
|
|
|
snprintf(lcktxt, sizeof(lcktxt), "%10s\n", "");
|
2014-09-24 10:35:53 +02:00
|
|
|
fd = _fd;
|
2013-04-05 11:51:51 +02:00
|
|
|
}
|
|
|
|
while (fd < 0 && timeout-- > 0) {
|
2022-02-02 15:31:33 +01:00
|
|
|
fd = open(lock_file, O_RDWR);
|
2014-09-24 10:35:53 +02:00
|
|
|
if (!lock && fd < 0) {
|
|
|
|
err = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-04-05 11:51:51 +02:00
|
|
|
if (fd < 0) {
|
2022-02-02 15:31:33 +01:00
|
|
|
fd = open(lock_file, O_RDWR|O_CREAT|O_EXCL, 0644);
|
2013-04-05 11:51:51 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EBUSY || errno == EAGAIN) {
|
|
|
|
sleep(1);
|
2020-12-11 23:46:23 +01:00
|
|
|
continue;
|
2013-04-05 11:51:51 +02:00
|
|
|
}
|
2020-12-11 23:46:23 +01:00
|
|
|
if (errno == EEXIST) {
|
2022-02-02 15:31:33 +01:00
|
|
|
fd = open(lock_file, O_RDWR);
|
2020-12-11 23:46:23 +01:00
|
|
|
if (fd >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
err = -errno;
|
|
|
|
goto out;
|
2013-04-05 11:51:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fd < 0 && timeout <= 0) {
|
|
|
|
err = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
err = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-09-24 10:35:53 +02:00
|
|
|
if (st.st_size != 11 || !lock) {
|
2013-04-05 11:51:51 +02:00
|
|
|
if (write(fd, lcktxt, 11) != 11) {
|
|
|
|
err = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-09-24 10:35:53 +02:00
|
|
|
if (lock && lseek(fd, 0, SEEK_SET)) {
|
2013-04-05 11:51:51 +02:00
|
|
|
err = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 20:45:27 +01:00
|
|
|
alarm_flag = 0;
|
|
|
|
memset(&sig_alarm, 0, sizeof(sig_alarm));
|
|
|
|
sigemptyset(&sig_alarm.sa_mask);
|
|
|
|
sig_alarm.sa_handler = signal_handler_alarm;
|
|
|
|
if (sigaction(SIGALRM, &sig_alarm, &sig_alarm_orig) < 0) {
|
|
|
|
err = -ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
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)
|
2013-04-05 11:51:51 +02:00
|
|
|
break;
|
2022-02-01 20:45:27 +01:00
|
|
|
if (errno == EAGAIN || errno == ERESTART)
|
|
|
|
continue;
|
2013-04-05 11:51:51 +02:00
|
|
|
}
|
2022-02-01 20:45:27 +01:00
|
|
|
memset(&itv, 0, sizeof(itv));
|
|
|
|
setitimer(ITIMER_REAL, &itv, NULL);
|
|
|
|
sigaction(SIGALRM, &sig_alarm_orig, NULL);
|
|
|
|
if (alarm_flag) {
|
2013-04-05 11:51:51 +02:00
|
|
|
err = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-09-24 10:35:53 +02:00
|
|
|
if (lock) {
|
|
|
|
if (write(fd, lcktxt, 11) != 11) {
|
|
|
|
err = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
return fd;
|
2013-04-05 11:51:51 +02:00
|
|
|
}
|
2014-09-24 10:35:53 +02:00
|
|
|
err = 0;
|
|
|
|
|
2013-04-05 11:51:51 +02:00
|
|
|
out:
|
2014-09-24 10:35:53 +02:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-02-02 15:31:33 +01:00
|
|
|
static void state_lock_file(char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
if (lockfile[0] == '/')
|
|
|
|
snprintf(buf, buflen, "%s", lockfile);
|
|
|
|
else
|
|
|
|
snprintf(buf, buflen, "%s/%s", lockpath, lockfile);
|
|
|
|
}
|
|
|
|
|
2014-09-24 10:35:53 +02:00
|
|
|
int state_lock(const char *file, int timeout)
|
|
|
|
{
|
2022-02-02 15:31:33 +01:00
|
|
|
char fn[PATH_SIZE];
|
2014-09-24 10:35:53 +02:00
|
|
|
int err;
|
|
|
|
|
2022-02-02 15:31:33 +01:00
|
|
|
state_lock_file(fn, sizeof(fn));
|
|
|
|
err = state_lock_(fn, 1, timeout, -1);
|
2014-09-24 10:35:53 +02:00
|
|
|
if (err < 0)
|
|
|
|
error("file %s lock error: %s", file, strerror(-err));
|
2013-04-05 11:51:51 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-09-24 10:35:53 +02:00
|
|
|
int state_unlock(int _fd, const char *file)
|
2013-04-05 11:51:51 +02:00
|
|
|
{
|
2022-02-02 15:31:33 +01:00
|
|
|
char fn[PATH_SIZE];
|
2013-04-05 11:51:51 +02:00
|
|
|
int err;
|
|
|
|
|
2022-02-02 15:31:33 +01:00
|
|
|
state_lock_file(fn, sizeof(fn));
|
|
|
|
err = state_lock_(fn, 0, 10, _fd);
|
2013-04-05 11:51:51 +02:00
|
|
|
if (err < 0)
|
2014-09-24 10:35:53 +02:00
|
|
|
error("file %s unlock error: %s", file, strerror(-err));
|
2013-04-05 11:51:51 +02:00
|
|
|
return err;
|
|
|
|
}
|
2022-02-02 15:31:33 +01:00
|
|
|
|
|
|
|
static void card_lock_file(char *buf, size_t buflen, int card_number)
|
|
|
|
{
|
|
|
|
snprintf(buf, buflen, "%s/card%i.lock", lockpath, card_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
int card_lock(int card_number, int timeout)
|
|
|
|
{
|
|
|
|
char fn[PATH_SIZE];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
card_lock_file(fn, sizeof(fn), card_number);
|
|
|
|
err = state_lock_(fn, 1, timeout, -1);
|
|
|
|
if (err < 0)
|
|
|
|
error("card %d lock error: %s", card_number, strerror(-err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int card_unlock(int _fd, int card_number)
|
|
|
|
{
|
|
|
|
char fn[PATH_SIZE];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
card_lock_file(fn, sizeof(fn), card_number);
|
|
|
|
err = state_lock_(fn, 0, 10, _fd);
|
|
|
|
if (err < 0)
|
|
|
|
error("card %d unlock error: %s", card_number, strerror(-err));
|
|
|
|
return err;
|
|
|
|
}
|