xref: /freebsd/tools/test/stress2/misc/midi2.sh (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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# Test threaded access to /dev/midistat.
31
32# "panic: vm_fault_hold: fault on nofault entry, addr: 0x8b352000" seen.
33# https://people.freebsd.org/~pho/stress/log/mark089.txt
34# Fixed by 351262
35
36. ../default.cfg
37[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
38
39dir=/tmp
40odir=`pwd`
41cd $dir
42sed '1,/^EOF/d' < $odir/$0 > $dir/midi2.c
43mycc -o midi2 -Wall -Wextra -O0 -g midi2.c -lpthread || exit 1
44
45$dir/midi2
46s=$?
47cat /dev/midistat > /dev/null || s=1
48
49rm -rf midi2 midi2.c midi2.core
50exit $s
51
52EOF
53#include <sys/param.h>
54
55#include <machine/atomic.h>
56
57#include <err.h>
58#include <errno.h>
59#include <fcntl.h>
60#include <pthread.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <time.h>
64#include <unistd.h>
65
66static volatile u_int share;
67static int fd;
68static char buf[0xbc59], buf2[0x40eddf];
69
70#define THREADS 4
71#define RUNTIME (1 * 60)
72#define SYNC 0
73
74static void *
75t1(void *data __unused)
76{
77
78	atomic_add_int(&share, 1);
79	while (share != THREADS)
80		;
81
82        pread(fd, buf, 0xbc59, 0x27dbb298LL);
83        pread(fd, buf, 0xbc59, 0x104e35c6d22eLL);
84        pread(fd, buf2, 0x40eddf, 0x405d1df88cbf41LL);
85
86	return (NULL);
87}
88
89int
90main(void)
91{
92	pthread_t tid[THREADS];
93	time_t start;
94	int i, rc;
95
96	if ((fd = open("/dev/midistat", O_RDONLY)) == -1)
97		err(1, "open(/dev/midistat)");
98	start = time(NULL);
99	while ((time(NULL) - start) < RUNTIME) {
100		share = 0;
101		for (i = 0; i < THREADS; i++) {
102			if ((rc = pthread_create(&tid[i], NULL, t1, NULL)) !=
103			    0)
104				errc(1, rc, "pthread_create");
105		}
106
107		for (i = 0; i < THREADS; i++) {
108			if ((rc = pthread_join(tid[i], NULL)) != 0)
109				errc(1, rc, "pthread_join");
110		}
111	}
112
113	return (0);
114}
115