xref: /freebsd/tools/test/stress2/misc/sndstat.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1#!/bin/sh
2
3#
4# Copyright (c) 2012 Peter Holm <pho@FreeBSD.org>
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# Test of access to /dev/sndstat
30
31# Permanent "/dev/sndstat: Device busy" seen.
32# "panic: sbuf_put_byte called with finished or corrupt sbuf" seen.
33# Fixed in r234932
34
35. ../default.cfg
36
37[ -r /dev/sndstat ] || exit 0
38
39here=`pwd`
40cd /tmp
41sed '1,/^EOF/d' < $here/$0 > sndstat.c
42mycc -o sndstat -Wall -Wextra sndstat.c -lpthread
43rm -f sndstat.c
44
45/tmp/sndstat > /dev/null
46
47rm -f /tmp/sndstat
48exit 0
49EOF
50#include <sys/types.h>
51#include <sys/syscall.h>
52#include <sys/uio.h>
53#include <dirent.h>
54#include <err.h>
55#include <fcntl.h>
56#include <pthread.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <strings.h>
60#include <sys/wait.h>
61#include <unistd.h>
62
63int fd;
64char path[] = "/dev/sndstat";
65
66void *
67test1(void *arg __unused)
68{
69
70        int sfd, i, n;
71	char buf[512];
72
73	n = 0;
74        for (i = 0; i < 5; i++) {
75                if ((sfd = open(path, O_RDONLY)) == -1)
76                        continue;
77
78                read(sfd, buf, sizeof(buf));
79		fprintf(stdout, "%s\n", buf);
80
81		n++;
82                close(sfd);
83        }
84	if (n == 0) {
85                if ((sfd = open(path, O_RDONLY)) == -1)
86			warn("FAIL open(%s)", path);
87		if (sfd > 0)
88			close(sfd);
89	}
90
91        return (0);
92}
93
94void *
95test2(void *arg __unused)
96{
97	char buf[512];
98
99	bzero(buf, sizeof(buf));
100	if (read(fd, buf, sizeof(buf)) != -1)
101		fprintf(stdout, "%s\n", buf);
102	else
103		warn("read()");
104
105        return (0);
106}
107
108int
109main(void)
110{
111        pthread_t rp[10];
112        int e, i, j;
113
114	/* Parallel open test */
115        for (i = 0; i < 10; i++) {
116                if ((e = pthread_create(&rp[i], NULL, test1, NULL)) != 0)
117                        errc(1, e, "pthread_create");
118        }
119        for (i = 0; i < 10; i++)
120                pthread_join(rp[i], NULL);
121
122	/* Parallel read test */
123	for (i = 0; i < 10; i++) {
124		if ((fd = open(path, O_RDONLY)) == -1) {
125			warn("open()");
126			continue;
127		}
128		for (j = 0; j < 4; j++)
129			if ((e = pthread_create(&rp[j], NULL, test2, NULL)) != 0)
130				errc(1, e, "pthread_create");
131		for (j = 0; j < 4; j++)
132			pthread_join(rp[j], NULL);
133
134		close(fd);
135	}
136
137        return (0);
138}
139