[pulseaudio-discuss] [PATCH 05/11] shm: Allow to open shm in writable mode

David Henningsson david.henningsson at canonical.com
Tue Apr 29 06:22:19 PDT 2014


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 at canonical.com>
---
 src/pulsecore/memblock.c |  2 +-
 src/pulsecore/shm.c      | 12 +++++++-----
 src/pulsecore/shm.h      |  2 +-
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index 9cc02c1..8da0fcd 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 efaee57..075c8bd 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 9d61551..2238239 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);
 
-- 
1.9.1



More information about the pulseaudio-discuss mailing list