xref: /freebsd/tools/test/stress2/misc/advlock.sh (revision 608c97bff921c8936eb71f4b1a177625cfa60250)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2016 EMC Corp.
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# From r238952's commit log:
308a272653SPeter Holm# The first change closes a race where an open() that will block with O_SHLOCK
318a272653SPeter Holm# or O_EXLOCK can increase the write count while it waits.  If the process
328a272653SPeter Holm# holding the current lock on the file then tries to call exec() on the file
338a272653SPeter Holm# it has locked, it can fail with ETXTBUSY even though the advisory lock is
348a272653SPeter Holm# preventing other threads from successfully completing a writable open().
358a272653SPeter Holm#
368a272653SPeter Holm# The second change closes a race where a read-only open() with O_SHLOCK or
378a272653SPeter Holm# O_EXLOCK may return successfully while the write count is non-zero due to
388a272653SPeter Holm# another descriptor that had the advisory lock and was blocking the open()
398a272653SPeter Holm# still being in the process of closing.  If the process that completed the
408a272653SPeter Holm# open() then attempts to call exec() on the file it locked, it can fail with
418a272653SPeter Holm# ETXTBUSY even though the other process that held a write lock has closed
428a272653SPeter Holm# the file and released the lock.
438a272653SPeter Holm
448a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/kostik859.txt
458a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/kostik860.txt
468a272653SPeter Holm
478a272653SPeter Holm# Fixed by r294204.
488a272653SPeter Holm
498a272653SPeter Holm. ../default.cfg
508a272653SPeter Holm
518a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
528a272653SPeter Holm
538a272653SPeter Holmdir=/tmp
548a272653SPeter Holmodir=`pwd`
558a272653SPeter Holmcd $dir
568a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/advlock.c
578a272653SPeter Holmmycc -o advlock -Wall -Wextra -O0 -g advlock.c || exit 1
588a272653SPeter Holmrm -f advlock.c
598a272653SPeter Holm
608a272653SPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
618a272653SPeter Holmmdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
628a272653SPeter Holmmdconfig -a -t swap -s 512m -u $mdstart || exit 1
63*608c97bfSPeter Holmnewfs $newfs_flags md$mdstart > /dev/null
64*608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint
658a272653SPeter Holm
668a272653SPeter Holmcp /usr/bin/true $mntpoint
678a272653SPeter Holmcd $mntpoint
688a272653SPeter Holm/tmp/advlock
698a272653SPeter Holmr=$?
708a272653SPeter Holmcd $odir
718a272653SPeter Holm
728a272653SPeter Holmwhile mount | grep "on $mntpoint " | grep -q /dev/md; do
738a272653SPeter Holm	umount $mntpoint || sleep 1
748a272653SPeter Holmdone
758a272653SPeter Holmmdconfig -d -u $mdstart
768a272653SPeter Holmrm -f /tmp/advlock
778a272653SPeter Holmexit $r
788a272653SPeter Holm
798a272653SPeter HolmEOF
808a272653SPeter Holm#include <sys/param.h>
818a272653SPeter Holm#include <sys/mman.h>
828a272653SPeter Holm#include <sys/stat.h>
838a272653SPeter Holm#include <sys/wait.h>
848a272653SPeter Holm
858a272653SPeter Holm#include <machine/atomic.h>
868a272653SPeter Holm
878a272653SPeter Holm#include <err.h>
888a272653SPeter Holm#include <errno.h>
898a272653SPeter Holm#include <fcntl.h>
908a272653SPeter Holm#include <signal.h>
918a272653SPeter Holm#include <stdio.h>
928a272653SPeter Holm#include <stdlib.h>
938a272653SPeter Holm#include <time.h>
948a272653SPeter Holm#include <unistd.h>
958a272653SPeter Holm
968a272653SPeter Holmvolatile u_int *share;
978a272653SPeter Holmchar *cmdline[] = { "./true", NULL };
988a272653SPeter Holmconst char *tp;
998a272653SPeter Holm
1008a272653SPeter Holm#define SYNC 0
1018a272653SPeter Holm#define PARALLEL 2
1028a272653SPeter Holm
1038a272653SPeter Holm#define RUNTIME (1 * 60)
1048a272653SPeter Holm
1058a272653SPeter Holmvoid
1068a272653SPeter Holmhandler(int i __unused) {
1078a272653SPeter Holm
1088a272653SPeter Holm        fprintf(stderr, "ALARM from %s.\n", tp);
1098a272653SPeter Holm	_exit(1);
1108a272653SPeter Holm}
1118a272653SPeter Holm
1128a272653SPeter Holmvoid
1138a272653SPeter Holmslock(void)
1148a272653SPeter Holm{
1158a272653SPeter Holm	int fd;
1168a272653SPeter Holm
1178a272653SPeter Holm	setproctitle("%s", __func__);
1188a272653SPeter Holm	atomic_add_int(&share[SYNC], 1);
1198a272653SPeter Holm	while (share[SYNC] != PARALLEL)
1208a272653SPeter Holm		;
1218a272653SPeter Holm
1228a272653SPeter Holm	tp = __func__;
1238a272653SPeter Holm	alarm(2);
1248a272653SPeter Holm	if ((fd = open(cmdline[0], O_RDONLY | O_SHLOCK)) == -1)
1258a272653SPeter Holm		err(1, "open(%s). %d", cmdline[0], __LINE__);
1268a272653SPeter Holm	usleep(500);
1278a272653SPeter Holm	close(fd);
1288a272653SPeter Holm
1298a272653SPeter Holm	_exit(0);
1308a272653SPeter Holm}
1318a272653SPeter Holm
1328a272653SPeter Holmvoid
1338a272653SPeter Holmelock(void)
1348a272653SPeter Holm{
1358a272653SPeter Holm	int fd;
1368a272653SPeter Holm
1378a272653SPeter Holm	setproctitle("%s", __func__);
1388a272653SPeter Holm	atomic_add_int(&share[SYNC], 1);
1398a272653SPeter Holm	while (share[SYNC] != PARALLEL)
1408a272653SPeter Holm		;
1418a272653SPeter Holm
1428a272653SPeter Holm	tp = __func__;
1438a272653SPeter Holm	alarm(2);
1448a272653SPeter Holm	if ((fd = open(cmdline[0], O_WRONLY | O_EXLOCK)) == -1) {
1458a272653SPeter Holm		if (errno != ETXTBSY)
1468a272653SPeter Holm			err(1, "open(%s). %d", cmdline[0], __LINE__);
1478a272653SPeter Holm	} else {
1488a272653SPeter Holm		usleep(500);
1498a272653SPeter Holm		close(fd);
1508a272653SPeter Holm	}
1518a272653SPeter Holm
1528a272653SPeter Holm	_exit(0);
1538a272653SPeter Holm}
1548a272653SPeter Holm
1558a272653SPeter Holmvoid
1568a272653SPeter Holmstest(void)
1578a272653SPeter Holm{
1588a272653SPeter Holm	int fd;
1598a272653SPeter Holm
1608a272653SPeter Holm	setproctitle("%s", __func__);
1618a272653SPeter Holm	atomic_add_int(&share[SYNC], 1);
1628a272653SPeter Holm	while (share[SYNC] != PARALLEL)
1638a272653SPeter Holm		;
1648a272653SPeter Holm
1658a272653SPeter Holm	tp = __func__;
1668a272653SPeter Holm	alarm(2);
1678a272653SPeter Holm	if ((fd = open(cmdline[0], O_RDONLY | O_SHLOCK)) == -1)
1688a272653SPeter Holm		err(1, "open(%s). %d", cmdline[0], __LINE__);
1698a272653SPeter Holm
1708a272653SPeter Holm	if (execve(cmdline[0], cmdline, NULL) == -1)
1718a272653SPeter Holm		err(1, "execve(%s) @ %d", cmdline[0], __LINE__);
1728a272653SPeter Holm
1738a272653SPeter Holm	_exit(0);
1748a272653SPeter Holm}
1758a272653SPeter Holm
1768a272653SPeter Holmvoid
1778a272653SPeter Holmetest(void)
1788a272653SPeter Holm{
1798a272653SPeter Holm	int fd;
1808a272653SPeter Holm
1818a272653SPeter Holm	setproctitle("%s", __func__);
1828a272653SPeter Holm	atomic_add_int(&share[SYNC], 1);
1838a272653SPeter Holm	while (share[SYNC] != PARALLEL)
1848a272653SPeter Holm		;
1858a272653SPeter Holm
1868a272653SPeter Holm	tp = __func__;
1878a272653SPeter Holm	alarm(2);
1888a272653SPeter Holm	if ((fd = open(cmdline[0], O_RDONLY | O_EXLOCK)) == -1)
1898a272653SPeter Holm		err(1, "open(%s). %d", cmdline[0], __LINE__);
1908a272653SPeter Holm
1918a272653SPeter Holm	if (execve(cmdline[0], cmdline, NULL) == -1)
1928a272653SPeter Holm		err(1, "execve(%s) @ %d", cmdline[0], __LINE__);
1938a272653SPeter Holm
1948a272653SPeter Holm	_exit(0);
1958a272653SPeter Holm}
1968a272653SPeter Holm
1978a272653SPeter Holmint
1988a272653SPeter Holmmain(void)
1998a272653SPeter Holm{
2008a272653SPeter Holm	size_t len;
2018a272653SPeter Holm	time_t start;
2028a272653SPeter Holm	int i, n, r, s;
2038a272653SPeter Holm
2048a272653SPeter Holm	len = PAGE_SIZE;
2058a272653SPeter Holm	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
2068a272653SPeter Holm	    -1, 0)) == MAP_FAILED)
2078a272653SPeter Holm		err(1, "mmap");
2088a272653SPeter Holm
2098a272653SPeter Holm	signal(SIGALRM, handler);
2108a272653SPeter Holm	n = r = 0;
2118a272653SPeter Holm	start = time(NULL);
2128a272653SPeter Holm	while ((time(NULL) - start) < RUNTIME) {
2138a272653SPeter Holm		n++;
2148a272653SPeter Holm		share[SYNC] = 0;
2158a272653SPeter Holm		if (fork() == 0)
2168a272653SPeter Holm			slock();
2178a272653SPeter Holm		if (fork() == 0)
2188a272653SPeter Holm			stest();
2198a272653SPeter Holm
2208a272653SPeter Holm		for (i = 0; i < PARALLEL; i++) {
2218a272653SPeter Holm			wait(&s);
2228a272653SPeter Holm			r += s == 0 ? 0 : 1;
2238a272653SPeter Holm		}
2248a272653SPeter Holm		if (r != 0)
2258a272653SPeter Holm			break;
2268a272653SPeter Holm
2278a272653SPeter Holm		share[SYNC] = 0;
2288a272653SPeter Holm		if (fork() == 0)
2298a272653SPeter Holm			elock();
2308a272653SPeter Holm		if (fork() == 0)
2318a272653SPeter Holm			etest();
2328a272653SPeter Holm
2338a272653SPeter Holm		for (i = 0; i < PARALLEL; i++) {
2348a272653SPeter Holm			wait(&s);
2358a272653SPeter Holm			r += s == 0 ? 0 : 1;
2368a272653SPeter Holm		}
2378a272653SPeter Holm		if (r != 0)
2388a272653SPeter Holm			break;
2398a272653SPeter Holm	}
2408a272653SPeter Holm	if (r != 0)
2418a272653SPeter Holm		fprintf(stderr, "FAIL @ %d\n", n);
2428a272653SPeter Holm
2438a272653SPeter Holm	return (r);
2448a272653SPeter Holm}
245