mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-03 09:01:50 -05:00
autospawn: if creating the lock file fails, pass error code cleanly back to main process
This makes sure PA clients don't hang if $HOME is for some reason unsuitable for autospawn lockfiles. Closes #539
This commit is contained in:
parent
49fd8ee72e
commit
277822053c
1 changed files with 45 additions and 21 deletions
|
|
@ -55,10 +55,16 @@ static pa_mutex *mutex;
|
||||||
static unsigned n_ref = 0;
|
static unsigned n_ref = 0;
|
||||||
static int lock_fd = -1;
|
static int lock_fd = -1;
|
||||||
static pa_mutex *lock_fd_mutex = NULL;
|
static pa_mutex *lock_fd_mutex = NULL;
|
||||||
static pa_bool_t taken = FALSE;
|
static pa_thread *thread = NULL;
|
||||||
static pa_thread *thread;
|
|
||||||
static int pipe_fd[2] = { -1, -1 };
|
static int pipe_fd[2] = { -1, -1 };
|
||||||
|
|
||||||
|
static enum {
|
||||||
|
STATE_IDLE,
|
||||||
|
STATE_OWNING,
|
||||||
|
STATE_TAKEN,
|
||||||
|
STATE_FAILED
|
||||||
|
} state = STATE_IDLE;
|
||||||
|
|
||||||
static void destroy_mutex(void) PA_GCC_DESTRUCTOR;
|
static void destroy_mutex(void) PA_GCC_DESTRUCTOR;
|
||||||
|
|
||||||
static int ref(void) {
|
static int ref(void) {
|
||||||
|
|
@ -67,15 +73,16 @@ static int ref(void) {
|
||||||
|
|
||||||
pa_assert(pipe_fd[0] >= 0);
|
pa_assert(pipe_fd[0] >= 0);
|
||||||
pa_assert(pipe_fd[1] >= 0);
|
pa_assert(pipe_fd[1] >= 0);
|
||||||
|
pa_assert(lock_fd_mutex);
|
||||||
|
|
||||||
n_ref++;
|
n_ref++;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pa_assert(lock_fd < 0);
|
|
||||||
pa_assert(!lock_fd_mutex);
|
pa_assert(!lock_fd_mutex);
|
||||||
pa_assert(!taken);
|
pa_assert(state == STATE_IDLE);
|
||||||
|
pa_assert(lock_fd < 0);
|
||||||
pa_assert(!thread);
|
pa_assert(!thread);
|
||||||
pa_assert(pipe_fd[0] < 0);
|
pa_assert(pipe_fd[0] < 0);
|
||||||
pa_assert(pipe_fd[1] < 0);
|
pa_assert(pipe_fd[1] < 0);
|
||||||
|
|
@ -83,14 +90,14 @@ static int ref(void) {
|
||||||
if (pipe(pipe_fd) < 0)
|
if (pipe(pipe_fd) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
lock_fd_mutex = pa_mutex_new(FALSE, FALSE);
|
|
||||||
|
|
||||||
pa_make_fd_cloexec(pipe_fd[0]);
|
pa_make_fd_cloexec(pipe_fd[0]);
|
||||||
pa_make_fd_cloexec(pipe_fd[1]);
|
pa_make_fd_cloexec(pipe_fd[1]);
|
||||||
|
|
||||||
pa_make_fd_nonblock(pipe_fd[1]);
|
pa_make_fd_nonblock(pipe_fd[1]);
|
||||||
pa_make_fd_nonblock(pipe_fd[0]);
|
pa_make_fd_nonblock(pipe_fd[0]);
|
||||||
|
|
||||||
|
lock_fd_mutex = pa_mutex_new(FALSE, FALSE);
|
||||||
|
|
||||||
n_ref = 1;
|
n_ref = 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -107,15 +114,18 @@ static void unref(pa_bool_t after_fork) {
|
||||||
if (n_ref > 0)
|
if (n_ref > 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
pa_assert(!taken);
|
|
||||||
|
|
||||||
if (thread) {
|
if (thread) {
|
||||||
pa_thread_free(thread);
|
pa_thread_free(thread);
|
||||||
thread = NULL;
|
thread = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pa_mutex_lock(lock_fd_mutex);
|
pa_mutex_lock(lock_fd_mutex);
|
||||||
if (lock_fd >= 0) {
|
|
||||||
|
pa_assert(state != STATE_TAKEN);
|
||||||
|
|
||||||
|
if (state == STATE_OWNING) {
|
||||||
|
|
||||||
|
pa_assert(lock_fd >= 0);
|
||||||
|
|
||||||
if (after_fork)
|
if (after_fork)
|
||||||
pa_close(lock_fd);
|
pa_close(lock_fd);
|
||||||
|
|
@ -127,10 +137,12 @@ static void unref(pa_bool_t after_fork) {
|
||||||
|
|
||||||
pa_unlock_lockfile(lf, lock_fd);
|
pa_unlock_lockfile(lf, lock_fd);
|
||||||
pa_xfree(lf);
|
pa_xfree(lf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lock_fd = -1;
|
lock_fd = -1;
|
||||||
}
|
state = STATE_IDLE;
|
||||||
}
|
|
||||||
pa_mutex_unlock(lock_fd_mutex);
|
pa_mutex_unlock(lock_fd_mutex);
|
||||||
|
|
||||||
pa_mutex_free(lock_fd_mutex);
|
pa_mutex_free(lock_fd_mutex);
|
||||||
|
|
@ -205,15 +217,24 @@ static void thread_func(void *u) {
|
||||||
|
|
||||||
if (!(lf = pa_runtime_path(AUTOSPAWN_LOCK))) {
|
if (!(lf = pa_runtime_path(AUTOSPAWN_LOCK))) {
|
||||||
pa_log_warn(_("Cannot access autospawn lock."));
|
pa_log_warn(_("Cannot access autospawn lock."));
|
||||||
goto finish;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((fd = pa_lock_lockfile(lf)) < 0)
|
if ((fd = pa_lock_lockfile(lf)) < 0)
|
||||||
goto finish;
|
goto fail;
|
||||||
|
|
||||||
pa_mutex_lock(lock_fd_mutex);
|
pa_mutex_lock(lock_fd_mutex);
|
||||||
pa_assert(lock_fd < 0);
|
pa_assert(state == STATE_IDLE);
|
||||||
lock_fd = fd;
|
lock_fd = fd;
|
||||||
|
state = STATE_OWNING;
|
||||||
|
pa_mutex_unlock(lock_fd_mutex);
|
||||||
|
|
||||||
|
goto finish;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
pa_mutex_lock(lock_fd_mutex);
|
||||||
|
pa_assert(state == STATE_IDLE);
|
||||||
|
state = STATE_FAILED;
|
||||||
pa_mutex_unlock(lock_fd_mutex);
|
pa_mutex_unlock(lock_fd_mutex);
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
|
|
@ -238,12 +259,10 @@ static void create_mutex(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void destroy_mutex(void) {
|
static void destroy_mutex(void) {
|
||||||
|
|
||||||
if (mutex)
|
if (mutex)
|
||||||
pa_mutex_free(mutex);
|
pa_mutex_free(mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int pa_autospawn_lock_init(void) {
|
int pa_autospawn_lock_init(void) {
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
|
|
@ -273,13 +292,18 @@ int pa_autospawn_lock_acquire(pa_bool_t block) {
|
||||||
|
|
||||||
empty_pipe();
|
empty_pipe();
|
||||||
|
|
||||||
if (lock_fd >= 0 && !taken) {
|
if (state == STATE_OWNING) {
|
||||||
taken = TRUE;
|
state = STATE_TAKEN;
|
||||||
ret = 1;
|
ret = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lock_fd < 0)
|
if (state == STATE_FAILED) {
|
||||||
|
ret = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == STATE_IDLE)
|
||||||
if (start_thread() < 0)
|
if (start_thread() < 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -310,8 +334,8 @@ void pa_autospawn_lock_release(void) {
|
||||||
pa_mutex_lock(mutex);
|
pa_mutex_lock(mutex);
|
||||||
pa_assert(n_ref >= 1);
|
pa_assert(n_ref >= 1);
|
||||||
|
|
||||||
pa_assert(taken);
|
pa_assert(state == STATE_TAKEN);
|
||||||
taken = FALSE;
|
state = STATE_OWNING;
|
||||||
|
|
||||||
ping();
|
ping();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue