Path: blob/main/tools/test/stress2/testcases/lockf2/lockf2.c
39566 views
/*-1* Copyright (c) 2008 Peter Holm <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627/* Test lockf(3) with overlapping ranges */2829/* Provoked:30lock order reversal:311st 0xc50057a0 vnode interlock (vnode interlock) @ kern/kern_lockf.c:190322nd 0xc14710e8 system map (system map) @ vm/vm_kern.c:29633*/3435#include <sys/types.h>3637#include <err.h>38#include <errno.h>39#include <fcntl.h>40#include <stdio.h>41#include <stdlib.h>42#include <strings.h>43#include <unistd.h>4445#include "stress.h"4647static char file[128];48static int fd;49static int freespace;5051int52setup(int nb)53{54int64_t bl;55int64_t in;56int64_t reserve_bl;57int64_t reserve_in;58int i;59char buf[1024];6061if (nb == 0) {62getdf(&bl, &in);6364/* Resource requirements: */65reserve_in = 1 * op->incarnations;66reserve_bl = 1081344 * op->incarnations;67freespace = (reserve_bl <= bl && reserve_in <= in);68if (!freespace)69reserve_bl = reserve_in = 0;7071if (op->verbose > 1)72printf("lockf2(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",73op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);74reservedf(reserve_bl, reserve_in);75putval(freespace);76} else {77freespace = getval();78}79if (!freespace)80exit (0);8182sprintf(file, "lockf.%d", getpid());83if ((fd = open(file,O_CREAT | O_TRUNC | O_RDWR, 0600)) == -1)84err(1, "creat(%s)", file);85bzero(buf, sizeof(buf));86for (i = 0; i < 1024; i++)87if (write(fd, &buf, sizeof(buf)) != sizeof(buf))88err(1, "write");89close(fd);90return (0);91}9293void94cleanup(void)95{96unlink(file);97}9899int100test(void)101{102off_t pos;103off_t size;104int i, r;105106if ((fd = open(file, O_RDWR, 0600)) == -1)107err(1, "open(%s)", file);108109for (i = 0; i < 1024 && done_testing == 0; i++) {110pos = random_int(0, 1024 * 1024 - 1);111if (lseek(fd, pos, SEEK_SET) == -1)112err(1, "lseek");113size = random_int(1, 1024 * 1024 - pos);114if (size > 64)115size = 64;116do {117r = lockf(fd, F_LOCK, size);118} while (r == -1 && errno == EINTR);119if (r == -1)120err(1, "lockf(%s, F_LOCK)", file);121size = random_int(1, size);122if (lockf(fd, F_ULOCK, size) == -1)123err(1, "lockf(%s, F_ULOCK)", file);124125}126close(fd);127128return (0);129}130131132