aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/ptrace7.sh
blob: c6c810b60d012413cbf7418371a7262c7b025c05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/sh

#
# Copyright (c) 2016 EMC Corp.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# It seems to be possible for ptrace(PT_ATTACH) to race with the delivery
# of a signal to the same process.

# $ while ./pt.sh; do date; done
# 15. juli 2016 kl. 05.59.05 CEST
# 15. juli 2016 kl. 06.03.07 CEST
#  UID   PID  PPID CPU PRI NI   VSZ  RSS MWCHAN STAT TT     TIME COMMAND
# 1001   863   862   0  44  0 13880 5268 wait   Is    0  0:00,13 -bash (bash)
# 1001 21053   863   0  52  0 13188 3088 wait   I+    0  0:00,01 /bin/sh ./pt.sh
# 1001 21096 21053   0  52  0 10544 2308 wait   I+    0  0:00,00 /tmp/pt
# 1001 21103 21096   0  20  0 12852 2456 wait   S+    0  0:00,00 pt: main (pt)
# 1001 21103 21096   0  35  0 12852 2456 wait   I+    0  0:55,30 pt: main (pt)
# 1001 21104 21096   0  72  0     0    0 -      Z+    0  0:00,00 <defunct>
# 1001 21105 21096   0  72  0     0    0 -      Z+    0  0:00,00 <defunct>
# 1001 21116 21103   0 103  0 10544 2336 -      RX+   0  4:22,41 pt: spinner (pt)
# 1001 37711 21103   0  20  0 21200 2960 -      R+    0  0:00,00 ps -Hl
# 1001   890   879   0  20  0 22184 6196 select Ss+   1  1:21,86 top -s 1
# 1001 85222 85221   0  21  0 13880 5276 ttyin  Is+   2  0:00,23 -bash (bash)
# SIGALRM state 2, pid 21116
# $

# Fixed by r303423.

. ../default.cfg

dir=/tmp
odir=`pwd`
cd $dir
sed '1,/^EOF/d' < $odir/$0 > $dir/ptrace7.c
mycc -o ptrace7 -Wall -Wextra -O0 -g ptrace7.c -pthread || exit 1
rm -f ptrace7.c
cd $odir

/tmp/ptrace7
s=$?

while pgrep -q swap; do
	pkill -9 swap
done
rm -rf /tmp/ptrace7
exit $s

EOF
#include <sys/param.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#ifdef __FreeBSD__
#include <pthread_np.h>
#define	__NP__
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

static pid_t pid;
static pthread_barrier_t barr;
static int cont;
static int state;

#define PARALLEL 8
#define RUNTIME (4 * 60)

static void
ahandler(int i __unused)
{
	system("ps -Hl");
	fprintf(stderr, "SIGALRM state %d, pid %d\n", state, pid);
	exit(1);
}

static void
handler(int i __unused)
{
}

static void *
t1(void *data __unused)
{
	int status;

#ifdef __NP__
	pthread_set_name_np(pthread_self(), __func__);
#endif
	while (cont == 1) {
		state = 1;
		if (ptrace(PT_ATTACH, pid, 0, 0) == -1)
			err(1, "ptrace(%d)", pid);

		state = 2;
		if (waitpid(pid, &status, 0) == -1)
			err(1, "waitpid");
		else if (!WIFSTOPPED(status))
			errx(1, "failed to stop child");
		state = 3;
		if (ptrace(PT_DETACH, pid, 0, 0) == -1)
			err(1, "ptrace");
		state = 4;
		usleep(arc4random() % 100 + 50);
	}

	return (NULL);
}

static void *
t2(void *data __unused)
{
#ifdef __NP__
	pthread_set_name_np(pthread_self(), __func__);
#endif
	while (cont == 1) {
		if (kill(pid, SIGHUP) == -1)
			err(1, "kill");
		usleep(arc4random() % 100 + 50);
	}

	return (NULL);
}

static void
test(void)
{
	pthread_t tid[2];
	struct sigaction sa;
	int r, status;

	r = pthread_barrier_wait(&barr);
	if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
	    errc(1, r, "pthread_barrier_wait");

	sa.sa_handler = ahandler;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	if (sigaction(SIGALRM, &sa, NULL) == -1)
		err(1, "sigaction");
	alarm(RUNTIME + 60);

	sa.sa_handler = handler;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	if (sigaction(SIGHUP, &sa, NULL) == -1)
		err(1, "sigaction");

	setproctitle("%s", "spinner");
	if ((pid = fork()) == 0) {
		for(;;)
			getuid();

		_exit(0);
	}

	cont = 1;
	if ((r = pthread_create(&tid[0], NULL, t1, NULL)) == -1)
		errc(1, r, "pthread_create");
	if ((r = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
		errc(1, r, "pthread_create");

	setproctitle("%s", "main");
	sleep(RUNTIME);

	cont = 0;
	if ((r = pthread_join(tid[0], NULL)) == -1)
			errc(1, r, "pthread_join");
	if ((r = pthread_join(tid[1], NULL)) == -1)
			errc(1, r, "pthread_join");
	if (kill(pid, SIGKILL) != 0)
		err(1, "kill(%d)", pid);
	if (waitpid(pid, &status, 0) == -1)
		err(1, "waitpid(%d)", pid);

	exit(0);
}

int
main(void)
{
	pthread_barrierattr_t attr;
	int e, i, pids[PARALLEL], r, status;

	if ((r = pthread_barrierattr_init(&attr)) != 0)
		errc(1, r, "pthread_barrierattr_init");
	if ((r = pthread_barrierattr_setpshared(&attr,
	    PTHREAD_PROCESS_SHARED)) != 0)
		errc(1, r, "pthread_barrierattr_setpshared");
	if ((r = pthread_barrier_init(&barr, &attr, PARALLEL)) != 0)
		errc(1, r, "pthread_barrier_init");

	e = 0;
	for (i = 0; i < PARALLEL; i++) {
		if ((pids[i] = fork()) == 0)
			test();
	}
	for (i = 0; i < PARALLEL; i++) {
		if (waitpid(pids[i], &status, 0) == -1)
			err(1, "waitpid%d)", pids[i]);
		e += status == 0 ? 0 : 1;
	}

	if ((r = pthread_barrier_destroy(&barr)) > 0)
		errc(1, r, "pthread_barrier_destroy");

	return (e);
}