186d7f5d3SJohn Marino /*-
286d7f5d3SJohn Marino * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
386d7f5d3SJohn Marino * All rights reserved.
486d7f5d3SJohn Marino *
586d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
686d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
786d7f5d3SJohn Marino * are met:
886d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
986d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
1086d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1186d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
1286d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
1386d7f5d3SJohn Marino *
1486d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1586d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1686d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1786d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1886d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1986d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2086d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2186d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2286d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2386d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2486d7f5d3SJohn Marino * SUCH DAMAGE.
2586d7f5d3SJohn Marino *
2686d7f5d3SJohn Marino */
2786d7f5d3SJohn Marino
2886d7f5d3SJohn Marino /* Test lockf(3) with overlapping ranges */
2986d7f5d3SJohn Marino
3086d7f5d3SJohn Marino /* Provoked:
3186d7f5d3SJohn Marino lock order reversal:
3286d7f5d3SJohn Marino 1st 0xc50057a0 vnode interlock (vnode interlock) @ kern/kern_lockf.c:190
3386d7f5d3SJohn Marino 2nd 0xc14710e8 system map (system map) @ vm/vm_kern.c:296
3486d7f5d3SJohn Marino */
3586d7f5d3SJohn Marino
3686d7f5d3SJohn Marino #include <stdio.h>
3786d7f5d3SJohn Marino #include <stdlib.h>
3886d7f5d3SJohn Marino #include <unistd.h>
3986d7f5d3SJohn Marino #include <fcntl.h>
4086d7f5d3SJohn Marino #include <sys/types.h>
4186d7f5d3SJohn Marino #include <sys/param.h>
4286d7f5d3SJohn Marino #include <strings.h>
4386d7f5d3SJohn Marino #include <errno.h>
4486d7f5d3SJohn Marino #include <err.h>
4586d7f5d3SJohn Marino
4686d7f5d3SJohn Marino #include <stress.h>
4786d7f5d3SJohn Marino
4886d7f5d3SJohn Marino char file[128];
4986d7f5d3SJohn Marino int fd;
5086d7f5d3SJohn Marino
5186d7f5d3SJohn Marino int
setup(int nb)5286d7f5d3SJohn Marino setup(int nb)
5386d7f5d3SJohn Marino {
5486d7f5d3SJohn Marino int i;
5586d7f5d3SJohn Marino char buf[1024];
5686d7f5d3SJohn Marino
5786d7f5d3SJohn Marino sprintf(file, "lockf.%d", getpid());
5886d7f5d3SJohn Marino if ((fd = open(file,O_CREAT | O_TRUNC | O_RDWR, 0600)) == -1)
5986d7f5d3SJohn Marino err(1, "creat(%s)", file);
6086d7f5d3SJohn Marino bzero(buf, sizeof(buf));
6186d7f5d3SJohn Marino for (i = 0; i < 1024; i++)
6286d7f5d3SJohn Marino if (write(fd, &buf, sizeof(buf)) != sizeof(buf))
6386d7f5d3SJohn Marino err(1, "write");
6486d7f5d3SJohn Marino close(fd);
6586d7f5d3SJohn Marino return (0);
6686d7f5d3SJohn Marino }
6786d7f5d3SJohn Marino
6886d7f5d3SJohn Marino void
cleanup(void)6986d7f5d3SJohn Marino cleanup(void)
7086d7f5d3SJohn Marino {
7186d7f5d3SJohn Marino unlink(file);
7286d7f5d3SJohn Marino }
7386d7f5d3SJohn Marino
7486d7f5d3SJohn Marino int
test(void)7586d7f5d3SJohn Marino test(void)
7686d7f5d3SJohn Marino {
7786d7f5d3SJohn Marino int i;
7886d7f5d3SJohn Marino off_t pos;
7986d7f5d3SJohn Marino off_t size;
8086d7f5d3SJohn Marino
8186d7f5d3SJohn Marino if ((fd = open(file, O_RDWR, 0600)) == -1)
8286d7f5d3SJohn Marino err(1, "open(%s)", file);
8386d7f5d3SJohn Marino
8486d7f5d3SJohn Marino for (i = 0; i < 1024; i++) {
8586d7f5d3SJohn Marino pos = random_int(0, 1024 * 1024 - 1);
8686d7f5d3SJohn Marino if (lseek(fd, pos, SEEK_SET) == -1)
8786d7f5d3SJohn Marino err(1, "lseek");
8886d7f5d3SJohn Marino size = random_int(1, 1024 * 1024 - pos);
8986d7f5d3SJohn Marino if (size > 64)
9086d7f5d3SJohn Marino size = 64;
9186d7f5d3SJohn Marino if (lockf(fd, F_LOCK, size) == -1)
9286d7f5d3SJohn Marino err(1, "lockf(%s, F_LOCK)", file);
9386d7f5d3SJohn Marino size = random_int(1, size);
9486d7f5d3SJohn Marino if (lockf(fd, F_ULOCK, size) == -1)
9586d7f5d3SJohn Marino err(1, "lockf(%s, F_ULOCK)", file);
9686d7f5d3SJohn Marino
9786d7f5d3SJohn Marino }
9886d7f5d3SJohn Marino close(fd);
9986d7f5d3SJohn Marino
10086d7f5d3SJohn Marino return (0);
10186d7f5d3SJohn Marino }
102