1 /*-
2 * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
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 <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <sys/param.h>
34 #include <sys/mman.h>
35 #include <err.h>
36 #include <string.h>
37 #include <errno.h>
38
39 #include "stress.h"
40
41 static char path[128];
42
43 #define INPUTFILE "/bin/date"
44
45 int
setup(int nb)46 setup(int nb)
47 {
48 umask(0);
49
50 sprintf(path,"%s.%05d", getprogname(), getpid());
51 if (mkdir(path, 0770) < 0)
52 err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
53
54 if (chdir(path) == -1)
55 err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
56
57 return (0);
58 }
59
60 void
cleanup(void)61 cleanup(void)
62 {
63 (void)chdir("..");
64 if (rmdir(path) == -1) {
65 warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
66 }
67
68 }
69
70 int
test(void)71 test(void)
72 {
73 int i;
74 pid_t pid;
75 char file[128];
76 int fdin, fdout;
77 char *src, *dst;
78 struct stat statbuf;
79
80 pid = getpid();
81 for (i = 0; i < 100 && done_testing == 0; i++) {
82 sprintf(file,"p%05d.%05d", pid, i);
83
84 if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)
85 err(1, INPUTFILE);
86
87 if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
88 err(1, "%s", file);
89
90 if (fstat(fdin, &statbuf) < 0)
91 err(1, "fstat error");
92
93 if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
94 err(1, "lseek error");
95
96 /* write a dummy byte at the last location */
97 if (write(fdout, "", 1) != 1)
98 err(1, "write error");
99
100 if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
101 (caddr_t) - 1)
102 err(1, "mmap error for input");
103
104 if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
105 MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
106 err(1, "mmap error for output");
107
108 memcpy(dst, src, statbuf.st_size);
109
110
111 if (munmap(src, statbuf.st_size) == -1)
112 err(1, "munmap");
113 close(fdin);
114
115 if (munmap(dst, statbuf.st_size) == -1)
116 err(1, "munmap");
117 close(fdout);
118
119 if (unlink(file) == -1)
120 err(3, "unlink(%s)", file);
121 }
122
123
124 return (0);
125 }
126