xref: /freebsd/tools/test/stress2/misc/shm.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2016 Dell EMC Isilon
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# Regression test for r310849. Hang in "vmpfw" was seen.
30*8a272653SPeter Holm
31*8a272653SPeter Holm# Test scenario suggestion by kib.
32*8a272653SPeter Holm
33*8a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34*8a272653SPeter Holm
35*8a272653SPeter Holm. ../default.cfg
36*8a272653SPeter Holm
37*8a272653SPeter HolmRUNTIME=60
38*8a272653SPeter Holmold=`sysctl -n kern.ipc.shm_use_phys`
39*8a272653SPeter Holmtrap "sysctl kern.ipc.shm_use_phys=$old" EXIT INT
40*8a272653SPeter Holmsysctl kern.ipc.shm_use_phys=1
41*8a272653SPeter Holm
42*8a272653SPeter Holmdir=/tmp
43*8a272653SPeter Holmodir=`pwd`
44*8a272653SPeter Holmcd $dir
45*8a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/shm.c
46*8a272653SPeter Holmmycc -o shm -Wall -Wextra -O0 -g shm.c -pthread || exit 1
47*8a272653SPeter Holmrm -f shm.c
48*8a272653SPeter Holm
49*8a272653SPeter Holm/tmp/shm &
50*8a272653SPeter Holm
51*8a272653SPeter Holms=0
52*8a272653SPeter Holmsleep $RUNTIME
53*8a272653SPeter Holmif pgrep -q shm; then
54*8a272653SPeter Holm	if pgrep shm | xargs ps -lHp | grep -q vmpfw; then
55*8a272653SPeter Holm		s=1
56*8a272653SPeter Holm		echo FAIL
57*8a272653SPeter Holm		pgrep shm | xargs ps -lHp
58*8a272653SPeter Holm		pkill -9 shm
59*8a272653SPeter Holm	fi
60*8a272653SPeter Holmfi
61*8a272653SPeter Holmwait
62*8a272653SPeter Holm
63*8a272653SPeter Holmrm -rf /tmp/shm
64*8a272653SPeter Holmexit $s
65*8a272653SPeter Holm
66*8a272653SPeter HolmEOF
67*8a272653SPeter Holm#include <sys/param.h>
68*8a272653SPeter Holm#include <sys/ipc.h>
69*8a272653SPeter Holm#include <sys/mman.h>
70*8a272653SPeter Holm#include <sys/shm.h>
71*8a272653SPeter Holm#include <sys/stat.h>
72*8a272653SPeter Holm#include <sys/wait.h>
73*8a272653SPeter Holm
74*8a272653SPeter Holm#include <err.h>
75*8a272653SPeter Holm#include <errno.h>
76*8a272653SPeter Holm#include <fcntl.h>
77*8a272653SPeter Holm#include <pthread.h>
78*8a272653SPeter Holm#include <pthread_np.h>
79*8a272653SPeter Holm#include <stdio.h>
80*8a272653SPeter Holm#include <stdlib.h>
81*8a272653SPeter Holm#include <time.h>
82*8a272653SPeter Holm#include <unistd.h>
83*8a272653SPeter Holm
84*8a272653SPeter Holmstatic void *shmp;
85*8a272653SPeter Holmstatic size_t len;
86*8a272653SPeter Holmstatic int cont, shmid;
87*8a272653SPeter Holm
88*8a272653SPeter Holm#define PAGES 64
89*8a272653SPeter Holm#define RUNTIME (1 * 60)
90*8a272653SPeter Holm#define STOP 1
91*8a272653SPeter Holm#define SYNC 0
92*8a272653SPeter Holm
93*8a272653SPeter Holmstatic void
94*8a272653SPeter Holmcleanup(void)
95*8a272653SPeter Holm{
96*8a272653SPeter Holm	if (shmp != MAP_FAILED)
97*8a272653SPeter Holm		shmdt(shmp);
98*8a272653SPeter Holm	if (shmid != 0)
99*8a272653SPeter Holm		shmctl(shmid, IPC_RMID, NULL);
100*8a272653SPeter Holm}
101*8a272653SPeter Holm
102*8a272653SPeter Holmstatic void *
103*8a272653SPeter Holmt1(void *arg __unused)
104*8a272653SPeter Holm{
105*8a272653SPeter Holm	time_t start;
106*8a272653SPeter Holm	char *cp;
107*8a272653SPeter Holm
108*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
109*8a272653SPeter Holm	start = time(NULL);
110*8a272653SPeter Holm	while (cont == 1 && (time(NULL) - start) < RUNTIME) {
111*8a272653SPeter Holm		if ((cp = mmap(NULL, len, PROT_READ | PROT_WRITE,
112*8a272653SPeter Holm		    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
113*8a272653SPeter Holm			err(1, "mmap");
114*8a272653SPeter Holm		usleep(arc4random() % 400);
115*8a272653SPeter Holm		if (munmap(cp, len) == -1)
116*8a272653SPeter Holm			warn("unmap(%p, %zu)", shmp, len);
117*8a272653SPeter Holm	}
118*8a272653SPeter Holm
119*8a272653SPeter Holm	return (NULL);
120*8a272653SPeter Holm}
121*8a272653SPeter Holm
122*8a272653SPeter Holmstatic void *
123*8a272653SPeter Holmt2(void *arg __unused)
124*8a272653SPeter Holm{
125*8a272653SPeter Holm	key_t shmkey;
126*8a272653SPeter Holm	size_t i;
127*8a272653SPeter Holm	time_t start;
128*8a272653SPeter Holm	char *cp;
129*8a272653SPeter Holm
130*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
131*8a272653SPeter Holm	shmkey = ftok("/tmp", getpid());
132*8a272653SPeter Holm	start = time(NULL);
133*8a272653SPeter Holm	atexit(cleanup);
134*8a272653SPeter Holm	while ((time(NULL) - start) < RUNTIME) {
135*8a272653SPeter Holm		if ((shmid = shmget(shmkey, len, IPC_CREAT | IPC_EXCL |
136*8a272653SPeter Holm		    0640)) == -1) {
137*8a272653SPeter Holm			if (errno != ENOSPC && errno != EEXIST)
138*8a272653SPeter Holm				err(1, "shmget (%s:%d)", __FILE__, __LINE__);
139*8a272653SPeter Holm			break;
140*8a272653SPeter Holm		}
141*8a272653SPeter Holm		if ((shmp = shmat(shmid, NULL, 0)) == (void *) -1)
142*8a272653SPeter Holm			break;
143*8a272653SPeter Holm		cp = (char *)(shmp);
144*8a272653SPeter Holm		for (i = 0; i < len; i = i + PAGE_SIZE)
145*8a272653SPeter Holm			cp[i] = 1;
146*8a272653SPeter Holm		if (shmdt(shmp) == -1) {
147*8a272653SPeter Holm			if (errno != EINVAL)
148*8a272653SPeter Holm				warn("shmdt(%p)", shmp);
149*8a272653SPeter Holm		}
150*8a272653SPeter Holm		if (shmctl(shmid, IPC_RMID, NULL) == -1)
151*8a272653SPeter Holm			warn("shmctl IPC_RMID");
152*8a272653SPeter Holm		usleep(50);
153*8a272653SPeter Holm
154*8a272653SPeter Holm		shmp = MAP_FAILED;
155*8a272653SPeter Holm		shmid = 0;
156*8a272653SPeter Holm	}
157*8a272653SPeter Holm	cont = 0;
158*8a272653SPeter Holm
159*8a272653SPeter Holm	return (NULL);
160*8a272653SPeter Holm}
161*8a272653SPeter Holm
162*8a272653SPeter Holmstatic int
163*8a272653SPeter Holmtest(void)
164*8a272653SPeter Holm{
165*8a272653SPeter Holm	pthread_t tid[2];
166*8a272653SPeter Holm	int i, rc;
167*8a272653SPeter Holm
168*8a272653SPeter Holm	shmp = MAP_FAILED;
169*8a272653SPeter Holm
170*8a272653SPeter Holm	cont = 1;
171*8a272653SPeter Holm	if ((rc = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
172*8a272653SPeter Holm		errc(1, rc, "pthread_create()");
173*8a272653SPeter Holm	if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
174*8a272653SPeter Holm		errc(1, rc, "pthread_create()");
175*8a272653SPeter Holm
176*8a272653SPeter Holm	for (i = 0; i < 2; i++)
177*8a272653SPeter Holm		if ((rc = pthread_join(tid[i], NULL)) != 0)
178*8a272653SPeter Holm			errc(1, rc, "pthread_join(%d)", i);
179*8a272653SPeter Holm
180*8a272653SPeter Holm	return (0);
181*8a272653SPeter Holm}
182*8a272653SPeter Holm
183*8a272653SPeter Holmint
184*8a272653SPeter Holmmain(void)
185*8a272653SPeter Holm{
186*8a272653SPeter Holm	pid_t pid;
187*8a272653SPeter Holm	time_t start;
188*8a272653SPeter Holm
189*8a272653SPeter Holm	len = PAGES * PAGE_SIZE;
190*8a272653SPeter Holm	start = time(NULL);
191*8a272653SPeter Holm	while ((time(NULL) - start) < RUNTIME) {
192*8a272653SPeter Holm		if ((pid = fork()) == 0) {
193*8a272653SPeter Holm			test();
194*8a272653SPeter Holm			exit(0);	/* calls cleanup() */
195*8a272653SPeter Holm		}
196*8a272653SPeter Holm		if (waitpid(pid, NULL,0) != pid)
197*8a272653SPeter Holm			err(1, "waitpid(%d)", pid);
198*8a272653SPeter Holm	}
199*8a272653SPeter Holm
200*8a272653SPeter Holm	return(0);
201*8a272653SPeter Holm}
202