xref: /freebsd/tools/test/stress2/misc/kevent8.sh (revision 2774f206809b8fd3a4904fe945f029a414fbc642)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 EMC Corp.
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# Out of VM seen due to missing close of kqueue handle.
30# Fixed in 256849
31
32. ../default.cfg
33
34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35ulimit -k 5000 || { echo FAIL; exit 1; }
36
37odir=`pwd`
38
39cd /tmp
40sed '1,/^EOF/d' < $odir/$0 > kevent8.c
41mycc -o kevent8 -Wall -Wextra -O2 -g kevent8.c -lpthread || exit 1
42rm -f kevent8.c
43cd $odir
44
45mount | grep "on $mntpoint " | grep -q md$mdstart && umount -f $mntpoint
46mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
47
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
52chmod 777 $mntpoint
53
54su $testuser -c "(cd $mntpoint; /tmp/kevent8)" &
55for i in `jot 99`; do
56	sleep 1
57	kill -0 $! 2>/dev/null || break
58done
59umount -f $mntpoint
60pkill kevent8
61wait
62
63while mount | grep -q $mntpoint; do
64	umount $mntpoint || sleep 1
65done
66mdconfig -d -u $mdstart
67rm -f /tmp/kevent8
68
69exit
70EOF
71#include <sys/types.h>
72#include <sys/event.h>
73#include <sys/mman.h>
74#include <sys/wait.h>
75
76#include <err.h>
77#include <errno.h>
78#include <fcntl.h>
79#include <pthread.h>
80#include <sched.h>
81#include <signal.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <string.h>
85#include <unistd.h>
86
87#define PARALLEL 64
88
89static int fd;
90static char path[80];
91
92static void *
93spin(void *arg __unused)
94{
95	int i;
96	setproctitle("spin");
97
98	for (i= 0;; i++) {
99		snprintf(path, sizeof(path), "file.%06d.%d", getpid(), i);
100		if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) ==
101		    -1)
102			err(1, "creat()");
103		close(fd);
104		fd = 0;
105		unlink(path);
106	}
107	return (NULL);
108}
109
110static void *
111test(void *arg __unused)
112{
113	int kq;
114	int i, n;
115	struct kevent ev;
116	struct timespec ts;
117
118	for (i = 0; i < 500000; i++) {
119		if ((kq = kqueue()) < 0)
120			if (errno != ENOMEM)
121				err(1, "kqueue()");
122
123		n = 0;
124		memset(&ev, 0, sizeof(ev));
125		EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
126			NOTE_DELETE|NOTE_RENAME|NOTE_EXTEND, 0, 0);
127		n++;
128
129		if (kevent(kq, &ev, n, NULL, 0, NULL) < 0)
130			continue;	/* Note: missing close(kq)! */
131
132		memset(&ev, 0, sizeof(ev));
133		ts.tv_sec  = 0;
134		ts.tv_nsec = 1000000;
135		if ((n = kevent(kq, NULL, 0, &ev, 1, &ts)) == -1)
136			err(1, "kevent()");
137
138		close(kq);
139	}
140	return (NULL);
141}
142
143int
144main(void)
145{
146	pthread_t cp[PARALLEL], sp;
147	int e, i;
148
149	if ((e = pthread_create(&sp, NULL, spin, NULL)) != 0)
150		errc(1, e, "pthread_create");
151
152	for (i = 0; i < PARALLEL; i++) {
153		if ((e = pthread_create(&cp[i], NULL, test, NULL)) != 0)
154			errc(1, e, "pthread_create");
155	}
156
157	for (i = 0; i < PARALLEL; i++)
158		pthread_join(cp[i], NULL);
159
160	close(fd);
161
162	return (0);
163}
164