File: on-fault-limit.c

package info (click to toggle)
linux 6.12.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,673,568 kB
  • sloc: ansic: 25,888,630; asm: 268,782; sh: 136,481; python: 64,809; makefile: 55,668; perl: 38,052; xml: 19,270; cpp: 5,893; yacc: 4,923; lex: 2,939; awk: 1,592; sed: 28; ruby: 25
file content (42 lines) | stat: -rw-r--r-- 937 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// SPDX-License-Identifier: GPL-2.0
#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "../kselftest.h"

static void test_limit(void)
{
	struct rlimit lims;
	void *map;

	if (getrlimit(RLIMIT_MEMLOCK, &lims))
		ksft_exit_fail_msg("getrlimit: %s\n", strerror(errno));

	if (mlockall(MCL_ONFAULT | MCL_FUTURE))
		ksft_exit_fail_msg("mlockall: %s\n", strerror(errno));

	map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
		   MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);

	ksft_test_result(map == MAP_FAILED, "The map failed respecting mlock limits\n");

	if (map != MAP_FAILED)
		munmap(map, 2 * lims.rlim_max);
	munlockall();
}

int main(int argc, char **argv)
{
	ksft_print_header();
	ksft_set_plan(1);

	if (!getuid())
		ksft_test_result_skip("The test must be run from a normal user\n");
	else
		test_limit();

	ksft_finished();
}