1/* Test the mlock2 function.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <stdio.h>
20#include <support/check.h>
21#include <support/xunistd.h>
22#include <sys/mman.h>
23
24/* Allocate a page using mmap. */
25static void *
26get_page (void)
27{
28 return xmmap (NULL, length: 1, PROT_READ | PROT_WRITE,
29 MAP_ANONYMOUS | MAP_PRIVATE, fd: -1);
30}
31
32static int
33do_test (void)
34{
35 /* Current kernels have a small reserve of locked memory, so this
36 test does not need any privileges to run. */
37
38 void *page = get_page ();
39 if (mlock (addr: page, len: 1) != 0)
40 FAIL_EXIT1 ("mlock: %m\n");
41 xmunmap (addr: page, length: 1);
42
43 page = get_page ();
44 if (mlock2 (addr: page, length: 1, flags: 0) != 0)
45 /* Should be implemented using mlock if necessary. */
46 FAIL_EXIT1 ("mlock2 (0): %m\n");
47 xmunmap (addr: page, length: 1);
48
49 page = get_page ();
50 int ret = mlock2 (addr: page, length: 1, MLOCK_ONFAULT);
51 if (ret != 0)
52 {
53 TEST_VERIFY (ret == -1);
54 if (errno != EINVAL)
55 /* EINVAL means the system does not support the mlock2 system
56 call. */
57 FAIL_EXIT1 ("mlock2 (0): %m\n");
58 else
59 puts (s: "warning: mlock2 system call not supported");
60 }
61 xmunmap (addr: page, length: 1);
62
63 return 0;
64}
65
66#include <support/test-driver.c>
67

source code of glibc/sysdeps/unix/sysv/linux/tst-mlock2.c