Path: blob/main/tools/test/stress2/misc/advlock.sh
39537 views
#!/bin/sh12#3# Copyright (c) 2016 EMC Corp.4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728# From r238952's commit log:29# The first change closes a race where an open() that will block with O_SHLOCK30# or O_EXLOCK can increase the write count while it waits. If the process31# holding the current lock on the file then tries to call exec() on the file32# it has locked, it can fail with ETXTBUSY even though the advisory lock is33# preventing other threads from successfully completing a writable open().34#35# The second change closes a race where a read-only open() with O_SHLOCK or36# O_EXLOCK may return successfully while the write count is non-zero due to37# another descriptor that had the advisory lock and was blocking the open()38# still being in the process of closing. If the process that completed the39# open() then attempts to call exec() on the file it locked, it can fail with40# ETXTBUSY even though the other process that held a write lock has closed41# the file and released the lock.4243# https://people.freebsd.org/~pho/stress/log/kostik859.txt44# https://people.freebsd.org/~pho/stress/log/kostik860.txt4546# Fixed by r294204.4748. ../default.cfg4950[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 15152dir=/tmp53odir=`pwd`54cd $dir55sed '1,/^EOF/d' < $odir/$0 > $dir/advlock.c56mycc -o advlock -Wall -Wextra -O0 -g advlock.c || exit 157rm -f advlock.c5859mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint60mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart61mdconfig -a -t swap -s 512m -u $mdstart || exit 162newfs $newfs_flags md$mdstart > /dev/null63mount /dev/md$mdstart $mntpoint6465cp /usr/bin/true $mntpoint66cd $mntpoint67/tmp/advlock68r=$?69cd $odir7071while mount | grep "on $mntpoint " | grep -q /dev/md; do72umount $mntpoint || sleep 173done74mdconfig -d -u $mdstart75rm -f /tmp/advlock76exit $r7778EOF79#include <sys/param.h>80#include <sys/mman.h>81#include <sys/stat.h>82#include <sys/wait.h>8384#include <machine/atomic.h>8586#include <err.h>87#include <errno.h>88#include <fcntl.h>89#include <signal.h>90#include <stdio.h>91#include <stdlib.h>92#include <time.h>93#include <unistd.h>9495volatile u_int *share;96char *cmdline[] = { "./true", NULL };97const char *tp;9899#define SYNC 0100#define PARALLEL 2101102#define RUNTIME (1 * 60)103104void105handler(int i __unused) {106107fprintf(stderr, "ALARM from %s.\n", tp);108_exit(1);109}110111void112slock(void)113{114int fd;115116setproctitle("%s", __func__);117atomic_add_int(&share[SYNC], 1);118while (share[SYNC] != PARALLEL)119;120121tp = __func__;122alarm(2);123if ((fd = open(cmdline[0], O_RDONLY | O_SHLOCK)) == -1)124err(1, "open(%s). %d", cmdline[0], __LINE__);125usleep(500);126close(fd);127128_exit(0);129}130131void132elock(void)133{134int fd;135136setproctitle("%s", __func__);137atomic_add_int(&share[SYNC], 1);138while (share[SYNC] != PARALLEL)139;140141tp = __func__;142alarm(2);143if ((fd = open(cmdline[0], O_WRONLY | O_EXLOCK)) == -1) {144if (errno != ETXTBSY)145err(1, "open(%s). %d", cmdline[0], __LINE__);146} else {147usleep(500);148close(fd);149}150151_exit(0);152}153154void155stest(void)156{157int fd;158159setproctitle("%s", __func__);160atomic_add_int(&share[SYNC], 1);161while (share[SYNC] != PARALLEL)162;163164tp = __func__;165alarm(2);166if ((fd = open(cmdline[0], O_RDONLY | O_SHLOCK)) == -1)167err(1, "open(%s). %d", cmdline[0], __LINE__);168169if (execve(cmdline[0], cmdline, NULL) == -1)170err(1, "execve(%s) @ %d", cmdline[0], __LINE__);171172_exit(0);173}174175void176etest(void)177{178int fd;179180setproctitle("%s", __func__);181atomic_add_int(&share[SYNC], 1);182while (share[SYNC] != PARALLEL)183;184185tp = __func__;186alarm(2);187if ((fd = open(cmdline[0], O_RDONLY | O_EXLOCK)) == -1)188err(1, "open(%s). %d", cmdline[0], __LINE__);189190if (execve(cmdline[0], cmdline, NULL) == -1)191err(1, "execve(%s) @ %d", cmdline[0], __LINE__);192193_exit(0);194}195196int197main(void)198{199size_t len;200time_t start;201int i, n, r, s;202203len = PAGE_SIZE;204if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,205-1, 0)) == MAP_FAILED)206err(1, "mmap");207208signal(SIGALRM, handler);209n = r = 0;210start = time(NULL);211while ((time(NULL) - start) < RUNTIME) {212n++;213share[SYNC] = 0;214if (fork() == 0)215slock();216if (fork() == 0)217stest();218219for (i = 0; i < PARALLEL; i++) {220wait(&s);221r += s == 0 ? 0 : 1;222}223if (r != 0)224break;225226share[SYNC] = 0;227if (fork() == 0)228elock();229if (fork() == 0)230etest();231232for (i = 0; i < PARALLEL; i++) {233wait(&s);234r += s == 0 ? 0 : 1;235}236if (r != 0)237break;238}239if (r != 0)240fprintf(stderr, "FAIL @ %d\n", n);241242return (r);243}244245246