summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Henningsson <david.henningsson@canonical.com>2014-04-25 13:58:26 +0200
committerDavid Henningsson <david.henningsson@canonical.com>2014-06-27 14:16:32 +0200
commit613177919f0876c082e4db3b610afcfced5f593a (patch)
tree1adeddd93295dcc5b68006d060207488808a40e0
parent0cd4d3531acc9894275959309967d0bc2adcc8f5 (diff)
shm: Allow to open shm in writable mode
This is a preparation for the shm ringbuffer, which needs to be able to be writable by both sides, because there are atomic variables they both need to modify. Signed-off-by: David Henningsson <david.henningsson@canonical.com>
-rw-r--r--src/pulsecore/memblock.c2
-rw-r--r--src/pulsecore/shm.c12
-rw-r--r--src/pulsecore/shm.h2
3 files changed, 9 insertions, 7 deletions
diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index 9cc02c1a7..8da0fcd45 100644
--- a/src/pulsecore/memblock.c
+++ b/src/pulsecore/memblock.c
@@ -913,7 +913,7 @@ static pa_memimport_segment* segment_attach(pa_memimport *i, uint32_t shm_id) {
seg = pa_xnew0(pa_memimport_segment, 1);
- if (pa_shm_attach_ro(&seg->memory, shm_id) < 0) {
+ if (pa_shm_attach(&seg->memory, shm_id, false) < 0) {
pa_xfree(seg);
return NULL;
}
diff --git a/src/pulsecore/shm.c b/src/pulsecore/shm.c
index efaee5796..075c8bd94 100644
--- a/src/pulsecore/shm.c
+++ b/src/pulsecore/shm.c
@@ -290,16 +290,17 @@ void pa_shm_punch(pa_shm *m, size_t offset, size_t size) {
#ifdef HAVE_SHM_OPEN
-int pa_shm_attach_ro(pa_shm *m, unsigned id) {
+int pa_shm_attach(pa_shm *m, unsigned id, bool writable) {
char fn[32];
int fd = -1;
+ int prot;
struct stat st;
pa_assert(m);
segment_name(fn, sizeof(fn), m->id = id);
- if ((fd = shm_open(fn, O_RDONLY, 0)) < 0) {
+ if ((fd = shm_open(fn, writable ? O_RDWR : O_RDONLY, 0)) < 0) {
if (errno != EACCES && errno != ENOENT)
pa_log("shm_open() failed: %s", pa_cstrerror(errno));
goto fail;
@@ -319,7 +320,8 @@ int pa_shm_attach_ro(pa_shm *m, unsigned id) {
m->size = (size_t) st.st_size;
- if ((m->ptr = mmap(NULL, PA_PAGE_ALIGN(m->size), PROT_READ, MAP_SHARED, fd, (off_t) 0)) == MAP_FAILED) {
+ prot = writable ? PROT_READ | PROT_WRITE : PROT_READ;
+ if ((m->ptr = mmap(NULL, PA_PAGE_ALIGN(m->size), prot, MAP_SHARED, fd, (off_t) 0)) == MAP_FAILED) {
pa_log("mmap() failed: %s", pa_cstrerror(errno));
goto fail;
}
@@ -340,7 +342,7 @@ fail:
#else /* HAVE_SHM_OPEN */
-int pa_shm_attach_ro(pa_shm *m, unsigned id) {
+int pa_shm_attach(pa_shm *m, unsigned id, bool writable) {
return -1;
}
@@ -375,7 +377,7 @@ int pa_shm_cleanup(void) {
if (pa_atou(de->d_name + SHM_ID_LEN, &id) < 0)
continue;
- if (pa_shm_attach_ro(&seg, id) < 0)
+ if (pa_shm_attach(&seg, id, false) < 0)
continue;
if (seg.size < SHM_MARKER_SIZE) {
diff --git a/src/pulsecore/shm.h b/src/pulsecore/shm.h
index 9d615518c..22382395d 100644
--- a/src/pulsecore/shm.h
+++ b/src/pulsecore/shm.h
@@ -35,7 +35,7 @@ typedef struct pa_shm {
} pa_shm;
int pa_shm_create_rw(pa_shm *m, size_t size, bool shared, mode_t mode);
-int pa_shm_attach_ro(pa_shm *m, unsigned id);
+int pa_shm_attach(pa_shm *m, unsigned id, bool writable);
void pa_shm_punch(pa_shm *m, size_t offset, size_t size);