[pulseaudio-commits] r1810 - in /branches/lennart/src: daemon/cmdline.c daemon/daemon-conf.h daemon/main.c pulsecore/shm.c pulsecore/shm.h

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Tue Sep 11 16:12:24 PDT 2007


Author: lennart
Date: Wed Sep 12 01:12:24 2007
New Revision: 1810

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=1810&root=pulseaudio&view=rev
Log:
on systems where we know that POSIX shm is mapped to /dev/shm, add the ability to cleanup stale SHM segments. (Right now only Linux)

Modified:
    branches/lennart/src/daemon/cmdline.c
    branches/lennart/src/daemon/daemon-conf.h
    branches/lennart/src/daemon/main.c
    branches/lennart/src/pulsecore/shm.c
    branches/lennart/src/pulsecore/shm.h

Modified: branches/lennart/src/daemon/cmdline.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/daemon/cmdline.c?rev=1810&root=pulseaudio&r1=1809&r2=1810&view=diff
==============================================================================
--- branches/lennart/src/daemon/cmdline.c (original)
+++ branches/lennart/src/daemon/cmdline.c Wed Sep 12 01:12:24 2007
@@ -64,7 +64,8 @@
     ARG_NO_CPU_LIMIT,
     ARG_DISABLE_SHM,
     ARG_DUMP_RESAMPLE_METHODS,
-    ARG_SYSTEM
+    ARG_SYSTEM,
+    ARG_CLEANUP_SHM
 };
 
 /* Tabel for getopt_long() */
@@ -94,6 +95,7 @@
     {"no-cpu-limit",                2, 0, ARG_NO_CPU_LIMIT},
     {"disable-shm",                 2, 0, ARG_DISABLE_SHM},
     {"dump-resample-methods",       2, 0, ARG_DUMP_RESAMPLE_METHODS},
+    {"cleanup-shm",                 2, 0, ARG_CLEANUP_SHM},
     {NULL, 0, 0, 0}
 };
 
@@ -114,6 +116,7 @@
            "      --dump-conf                       Dump default configuration\n"
            "      --dump-modules                    Dump list of available modules\n"
            "      --dump-resample-methods           Dump available resample methods\n"
+           "      --cleanup-shm                     Cleanup stale shared memory segments\n"
            "  -k  --kill                            Kill a running daemon\n"
            "      --check                           Check for a running daemon\n\n"
 
@@ -188,6 +191,10 @@
             case ARG_DUMP_RESAMPLE_METHODS:
                 conf->cmd = PA_CMD_DUMP_RESAMPLE_METHODS;
                 break;
+
+            case ARG_CLEANUP_SHM:
+                conf->cmd = PA_CMD_CLEANUP_SHM;
+                break;
                 
             case 'k':
             case ARG_KILL:

Modified: branches/lennart/src/daemon/daemon-conf.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/daemon/daemon-conf.h?rev=1810&root=pulseaudio&r1=1809&r2=1810&view=diff
==============================================================================
--- branches/lennart/src/daemon/daemon-conf.h (original)
+++ branches/lennart/src/daemon/daemon-conf.h Wed Sep 12 01:12:24 2007
@@ -41,7 +41,8 @@
     PA_CMD_DUMP_MODULES,
     PA_CMD_KILL,
     PA_CMD_CHECK,
-    PA_CMD_DUMP_RESAMPLE_METHODS
+    PA_CMD_DUMP_RESAMPLE_METHODS,
+    PA_CMD_CLEANUP_SHM
 } pa_daemon_conf_cmd_t;
 
 #ifdef HAVE_SYS_RESOURCE_H

Modified: branches/lennart/src/daemon/main.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/daemon/main.c?rev=1810&root=pulseaudio&r1=1809&r2=1810&view=diff
==============================================================================
--- branches/lennart/src/daemon/main.c (original)
+++ branches/lennart/src/daemon/main.c Wed Sep 12 01:12:24 2007
@@ -58,13 +58,12 @@
 #include <tcpd.h>
 #endif
 
-#include "../pulsecore/winsock.h"
-
 #include <pulse/mainloop.h>
 #include <pulse/mainloop-signal.h>
 #include <pulse/timeval.h>
 #include <pulse/xmalloc.h>
 
+#include <pulsecore/winsock.h>
 #include <pulsecore/core-error.h>
 #include <pulsecore/core.h>
 #include <pulsecore/memblock.h>
@@ -83,6 +82,7 @@
 #include <pulsecore/mutex.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/once.h>
+#include <pulsecore/shm.h>
 
 #include "cmdline.h"
 #include "cpulimit.h"
@@ -496,6 +496,13 @@
 
             goto finish;
 
+        case PA_CMD_CLEANUP_SHM:
+
+            if (pa_shm_cleanup() >= 0)
+                retval = 0;
+
+            goto finish;
+            
         default:
             pa_assert(conf->cmd == PA_CMD_DAEMON);
     }

