]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sigbus.c
Merge pull request #3162 from keszybz/alias-refusal
[thirdparty/systemd.git] / src / test / test-sigbus.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sys/mman.h>
21
22 #include "fd-util.h"
23 #include "sigbus.h"
24 #include "util.h"
25
26 int main(int argc, char *argv[]) {
27 _cleanup_close_ int fd = -1;
28 char template[] = "/tmp/sigbus-test-XXXXXX";
29 void *addr = NULL;
30 uint8_t *p;
31
32 sigbus_install();
33
34 assert_se(sigbus_pop(&addr) == 0);
35
36 assert_se((fd = mkostemp(template, O_RDWR|O_CREAT|O_EXCL)) >= 0);
37 assert_se(unlink(template) >= 0);
38 assert_se(fallocate(fd, 0, 0, page_size() * 8) >= 0);
39
40 p = mmap(NULL, page_size() * 16, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
41 assert_se(p != MAP_FAILED);
42
43 assert_se(sigbus_pop(&addr) == 0);
44
45 p[0] = 0xFF;
46 assert_se(sigbus_pop(&addr) == 0);
47
48 p[page_size()] = 0xFF;
49 assert_se(sigbus_pop(&addr) == 0);
50
51 p[page_size()*8] = 0xFF;
52 p[page_size()*8+1] = 0xFF;
53 p[page_size()*10] = 0xFF;
54 assert_se(sigbus_pop(&addr) > 0);
55 assert_se(addr == p + page_size() * 8);
56 assert_se(sigbus_pop(&addr) > 0);
57 assert_se(addr == p + page_size() * 10);
58 assert_se(sigbus_pop(&addr) == 0);
59
60 sigbus_reset();
61 }