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)

git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1810 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2007-09-11 23:12:24 +00:00
parent d5bedbcd98
commit 54506ab44a
5 changed files with 134 additions and 9 deletions

View file

@ -64,7 +64,8 @@ enum {
ARG_NO_CPU_LIMIT, ARG_NO_CPU_LIMIT,
ARG_DISABLE_SHM, ARG_DISABLE_SHM,
ARG_DUMP_RESAMPLE_METHODS, ARG_DUMP_RESAMPLE_METHODS,
ARG_SYSTEM ARG_SYSTEM,
ARG_CLEANUP_SHM
}; };
/* Tabel for getopt_long() */ /* Tabel for getopt_long() */
@ -94,6 +95,7 @@ static struct option long_options[] = {
{"no-cpu-limit", 2, 0, ARG_NO_CPU_LIMIT}, {"no-cpu-limit", 2, 0, ARG_NO_CPU_LIMIT},
{"disable-shm", 2, 0, ARG_DISABLE_SHM}, {"disable-shm", 2, 0, ARG_DISABLE_SHM},
{"dump-resample-methods", 2, 0, ARG_DUMP_RESAMPLE_METHODS}, {"dump-resample-methods", 2, 0, ARG_DUMP_RESAMPLE_METHODS},
{"cleanup-shm", 2, 0, ARG_CLEANUP_SHM},
{NULL, 0, 0, 0} {NULL, 0, 0, 0}
}; };
@ -114,6 +116,7 @@ void pa_cmdline_help(const char *argv0) {
" --dump-conf Dump default configuration\n" " --dump-conf Dump default configuration\n"
" --dump-modules Dump list of available modules\n" " --dump-modules Dump list of available modules\n"
" --dump-resample-methods Dump available resample methods\n" " --dump-resample-methods Dump available resample methods\n"
" --cleanup-shm Cleanup stale shared memory segments\n"
" -k --kill Kill a running daemon\n" " -k --kill Kill a running daemon\n"
" --check Check for a running daemon\n\n" " --check Check for a running daemon\n\n"
@ -189,6 +192,10 @@ int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d
conf->cmd = PA_CMD_DUMP_RESAMPLE_METHODS; conf->cmd = PA_CMD_DUMP_RESAMPLE_METHODS;
break; break;
case ARG_CLEANUP_SHM:
conf->cmd = PA_CMD_CLEANUP_SHM;
break;
case 'k': case 'k':
case ARG_KILL: case ARG_KILL:
conf->cmd = PA_CMD_KILL; conf->cmd = PA_CMD_KILL;

View file

@ -41,7 +41,8 @@ typedef enum pa_daemon_conf_cmd {
PA_CMD_DUMP_MODULES, PA_CMD_DUMP_MODULES,
PA_CMD_KILL, PA_CMD_KILL,
PA_CMD_CHECK, PA_CMD_CHECK,
PA_CMD_DUMP_RESAMPLE_METHODS PA_CMD_DUMP_RESAMPLE_METHODS,
PA_CMD_CLEANUP_SHM
} pa_daemon_conf_cmd_t; } pa_daemon_conf_cmd_t;
#ifdef HAVE_SYS_RESOURCE_H #ifdef HAVE_SYS_RESOURCE_H

View file

