/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <signal.h>
#include <stddef.h>
#include "macro.h"
#include "sigbus.h"
#include "util.h"
static unsigned n_installed = 0;
/* We maintain a fixed size list of page addresses that triggered a
SIGBUS. We access with list with atomic operations, so that we
don't have to deal with locks between signal handler and main
programs in possibly multiple threads. */
unsigned u;
/* Find a free place, increase the number of entries and leave, if we can */
for (u = 0; u < SIGBUS_QUEUE_MAX; u++)
return;
}
/* If we can't, make sure the queue size is out of bounds, to
* mark it as overflow */
for (;;) {
unsigned c;
c = n_sigbus_queue;
if (c > SIGBUS_QUEUE_MAX) /* already overflow */
return;
return;
}
}
for (;;) {
unsigned u, c;
c = n_sigbus_queue;
if (_likely_(c == 0))
return 0;
if (_unlikely_(c >= SIGBUS_QUEUE_MAX))
return -EOVERFLOW;
for (u = 0; u < SIGBUS_QUEUE_MAX; u++) {
void *addr;
addr = sigbus_queue[u];
if (!addr)
continue;
return 1;
}
}
}
}
unsigned long ul;
void *aligned;
return;
}
/* Let's remember which address failed */
/* Replace mapping with an anonymous page, so that the
* execution can continue, however with a zeroed out page */
assert_se(mmap(aligned, page_size(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == aligned);
}
void sigbus_install(void) {
.sa_flags = SA_SIGINFO,
};
n_installed++;
if (n_installed == 1)
return;
}
void sigbus_reset(void) {
if (n_installed <= 0)
return;
n_installed--;
if (n_installed == 0)
return;
}