xref: /freebsd/tools/test/stress2/misc/tmpfs14.sh (revision 61e21613)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Verify that mmap access updates mtime and ctime on files located on a
30# tmpfs FS.
31# Fixed by r277828 and r277969.
32
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35. ../default.cfg
36
37odir=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $odir/$0 > tmpfs14.c
40rm -f /tmp/tmpfs14
41mycc -o tmpfs14 -Wall -Wextra -g -O2 tmpfs14.c || exit 1
42rm -f tmpfs14.c
43cd $odir
44
45mount | grep -q "$mntpoint " && umount -f $mntpoint
46mount -t tmpfs tmpfs $mntpoint
47
48(cd $mntpoint; /tmp/tmpfs14) &
49
50sleep .5
51set `stat -f "%m %c" $mntpoint/test`
52m1=$1
53c1=$2
54while pgrep -q tmpfs14; do
55	set `stat -f "%m %c" $mntpoint/test`
56	[ "$m1" != "$1" ] && break
57	[ "$c1" != "$2" ] && break
58	sleep 1
59done
60[ "$m1" = "$1" -o "$c1" = "$2" ] &&
61    echo "FAIL Unchanged time $m1 $c1 / $1 $2"
62wait
63
64while mount | grep $mntpoint | grep -q tmpfs; do
65	umount $mntpoint || sleep 1
66done
67rm -f /tmp/tmpfs14
68exit 0
69EOF
70
71#include <sys/types.h>
72#include <sys/mman.h>
73
74#include <err.h>
75#include <errno.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <unistd.h>
79#include <fcntl.h>
80#include <time.h>
81
82char *file = "test";
83
84int
85main(void)
86{
87	char *p;
88	size_t len;
89	int fd, i;
90
91	if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
92		err(1, "open(%s)", file);
93	if (write(fd, "abcdef", 7) < 0)
94		err(1, "write");
95	close(fd);
96
97	len = getpagesize();
98	for (i = 0; i < 10; i++) {
99		sleep(1);
100		if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
101			err(1, "open(%s)", file);
102
103		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
104			err(1, "mmap");
105
106		p[0] = '0' + i;
107		munmap(p, len);
108		close(fd);
109	}
110
111	return 0;
112}
113