xref: /freebsd/tools/test/stress2/misc/ptrace9.sh (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2016 Mark Johnston <markj@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# ptrace(2) test scenario by Mark Johnston
31# https://people.freebsd.org/~markj/ptrace_stop.c
32# Fixed by r303423.
33
34. ../default.cfg
35
36cd /tmp
37cat > ptrace9.c <<EOF
38#include <sys/types.h>
39#include <sys/ptrace.h>
40#include <sys/wait.h>
41
42#include <err.h>
43#include <signal.h>
44#include <stdio.h>
45#include <unistd.h>
46
47static void
48sigalrm(int sig __unused)
49{
50	_exit(0);
51}
52
53static void
54sighup(int sig __unused)
55{
56}
57
58int
59main(void)
60{
61	struct sigaction act;
62	pid_t pid;
63	int e, status;
64
65	signal(SIGALRM, sigalrm);
66	e = 1;
67	pid = fork();
68	if (pid < 0)
69		err(1, "fork");
70	if (pid == 0) {
71		act.sa_handler = sighup;
72		act.sa_flags = 0;
73		sigemptyset(&act.sa_mask);
74		if (sigaction(SIGHUP, &act, NULL) != 0)
75			err(1, "sigaction");
76		alarm(5);
77		while (1) {
78			sleep(1);
79		}
80	} else {
81		alarm(5);
82		sleep(1); /* give the child a chance to call sigaction */
83
84		if (kill(pid, SIGSTOP) != 0)
85			err(1, "kill(SIGSTOP)");
86
87		printf("waiting for child to stop...\n");
88		if (waitpid(pid, &status, WUNTRACED) != pid)
89			err(1, "waitpid");
90		if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
91			errx(1, "unexpected status %d after SIGSTOP", status);
92
93		if (kill(pid, SIGHUP) != 0)
94			err(1, "kill(SIGHUP)");
95
96		if (ptrace(PT_ATTACH, pid, NULL, 0) != 0)
97			err(1, "ptrace(PT_ATTACH)");
98		if (waitpid(pid, &status, WUNTRACED) != pid)
99			err(1, "waitpid");
100		if (!WIFSTOPPED(status))
101			errx(1, "unexpected status %d after PT_ATTACH", status);
102		printf("stopping signal is %d\n", WSTOPSIG(status));
103		if (ptrace(PT_DETACH, pid, NULL, 0) != 0)
104			err(1, "ptrace(PT_DETACH)");
105
106		/* if ptrace works as expected, we'll block here */
107		printf("waiting on child...\n"); fflush(stdout);
108		if (waitpid(pid, &status, WUNTRACED) != pid)
109			err(1, "waitpid");
110		if (!WIFSTOPPED(status))
111			errx(1, "unexpected status %d after PT_DETACH", status);
112		printf("child is stopped after detach (sig %d)\n",
113		    WSTOPSIG(status)); fflush(stdout);
114		e = 1;
115	}
116
117	return (e);
118}
119EOF
120
121mycc -o ptrace9 -Wall -Wextra -O2 -g ptrace9.c || exit 1
122rm ptrace9.c
123
124echo "Expect:
125	waiting for child to stop...
126	stopping signal is 17
127	waiting on child..."
128./ptrace9
129s=$?
130
131pkill -9 ptrace9
132rm -f ptrace9
133exit $s
134