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