xref: /freebsd/tools/test/stress2/misc/getrandom.sh (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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
49newfs $newfs_flags md$mdstart > /dev/null
50mount /dev/md$mdstart $mntpoint
51set +e
52
53(cd $odir/../testcases/swap; ./swap -t 5m -i 40 -l 100) &
54cd $mntpoint
55timeout 5m limits -c 0 $dir/getrandom
56s=0
57while pgrep -q swap; do pkill swap; done
58cd $odir
59
60for i in `jot 6`; do
61	mount | grep -q "on $mntpoint " || break
62	umount $mntpoint && break || sleep 10
63	[ $i -eq 6 ] &&
64	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
65done
66mdconfig -d -u $mdstart
67rm -rf $dir/getrandom
68exit $s
69
70EOF
71#include <sys/param.h>
72#include <sys/mman.h>
73#include <sys/random.h>
74#include <sys/stat.h>
75#include <sys/wait.h>
76
77#include <machine/atomic.h>
78
79#include <err.h>
80#include <errno.h>
81#include <fcntl.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <time.h>
85#include <unistd.h>
86
87static volatile u_int *share;
88
89#define PARALLEL 50
90#define RUNTIME (5 * 60)
91#define SYNC 0
92
93static void
94test(void)
95{
96	time_t start;
97
98	atomic_add_int(&share[SYNC], 1);
99	while (share[SYNC] != PARALLEL)
100		;
101
102	alarm(65);
103	start = time(NULL);
104	while ((time(NULL) - start) < 60)
105		getrandom((void *)arc4random(), arc4random(), arc4random() & 3);
106
107	_exit(0);
108}
109
110int
111main(void)
112{
113	pid_t pids[PARALLEL];
114	size_t len;
115	time_t start;
116	int e, i, status;
117
118	e = 0;
119	len = PAGE_SIZE;
120	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
121	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
122		err(1, "mmap");
123
124	start = time(NULL);
125	while ((time(NULL) - start) < RUNTIME) {
126		share[SYNC] = 0;
127		for (i = 0; i < PARALLEL; i++) {
128			if ((pids[i] = fork()) == 0)
129				test();
130			if (pids[i] == -1)
131				err(1, "fork()");
132		}
133		for (i = 0; i < PARALLEL; i++) {
134			if (waitpid(pids[i], &status, 0) == -1)
135				err(1, "waitpid(%d)", pids[i]);
136			e += status == 0 ? 0 : 1;
137		}
138	}
139
140	return (e);
141}
142