1#!/bin/sh 2 3# 4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5# 6# Copyright (c) 2022 Peter Holm <pho@FreeBSD.org> 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions 10# are met: 11# 1. Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# 2. Redistributions in binary form must reproduce the above copyright 14# notice, this list of conditions and the following disclaimer in the 15# documentation and/or other materials provided with the distribution. 16# 17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27# SUCH DAMAGE. 28# 29 30# procctl(2) threaded test 31 32# panic: exiting process is stopped" seen: 33# https://people.freebsd.org/~pho/stress/log/log0285.txt 34 35prog=`basename ${0%.sh}` 36cat > /tmp/$prog.c <<EOF 37#include <sys/param.h> 38#include <sys/mman.h> 39#include <sys/procctl.h> 40 41#include <err.h> 42#include <pthread.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <time.h> 46#include <unistd.h> 47 48static volatile u_int *share; 49 50#define PARALLEL 25 51#define RUNTIME 120 52 53static void * 54tr(void *arg __unused) 55{ 56 for (;;) 57 pause(); 58 59 return (NULL); 60} 61 62static void 63test(void) { 64 pthread_t thr; 65 struct procctl_reaper_kill killemall; 66 pid_t pid; 67 time_t start; 68 int data[20], e, n, m; 69 70 n = m = 0; 71 if (procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == -1) 72 err(EXIT_FAILURE, "Fail to acquire the reaper"); 73 start = time(NULL); 74 while (time(NULL) - start < RUNTIME) { 75 m++; 76 share[0] = 0; 77 if ((pid = fork()) == 0) { 78 e = pthread_create(&thr, NULL, tr, NULL); 79 if (e != 0) 80 errc(1, e, "pthread_create"); 81 share[0] = 1; 82 setproctitle("child"); 83 usleep(arc4random() % 200); 84 _exit(0); 85 } 86 arc4random_buf(data, sizeof(data)); 87 while (share[0] == 0) 88 usleep(10); 89 killemall.rk_sig = SIGTERM; 90 killemall.rk_flags = 0; 91 if (procctl(P_PID, getpid(), PROC_REAP_KILL, 92 &killemall) == 0) 93 n++; 94 if (waitpid(pid, NULL, 0) != pid) 95 err(1, "waitpid()"); 96 } 97#if defined(DEBUG) 98 fprintf(stderr, "n = %d out of %d\n", n, m); 99#endif 100 _exit(0); 101} 102 103int 104main(void) { 105 pid_t pids[PARALLEL]; 106 size_t len; 107 int i; 108 109 len = PAGE_SIZE; 110 if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, 111 MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) 112 err(1, "mmap"); 113 for (i = 0; i < PARALLEL; i++) { 114 if ((pids[i] = fork()) == 0) 115 test(); 116 } 117 for (i = 0; i < PARALLEL; i++) 118 if (waitpid(pids[i], NULL, 0) != pids[i]) 119 err(1, "waitpid()"); 120} 121EOF 122cc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c -lpthread || exit 1 123rm /tmp/$prog.c 124 125here=`pwd` 126cd /tmp 127./$prog; s=$? 128cd $here 129 130rm /tmp/$prog 131exit $s 132