xref: /freebsd/tools/test/stress2/misc/kevent8.sh (revision 9768746ba83efa02837c5b9c66348db6e900208f)
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
49newfs $newfs_flags md$mdstart > /dev/null
50mount /dev/md$mdstart $mntpoint
51chmod 777 $mntpoint
52
53su $testuser -c "(cd $mntpoint; /tmp/kevent8)" &
54for i in `jot 99`; do
55	sleep 1
56	kill -0 $! 2>/dev/null || break
57done
58umount -f $mntpoint
59pkill kevent8
60wait
61
62while mount | grep -q $mntpoint; do
63	umount $mntpoint || sleep 1
64done
65mdconfig -d -u $mdstart
66rm -f /tmp/kevent8
67
68exit
69EOF
70#include <sys/types.h>
71#include <sys/event.h>
72#include <sys/mman.h>
73#include <sys/wait.h>
74
75#include <err.h>
76#include <errno.h>
77#include <fcntl.h>
78#include <pthread.h>
79#include <sched.h>
80#include <signal.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <string.h>
84#include <unistd.h>
85
86#define PARALLEL 64
87
88static int fd;
89static char path[80];
90
91static void *
92spin(void *arg __unused)
93{
94	int i;
95	setproctitle("spin");
96
97	for (i= 0;; i++) {
98		snprintf(path, sizeof(path), "file.%06d.%d", getpid(), i);
99		if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) ==
100		    -1)
101			err(1, "creat()");
102		close(fd);
103		fd = 0;
104		unlink(path);
105	}
106	return (NULL);
107}
108
109static void *
110test(void *arg __unused)
111{
112	int kq;
113	int i, n;
114	struct kevent ev;
115	struct timespec ts;
116
117	for (i = 0; i < 500000; i++) {
118		if ((kq = kqueue()) < 0)
119			if (errno != ENOMEM)
120				err(1, "kqueue()");
121
122		n = 0;
123		memset(&ev, 0, sizeof(ev));
124		EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
125			NOTE_DELETE|NOTE_RENAME|NOTE_EXTEND, 0, 0);
126		n++;
127
128		if (kevent(kq, &ev, n, NULL, 0, NULL) < 0)
129			continue;	/* Note: missing close(kq)! */
130
131		memset(&ev, 0, sizeof(ev));
132		ts.tv_sec  = 0;
133		ts.tv_nsec = 1000000;
134		if ((n = kevent(kq, NULL, 0, &ev, 1, &ts)) == -1)
135			err(1, "kevent()");
136
137		close(kq);
138	}
139	return (NULL);
140}
141
142int
143main(void)
144{
145	pthread_t cp[PARALLEL], sp;
146	int e, i;
147
148	if ((e = pthread_create(&sp, NULL, spin, NULL)) != 0)
149		errc(1, e, "pthread_create");
150
151	for (i = 0; i < PARALLEL; i++) {
152		if ((e = pthread_create(&cp[i], NULL, test, NULL)) != 0)
153			errc(1, e, "pthread_create");
154	}
155
156	for (i = 0; i < PARALLEL; i++)
157		pthread_join(cp[i], NULL);
158
159	close(fd);
160
161	return (0);
162}
163