Path: blob/main/tools/test/stress2/testcases/mmap/mmap.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#include <sys/param.h>28#include <sys/mman.h>29#include <sys/stat.h>30#include <err.h>31#include <errno.h>32#include <fcntl.h>33#include <stdio.h>34#include <stdlib.h>35#include <string.h>36#include <unistd.h>3738#include "stress.h"3940static char path[128];4142#define INPUTFILE "/bin/date"4344int45setup(int nb)46{47int64_t bl;48int64_t in;49int64_t reserve_bl;50int64_t reserve_in;51int freespace;5253if (nb == 0) {54getdf(&bl, &in);5556/* Resource requirements: */57reserve_in = 2 * op->incarnations;58reserve_bl = 20480 * op->incarnations;59freespace = (reserve_bl <= bl && reserve_in <= in);60if (!freespace)61reserve_bl = reserve_in = 0;6263if (op->verbose > 1)64printf("mmap(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",65op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);66reservedf(reserve_bl, reserve_in);67putval(freespace);68} else {69freespace = getval();70}71if (!freespace)72exit(0);73umask(0);7475sprintf(path,"%s.%05d", getprogname(), getpid());76if (mkdir(path, 0770) < 0)77err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);7879if (chdir(path) == -1)80err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);8182return (0);83}8485void86cleanup(void)87{88(void)chdir("..");89if (rmdir(path) == -1) {90warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);91}92}9394int95test(void)96{97struct stat statbuf;98pid_t pid;99char file[128];100int fdin, fdout;101int i;102char *src, *dst;103104pid = getpid();105for (i = 0; i < 100 && done_testing == 0; i++) {106sprintf(file,"p%05d.%05d", pid, i);107108if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)109err(1, INPUTFILE);110111if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)112err(1, "%s", file);113114if (fstat(fdin, &statbuf) < 0)115err(1, "fstat error");116117if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)118err(1, "lseek error");119120/* write a dummy byte at the last location */121if (write(fdout, "", 1) != 1)122err(1, "write error");123124if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==125(caddr_t) - 1)126err(1, "mmap error for input");127128if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,129MAP_SHARED, fdout, 0)) == (caddr_t) - 1)130err(1, "mmap error for output");131132memcpy(dst, src, statbuf.st_size);133134if (munmap(src, statbuf.st_size) == -1)135err(1, "munmap");136close(fdin);137138if (munmap(dst, statbuf.st_size) == -1)139err(1, "munmap");140close(fdout);141142if (unlink(file) == -1)143err(3, "unlink(%s)", file);144}145146return (0);147}148149150