diff options
Diffstat (limited to 'src/pulsecore/shm.c')
-rw-r--r-- | src/pulsecore/shm.c | 68 |
1 files changed, 36 insertions, 32 deletions
diff --git a/src/pulsecore/shm.c b/src/pulsecore/shm.c index 02f6a7bd..298bf716 100644 --- a/src/pulsecore/shm.c +++ b/src/pulsecore/shm.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /*** This file is part of PulseAudio. @@ -42,6 +40,7 @@ #endif #include <pulse/xmalloc.h> +#include <pulse/gccmacro.h> #include <pulsecore/core-error.h> #include <pulsecore/log.h> @@ -56,7 +55,7 @@ #define MADV_REMOVE 9 #endif -#define MAX_SHM_SIZE (PA_ALIGN(1024*1024*20)) +#define MAX_SHM_SIZE (PA_ALIGN(1024*1024*64)) #ifdef __linux__ /* On Linux we know that the shared memory blocks are files in @@ -69,37 +68,38 @@ #define SHM_MARKER ((int) 0xbeefcafe) -/* We now put this SHM marker at the end of each segment. It's optional to not require a reboot when upgrading, though */ -struct shm_marker { +/* We now put this SHM marker at the end of each segment. It's + * optional, to not require a reboot when upgrading, though */ +struct shm_marker PA_GCC_PACKED { pa_atomic_t marker; /* 0xbeefcafe */ pa_atomic_t pid; - void *_reserverd1; - void *_reserverd2; - void *_reserverd3; - void *_reserverd4; + uint64_t *_reserverd1; + uint64_t *_reserverd2; + uint64_t *_reserverd3; + uint64_t *_reserverd4; }; - + static char *segment_name(char *fn, size_t l, unsigned id) { pa_snprintf(fn, l, "/pulse-shm-%u", id); return fn; } -int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) { +int pa_shm_create_rw(pa_shm *m, size_t size, pa_bool_t shared, mode_t mode) { char fn[32]; int fd = -1; pa_assert(m); pa_assert(size > 0); - pa_assert(size < MAX_SHM_SIZE); + pa_assert(size <= MAX_SHM_SIZE); pa_assert(mode >= 0600); /* Each time we create a new SHM area, let's first drop all stale * ones */ pa_shm_cleanup(); - + /* Round up to make it aligned */ size = PA_ALIGN(size); - + if (!shared) { m->id = 0; m->size = size; @@ -122,12 +122,12 @@ int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) { m->ptr = pa_xmalloc(m->size); #endif - m->do_unlink = 0; + m->do_unlink = FALSE; } else { #ifdef HAVE_SHM_OPEN struct shm_marker *marker; - + pa_random(&m->id, sizeof(m->id)); segment_name(fn, sizeof(fn), m->id); @@ -153,9 +153,9 @@ int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) { marker = (struct shm_marker*) ((uint8_t*) m->ptr + m->size - PA_ALIGN(sizeof(struct shm_marker))); pa_atomic_store(&marker->pid, (int) getpid()); pa_atomic_store(&marker->marker, SHM_MARKER); - + pa_assert_se(close(fd) == 0); - m->do_unlink = 1; + m->do_unlink = TRUE; #else return -1; #endif @@ -185,7 +185,7 @@ void pa_shm_free(pa_shm *m) { #ifdef MAP_FAILED pa_assert(m->ptr != MAP_FAILED); #endif - + if (!m->shared) { #ifdef MAP_ANONYMOUS if (munmap(m->ptr, m->size) < 0) @@ -199,12 +199,12 @@ void pa_shm_free(pa_shm *m) { #ifdef HAVE_SHM_OPEN if (munmap(m->ptr, m->size) < 0) pa_log("munmap() failed: %s", pa_cstrerror(errno)); - + if (m->do_unlink) { char fn[32]; - + segment_name(fn, sizeof(fn), m->id); - + if (shm_unlink(fn) < 0) pa_log(" shm_unlink(%s) failed: %s", fn, pa_cstrerror(errno)); } @@ -236,7 +236,7 @@ void pa_shm_punch(pa_shm *m, size_t offset, size_t size) { /* Align this to multiples of the page size */ ptr = (uint8_t*) m->ptr + offset; o = (uint8_t*) ptr - (uint8_t*) PA_PAGE_ALIGN_PTR(ptr); - + if (o > 0) { ps = PA_PAGE_SIZE; ptr = (uint8_t*) ptr + (ps - o); @@ -282,7 +282,9 @@ int pa_shm_attach_ro(pa_shm *m, unsigned id) { goto fail; } - if (st.st_size <= 0 || st.st_size > MAX_SHM_SIZE+PA_ALIGN(sizeof(struct shm_marker)) || PA_ALIGN(st.st_size) != st.st_size) { + if (st.st_size <= 0 || + st.st_size > (off_t) (MAX_SHM_SIZE+PA_ALIGN(sizeof(struct shm_marker))) || + PA_ALIGN((size_t) st.st_size) != (size_t) st.st_size) { pa_log("Invalid shared memory segment size"); goto fail; } @@ -318,6 +320,7 @@ int pa_shm_attach_ro(pa_shm *m, unsigned id) { int pa_shm_cleanup(void) { +#ifdef HAVE_SHM_OPEN #ifdef SHM_PATH DIR *d; struct dirent *de; @@ -333,13 +336,13 @@ int pa_shm_cleanup(void) { pid_t pid; char fn[128]; struct shm_marker *m; - + if (strncmp(de->d_name, "pulse-shm-", 10)) continue; if (pa_atou(de->d_name + 10, &id) < 0) continue; - + if (pa_shm_attach_ro(&seg, id) < 0) continue; @@ -347,14 +350,14 @@ int pa_shm_cleanup(void) { pa_shm_free(&seg); continue; } - + m = (struct shm_marker*) ((uint8_t*) seg.ptr + seg.size - PA_ALIGN(sizeof(struct shm_marker))); - + if (pa_atomic_load(&m->marker) != SHM_MARKER) { pa_shm_free(&seg); continue; } - + if (!(pid = (pid_t) pa_atomic_load(&m->pid))) { pa_shm_free(&seg); continue; @@ -366,16 +369,17 @@ int pa_shm_cleanup(void) { } pa_shm_free(&seg); - + /* Ok, the owner of this shms segment is dead, so, let's remove the segment */ segment_name(fn, sizeof(fn), id); - if (shm_unlink(fn) < 0 && errno != EACCES) + if (shm_unlink(fn) < 0 && errno != EACCES && errno != ENOENT) pa_log_warn("Failed to remove SHM segment %s: %s\n", fn, pa_cstrerror(errno)); } closedir(d); -#endif +#endif /* SHM_PATH */ +#endif /* HAVE_SHM_OPEN */ return 0; } |