xref: /freebsd/tools/test/stress2/misc/vfork.sh (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
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# Regression test for r246484.
30
31# "panic: failed to set signal flags for ast p ... fl 4" seen.
32# Fixed in r302999.
33
34# Test scenario updated after commit:
35# ecc662c749b1 - main - PT_ATTACH: do not interrupt interruptible sleeps
36
37. ../default.cfg
38
39cd /tmp
40cat > vfork1.c <<- EOF
41#include <err.h>
42#include <stdio.h>
43#include <unistd.h>
44
45int
46main(void)
47{
48	pid_t pid;
49
50	fprintf(stderr, "%d\n", getpid());
51	if ((pid = vfork()) == 0) {
52		sleep(30);
53		_exit(0);
54	}
55	if (pid == -1)
56		err(1, "vfork");
57}
58EOF
59mycc -o vfork1 -Wall -Wextra -g vfork1.c || exit 1
60rm vfork1.c
61
62cat > vfork2.c <<- EOF
63#include <sys/types.h>
64#include <sys/ptrace.h>
65#include <sys/resource.h>
66#include <sys/time.h>
67#include <sys/wait.h>
68#include <err.h>
69#include <errno.h>
70#include <signal.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <strings.h>
74#include <unistd.h>
75
76int
77main(int argc, char *argv[])
78{
79	pid_t pid, rpid;
80	struct rusage ru;
81	int status;
82
83	if (argc != 2)
84		errx(1, "Usage: %s <pid>", argv[0]);
85	pid = atoi(argv[1]);
86	status = 0;
87
88	if (pid == -1)
89		err(1, "fork()");
90	if (ptrace(PT_ATTACH, pid, NULL, 0) == -1)
91		err(1, "ptrace(%d) attach", pid);
92	if (wait(NULL) == -1)
93		err(1, "wait");
94	bzero(&ru, sizeof(ru));
95	usleep(2000);
96	if ((rpid = wait4(-1, &status, WNOHANG, &ru)) == -1) {
97			err(0, "OK wait4");
98	}
99	if (rpid == 0) {
100		if (ptrace(PT_DETACH, pid, NULL, 0) == -1)
101			err(1, "ptrace(%d) detach", pid);
102	} else {
103		fprintf(stderr, "FAIL Got unexpected rusage.\n");
104		if (ru.ru_utime.tv_sec != 0)
105			fprintf(stderr, "FAIL tv_sec\n");
106	}
107}
108EOF
109mycc -o vfork2 -Wall -Wextra -g vfork2.c || exit 1
110rm vfork2.c
111
112./vfork1 &
113sleep .2
114childpid=`ps -lx | grep -v grep | grep vfork1 |
115    tail -1 | grep nanslp | awk '{print $2}'`
116# Seen before fix:
117# failed to set signal flags properly for ast()
118./vfork2 $childpid
119s=$?
120
121pkill vfork1
122rm -f vfork1 vfork2
123exit $s
124