1*8a272653SPeter Holm#!/bin/sh 2*8a272653SPeter Holm 3*8a272653SPeter Holm# 4*8a272653SPeter Holm# Copyright (c) 2013 EMC Corp. 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 kern/150138: signal sent to stopped, traced process 30*8a272653SPeter Holm# not immediately handled on continue. 31*8a272653SPeter Holm# Fixed in r212047. 32*8a272653SPeter Holm 33*8a272653SPeter Holm# Test scenario by Dan McNulty <dkmcnulty gmail.com> 34*8a272653SPeter Holm 35*8a272653SPeter Holm. ../default.cfg 36*8a272653SPeter Holm 37*8a272653SPeter Holmhere=`pwd` 38*8a272653SPeter Holmwd=/tmp/signal.dir 39*8a272653SPeter Holmrm -rf $wd 40*8a272653SPeter Holmmkdir -p $wd 41*8a272653SPeter Holmcd $wd 42*8a272653SPeter Holmcat > waitthread.c <<EOF 43*8a272653SPeter Holm#include <unistd.h> 44*8a272653SPeter Holm#include <stdlib.h> 45*8a272653SPeter Holm#include <pthread.h> 46*8a272653SPeter Holm#include <stdio.h> 47*8a272653SPeter Holm#include <assert.h> 48*8a272653SPeter Holm#include <signal.h> 49*8a272653SPeter Holm#include <time.h> 50*8a272653SPeter Holm#include <sys/syscall.h> 51*8a272653SPeter Holm#include <string.h> 52*8a272653SPeter Holm#include <fcntl.h> 53*8a272653SPeter Holm#include <sys/types.h> 54*8a272653SPeter Holm#include <sys/stat.h> 55*8a272653SPeter Holm#include <errno.h> 56*8a272653SPeter Holm 57*8a272653SPeter Holmstruct threadArg { 58*8a272653SPeter Holm int id; 59*8a272653SPeter Holm pthread_mutex_t *mutex; 60*8a272653SPeter Holm}; 61*8a272653SPeter Holm 62*8a272653SPeter Holmvoid *entry(void *arg) { 63*8a272653SPeter Holm struct threadArg *thisArg = (struct threadArg *)arg; 64*8a272653SPeter Holm 65*8a272653SPeter Holm long lwp_id = thisArg->id; 66*8a272653SPeter Holm if( syscall(SYS_thr_self, &lwp_id) ) { 67*8a272653SPeter Holm perror("syscall"); 68*8a272653SPeter Holm } 69*8a272653SPeter Holm 70*8a272653SPeter Holm printf("%ld waiting on lock\n", lwp_id); 71*8a272653SPeter Holm 72*8a272653SPeter Holm if( pthread_mutex_lock(thisArg->mutex) != 0 ) { 73*8a272653SPeter Holm perror("pthread_mutex_lock"); 74*8a272653SPeter Holm return NULL; 75*8a272653SPeter Holm } 76*8a272653SPeter Holm 77*8a272653SPeter Holm printf("%ld obtained lock\n", lwp_id); 78*8a272653SPeter Holm 79*8a272653SPeter Holm if( pthread_mutex_unlock(thisArg->mutex) != 0 ) { 80*8a272653SPeter Holm perror("pthread_mutex_unlock"); 81*8a272653SPeter Holm return NULL; 82*8a272653SPeter Holm } 83*8a272653SPeter Holm 84*8a272653SPeter Holm printf("%ld released lock\n", lwp_id); 85*8a272653SPeter Holm 86*8a272653SPeter Holm return NULL; 87*8a272653SPeter Holm} 88*8a272653SPeter Holm 89*8a272653SPeter Holmint main(int argc, char **argv) { 90*8a272653SPeter Holm if( 2 != argc ) { 91*8a272653SPeter Holm printf("Usage: %s <num. of threads>\n", argv[0]); 92*8a272653SPeter Holm return EXIT_FAILURE; 93*8a272653SPeter Holm } 94*8a272653SPeter Holm 95*8a272653SPeter Holm printf("%d\n", getpid()); 96*8a272653SPeter Holm 97*8a272653SPeter Holm int numThreads; 98*8a272653SPeter Holm sscanf(argv[1], "%d", &numThreads); 99*8a272653SPeter Holm if( numThreads < 1 ) numThreads = 1; 100*8a272653SPeter Holm 101*8a272653SPeter Holm pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t)*numThreads); 102*8a272653SPeter Holm 103*8a272653SPeter Holm pthread_mutex_t *mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); 104*8a272653SPeter Holm 105*8a272653SPeter Holm if( pthread_mutex_init(mutex, NULL) != 0 ) { 106*8a272653SPeter Holm perror("pthread_mutex_init"); 107*8a272653SPeter Holm return EXIT_FAILURE; 108*8a272653SPeter Holm } 109*8a272653SPeter Holm 110*8a272653SPeter Holm if( pthread_mutex_lock(mutex) != 0 ) { 111*8a272653SPeter Holm perror("pthread_mutex_lock"); 112*8a272653SPeter Holm return EXIT_FAILURE; 113*8a272653SPeter Holm } 114*8a272653SPeter Holm 115*8a272653SPeter Holm int i; 116*8a272653SPeter Holm for(i = 0; i < numThreads; ++i) { 117*8a272653SPeter Holm struct threadArg *arg = (struct threadArg *)malloc(sizeof(struct threadArg)); 118*8a272653SPeter Holm arg->id = i; 119*8a272653SPeter Holm arg->mutex = mutex; 120*8a272653SPeter Holm assert( !pthread_create(&threads[i], NULL, &entry, (void *)arg) ); 121*8a272653SPeter Holm } 122*8a272653SPeter Holm 123*8a272653SPeter Holm // Wait on the named pipe 124*8a272653SPeter Holm unlink("/tmp/waitthread"); 125*8a272653SPeter Holm if( mkfifo("/tmp/waitthread", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) ) { 126*8a272653SPeter Holm perror("mkfifo"); 127*8a272653SPeter Holm return EXIT_FAILURE; 128*8a272653SPeter Holm } 129*8a272653SPeter Holm 130*8a272653SPeter Holm FILE *fifo; 131*8a272653SPeter Holm do { 132*8a272653SPeter Holm if( (fifo = fopen("/tmp/waitthread", "r")) == NULL ) { 133*8a272653SPeter Holm if( errno == EINTR ) continue; 134*8a272653SPeter Holm 135*8a272653SPeter Holm perror("fopen"); 136*8a272653SPeter Holm return EXIT_FAILURE; 137*8a272653SPeter Holm } 138*8a272653SPeter Holm break; 139*8a272653SPeter Holm }while(1); 140*8a272653SPeter Holm 141*8a272653SPeter Holm unsigned char byte; 142*8a272653SPeter Holm if( fread(&byte, sizeof(unsigned char), 1, fifo) != 1 ) { 143*8a272653SPeter Holm perror("fread"); 144*8a272653SPeter Holm } 145*8a272653SPeter Holm 146*8a272653SPeter Holm fclose(fifo); 147*8a272653SPeter Holm 148*8a272653SPeter Holm unlink("/tmp/waitthread"); 149*8a272653SPeter Holm 150*8a272653SPeter Holm printf("Received notification\n"); 151*8a272653SPeter Holm 152*8a272653SPeter Holm if( pthread_mutex_unlock(mutex) != 0 ) { 153*8a272653SPeter Holm perror("pthread_mutex_unlock"); 154*8a272653SPeter Holm return EXIT_FAILURE; 155*8a272653SPeter Holm } 156*8a272653SPeter Holm 157*8a272653SPeter Holm printf("Unlocked mutex, joining\n"); 158*8a272653SPeter Holm 159*8a272653SPeter Holm for(i = 0; i < numThreads; ++i ) { 160*8a272653SPeter Holm assert( !pthread_join(threads[i], NULL) ); 161*8a272653SPeter Holm } 162*8a272653SPeter Holm 163*8a272653SPeter Holm return EXIT_SUCCESS; 164*8a272653SPeter Holm} 165*8a272653SPeter HolmEOF 166*8a272653SPeter Holmcat > tkill.c <<EOF 167*8a272653SPeter Holm#include <sys/syscall.h> 168*8a272653SPeter Holm#include <unistd.h> 169*8a272653SPeter Holm#include <stdio.h> 170*8a272653SPeter Holm#include <errno.h> 171*8a272653SPeter Holm#include <stdlib.h> 172*8a272653SPeter Holm#include <signal.h> 173*8a272653SPeter Holm 174*8a272653SPeter Holmint main(int argc, char *argv[]) { 175*8a272653SPeter Holm if( argc != 4 ) { 176*8a272653SPeter Holm printf("Usage: %s <pid> <lwp_id> <sig>\n", argv[0]); 177*8a272653SPeter Holm return EXIT_FAILURE; 178*8a272653SPeter Holm } 179*8a272653SPeter Holm 180*8a272653SPeter Holm pid_t pid; 181*8a272653SPeter Holm sscanf(argv[1], "%d", &pid); 182*8a272653SPeter Holm 183*8a272653SPeter Holm long id; 184*8a272653SPeter Holm sscanf(argv[2], "%ld", &id); 185*8a272653SPeter Holm 186*8a272653SPeter Holm int sig; 187*8a272653SPeter Holm sscanf(argv[3], "%d", &sig); 188*8a272653SPeter Holm 189*8a272653SPeter Holm if( syscall(SYS_thr_kill2, pid, id, sig) ) { 190*8a272653SPeter Holm perror("syscall"); 191*8a272653SPeter Holm } 192*8a272653SPeter Holm 193*8a272653SPeter Holm return EXIT_SUCCESS; 194*8a272653SPeter Holm} 195*8a272653SPeter HolmEOF 196*8a272653SPeter Holmmycc -o waitthread -Wall -Wextra waitthread.c -lpthread || exit 197*8a272653SPeter Holmmycc -o tkill -Wall -Wextra tkill.c || exit 198*8a272653SPeter Holmrm -f waitthread.c tkill.c 199*8a272653SPeter Holm 200*8a272653SPeter Holmrm -f gdbfifo gdbout pstat /tmp/waitthread 201*8a272653SPeter Holmpkill -9 waitthread 202*8a272653SPeter Holmpgrep -q waitthread && { pgrep waitthread | xargs ps -lHp; exit 1; } 203*8a272653SPeter Holmmkfifo gdbfifo 204*8a272653SPeter Holmtrap "rm -f gdbfifo" EXIT INT 205*8a272653SPeter Holmsleep 300 > gdbfifo & # Keep the fifo open 206*8a272653SPeter Holmsleep .5 207*8a272653SPeter Holmfifopid=$! 208*8a272653SPeter Holm 209*8a272653SPeter Holm(trap - INT; gdb ./waitthread < gdbfifo > gdbout 2>&1) & 210*8a272653SPeter Holmsleep .8 211*8a272653SPeter Holmgdbpid=$! 212*8a272653SPeter Holmecho "set args 8" > gdbfifo 213*8a272653SPeter Holmecho "run" > gdbfifo 214*8a272653SPeter Holmsleep .8 215*8a272653SPeter Holm 216*8a272653SPeter Holmfor i in `jot 5`; do 217*8a272653SPeter Holm pid=`ps -x | grep -v grep | grep "waitthread 8" | 218*8a272653SPeter Holm sed 's/^ *//;s/ .*//'` 219*8a272653SPeter Holm [ -n "$pid" ] && break 220*8a272653SPeter Holm sleep 1 221*8a272653SPeter Holmdone 222*8a272653SPeter Holms=0 223*8a272653SPeter Holmif [ -n "$pid" ]; then 224*8a272653SPeter Holm procstat -t $pid > pstat 225*8a272653SPeter Holm 226*8a272653SPeter Holm t1=`grep fifo pstat | awk '{print $2}'` 227*8a272653SPeter Holm t2=`grep umtxn pstat | awk '{print $2}' | tail -1` 228*8a272653SPeter Holm 229*8a272653SPeter Holm set -e 230*8a272653SPeter Holm ./tkill $pid $t1 5 # SIGTRAP 231*8a272653SPeter Holm ./tkill $pid $t2 2 # SIGINT 232*8a272653SPeter Holm set +e 233*8a272653SPeter Holm 234*8a272653SPeter Holm echo "c" > gdbfifo 235*8a272653SPeter Holm echo "quit" > gdbfifo 236*8a272653SPeter Holm sleep 1 237*8a272653SPeter Holm grep -q "signal SIGINT" gdbout || { echo FAIL; cat gdbout; s=1; } 238*8a272653SPeter Holmelse 239*8a272653SPeter Holm echo "Did not find pid for test program waitthread" 240*8a272653SPeter Holm s=2 241*8a272653SPeter Holmfi 242*8a272653SPeter Holm 243*8a272653SPeter Holmkill -9 $fifopid $gdbpid > /dev/null 2>&1 244*8a272653SPeter Holmpgrep -q waitthread && pkill -9 waitthread 245*8a272653SPeter Holmcd $here 246*8a272653SPeter Holmrm -rf $wd /tmp/waitthread 247*8a272653SPeter Holmexit $s 248