xref: /freebsd/tests/sys/cddl/zfs/bin/mmapwrite.c (revision ac00d4d59b18a76c6148ca5d7439bb446d38da5c)
1*2fae26bdSAlan Somers /*
2*2fae26bdSAlan Somers  * CDDL HEADER START
3*2fae26bdSAlan Somers  *
4*2fae26bdSAlan Somers  * The contents of this file are subject to the terms of the
5*2fae26bdSAlan Somers  * Common Development and Distribution License (the "License").
6*2fae26bdSAlan Somers  * You may not use this file except in compliance with the License.
7*2fae26bdSAlan Somers  *
8*2fae26bdSAlan Somers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2fae26bdSAlan Somers  * or http://www.opensolaris.org/os/licensing.
10*2fae26bdSAlan Somers  * See the License for the specific language governing permissions
11*2fae26bdSAlan Somers  * and limitations under the License.
12*2fae26bdSAlan Somers  *
13*2fae26bdSAlan Somers  * When distributing Covered Code, include this CDDL HEADER in each
14*2fae26bdSAlan Somers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2fae26bdSAlan Somers  * If applicable, add the following below this CDDL HEADER, with the
16*2fae26bdSAlan Somers  * fields enclosed by brackets "[]" replaced with your own identifying
17*2fae26bdSAlan Somers  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2fae26bdSAlan Somers  *
19*2fae26bdSAlan Somers  * CDDL HEADER END
20*2fae26bdSAlan Somers  */
21*2fae26bdSAlan Somers 
22*2fae26bdSAlan Somers /*
23*2fae26bdSAlan Somers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*2fae26bdSAlan Somers  * Use is subject to license terms.
25*2fae26bdSAlan Somers  */
26*2fae26bdSAlan Somers 
27*2fae26bdSAlan Somers 
28*2fae26bdSAlan Somers #include <unistd.h>
29*2fae26bdSAlan Somers #include <fcntl.h>
30*2fae26bdSAlan Somers #include <stdio.h>
31*2fae26bdSAlan Somers #include <stdlib.h>
32*2fae26bdSAlan Somers #include <sys/mman.h>
33*2fae26bdSAlan Somers #include <pthread.h>
34*2fae26bdSAlan Somers 
35*2fae26bdSAlan Somers /*
36*2fae26bdSAlan Somers  * --------------------------------------------------------------------
37*2fae26bdSAlan Somers  * Bug Id: 5032643
38*2fae26bdSAlan Somers  *
39*2fae26bdSAlan Somers  * Simply writing to a file and mmaping that file at the same time can
40*2fae26bdSAlan Somers  * result in deadlock.  Nothing perverse like writing from the file's
41*2fae26bdSAlan Somers  * own mapping is required.
42*2fae26bdSAlan Somers  * --------------------------------------------------------------------
43*2fae26bdSAlan Somers  */
44*2fae26bdSAlan Somers 
45*2fae26bdSAlan Somers static void *
mapper(void * fdp)46*2fae26bdSAlan Somers mapper(void *fdp)
47*2fae26bdSAlan Somers {
48*2fae26bdSAlan Somers 	void *addr;
49*2fae26bdSAlan Somers 	int fd = *(int *)fdp;
50*2fae26bdSAlan Somers 
51*2fae26bdSAlan Somers 	if ((addr =
52*2fae26bdSAlan Somers 	    mmap(0, 8192, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
53*2fae26bdSAlan Somers 		perror("mmap");
54*2fae26bdSAlan Somers 		exit(1);
55*2fae26bdSAlan Somers 	}
56*2fae26bdSAlan Somers 	for (;;) {
57*2fae26bdSAlan Somers 		if (mmap(addr, 8192, PROT_READ,
58*2fae26bdSAlan Somers 		    MAP_SHARED|MAP_FIXED, fd, 0) == MAP_FAILED) {
59*2fae26bdSAlan Somers 			perror("mmap");
60*2fae26bdSAlan Somers 			exit(1);
61*2fae26bdSAlan Somers 		}
62*2fae26bdSAlan Somers 	}
63*2fae26bdSAlan Somers 	/* NOTREACHED */
64*2fae26bdSAlan Somers 	return ((void *)1);
65*2fae26bdSAlan Somers }
66*2fae26bdSAlan Somers 
67*2fae26bdSAlan Somers int
main(int argc,char ** argv)68*2fae26bdSAlan Somers main(int argc, char **argv)
69*2fae26bdSAlan Somers {
70*2fae26bdSAlan Somers 	int fd;
71*2fae26bdSAlan Somers 	char buf[BUFSIZ];
72*2fae26bdSAlan Somers 	pthread_t pt;
73*2fae26bdSAlan Somers 
74*2fae26bdSAlan Somers 	if (argc != 2) {
75*2fae26bdSAlan Somers 		(void) printf("usage: %s <file name>\n", argv[0]);
76*2fae26bdSAlan Somers 		exit(1);
77*2fae26bdSAlan Somers 	}
78*2fae26bdSAlan Somers 
79*2fae26bdSAlan Somers 	if ((fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1) {
80*2fae26bdSAlan Somers 		perror("open");
81*2fae26bdSAlan Somers 		exit(1);
82*2fae26bdSAlan Somers 	}
83*2fae26bdSAlan Somers 
84*2fae26bdSAlan Somers 	if (pthread_create(&pt, NULL, mapper, &fd) != 0) {
85*2fae26bdSAlan Somers 		perror("pthread_create");
86*2fae26bdSAlan Somers 		exit(1);
87*2fae26bdSAlan Somers 	}
88*2fae26bdSAlan Somers 	for (;;) {
89*2fae26bdSAlan Somers 		if (write(fd, buf, sizeof (buf)) == -1) {
90*2fae26bdSAlan Somers 			perror("write");
91*2fae26bdSAlan Somers 			exit(1);
92*2fae26bdSAlan Somers 		}
93*2fae26bdSAlan Somers 	}
94*2fae26bdSAlan Somers 
95*2fae26bdSAlan Somers 	/* NOTREACHED */
96*2fae26bdSAlan Somers 	return (0);
97*2fae26bdSAlan Somers }
98