xref: /freebsd/tools/test/stress2/misc/getrandom.sh (revision c1d255d3)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# getrandom() non threaded test
31# Reset seen on i386
32
33. ../default.cfg
34[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
35[ `uname -m` = "i386" ] || exit 0
36
37dir=/tmp
38odir=`pwd`
39cd $dir
40sed '1,/^EOF/d' < $odir/$0 > $dir/getrandom.c
41mycc -o getrandom -Wall -Wextra -O0 -g getrandom.c || exit 1
42rm -f getrandom.c
43cd $odir
44
45set -e
46mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
47[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
48mdconfig -a -t swap -s 2g -u $mdstart
49bsdlabel -w md$mdstart auto
50newfs $newfs_flags md${mdstart}$part > /dev/null
51mount /dev/md${mdstart}$part $mntpoint
52set +e
53
54(cd $odir/../testcases/swap; ./swap -t 5m -i 40 -l 100) &
55cd $mntpoint
56timeout 5m limits -c 0 $dir/getrandom
57s=0
58while pgrep -q swap; do pkill swap; done
59cd $odir
60
61for i in `jot 6`; do
62	mount | grep -q "on $mntpoint " || break
63	umount $mntpoint && break || sleep 10
64	[ $i -eq 6 ] &&
65	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
66done
67mdconfig -d -u $mdstart
68rm -rf $dir/getrandom
69exit $s
70
71EOF
72#include <sys/param.h>
73#include <sys/mman.h>
74#include <sys/random.h>
75#include <sys/stat.h>
76#include <sys/wait.h>
77
78#include <machine/atomic.h>
79
80#include <err.h>
81#include <errno.h>
82#include <fcntl.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <time.h>
86#include <unistd.h>
87
88static volatile u_int *share;
89
90#define PARALLEL 50
91#define RUNTIME (5 * 60)
92#define SYNC 0
93
94static void
95test(void)
96{
97	time_t start;
98
99	atomic_add_int(&share[SYNC], 1);
100	while (share[SYNC] != PARALLEL)
101		;
102
103	alarm(65);
104	start = time(NULL);
105	while ((time(NULL) - start) < 60)
106		getrandom((void *)arc4random(), arc4random(), arc4random() & 3);
107
108	_exit(0);
109}
110
111int
112main(void)
113{
114	pid_t pids[PARALLEL];
115	size_t len;
116	time_t start;
117	int e, i, status;
118
119	e = 0;
120	len = PAGE_SIZE;
121	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
122	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
123		err(1, "mmap");
124
125	start = time(NULL);
126	while ((time(NULL) - start) < RUNTIME) {
127		share[SYNC] = 0;
128		for (i = 0; i < PARALLEL; i++) {
129			if ((pids[i] = fork()) == 0)
130				test();
131			if (pids[i] == -1)
132				err(1, "fork()");
133		}
134		for (i = 0; i < PARALLEL; i++) {
135			if (waitpid(pids[i], &status, 0) == -1)
136				err(1, "waitpid(%d)", pids[i]);
137			e += status == 0 ? 0 : 1;
138		}
139	}
140
141	return (e);
142}
143