xref: /freebsd/tools/test/stress2/misc/lockf3.sh (revision 370e009188ba90c3290b1479aa06ec98b66e140a)
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. ../default.cfg
32
33here=`pwd`
34cd /tmp
35sed '1,/^EOF/d' < $here/$0 > lockf3.c
36mycc -o lockf3 -Wall -Wextra -O2 lockf3.c || exit 1
37rm -f lockf3.c
38
39mount | grep -q "$mntpoint" && umount $mntpoint
40mdconfig -l | grep -q $mdstart &&  mdconfig -d -u $mdstart
41
42mdconfig -a -t swap -s 40m -u $mdstart
43
44newfs $newfs_flags md$mdstart > /dev/null
45mount /dev/md$mdstart $mntpoint
46
47cd $mntpoint
48for i in `jot 3`; do
49	$here/../testcases/swap/swap -t 10m -i 200 > /dev/null &
50	/tmp/lockf3 || break
51	pkill swap
52	wait
53done
54while pgrep -q swap; do
55	pkill swap
56done
57pkill lockf3
58wait
59cd $here
60
61while mount | grep -q "on $mntpoint "; do
62	umount $mntpoint || sleep 1
63done
64rm -rf /tmp/lockf3
65exit 0
66EOF
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <signal.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
74#include <sys/types.h>
75#include <sys/wait.h>
76#include <unistd.h>
77
78#define LOOPS 10000
79#define PIDS 100
80
81void
82handler(int s __unused)
83{
84}
85
86void
87int_handler(int s __unused)
88{
89	_exit(0);
90}
91
92void
93ahandler(int s __unused)
94{
95	fprintf(stderr, "FAIL timed out\n");
96	_exit(1);
97}
98
99int
100main(void)
101{
102	int fd, i, j;
103	char name[128];
104	pid_t pid[PIDS];
105	struct sigaction sa;
106	int status;
107
108	signal(SIGALRM, ahandler);
109	alarm(15 * 60);
110	for (i = 0; i < PIDS; i++) {
111		sprintf(name, "lock.%06d", getpid());
112		if ((fd = open(name, O_CREAT | O_TRUNC | O_RDWR, 0640)) == -1)
113			err(1, "open(%s)", name);
114		if (lockf(fd, F_LOCK, 0) == -1)
115			err(1, "flock 1");
116
117		if ((pid[i] = fork()) == -1)
118			err(1, "fork");
119
120		if (pid[i] == 0) {
121			memset(&sa, 0, sizeof(sa));
122			sa.sa_handler = handler;
123			if (sigaction(SIGHUP, &sa, NULL) == -1)
124				err(1, "signal");
125			sa.sa_handler = int_handler;
126			if (sigaction(SIGINT, &sa, NULL) == -1)
127				err(1, "signal");
128
129			for (;;) {
130				if (lockf(fd, F_LOCK, 0) == -1)
131					if (errno != EINTR)
132						warn("lockf");
133			}
134			_exit(0);
135		}
136		unlink(name);
137	}
138
139	usleep(10000);
140
141	for (i = 0; i < LOOPS; i++) {
142		for (j = 0; j < PIDS; j++) {
143			if (kill(pid[j], SIGHUP) == -1) {
144				warn("kill(%d), i = %d, j = %d", pid[j], i, j);
145				pid[j] = 0;
146			}
147		}
148	}
149	for (j = 0; j < PIDS; j++) {
150		if (kill(pid[j], SIGINT) == -1) {
151			warn("kill(%d), i = %d, j = %d", pid[j], i, j);
152			pid[j] = 0;
153		}
154	}
155
156	for (j = 0; j < PIDS; j++) {
157		if (waitpid(pid[j], &status, 0) == -1)
158			err(1, "waitpid(%d)", pid[j]);
159	}
160
161	return (0);
162}
163