@ -58,13 +58,12 @@
#include <tcpd.h> #include <tcpd.h>
#endif #endif
#include "../pulsecore/winsock.h"
#include <pulse/mainloop.h> #include <pulse/mainloop.h>
#include <pulse/mainloop-signal.h> #include <pulse/mainloop-signal.h>
#include <pulse/timeval.h> #include <pulse/timeval.h>
#include <pulse/xmalloc.h> #include <pulse/xmalloc.h>
#include <pulsecore/winsock.h>
#include <pulsecore/core-error.h> #include <pulsecore/core-error.h>
#include <pulsecore/core.h> #include <pulsecore/core.h>
#include <pulsecore/memblock.h> #include <pulsecore/memblock.h>
@ -83,6 +82,7 @@
#include <pulsecore/mutex.h> #include <pulsecore/mutex.h>
#include <pulsecore/thread.h> #include <pulsecore/thread.h>
#include <pulsecore/once.h> #include <pulsecore/once.h>
#include <pulsecore/shm.h>
#include "cmdline.h" #include "cmdline.h"
#include "cpulimit.h" #include "cpulimit.h"
@ -496,6 +496,13 @@ int main(int argc, char *argv[]) {
goto finish; goto finish;
case PA_CMD_CLEANUP_SHM:
if (pa_shm_cleanup() >= 0)
retval = 0;
goto finish;
default: default:
pa_assert(conf->cmd == PA_CMD_DAEMON); pa_assert(conf->cmd == PA_CMD_DAEMON);
} }

View file

@ -33,17 +33,22 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <signal.h>
#ifdef HAVE_SYS_MMAN_H #ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h> #include <pulsecore/core-error.h>
#include <pulsecore/log.h> #include <pulsecore/log.h>
#include <pulsecore/random.h> #include <pulsecore/random.h>
#include <pulsecore/core-util.h> #include <pulsecore/core-util.h>
#include <pulsecore/macro.h> #include <pulsecore/macro.h>
#include <pulse/xmalloc.h> #include <pulsecore/atomic.h>
#include "shm.h" #include "shm.h"
@ -51,7 +56,28 @@
#define MADV_REMOVE 9 #define MADV_REMOVE 9
#endif #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) { static char *segment_name(char *fn, size_t l, unsigned id) {
pa_snprintf(fn, l, "/pulse-shm-%u", id); pa_snprintf(fn, l, "/pulse-shm-%u", id);
@ -67,6 +93,13 @@ int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) {
pa_assert(size < MAX_SHM_SIZE); pa_assert(size < MAX_SHM_SIZE);
pa_assert(mode >= 0600); 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) { if (!shared) {
m->id = 0; m->id = 0;
m->size = size; m->size = size;
@ -93,6 +126,8 @@ int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) {
} else { } else {
#ifdef HAVE_SHM_OPEN #ifdef HAVE_SHM_OPEN
struct shm_marker *marker;
pa_random(&m->id, sizeof(m->id)); pa_random(&m->id, sizeof(m->id));
segment_name(fn, sizeof(fn), m->id); segment_name(fn, sizeof(fn), m->id);
@ -101,7 +136,9 @@ int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) {
goto fail; 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)); pa_log("ftruncate() failed: %s", pa_cstrerror(errno));
goto fail; goto fail;
} }
@ -111,6 +148,12 @@ int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) {
goto fail; 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); close(fd);
m->do_unlink = 1; m->do_unlink = 1;
#else #else
@ -229,6 +272,7 @@ int pa_shm_attach_ro(pa_shm *m, unsigned id) {
segment_name(fn, sizeof(fn), m->id = id); segment_name(fn, sizeof(fn), m->id = id);
if ((fd = shm_open(fn, O_RDONLY, 0)) < 0) { if ((fd = shm_open(fn, O_RDONLY, 0)) < 0) {
if (errno != EACCES)
pa_log("shm_open() failed: %s", pa_cstrerror(errno)); pa_log("shm_open() failed: %s", pa_cstrerror(errno));
goto fail; goto fail;
} }
@ -238,7 +282,7 @@ int pa_shm_attach_ro(pa_shm *m, unsigned id) {
goto fail; 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"); pa_log("Invalid shared memory segment size");
goto fail; goto fail;
} }
@ -271,3 +315,67 @@ int pa_shm_attach_ro(pa_shm *m, unsigned id) {
} }
#endif /* HAVE_SHM_OPEN */ #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;
}

View file

@ -41,4 +41,6 @@ void pa_shm_punch(pa_shm *m, size_t offset, size_t size);
void pa_shm_free(pa_shm *m); void pa_shm_free(pa_shm *m);
int pa_shm_cleanup(void);
#endif #endif