xref: /freebsd/tools/test/stress2/misc/kevent13.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2018 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 based on scenario by Babcia Padlina
31
32# "panic: mutex pipe mutex not owned at sys_pipe.c:1769" seen:
33# https://people.freebsd.org/~pho/stress/log/kevent13.txt
34# Fixed by r235640.
35
36. ../default.cfg
37
38odir=`pwd`
39cd /tmp
40sed '1,/^EOF/d' < $odir/$0 > kevent13.c
41cc -o kevent13 -Wall kevent13.c -lpthread
42rm -f kevent13.c
43
44[ -d "$RUNDIR" ] || mkdir -p $RUNDIR
45cd $RUNDIR
46start=`date +%s`
47while [ $((`date +%s` - start)) -lt 300 ]; do
48	for i in `jot 10`; do
49		/tmp/kevent13 &
50		/tmp/kevent13 &
51		wait
52	done
53done
54
55rm -f /tmp/kevent13
56exit 0
57EOF
58/*
59 * 29.08.2009, babcia padlina
60 */
61
62#include <sys/types.h>
63#include <sys/event.h>
64#include <sys/param.h>
65#include <sys/timespec.h>
66#include <unistd.h>
67
68#include <err.h>
69#include <fcntl.h>
70#include <pthread.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
74
75#define LOOPS 200000
76
77struct kevent kev[2], ke[2];
78struct timespec timeout;
79volatile int go;
80int fd[2], kq;
81
82void
83do_thread(void) {
84	int i;
85
86	go = 1;
87	for (i = 0; i < LOOPS; i++) {
88		if (pipe(fd) < 0)
89			err(1, "pipe");
90		memset(&kev, 0, sizeof(kev));
91		EV_SET(&kev[0], fd[0], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, NULL);
92		EV_SET(&kev[1], fd[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, NULL);
93
94		if (kevent(kq, kev, 2, ke, 2, &timeout) < 0)
95			err(1, "kevent");
96
97		close(fd[0]);
98		close(fd[1]);
99	}
100}
101
102void
103do_thread2(void) {
104	int i;
105
106	while (go == 0)
107		usleep(10);
108	for (i = 0; i < LOOPS; i++) {
109		if (arc4random() % 2 == 0) {
110			close(fd[0]);
111			close(fd[1]);
112		} else {
113			close(fd[1]);
114			close(fd[0]);
115		}
116	}
117}
118
119int
120main(void) {
121	pthread_t pth, pth2;
122	int r;
123
124	if ((kq = kqueue()) < 0)
125		err(1, "kqueue");
126
127	if ((r = pthread_create(&pth, NULL, (void *)do_thread, NULL)) != 0)
128		errc(1, r, "pthread_create");
129	if ((r = pthread_create(&pth2, NULL, (void *)do_thread2, NULL)) != 0)
130		errc(1, r, "pthread_create");
131
132	timeout.tv_sec = 0;
133	timeout.tv_nsec = 1;
134
135	if ((r = pthread_join(pth, NULL)) != 0)
136		errc(1, r, "pthread_join");
137	if ((r = pthread_join(pth2, NULL)) != 0)
138		errc(1, r, "pthread_join");
139
140	return (0);
141}
142