xref: /freebsd/tools/test/stress2/misc/temp.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Temp file test scenario.
30# "temp: unlink(file.034434.48): Permission denied" seen.
31
32. ../default.cfg
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35export LANG=C
36dir=/tmp
37odir=`pwd`
38cd $dir
39sed '1,/^EOF/d' < $odir/$0 > $dir/temp.c
40mycc -o temp -Wall -Wextra -O0 -g temp.c || exit 1
41rm -f temp.c
42cd $odir
43
44[ -z "$nfs_export" ] && exit 0
45ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
46    exit 0
47
48mount | grep "on $mntpoint " | grep -q nfs && umount $mntpoint
49mount -t nfs -o tcp -o retrycnt=3 -o soft -o rw $nfs_export $mntpoint
50mp2=$mntpoint/temp.`jot -rc 8 a z | tr -d '\n'`/temp.dir
51rm -rf $mp2
52mkdir -p $mp2
53chmod 0777 $mp2
54
55log=/tmp/temp.log
56/tmp/temp $mp2 2> $log
57s=$?
58#[ $s -eq 0 ] && ministat -C 3 -A < $log
59rm -rf $mp2
60
61while mount | grep "on $mntpoint " | grep -q nfs; do
62	umount $mntpoint || sleep 1
63done
64rm -rf /tmp/temp $log
65exit $s
66
67EOF
68#include <sys/param.h>
69#include <sys/mman.h>
70#include <sys/stat.h>
71#include <sys/wait.h>
72
73#include <machine/atomic.h>
74
75#include <err.h>
76#include <errno.h>
77#include <fcntl.h>
78#include <stdio.h>
79#include <stdlib.h>
80#include <time.h>
81#include <unistd.h>
82
83volatile u_int *share;
84
85#define SYNC 0
86
87#define FILES 1024
88#define PARALLEL 4
89#define RUNTIME (10 * 60)
90
91void
92test(char *dir)
93{
94	pid_t pid;
95	int fd, i;
96	char path[1024];
97
98	atomic_add_int(&share[SYNC], 1);
99	while (share[SYNC] != PARALLEL)
100		;
101
102	pid = getpid();
103	for (i = 0; i < FILES; i++) {
104		snprintf(path, sizeof(path), "%s/file.%06d.%d", dir, pid, i);
105		if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
106			err(1, "open(%s)", path);
107		if (write(fd, path, sizeof(path)) != sizeof(path))
108			err(1, "write");
109		close(fd);
110		if (unlink(path) == -1)
111			err(1, "unlink(%s)", path);
112	}
113	_exit(0);
114}
115
116int
117main(int argc, char *argv[])
118{
119	size_t len;
120	struct timeval start, stop, diff;
121	struct tm *tp;
122	time_t now, then;
123	uint64_t usec;
124	int e, i, loop, pids[PARALLEL], status;
125	char buf[80];
126
127	e = 0;
128	if (argc != 2) {
129		fprintf(stderr, "Usage: %s <path>\n", argv[0]);
130		_exit(1);
131	}
132	len = PAGE_SIZE;
133	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
134	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
135		err(1, "mmap");
136
137	loop = 0;
138	alarm(2 * RUNTIME);
139	now = time(NULL);
140	while ((time(NULL) - now) < RUNTIME && e == 0) {
141		share[SYNC] = 0;
142		gettimeofday(&start, NULL);
143		for (i = 0; i < PARALLEL; i++) {
144			if ((pids[i] = fork()) == 0)
145				test(argv[1]);
146		}
147		for (i = 0; i < PARALLEL; i++) {
148			waitpid(pids[i], &status, 0);
149			e += status == 0 ? 0 : 1;
150		}
151		gettimeofday(&stop, NULL);
152		timersub(&stop, &start, &diff);
153		usec  = ((uint64_t)1000000 *
154		    diff.tv_sec + diff.tv_usec);
155		then = time(NULL);
156		tp = localtime(&then);
157		strftime(buf, sizeof(buf), "%H:%M:%S", tp);
158		if (loop != 0) /* Skip warmup */
159			fprintf(stderr, "%s %6d %.3f\n",
160			    buf, loop,
161			    (double)usec / 1000000);
162		loop++;
163		sleep(5);
164	}
165
166	return (e);
167}
168