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