1#!/bin/sh 2 3# 4# Copyright (c) 2009 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# $FreeBSD$ 29# 30 31# Regression test for panic in semexit_myhook 32# Test scenario by kib@ 33 34. ../default.cfg 35 36odir=`pwd` 37 38cd /tmp 39sed '1,/^EOF/d' < $odir/$0 > sem.c 40cc -o sem -Wall sem.c 41rm -f sem.c 42 43cd $RUNDIR/.. 44for i in `jot 5`; do 45 /tmp/sem& 46done 47for i in `jot 5`; do 48 wait 49done 50 51rm -f /tmp/sem 52 53exit 54EOF 55 56#include <stdio.h> 57#include <stdlib.h> 58#include <unistd.h> 59#include <sys/types.h> 60#include <sys/param.h> 61#include <sys/ipc.h> 62#include <sys/msg.h> 63#include <sys/shm.h> 64#include <sys/sem.h> 65#include <signal.h> 66#include <sys/wait.h> 67#include <sched.h> 68#include <errno.h> 69#include <err.h> 70 71int semid = -1; 72key_t semkey; 73struct sembuf sop[2]; 74 75size_t pgsize; 76pid_t pid; 77 78void 79random_work(void) 80{ 81 int i, n; 82 83 n = (arc4random() % 5000) + 200; 84 for (i = 0; i < n; i++) 85 (void) getpid(); 86} 87 88int 89main() 90{ 91 int i, j, seed, status; 92 93 seed = getpid(); 94 semkey = ftok("/", seed); 95 96 for (i = 0; i < 5000; i++) { 97 98 pid = fork(); 99 if (pid == -1) { 100 perror("fork"); 101 exit(2); 102 } 103 104 if (pid == 0) { /* child */ 105 j = 0; 106 /* get sem */ 107 do { 108 sched_yield(); 109 semid = semget(semkey, 0, 0); 110 } while (semid == -1 && j++ < 10000); 111 if (semid == -1) 112 exit(1); 113 114 /* 115 * Attempt to acquire the semaphore. 116 */ 117 sop[0].sem_num = 0; 118 sop[0].sem_op = -1; 119 sop[0].sem_flg = SEM_UNDO; 120 121 semop(semid, sop, 1); 122 random_work(); 123 _exit(0); 124 125 } else { /* parent */ 126 /* create sem */ 127 if ((semid = semget(semkey, 1, IPC_CREAT | 010640)) == -1) 128 err(1, "semget (%s:%d)", __FILE__, __LINE__); 129 usleep(2000); 130 131 sop[0].sem_num = 0; 132 sop[0].sem_op = 1; 133 sop[0].sem_flg = 0; 134 if (semop(semid, sop, 1) == -1) 135 warn("init: semop (%s:%d)", __FILE__, __LINE__); 136 137 random_work(); 138 if (semctl(semid, 0, IPC_RMID, 0) == -1 && errno != EINVAL) 139 warn("shmctl IPC_RMID (%s:%d)", __FILE__, __LINE__); 140 141 } 142 waitpid(pid, &status, 0); 143 } 144 return (0); 145} 146