xref: /freebsd/tools/test/stress2/misc/ptrace3.sh (revision 61e21613)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 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# A regression test for a bug introduced in r269656.
30# Page fault in proc_realparent+0x70 seen.
31# Fixed in r270024.
32
33# Test scenario by Mark Johnston markj@
34
35. ../default.cfg
36
37here=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $here/$0 > ptrace3.c
40mycc -o ptrace3 -Wall -Wextra -g ptrace3.c || exit 1
41rm -f ptrace3.c
42cd $here
43
44/tmp/ptrace3
45
46rm -f /tmp/ptrace3
47exit
48EOF
49#include <sys/types.h>
50#include <sys/ptrace.h>
51#include <sys/wait.h>
52
53#include <err.h>
54#include <signal.h>
55#include <unistd.h>
56
57/*
58 * A regression test for a bug introduced in r269656. The test process p creates
59 * child processes c1 and c2 which do nothing but sleep; a third child process
60 * (c3) uses ptrace(2) to reparent c1 and c2 from p to c3. Then c3 detaches from
61 * c1 and c2, causing a crash when c1 and c2 are removed from p's now-corrupt
62 * orphan list.
63 */
64
65pid_t
66sleeper()
67{
68	pid_t p;
69
70	p = fork();
71	if (p == -1)
72		err(1, "fork");
73	else if (p == 0)
74		while (1)
75			sleep(1000000);
76	return (p);
77}
78
79void
80attach(pid_t p)
81{
82	int status;
83
84	if (ptrace(PT_ATTACH, p, 0, 0) == -1)
85		err(1, "ptrace");
86
87	if (waitpid(p, &status, 0) == -1)
88		err(1, "waitpid");
89	else if (!WIFSTOPPED(status))
90		errx(1, "failed to stop child");
91}
92
93void
94detach(pid_t p)
95{
96
97	if (ptrace(PT_DETACH, p, 0, 0) == -1)
98		err(1, "ptrace");
99}
100
101int
102main(void)
103{
104	int status;
105	pid_t c1, c2, p;
106
107	c1 = sleeper();
108	c2 = sleeper();
109
110	p = fork();
111	if (p == -1) {
112		err(1, "fork");
113	} else if (p == 0) {
114		attach(c1);
115		attach(c2);
116		detach(c1);
117		detach(c2);
118	} else {
119		if (waitpid(p, &status, 0) == -1)
120			err(1, "waitpid");
121		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
122			errx(1, "child exited abnormally");
123	}
124
125	/* Clean up. */
126	(void)kill(c1, SIGKILL);
127	(void)kill(c2, SIGKILL);
128
129	return (0);
130}
131