xref: /freebsd/tools/test/stress2/misc/exlock.sh (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 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[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# Simple O_EXLOCK test scenario.
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > exlock.c
38mycc -o exlock -Wall -Wextra exlock.c || exit 1
39rm -f exlock.c
40cd $here
41
42mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
43mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
44
45mdconfig -a -t swap -s 2g -u $mdstart || exit 1
46newfs $newfs_flags md$mdstart > /dev/null
47mount /dev/md$mdstart $mntpoint
48chmod 777 $mntpoint
49
50su $testuser -c "cd $mntpoint; /tmp/exlock"
51
52while mount | grep $mntpoint | grep -q /dev/md; do
53	umount $mntpoint || sleep 1
54done
55mdconfig -d -u $mdstart
56rm -f /tmp/exlock
57exit
58EOF
59#include <sys/types.h>
60#include <sys/wait.h>
61
62#include <err.h>
63#include <fcntl.h>
64#include <stdio.h>
65#include <unistd.h>
66
67char buf[128];
68
69#define PARALLEL 3
70
71static void
72tst(char *file, int n)
73{
74	int fd, i;
75
76	for (i = 0; i < (int)sizeof(buf); i++)
77		buf[i] = '0' + n;
78
79	for (i = 0; i < 1024 * 1024; i++) {
80		if ((fd = open(file, O_RDWR | O_CREAT | O_APPEND | O_EXLOCK,
81		    0644)) == -1)
82			err(1, "open(%s)", file);
83		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
84			err(1, "write");
85		close(fd);
86	}
87
88	_exit(0);
89}
90static void
91test(void)
92{
93	int i;
94	char file[80];
95
96	snprintf(file, sizeof(file), "f06%d", getpid());
97
98	for (i = 0; i < 3; i++)
99		if (fork() == 0)
100			tst(file, i);
101	for (i = 0; i < 3; i++)
102		wait(NULL);
103
104	unlink(file);
105
106	_exit(0);
107}
108
109int
110main(void)
111{
112	int i;
113
114	for (i = 0; i < PARALLEL; i++)
115		if (fork() == 0)
116			test();
117	for (i = 0; i < PARALLEL; i++)
118		wait(NULL);
119
120	return (0);
121}
122