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
|
/* $OpenBSD: cancel_wait.c,v 1.2 2015/09/14 08:36:32 guenther Exp $ */
/* PUBLIC DOMAIN <marc@snafu.org> */
/*
* Check that a thread waiting in wait/waitpid/wait3/wait4 can be
* cancelled.
*/
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <err.h>
#include <pthread.h>
#include <unistd.h>
#include "test.h"
pid_t child;
int status;
static void *
wait_thread(void *arg)
{
wait(&status);
return (arg);
}
static void *
waitpid_thread(void *arg)
{
waitpid(child, &status, 0);
return (arg);
}
static void *
wait3_thread(void *arg)
{
wait3(&status, 0, NULL);
return (arg);
}
static void *
wait4_thread(void *arg)
{
wait4(child, &status, 0, NULL);
return (arg);
}
int
main(int argc, char *argv[])
{
pthread_t thread;
void *ret = NULL;
child = fork();
if (child == -1)
err(1, "fork");
if (child == 0) {
sleep(1000000);
_exit(0);
}
status = 42;
printf("trying wait\n");
CHECKr(pthread_create(&thread, NULL, wait_thread, NULL));
sleep(1);
CHECKr(pthread_cancel(thread));
CHECKr(pthread_join(thread, &ret));
ASSERT(ret == PTHREAD_CANCELED);
ASSERT(status == 42);
printf("trying waitpid\n");
CHECKr(pthread_create(&thread, NULL, waitpid_thread, NULL));
sleep(1);
CHECKr(pthread_cancel(thread));
CHECKr(pthread_join(thread, &ret));
ASSERT(ret == PTHREAD_CANCELED);
ASSERT(status == 42);
printf("trying wait3\n");
CHECKr(pthread_create(&thread, NULL, wait3_thread, NULL));
sleep(1);
CHECKr(pthread_cancel(thread));
CHECKr(pthread_join(thread, &ret));
ASSERT(ret == PTHREAD_CANCELED);
ASSERT(status == 42);
printf("trying wait4\n");
CHECKr(pthread_create(&thread, NULL, wait4_thread, NULL));
sleep(1);
CHECKr(pthread_cancel(thread));
CHECKr(pthread_join(thread, &ret));
ASSERT(ret == PTHREAD_CANCELED);
ASSERT(status == 42);
kill(child, SIGKILL);
CHECKr(pthread_create(&thread, NULL, wait4_thread, NULL));
sleep(1);
CHECKr(pthread_join(thread, &ret));
ASSERT(ret == NULL);
ASSERT(WIFSIGNALED(status));
ASSERT(WTERMSIG(status) == 9);
SUCCEED;
}
|