xref: /freebsd/tools/test/stress2/misc/tmpfs22.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2020 Peter Holm
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# A tmpfs "nomtime" mount option test.
31
32# kib@ wrote:
33# A test program should do something along the lines:
34# 1. open tmpfs file, ftruncate it, mmap
35# 2. write to the mmaped file area periodically, for long time
36#    e.g. 1 write in 10 secs, for 120 secs
37# 3. the mtime of the file should be the same as after the truncation
38#    if the nomtime flag is set.  Otherwise, it should be around +-30
39#    secs of the last write to the area.
40#
41# Really the current (unpatched) situation is some not well defined
42# mix between nomtime and its absence.
43
44. ../default.cfg
45
46odir=`pwd`
47cd /tmp
48sed '1,/^EOF/d' < $odir/$0 > tmpfs22.c
49mycc -o tmpfs22 -Wall -Wextra -O2 -g tmpfs22.c || exit 1
50rm -f tmpfs22.c
51cd $odir
52
53mount | grep "$mntpoint" | grep -q tmpfs && umount $mntpoint
54mount -o size=2g,nomtime -t tmpfs tmpfs  $mntpoint || exit 1
55
56(cd $mntpoint; /tmp/tmpfs22); s=$?
57
58for i in `jot 6`; do
59	umount $mntpoint && break
60	sleep 2
61done
62rm -f /tmp/tmpfs22
63exit $s
64
65EOF
66#include <sys/types.h>
67#include <sys/mman.h>
68#include <sys/stat.h>
69
70#include <err.h>
71#include <errno.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <unistd.h>
75#include <fcntl.h>
76
77static char *file = "test";
78
79int
80main(void)
81{
82	struct stat st1, st2;
83	size_t len;
84	int fd, i, s;
85	char *p;
86
87	if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
88		err(1, "open(%s)", file);
89	len = 2LL * 1024 * 1024;
90	if (ftruncate(fd, len) == -1)
91		err(1, "ftruncate");
92	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
93			MAP_FAILED) {
94		if (errno == ENOMEM)
95			return (1);
96		err(1, "mmap()");
97	}
98	if (fstat(fd, &st1) == -1)
99		err(1, "fstat 1");
100
101	for (i = 0; i < 12; i++) {
102		sleep(10);
103		p[arc4random() % len] = 1;
104	}
105	if (fstat(fd, &st2) == -1)
106		err(1, "fstat 2");
107	s = 0;
108	if (st1.st_mtime == st2.st_mtime) {
109		fprintf(stderr, "mtime is unchanged: %ld %ld\n",
110		    (long)st1.st_mtime, (long)st2.st_mtime);
111		s=1;
112	}
113	munmap(p, len);
114	close(fd);
115
116	return (s);
117}
118