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