diff options
Diffstat (limited to 'src/pulsecore')
-rw-r--r-- | src/pulsecore/core-util.c | 39 | ||||
-rw-r--r-- | src/pulsecore/llist.h | 3 | ||||
-rw-r--r-- | src/pulsecore/log.c | 4 | ||||
-rw-r--r-- | src/pulsecore/memblock.c | 7 | ||||
-rw-r--r-- | src/pulsecore/memtrap.c | 249 | ||||
-rw-r--r-- | src/pulsecore/memtrap.h | 51 | ||||
-rw-r--r-- | src/pulsecore/mutex-posix.c | 2 | ||||
-rw-r--r-- | src/pulsecore/mutex.h | 4 | ||||
-rw-r--r-- | src/pulsecore/object.c | 4 | ||||
-rw-r--r-- | src/pulsecore/ratelimit.c | 2 | ||||
-rw-r--r-- | src/pulsecore/semaphore-posix.c | 22 | ||||
-rw-r--r-- | src/pulsecore/semaphore.h | 15 |
12 files changed, 391 insertions, 11 deletions
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 24d929d7..294f63cb 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -296,7 +296,15 @@ ssize_t pa_read(int fd, void *buf, size_t count, int *type) { #endif - return read(fd, buf, count); + for (;;) { + ssize_t r; + + if ((r = read(fd, buf, count)) < 0) + if (errno == EINTR) + continue; + + return r; + } } /** Similar to pa_read(), but handles writes */ @@ -305,8 +313,17 @@ ssize_t pa_write(int fd, const void *buf, size_t count, int *type) { if (!type || *type == 0) { ssize_t r; - if ((r = send(fd, buf, count, MSG_NOSIGNAL)) >= 0) + for (;;) { + if ((r = send(fd, buf, count, MSG_NOSIGNAL)) < 0) { + + if (errno == EINTR) + continue; + + break; + } + return r; + } #ifdef OS_IS_WIN32 if (WSAGetLastError() != WSAENOTSOCK) { @@ -322,7 +339,15 @@ ssize_t pa_write(int fd, const void *buf, size_t count, int *type) { *type = 1; } - return write(fd, buf, count); + for (;;) { + ssize_t r; + + if ((r = write(fd, buf, count)) < 0) + if (errno == EINTR) + continue; + + return r; + } } /** Calls read() in a loop. Makes sure that as much as 'size' bytes, @@ -407,11 +432,11 @@ int pa_close(int fd) { for (;;) { int r; - if ((r = close(fd)) >= 0) - return r; + if ((r = close(fd)) < 0) + if (errno == EINTR) + continue; - if (errno != EINTR) - return r; + return r; } } diff --git a/src/pulsecore/llist.h b/src/pulsecore/llist.h index 77a1749f..58b51c68 100644 --- a/src/pulsecore/llist.h +++ b/src/pulsecore/llist.h @@ -104,4 +104,7 @@ } \ } while (0) +#define PA_LLIST_FOREACH(i,head) \ + for (i = (head); i; i = i->next) + #endif diff --git a/src/pulsecore/log.c b/src/pulsecore/log.c index 60ea9c59..15d192d6 100644 --- a/src/pulsecore/log.c +++ b/src/pulsecore/log.c @@ -285,7 +285,7 @@ void pa_log_levelv_meta( if ((_flags & PA_LOG_PRINT_META) && file && line > 0 && func) pa_snprintf(location, sizeof(location), "[%s:%i %s()] ", file, line, func); - else if (_flags & (PA_LOG_PRINT_META|PA_LOG_PRINT_FILE)) + else if ((_flags & (PA_LOG_PRINT_META|PA_LOG_PRINT_FILE)) && file) pa_snprintf(location, sizeof(location), "%s: ", pa_path_get_filename(file)); else location[0] = 0; @@ -323,7 +323,7 @@ void pa_log_levelv_meta( #endif if (!pa_utf8_valid(text)) - pa_log_level(level, __FILE__": invalid UTF-8 string following below:"); + pa_logl(level, "Invalid UTF-8 string following below:"); for (t = text; t; t = n) { if ((n = strchr(t, '\n'))) { diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c index 6cc0ff3f..9a57895b 100644 --- a/src/pulsecore/memblock.c +++ b/src/pulsecore/memblock.c @@ -45,6 +45,7 @@ #include <pulsecore/macro.h> #include <pulsecore/flist.h> #include <pulsecore/core-util.h> +#include <pulsecore/memtrap.h> #include "memblock.h" @@ -91,6 +92,7 @@ struct pa_memblock { struct pa_memimport_segment { pa_memimport *import; pa_shm memory; + pa_memtrap *trap; unsigned n_blocks; }; @@ -892,6 +894,7 @@ static pa_memimport_segment* segment_attach(pa_memimport *i, uint32_t shm_id) { seg->import = i; seg->n_blocks = 0; + seg->trap = pa_memtrap_add(seg->memory.ptr, seg->memory.size); pa_hashmap_put(i->segments, PA_UINT32_TO_PTR(shm_id), seg); return seg; @@ -903,6 +906,10 @@ static void segment_detach(pa_memimport_segment *seg) { pa_hashmap_remove(seg->import->segments, PA_UINT32_TO_PTR(seg->memory.id)); pa_shm_free(&seg->memory); + + if (seg->trap) + pa_memtrap_remove(seg->trap); + pa_xfree(seg); } diff --git a/src/pulsecore/memtrap.c b/src/pulsecore/memtrap.c new file mode 100644 index 00000000..e04e838e --- /dev/null +++ b/src/pulsecore/memtrap.c @@ -0,0 +1,249 @@ +/*** + This file is part of PulseAudio. + + Copyright 2009 Lennart Poettering + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + PulseAudio 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 Lesser General Public + License along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <signal.h> +#include <sys/mman.h> + +#include <pulse/xmalloc.h> + +#include <pulsecore/semaphore.h> +#include <pulsecore/macro.h> +#include <pulsecore/mutex.h> +#include <pulsecore/core-util.h> + +#include "memtrap.h" + +struct pa_memtrap { + void *start; + size_t size; + pa_atomic_t bad; + pa_memtrap *next[2], *prev[2]; +}; + +static pa_memtrap *memtraps[2] = { NULL, NULL }; +static pa_atomic_t read_lock = PA_ATOMIC_INIT(0); +static pa_static_semaphore semaphore = PA_STATIC_SEMAPHORE_INIT; +static pa_static_mutex write_lock = PA_STATIC_MUTEX_INIT; + +#define MSB (1U << (sizeof(unsigned)*8U-1)) +#define WHICH(n) (!!((n) & MSB)) +#define COUNTER(n) ((n) & ~MSB) + +pa_bool_t pa_memtrap_is_good(pa_memtrap *m) { + pa_assert(m); + + return !pa_atomic_load(&m->bad); +} + +static void sigsafe_error(const char *s) { + write(STDERR_FILENO, s, strlen(s)); +} + +static void signal_handler(int sig, siginfo_t* si, void *data) { + unsigned n, j; + pa_memtrap *m; + void *r; + + /* Increase the lock counter */ + n = (unsigned) pa_atomic_inc(&read_lock); + + /* The uppermost bit tells us which list to look at */ + j = WHICH(n); + + /* When n is 0 we have about 2^31 threads running that + * all got a sigbus at the same time, oh my! */ + pa_assert(COUNTER(n)+1 > 0); + + for (m = memtraps[j]; m; m = m->next[j]) + if (si->si_addr >= m->start && + (uint8_t*) si->si_addr < (uint8_t*) m->start + m->size) + break; + + if (!m) + goto fail; + + pa_atomic_store(&m->bad, 1); + + /* Remap anonymous memory into the bad segment */ + if ((r = mmap(m->start, m->size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { + sigsafe_error("mmap() failed.\n"); + goto fail; + } + + pa_assert(r == m->start); + + pa_atomic_dec(&read_lock); + + /* Post the semaphore */ + pa_semaphore_post(pa_static_semaphore_get(&semaphore, 0)); + + return; + +fail: + sigsafe_error("Failed to handle SIGBUS.\n"); + pa_atomic_dec(&read_lock); + abort(); +} + +static void memtrap_swap(unsigned n) { + + for (;;) { + + /* If the read counter is > 0 wait; if it is 0 try to swap the lists */ + if (COUNTER(n) > 0) + pa_semaphore_wait(pa_static_semaphore_get(&semaphore, 0)); + else if (pa_atomic_cmpxchg(&read_lock, (int) n, (int) (n ^ MSB))) + break; + + n = (unsigned) pa_atomic_load(&read_lock); + } +} + +static void memtrap_link(pa_memtrap *m, unsigned j) { + pa_assert(m); + + m->prev[j] = NULL; + m->next[j] = memtraps[j]; + memtraps[j] = m; +} + +static void memtrap_unlink(pa_memtrap *m, unsigned j) { + pa_assert(m); + + if (m->next[j]) + m->next[j]->prev[j] = m->prev[j]; + + if (m->prev[j]) + m->prev[j]->next[j] = m->next[j]; + else + memtraps[j] = m->next[j]; +} + +pa_memtrap* pa_memtrap_add(const void *start, size_t size) { + pa_memtrap *m = NULL; + pa_mutex *lock; + unsigned n, j; + + pa_assert(start); + pa_assert(size > 0); + pa_assert(PA_PAGE_ALIGN_PTR(start) == start); + pa_assert(PA_PAGE_ALIGN(size) == size); + + lock = pa_static_mutex_get(&write_lock, FALSE, FALSE); + pa_mutex_lock(lock); + + n = (unsigned) pa_atomic_load(&read_lock); + j = WHICH(n); + + m = pa_xnew(pa_memtrap, 1); + m->start = (void*) start; + m->size = size; + pa_atomic_store(&m->bad, 0); + + memtrap_link(m, !j); + memtrap_swap(n); + memtrap_link(m, j); + + pa_mutex_unlock(lock); + + return m; +} + +void pa_memtrap_remove(pa_memtrap *m) { + unsigned n, j; + pa_mutex *lock; + + pa_assert(m); + + lock = pa_static_mutex_get(&write_lock, FALSE, FALSE); + pa_mutex_lock(lock); + + n = (unsigned) pa_atomic_load(&read_lock); + j = WHICH(n); + + memtrap_unlink(m, !j); + memtrap_swap(n); + memtrap_unlink(m, j); + + pa_xfree(m); + + pa_mutex_unlock(lock); +} + +pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) { + unsigned n, j; + pa_mutex *lock; + + pa_assert(m); + + pa_assert(start); + pa_assert(size > 0); + pa_assert(PA_PAGE_ALIGN_PTR(start) == start); + pa_assert(PA_PAGE_ALIGN(size) == size); + + lock = pa_static_mutex_get(&write_lock, FALSE, FALSE); + pa_mutex_lock(lock); + + if (m->start == start && m->size == size) + goto unlock; + + n = (unsigned) pa_atomic_load(&read_lock); + j = WHICH(n); + + memtrap_unlink(m, !j); + memtrap_swap(n); + memtrap_unlink(m, j); + + m->start = (void*) start; + m->size = size; + pa_atomic_store(&m->bad, 0); + + n = (unsigned) pa_atomic_load(&read_lock); + j = WHICH(n); + + memtrap_link(m, !j); + memtrap_swap(n); + memtrap_link(m, j); + +unlock: + pa_mutex_unlock(lock); + + return m; +} + +void pa_memtrap_install(void) { + struct sigaction sa; + + /* Before we install the signal handler, make sure the semaphore + * is valid so that the initialization of the semaphore + * doesn't have to happen from the signal handler */ + pa_static_semaphore_get(&semaphore, 0); + + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = signal_handler; + sa.sa_flags = SA_RESTART|SA_SIGINFO; + + pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0); +} diff --git a/src/pulsecore/memtrap.h b/src/pulsecore/memtrap.h new file mode 100644 index 00000000..f7da7083 --- /dev/null +++ b/src/pulsecore/memtrap.h @@ -0,0 +1,51 @@ +#ifndef foopulsecorememtraphfoo +#define foopulsecorememtraphfoo + +/*** + This file is part of PulseAudio. + + Copyright 2009 Lennart Poettering + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + PulseAudio 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 Lesser General Public + License along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include <sys/types.h> + +#include <pulsecore/macro.h> + +/* This subsystem will trap SIGBUS on specific memory regions. The + * regions will be remapped to anonymous memory (i.e. writable NUL + * bytes) on SIGBUS, so that execution of the main program can + * continue though with memory having changed beneath its hands. With + * pa_memtrap_is_good() it is possible to query if a memory region is + * still 'good' i.e. no SIGBUS has happened yet for it. + * + * Intended usage is to handle memory mapped in which is controlled by + * other processes that might execute ftruncate() or when mapping in + * hardware resources that might get invalidated when unplugged. */ + +typedef struct pa_memtrap pa_memtrap; + +pa_memtrap* pa_memtrap_add(const void *start, size_t size); +pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size); + +void pa_memtrap_remove(pa_memtrap *m); + +pa_bool_t pa_memtrap_is_good(pa_memtrap *m); + +void pa_memtrap_install(void); + +#endif diff --git a/src/pulsecore/mutex-posix.c b/src/pulsecore/mutex-posix.c index b3e5256a..0ff4bee6 100644 --- a/src/pulsecore/mutex-posix.c +++ b/src/pulsecore/mutex-posix.c @@ -153,6 +153,8 @@ pa_mutex* pa_static_mutex_get(pa_static_mutex *s, pa_bool_t recursive, pa_bool_t if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m))) return m; + pa_mutex_free(m); + /* Him, filling in failed, so someone else must have filled in * already */ pa_assert_se(m = pa_atomic_ptr_load(&s->ptr)); diff --git a/src/pulsecore/mutex.h b/src/pulsecore/mutex.h index a4dd6738..b1edc132 100644 --- a/src/pulsecore/mutex.h +++ b/src/pulsecore/mutex.h @@ -46,10 +46,14 @@ void pa_cond_free(pa_cond *c); void pa_cond_signal(pa_cond *c, int broadcast); int pa_cond_wait(pa_cond *c, pa_mutex *m); +/* Static mutexes are basically just atomically updated pointers to pa_mutex objects */ + typedef struct pa_static_mutex { pa_atomic_ptr_t ptr; } pa_static_mutex; +#define PA_STATIC_MUTEX_INIT { PA_ATOMIC_PTR_INIT(NULL) } + pa_mutex* pa_static_mutex_get(pa_static_mutex *m, pa_bool_t recursive, pa_bool_t inherit_priority); #endif diff --git a/src/pulsecore/object.c b/src/pulsecore/object.c index 8fd05fb6..f3ead9c5 100644 --- a/src/pulsecore/object.c +++ b/src/pulsecore/object.c @@ -24,6 +24,8 @@ #include <config.h> #endif +#include <pulsecore/core-util.h> + #include "object.h" pa_object *pa_object_new_internal(size_t size, const char *type_name, int (*check_type)(const char *type_name)) { @@ -66,5 +68,5 @@ void pa_object_unref(pa_object *o) { int pa_object_check_type(const char *type_name) { pa_assert(type_name); - return strcmp(type_name, "pa_object") == 0; + return pa_streq(type_name, "pa_object"); } diff --git a/src/pulsecore/ratelimit.c b/src/pulsecore/ratelimit.c index 29e6fb10..e913ca19 100644 --- a/src/pulsecore/ratelimit.c +++ b/src/pulsecore/ratelimit.c @@ -29,7 +29,7 @@ #include "ratelimit.h" -static pa_static_mutex mutex; +static pa_static_mutex mutex = PA_STATIC_MUTEX_INIT; /* Modelled after Linux' lib/ratelimit.c by Dave Young * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */ diff --git a/src/pulsecore/semaphore-posix.c b/src/pulsecore/semaphore-posix.c index 616d897d..2aa1bce9 100644 --- a/src/pulsecore/semaphore-posix.c +++ b/src/pulsecore/semaphore-posix.c @@ -65,3 +65,25 @@ void pa_semaphore_wait(pa_semaphore *s) { pa_assert(ret == 0); } + +pa_semaphore* pa_static_semaphore_get(pa_static_semaphore *s, unsigned value) { + pa_semaphore *m; + + pa_assert(s); + + /* First, check if already initialized and short cut */ + if ((m = pa_atomic_ptr_load(&s->ptr))) + return m; + + /* OK, not initialized, so let's allocate, and fill in */ + m = pa_semaphore_new(value); + if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m))) + return m; + + pa_semaphore_free(m); + + /* Him, filling in failed, so someone else must have filled in + * already */ + pa_assert_se(m = pa_atomic_ptr_load(&s->ptr)); + return m; +} diff --git a/src/pulsecore/semaphore.h b/src/pulsecore/semaphore.h index dc3ca6a5..2bf81496 100644 --- a/src/pulsecore/semaphore.h +++ b/src/pulsecore/semaphore.h @@ -22,6 +22,9 @@ USA. ***/ +#include <pulsecore/macro.h> +#include <pulsecore/atomic.h> + typedef struct pa_semaphore pa_semaphore; pa_semaphore* pa_semaphore_new(unsigned value); @@ -30,4 +33,16 @@ void pa_semaphore_free(pa_semaphore *m); void pa_semaphore_post(pa_semaphore *m); void pa_semaphore_wait(pa_semaphore *m); +/* Static semaphores are basically just atomically updated pointers to + * pa_semaphore objects */ + +typedef struct pa_static_semaphore { + pa_atomic_ptr_t ptr; +} pa_static_semaphore; + +#define PA_STATIC_SEMAPHORE_INIT { PA_ATOMIC_PTR_INIT(NULL) } + +/* When you call this make sure to pass always the same value parameter! */ +pa_semaphore* pa_static_semaphore_get(pa_static_semaphore *m, unsigned value); + #endif |