]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/sigbus.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #define SIGBUS_QUEUE_MAX 64
33 static struct sigaction old_sigaction
;
34 static unsigned n_installed
= 0;
36 /* We maintain a fixed size list of page addresses that triggered a
37 SIGBUS. We access with list with atomic operations, so that we
38 don't have to deal with locks between signal handler and main
39 programs in possibly multiple threads. */
41 static void* volatile sigbus_queue
[SIGBUS_QUEUE_MAX
];
42 static volatile sig_atomic_t n_sigbus_queue
= 0;
44 static void sigbus_push(void *addr
) {
49 /* Find a free place, increase the number of entries and leave, if we can */
50 for (u
= 0; u
< SIGBUS_QUEUE_MAX
; u
++)
51 if (__sync_bool_compare_and_swap(&sigbus_queue
[u
], NULL
, addr
)) {
52 __sync_fetch_and_add(&n_sigbus_queue
, 1);
56 /* If we can't, make sure the queue size is out of bounds, to
57 * mark it as overflow */
64 if (c
> SIGBUS_QUEUE_MAX
) /* already overflow */
67 if (__sync_bool_compare_and_swap(&n_sigbus_queue
, c
, c
+ SIGBUS_QUEUE_MAX
))
72 int sigbus_pop(void **ret
) {
84 if (_unlikely_(c
>= SIGBUS_QUEUE_MAX
))
87 for (u
= 0; u
< SIGBUS_QUEUE_MAX
; u
++) {
90 addr
= sigbus_queue
[u
];
94 if (__sync_bool_compare_and_swap(&sigbus_queue
[u
], addr
, NULL
)) {
95 __sync_fetch_and_sub(&n_sigbus_queue
, 1);
103 static void sigbus_handler(int sn
, siginfo_t
*si
, void *data
) {
107 assert(sn
== SIGBUS
);
110 if (si
->si_code
!= BUS_ADRERR
|| !si
->si_addr
) {
111 assert_se(sigaction(SIGBUS
, &old_sigaction
, NULL
) == 0);
116 ul
= (unsigned long) si
->si_addr
;
117 ul
= ul
/ page_size();
118 ul
= ul
* page_size();
119 aligned
= (void*) ul
;
121 /* Let's remember which address failed */
122 sigbus_push(aligned
);
124 /* Replace mapping with an anonymous page, so that the
125 * execution can continue, however with a zeroed out page */
126 assert_se(mmap(aligned
, page_size(), PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_FIXED
, -1, 0) == aligned
);
129 void sigbus_install(void) {
130 struct sigaction sa
= {
131 .sa_sigaction
= sigbus_handler
,
132 .sa_flags
= SA_SIGINFO
,
137 if (n_installed
== 1)
138 assert_se(sigaction(SIGBUS
, &sa
, &old_sigaction
) == 0);
143 void sigbus_reset(void) {
145 if (n_installed
<= 0)
150 if (n_installed
== 0)
151 assert_se(sigaction(SIGBUS
, &old_sigaction
, NULL
) == 0);