xref: /freebsd/tools/test/stress2/misc/ftruncate3.sh (revision ef777be98543f7daae90bd123d4fc1ec4a54efc2)
1#!/bin/sh
2
3# Test scenario from Bug 64816: [nfs] [patch] mmap and/or ftruncate does not work correctly on nfs mounted file systems
4
5. ../default.cfg
6
7set -u
8grep -q $mntpoint /etc/exports ||
9    { echo "$mntpoint missing from /etc/exports"; exit 0; }
10rpcinfo 2>/dev/null | grep -q mountd || exit 0
11
12prog=$(basename "$0" .sh)
13cat > /tmp/$prog.c <<EOF
14#include <sys/types.h>
15#include <sys/mman.h>
16#include <sys/stat.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24void error(char *msg)
25{
26	fprintf(stderr, "Error: %s\nSystem error %d: %s\n", msg, errno, strerror(errno));
27	exit(-1);
28}
29
30#define SZ 1024 // Less than page size
31
32int main(int argn, char *argv[])
33{
34	int fd, s;
35	char buffer[SZ];
36	char *map;
37
38	if (argn!=2)
39	{
40		fprintf(stderr, "Usage:\n %s [filename]\n", argv[0]);
41		_exit(-1);
42	}
43
44	memset(buffer, 0, SZ);
45	s = 0;
46
47	fd=open(argv[1], O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
48	if (fd==-1)
49		error("Could not create file");
50
51	if (write(fd, buffer, SZ)!=SZ)
52		error("Could not write buffer");
53
54	map=mmap(NULL, SZ, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
55	if (map==MAP_FAILED)
56		error("Map failed");
57	map[SZ-1]=1;
58
59	if (ftruncate(fd, SZ+1)!=0)
60		error("Could not truncate file");
61
62	if (map[SZ-1]==1)
63		printf("Test passed\n");
64	else {
65		printf("Test failed\n");
66		s = 1;
67	}
68
69	exit(s);
70}
71EOF
72mycc -o /tmp/$prog -Wall -Wextra -O0 -g /tmp/$prog.c || exit 1
73
74mount | grep -q "on $mntpoint " && umount -f $mntpoint
75mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
76mdconfig -s 1g -u $mdstart
77newfs -n $newfs_flags /dev/md$mdstart > /dev/null
78mount /dev/md$mdstart $mntpoint
79
80mp2=${mntpoint}2
81mkdir -p $mp2
82mount | grep -q "on $mp2 " && umount -f $mp2
83mount -t nfs -o retrycnt=3 127.0.0.1:$mntpoint $mp2 || exit 1
84sleep .2
85mount | grep  $mntpoint
86
87cd $mp2
88/tmp/$prog $prog.data; s=$?
89ls -ls $mp2/$prog.data
90cd -
91
92umount $mp2
93umount $mntpoint
94mdconfig -d -u $mdstart
95rm -f /tmp/$prog /tmp/$prog.c
96exit $s
97