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