]> granicus.if.org Git - strace/blob - tests/mincore.c
Update copyright headers
[strace] / tests / mincore.c
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2018 The strace developers.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 #include "tests.h"
10 #include <stdio.h>
11 #include <sys/mman.h>
12
13 static void
14 print_mincore(const unsigned int pages, void *const addr,
15               const size_t size, unsigned char *const vec)
16 {
17         unsigned int i;
18
19         if (mincore(addr, size, vec))
20                 perror_msg_and_skip("mincore");
21
22         printf("mincore(%p, %zu, [", addr, size);
23         for (i = 0; i < pages; ++i) {
24                 if (i)
25                         printf(", ");
26                 if (i >= DEFAULT_STRLEN) {
27                         printf("...");
28                         break;
29                 }
30                 printf("%u", vec[i] & 1);
31         }
32         puts("]) = 0");
33 }
34
35 static void
36 test_mincore(const unsigned int pages)
37 {
38         const size_t page_size = get_page_size();
39         const size_t size = pages * page_size;
40         void *const addr = tail_alloc(size);
41         unsigned char *const vec = tail_alloc(pages);
42
43         mincore(addr, size, NULL);
44         printf("mincore(%p, %zu, NULL) = -1 %s (%m)\n",
45                addr, size, errno2name());
46
47         print_mincore(pages, addr, size, vec);
48         if (size)
49                 print_mincore(pages, addr, size - page_size + 1, vec);
50 }
51
52 int main(void)
53 {
54         test_mincore(1);
55         test_mincore(2);
56         test_mincore(DEFAULT_STRLEN);
57         test_mincore(DEFAULT_STRLEN + 1);
58
59         puts("+++ exited with 0 +++");
60         return 0;
61 }