default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / pulsecore / memtrap.c
blob4df1fb715cacf0dacc4ce4906bf0d707c1a23344
1 /***
2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <signal.h>
28 #ifdef HAVE_SYS_MMAN_H
29 #include <sys/mman.h>
30 #endif
32 /* This is deprecated on glibc but is still used by FreeBSD */
33 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
34 # define MAP_ANONYMOUS MAP_ANON
35 #endif
37 #include <pulse/xmalloc.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/aupdate.h>
41 #include <pulsecore/atomic.h>
42 #include <pulsecore/once.h>
43 #include <pulsecore/mutex.h>
45 #include "memtrap.h"
47 struct pa_memtrap {
48 void *start;
49 size_t size;
50 pa_atomic_t bad;
51 pa_memtrap *next[2], *prev[2];
54 static pa_memtrap *memtraps[2] = { NULL, NULL };
55 static pa_aupdate *aupdate;
56 static pa_static_mutex mutex = PA_STATIC_MUTEX_INIT; /* only required to serialize access to the write side */
58 static void allocate_aupdate(void) {
59 PA_ONCE_BEGIN {
60 aupdate = pa_aupdate_new();
61 } PA_ONCE_END;
64 pa_bool_t pa_memtrap_is_good(pa_memtrap *m) {
65 pa_assert(m);
67 return !pa_atomic_load(&m->bad);
70 #ifdef HAVE_SIGACTION
71 static void sigsafe_error(const char *s) {
72 (void) write(STDERR_FILENO, s, strlen(s));
75 static void signal_handler(int sig, siginfo_t* si, void *data) {
76 unsigned j;
77 pa_memtrap *m;
78 void *r;
80 j = pa_aupdate_read_begin(aupdate);
82 for (m = memtraps[j]; m; m = m->next[j])
83 if (si->si_addr >= m->start &&
84 (uint8_t*) si->si_addr < (uint8_t*) m->start + m->size)
85 break;
87 if (!m)
88 goto fail;
90 pa_atomic_store(&m->bad, 1);
92 /* Remap anonymous memory into the bad segment */
93 if ((r = mmap(m->start, m->size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
94 sigsafe_error("mmap() failed.\n");
95 goto fail;
98 pa_assert(r == m->start);
100 pa_aupdate_read_end(aupdate);
101 return;
103 fail:
104 pa_aupdate_read_end(aupdate);
106 sigsafe_error("Failed to handle SIGBUS.\n");
107 abort();
109 #endif
111 static void memtrap_link(pa_memtrap *m, unsigned j) {
112 pa_assert(m);
114 m->prev[j] = NULL;
116 if ((m->next[j] = memtraps[j]))
117 m->next[j]->prev[j] = m;
119 memtraps[j] = m;
122 static void memtrap_unlink(pa_memtrap *m, unsigned j) {
123 pa_assert(m);
125 if (m->next[j])
126 m->next[j]->prev[j] = m->prev[j];
128 if (m->prev[j])
129 m->prev[j]->next[j] = m->next[j];
130 else
131 memtraps[j] = m->next[j];
134 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
135 pa_memtrap *m = NULL;
136 unsigned j;
137 pa_mutex *mx;
139 pa_assert(start);
140 pa_assert(size > 0);
142 start = PA_PAGE_ALIGN_PTR(start);
143 size = PA_PAGE_ALIGN(size);
145 m = pa_xnew(pa_memtrap, 1);
146 m->start = (void*) start;
147 m->size = size;
148 pa_atomic_store(&m->bad, 0);
150 allocate_aupdate();
152 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
153 pa_mutex_lock(mx);
155 j = pa_aupdate_write_begin(aupdate);
156 memtrap_link(m, j);
157 j = pa_aupdate_write_swap(aupdate);
158 memtrap_link(m, j);
159 pa_aupdate_write_end(aupdate);
161 pa_mutex_unlock(mx);
163 return m;
166 void pa_memtrap_remove(pa_memtrap *m) {
167 unsigned j;
168 pa_mutex *mx;
170 pa_assert(m);
172 allocate_aupdate();
174 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
175 pa_mutex_lock(mx);
177 j = pa_aupdate_write_begin(aupdate);
178 memtrap_unlink(m, j);
179 j = pa_aupdate_write_swap(aupdate);
180 memtrap_unlink(m, j);
181 pa_aupdate_write_end(aupdate);
183 pa_mutex_unlock(mx);
185 pa_xfree(m);
188 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
189 unsigned j;
190 pa_mutex *mx;
192 pa_assert(m);
194 pa_assert(start);
195 pa_assert(size > 0);
197 start = PA_PAGE_ALIGN_PTR(start);
198 size = PA_PAGE_ALIGN(size);
200 allocate_aupdate();
202 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
203 pa_mutex_lock(mx);
205 j = pa_aupdate_write_begin(aupdate);
207 if (m->start == start && m->size == size)
208 goto unlock;
210 memtrap_unlink(m, j);
211 pa_aupdate_write_swap(aupdate);
213 m->start = (void*) start;
214 m->size = size;
215 pa_atomic_store(&m->bad, 0);
217 pa_assert_se(pa_aupdate_write_swap(aupdate) == j);
218 memtrap_link(m, j);
220 unlock:
221 pa_aupdate_write_end(aupdate);
223 pa_mutex_unlock(mx);
225 return m;
228 void pa_memtrap_install(void) {
229 #ifdef HAVE_SIGACTION
230 struct sigaction sa;
232 allocate_aupdate();
234 memset(&sa, 0, sizeof(sa));
235 sa.sa_sigaction = signal_handler;
236 sa.sa_flags = SA_RESTART|SA_SIGINFO;
238 pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
239 #endif