xref: /freebsd/tools/test/stress2/misc/setrlimit2.sh (revision d91f8db5f1822c43cd256f19aae1d059e4b25a26)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2022 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Demonstrate that a mapped SHARED file can be updated past LIMIT_FSIZE
31
32# Kostik wrote:
33# This one should be reproducible when you
34# - have file larger than e.g. RLIMIT_FSIZE
35# - mmaped it without closing the file descriptor
36# - dirty its pages beyond the limit
37# - then unmap
38# - then close the file descriptor.
39
40[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
41
42. ../default.cfg
43
44cat > /tmp/setrlimit2.c <<EOF
45#include <sys/param.h>
46#include <sys/mman.h>
47#include <sys/resource.h>
48#include <sys/stat.h>
49
50#include <err.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <signal.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <unistd.h>
57
58static int signals;
59
60static void
61handler(int sig __unused)
62{
63	signals++;
64}
65
66int
67main(int argc, char *argv[])
68{
69	struct rlimit rlim;
70	struct sigaction act;
71	struct stat st;
72	size_t len;
73	int error, fd, ps;
74	char *file, *p;
75
76	if (argc != 2) {
77		fprintf(stderr, "Usage: %s <data file>\n", argv[0]);
78		exit(1);
79	}
80	act.sa_handler = handler;
81	act.sa_flags = 0;
82	sigemptyset(&act.sa_mask);
83	if (sigaction(SIGXFSZ, &act, NULL) != 0)
84		err(1, "sigaction");
85
86	file = argv[1];
87	ps = getpagesize();
88	if ((fd = open(file, O_RDWR)) == -1)
89		err(1, "open(%s)", file);
90	if ((error = fstat(fd, &st)) == -1)
91		err(1, "stat(%s)", file);
92	len = round_page(st.st_size);
93	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
94			err(1, "mmap");
95	rlim.rlim_cur = rlim.rlim_max = len / 2;;
96	if (setrlimit(RLIMIT_FSIZE, &rlim) == -1)
97		err(1, "setrlimit(%ld)", len / 2);
98
99	p[len / 2 + ps] = 'a';
100
101	if (munmap(p, len) == -1)
102		err(1, "unmap()");
103	close(fd);
104
105}
106EOF
107here=`pwd`
108cd /tmp
109mycc -o setrlimit2 -Wall -Wextra -O0 -g setrlimit2.c || exit 1
110data=/tmp/setrlimit2.data
111dd if=/dev/zero of=$data bs=1m count=1 status=none
112h1=`md5 < $data`
113
114./setrlimit2 $data
115
116h2=`md5 < $data`
117rm -f /tmp/setrlimit2 /tmp/setrlimit2.c
118[ $h1 = $h2 ] && exit 1 || exit 0
119