Path: blob/main/tests/sys/cddl/zfs/bin/mmapwrite.c
39536 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2007 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/252627#include <unistd.h>28#include <fcntl.h>29#include <stdio.h>30#include <stdlib.h>31#include <sys/mman.h>32#include <pthread.h>3334/*35* --------------------------------------------------------------------36* Bug Id: 503264337*38* Simply writing to a file and mmaping that file at the same time can39* result in deadlock. Nothing perverse like writing from the file's40* own mapping is required.41* --------------------------------------------------------------------42*/4344static void *45mapper(void *fdp)46{47void *addr;48int fd = *(int *)fdp;4950if ((addr =51mmap(0, 8192, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {52perror("mmap");53exit(1);54}55for (;;) {56if (mmap(addr, 8192, PROT_READ,57MAP_SHARED|MAP_FIXED, fd, 0) == MAP_FAILED) {58perror("mmap");59exit(1);60}61}62/* NOTREACHED */63return ((void *)1);64}6566int67main(int argc, char **argv)68{69int fd;70char buf[BUFSIZ];71pthread_t pt;7273if (argc != 2) {74(void) printf("usage: %s <file name>\n", argv[0]);75exit(1);76}7778if ((fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1) {79perror("open");80exit(1);81}8283if (pthread_create(&pt, NULL, mapper, &fd) != 0) {84perror("pthread_create");85exit(1);86}87for (;;) {88if (write(fd, buf, sizeof (buf)) == -1) {89perror("write");90exit(1);91}92}9394/* NOTREACHED */95return (0);96}979899