Modified: branches/lennart/src/pulsecore/shm.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/shm.c?rev=1810&root=pulseaudio&r1=1809&r2=1810&view=diff
==============================================================================
--- branches/lennart/src/pulsecore/shm.c (original)
+++ branches/lennart/src/pulsecore/shm.c Wed Sep 12 01:12:24 2007
@@ -33,17 +33,22 @@
 #include <errno.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <signal.h>
 
 #ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #endif
+
+#include <pulse/xmalloc.h>
 
 #include <pulsecore/core-error.h>
 #include <pulsecore/log.h>
 #include <pulsecore/random.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/macro.h>
-#include <pulse/xmalloc.h>
+#include <pulsecore/atomic.h>
 
 #include "shm.h"
 
@@ -51,8 +56,29 @@
 #define MADV_REMOVE 9
 #endif
 
-#define MAX_SHM_SIZE (1024*1024*20)
-
+#define MAX_SHM_SIZE (PA_ALIGN(1024*1024*20))
+
+#ifdef __linux__
+/* On Linux we know that the shared memory blocks are files in
+ * /dev/shm. We can use that information to list all blocks and
+ * cleanup unused ones */
+#define SHM_PATH "/dev/shm/"
+#else
+#undef SHM_PATH
+#endif
+
+#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 {
+    pa_atomic_t marker; /* 0xbeefcafe */
+    pa_atomic_t pid;
+    void *_reserverd1;
+    void *_reserverd2;
+    void *_reserverd3;
+    void *_reserverd4;
+};
+    
 static char *segment_name(char *fn, size_t l, unsigned id) {
     pa_snprintf(fn, l, "/pulse-shm-%u", id);
     return fn;
@@ -67,6 +93,13 @@
     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;
@@ -93,6 +126,8 @@
 
     } else {
 #ifdef HAVE_SHM_OPEN
+        struct shm_marker *marker;
+        
         pa_random(&m->id, sizeof(m->id));
         segment_name(fn, sizeof(fn), m->id);
 
@@ -101,7 +136,9 @@
             goto fail;
         }
 
-        if (ftruncate(fd, m->size = size) < 0) {
+        m->size = size + PA_ALIGN(sizeof(struct shm_marker));
+
+        if (ftruncate(fd, m->size) < 0) {
             pa_log("ftruncate() failed: %s", pa_cstrerror(errno));
             goto fail;
         }
@@ -111,6 +148,12 @@
             goto fail;
         }
 
+        /* We store our PID at the end of the shm block, so that we
+         * can check for dead shm segments later */
+        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);
+        
         close(fd);
         m->do_unlink = 1;
 #else
@@ -229,7 +272,8 @@
     segment_name(fn, sizeof(fn), m->id = id);
 
     if ((fd = shm_open(fn, O_RDONLY, 0)) < 0) {
-        pa_log("shm_open() failed: %s", pa_cstrerror(errno));
+        if (errno != EACCES)
+            pa_log("shm_open() failed: %s", pa_cstrerror(errno));
         goto fail;
     }
 
@@ -238,7 +282,7 @@
         goto fail;
     }
 
-    if (st.st_size <= 0 || st.st_size > MAX_SHM_SIZE) {
+    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) {
         pa_log("Invalid shared memory segment size");
         goto fail;
     }
@@ -271,3 +315,67 @@
 }
 
 #endif /* HAVE_SHM_OPEN */
+
+int pa_shm_cleanup(void) {
+
+#ifdef SHM_PATH
+    DIR *d;
+    struct dirent *de;
+
+    if (!(d = opendir(SHM_PATH))) {
+        pa_log_warn("Failed to read "SHM_PATH": %s", pa_cstrerror(errno));
+        return -1;
+    }
+
+    while ((de = readdir(d))) {
+        pa_shm seg;
+        unsigned id;
+        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;
+
+        if (seg.size < PA_ALIGN(sizeof(struct shm_marker))) {
+            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;
+        }
+
+        if (kill(pid, 0) == 0 || errno != ESRCH) {
+            pa_shm_free(&seg);
+            continue;
+        }
+
+        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)
+            pa_log_warn("Failed to remove SHM segment %s: %s\n", fn, pa_cstrerror(errno));
+    }
+
+    closedir(d);
+#endif
+
+    return 0;
+}

Modified: branches/lennart/src/pulsecore/shm.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/shm.h?rev=1810&root=pulseaudio&r1=1809&r2=1810&view=diff
==============================================================================
--- branches/lennart/src/pulsecore/shm.h (original)
+++ branches/lennart/src/pulsecore/shm.h Wed Sep 12 01:12:24 2007
@@ -41,4 +41,6 @@
 
 void pa_shm_free(pa_shm *m);
 
+int pa_shm_cleanup(void);
+
 #endif




More information about the pulseaudio-commits mailing list