xref: /freebsd/tools/test/stress2/testcases/mmap/mmap.c (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm /*-
2*8a272653SPeter Holm  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3*8a272653SPeter Holm  * All rights reserved.
4*8a272653SPeter Holm  *
5*8a272653SPeter Holm  * Redistribution and use in source and binary forms, with or without
6*8a272653SPeter Holm  * modification, are permitted provided that the following conditions
7*8a272653SPeter Holm  * are met:
8*8a272653SPeter Holm  * 1. Redistributions of source code must retain the above copyright
9*8a272653SPeter Holm  *    notice, this list of conditions and the following disclaimer.
10*8a272653SPeter Holm  * 2. Redistributions in binary form must reproduce the above copyright
11*8a272653SPeter Holm  *    notice, this list of conditions and the following disclaimer in the
12*8a272653SPeter Holm  *    documentation and/or other materials provided with the distribution.
13*8a272653SPeter Holm  *
14*8a272653SPeter Holm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*8a272653SPeter Holm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*8a272653SPeter Holm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*8a272653SPeter Holm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*8a272653SPeter Holm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*8a272653SPeter Holm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*8a272653SPeter Holm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*8a272653SPeter Holm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*8a272653SPeter Holm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*8a272653SPeter Holm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*8a272653SPeter Holm  * SUCH DAMAGE.
25*8a272653SPeter Holm  *
26*8a272653SPeter Holm  */
27*8a272653SPeter Holm 
28*8a272653SPeter Holm #include <sys/param.h>
29*8a272653SPeter Holm #include <sys/mman.h>
30*8a272653SPeter Holm #include <sys/stat.h>
31*8a272653SPeter Holm #include <err.h>
32*8a272653SPeter Holm #include <errno.h>
33*8a272653SPeter Holm #include <fcntl.h>
34*8a272653SPeter Holm #include <stdio.h>
35*8a272653SPeter Holm #include <stdlib.h>
36*8a272653SPeter Holm #include <string.h>
37*8a272653SPeter Holm #include <unistd.h>
38*8a272653SPeter Holm 
39*8a272653SPeter Holm #include "stress.h"
40*8a272653SPeter Holm 
41*8a272653SPeter Holm static char path[128];
42*8a272653SPeter Holm 
43*8a272653SPeter Holm #define INPUTFILE "/bin/date"
44*8a272653SPeter Holm 
45*8a272653SPeter Holm int
setup(int nb)46*8a272653SPeter Holm setup(int nb)
47*8a272653SPeter Holm {
48*8a272653SPeter Holm 	int64_t bl;
49*8a272653SPeter Holm 	int64_t in;
50*8a272653SPeter Holm 	int64_t reserve_bl;
51*8a272653SPeter Holm 	int64_t reserve_in;
52*8a272653SPeter Holm 	int freespace;
53*8a272653SPeter Holm 
54*8a272653SPeter Holm 	if (nb == 0) {
55*8a272653SPeter Holm 		getdf(&bl, &in);
56*8a272653SPeter Holm 
57*8a272653SPeter Holm 		/* Resource requirements: */
58*8a272653SPeter Holm 		reserve_in =    2 * op->incarnations;
59*8a272653SPeter Holm 		reserve_bl = 20480 * op->incarnations;
60*8a272653SPeter Holm 		freespace = (reserve_bl <= bl && reserve_in <= in);
61*8a272653SPeter Holm 		if (!freespace)
62*8a272653SPeter Holm 			reserve_bl = reserve_in = 0;
63*8a272653SPeter Holm 
64*8a272653SPeter Holm 		if (op->verbose > 1)
65*8a272653SPeter Holm 			printf("mmap(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
66*8a272653SPeter Holm 			    op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
67*8a272653SPeter Holm 		reservedf(reserve_bl, reserve_in);
68*8a272653SPeter Holm 		putval(freespace);
69*8a272653SPeter Holm 	} else {
70*8a272653SPeter Holm 		freespace = getval();
71*8a272653SPeter Holm 	}
72*8a272653SPeter Holm 	if (!freespace)
73*8a272653SPeter Holm 		exit(0);
74*8a272653SPeter Holm 	umask(0);
75*8a272653SPeter Holm 
76*8a272653SPeter Holm 	sprintf(path,"%s.%05d", getprogname(), getpid());
77*8a272653SPeter Holm 	if (mkdir(path, 0770) < 0)
78*8a272653SPeter Holm 		err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
79*8a272653SPeter Holm 
80*8a272653SPeter Holm 	if (chdir(path) == -1)
81*8a272653SPeter Holm 		err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
82*8a272653SPeter Holm 
83*8a272653SPeter Holm 	return (0);
84*8a272653SPeter Holm }
85*8a272653SPeter Holm 
86*8a272653SPeter Holm void
cleanup(void)87*8a272653SPeter Holm cleanup(void)
88*8a272653SPeter Holm {
89*8a272653SPeter Holm 	(void)chdir("..");
90*8a272653SPeter Holm 	if (rmdir(path) == -1) {
91*8a272653SPeter Holm 		warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
92*8a272653SPeter Holm 	}
93*8a272653SPeter Holm }
94*8a272653SPeter Holm 
95*8a272653SPeter Holm int
test(void)96*8a272653SPeter Holm test(void)
97*8a272653SPeter Holm {
98*8a272653SPeter Holm 	struct stat statbuf;
99*8a272653SPeter Holm 	pid_t pid;
100*8a272653SPeter Holm 	char file[128];
101*8a272653SPeter Holm 	int fdin, fdout;
102*8a272653SPeter Holm 	int i;
103*8a272653SPeter Holm 	char *src, *dst;
104*8a272653SPeter Holm 
105*8a272653SPeter Holm 	pid = getpid();
106*8a272653SPeter Holm 	for (i = 0; i < 100 && done_testing == 0; i++) {
107*8a272653SPeter Holm 		sprintf(file,"p%05d.%05d", pid, i);
108*8a272653SPeter Holm 
109*8a272653SPeter Holm 		if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)
110*8a272653SPeter Holm 			err(1, INPUTFILE);
111*8a272653SPeter Holm 
112*8a272653SPeter Holm 		if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
113*8a272653SPeter Holm 			err(1, "%s", file);
114*8a272653SPeter Holm 
115*8a272653SPeter Holm 		if (fstat(fdin, &statbuf) < 0)
116*8a272653SPeter Holm 			err(1, "fstat error");
117*8a272653SPeter Holm 
118*8a272653SPeter Holm 		if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
119*8a272653SPeter Holm 			err(1, "lseek error");
120*8a272653SPeter Holm 
121*8a272653SPeter Holm 		/* write a dummy byte at the last location */
122*8a272653SPeter Holm 		if (write(fdout, "", 1) != 1)
123*8a272653SPeter Holm 			err(1, "write error");
124*8a272653SPeter Holm 
125*8a272653SPeter Holm 		if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
126*8a272653SPeter Holm 			(caddr_t) - 1)
127*8a272653SPeter Holm 			err(1, "mmap error for input");
128*8a272653SPeter Holm 
129*8a272653SPeter Holm 		if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
130*8a272653SPeter Holm 			MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
131*8a272653SPeter Holm 			err(1, "mmap error for output");
132*8a272653SPeter Holm 
133*8a272653SPeter Holm 		memcpy(dst, src, statbuf.st_size);
134*8a272653SPeter Holm 
135*8a272653SPeter Holm 		if (munmap(src, statbuf.st_size) == -1)
136*8a272653SPeter Holm 			err(1, "munmap");
137*8a272653SPeter Holm 		close(fdin);
138*8a272653SPeter Holm 
139*8a272653SPeter Holm 		if (munmap(dst, statbuf.st_size) == -1)
140*8a272653SPeter Holm 			err(1, "munmap");
141*8a272653SPeter Holm 		close(fdout);
142*8a272653SPeter Holm 
143*8a272653SPeter Holm 		if (unlink(file) == -1)
144*8a272653SPeter Holm 			err(3, "unlink(%s)", file);
145*8a272653SPeter Holm 	}
146*8a272653SPeter Holm 
147*8a272653SPeter Holm 	return (0);
148*8a272653SPeter Holm }
149