]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/sigbus.c
Merge pull request #1934 from martinpitt/master
[thirdparty/systemd.git] / src / basic / sigbus.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <errno.h>
23 #include <signal.h>
24 #include <stddef.h>
25 #include <sys/mman.h>
26
27 #include "macro.h"
28 #include "sigbus.h"
29 #include "util.h"
30
31 #define SIGBUS_QUEUE_MAX 64
32
33 static struct sigaction old_sigaction;
34 static unsigned n_installed = 0;
35
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. */
40
41 static void* volatile sigbus_queue[SIGBUS_QUEUE_MAX];
42 static volatile sig_atomic_t n_sigbus_queue = 0;
43
44 static void sigbus_push(void *addr) {
45 unsigned u;
46
47 assert(addr);
48
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);
53 return;
54 }
55
56 /* If we can't, make sure the queue size is out of bounds, to
57 * mark it as overflow */
58 for (;;) {
59 unsigned c;
60
61 __sync_synchronize();
62 c = n_sigbus_queue;
63
64 if (c > SIGBUS_QUEUE_MAX) /* already overflow */
65 return;
66
67 if (__sync_bool_compare_and_swap(&n_sigbus_queue, c, c + SIGBUS_QUEUE_MAX))
68 return;
69 }
70 }
71
72 int sigbus_pop(void **ret) {
73 assert(ret);
74
75 for (;;) {
76 unsigned u, c;
77
78 __sync_synchronize();
79 c = n_sigbus_queue;
80
81 if (_likely_(c == 0))
82 return 0;
83
84 if (_unlikely_(c >= SIGBUS_QUEUE_MAX))
85 return -EOVERFLOW;
86
87 for (u = 0; u < SIGBUS_QUEUE_MAX; u++) {
88 void *addr;
89
90 addr = sigbus_queue[u];
91 if (!addr)
92 continue;
93
94 if (__sync_bool_compare_and_swap(&sigbus_queue[u], addr, NULL)) {
95 __sync_fetch_and_sub(&n_sigbus_queue, 1);
96 *ret = addr;
97 return 1;
98 }
99 }
100 }
101 }
102
103 static void sigbus_handler(int sn, siginfo_t *si, void *data) {
104 unsigned long ul;
105 void *aligned;
106
107 assert(sn == SIGBUS);
108 assert(si);
109
110 if (si->si_code != BUS_ADRERR || !si->si_addr) {
111 assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
112 raise(SIGBUS);
113 return;
114 }
115
116 ul = (unsigned long) si->si_addr;
117 ul = ul / page_size();
118 ul = ul * page_size();
119 aligned = (void*) ul;
120
121 /* Let's remember which address failed */
122 sigbus_push(aligned);
123
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);
127 }
128
129 void sigbus_install(void) {
130 struct sigaction sa = {
131 .sa_sigaction = sigbus_handler,
132 .sa_flags = SA_SIGINFO,
133 };
134
135 n_installed++;
136
137 if (n_installed == 1)
138 assert_se(sigaction(SIGBUS, &sa, &old_sigaction) == 0);
139
140 return;
141 }
142
143 void sigbus_reset(void) {
144
145 if (n_installed <= 0)
146 return;
147
148 n_installed--;
149
150 if (n_installed == 0)
151 assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
152
153 return;
154 }