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
|
/* $OpenBSD: errno.c,v 1.2 2012/02/20 02:34:33 guenther Exp $ */
/* PUBLIC DOMAIN Sep 2011 <guenther@openbsd.org> */
/*
* Verify that &errno is different for each thread and is stable across
* context switches and in signal handlers
*/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "test.h"
int *main_errno, *t1_errno, *t2_errno, **handler_errno;
pthread_t main_tid, t1_tid, t2_tid;
enum state
{
START,
T1_START,
T1_SIGNAL,
T1_CHECK2,
T1_EXIT,
} state;
pthread_mutex_t m;
pthread_cond_t c;
sigset_t sigusr2;
static void
set_state(enum state new_state)
{
CHECKe(pthread_mutex_lock(&m));
ASSERT(state == new_state - 1);
state = new_state;
CHECKe(pthread_cond_signal(&c));
CHECKe(pthread_mutex_unlock(&m));
}
static void
wait_for_state(enum state new_state)
{
CHECKe(pthread_mutex_lock(&m));
while(state != new_state)
CHECKe(pthread_cond_wait(&c, &m));
CHECKe(pthread_mutex_unlock(&m));
}
/*
* Yes, pthread_self() isn't async-signal-safe in general, but it should
* be okay for the regress test here
*/
static void
act_handler(int signal)
{
ASSERT(signal == SIGUSR1);
if (handler_errno == &main_errno) {
CHECKe(write(STDOUT_FILENO, "m", 1));
ASSERT(&errno == main_errno);
ASSERTe(errno, == EXDEV);
ASSERT(pthread_equal(t1_tid, pthread_self()));
} else if (handler_errno == &t1_errno) {
CHECKe(write(STDOUT_FILENO, "\n", 1));
ASSERT(&errno == t1_errno);
ASSERTe(errno, == EXDEV);
ASSERT(pthread_equal(t1_tid, pthread_self()));
errno = ENODEV;
CHECKe(kill(getpid(), SIGUSR2));
ASSERTe(errno, == ENODEV);
} else if (handler_errno == &t2_errno) {
CHECKe(write(STDOUT_FILENO, "2", 1));
ASSERT(&errno == t2_errno);
ASSERTe(errno, == EXDEV);
ASSERT(pthread_equal(t2_tid, pthread_self()));
} else {
PANIC("unknown thread in act_handler!");
}
}
void *
tmain(void *arg)
{
t1_errno = &errno;
ASSERT(t1_errno != main_errno);
ASSERT(*t1_errno == 0);
/* verify preservation across switch */
errno = EXDEV;
wait_for_state(T1_START);
t1_tid = pthread_self();
ASSERT(pthread_equal(main_tid, t1_tid) == 0);
ASSERT(&errno == t1_errno);
ASSERTe(*t1_errno, == EXDEV);
set_state(T1_SIGNAL);
ASSERT(&errno == t1_errno);
wait_for_state(T1_CHECK2);
ASSERTe(*t1_errno, == ENODEV);
ASSERT(&errno == t1_errno);
ASSERT(pthread_equal(t1_tid, pthread_self()));
set_state(T1_EXIT);
return (NULL);
}
int
main(int argc, char **argv)
{
struct sigaction act;
int r;
pthread_mutex_init(&m, NULL);
pthread_cond_init(&c, NULL);
state = START;
main_errno = &errno;
main_tid = pthread_self();
act.sa_handler = act_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
errno = 0;
CHECKe(sigaction(SIGUSR1, &act, NULL));
ASSERT(*main_errno == 0);
ASSERT(errno == 0);
/*
* we'll use SIGUSR2 for signal state change from act_handler,
* detecting it with sigwait(), so block it now
*/
CHECKe(sigaction(SIGUSR2, &act, NULL));
sigemptyset(&sigusr2);
sigaddset(&sigusr2, SIGUSR2);
CHECKr(pthread_sigmask(SIG_BLOCK, &sigusr2, NULL));
sched_yield();
ASSERT(&errno == main_errno);
/* do something to force an error */
r = close(11);
if (r != 0) {
ASSERT(r == -1);
ASSERTe(*main_errno, == EBADF);
ASSERTe(errno, == EBADF);
}
r = write(11, "", 1);
ASSERT(r == -1);
ASSERTe(*main_errno, == EBADF);
ASSERTe(errno, == EBADF);
/* verify that a succesfull syscall doesn't change errno */
CHECKe(write(STDOUT_FILENO, "X", 1));
ASSERTe(*main_errno, == EBADF);
ASSERTe(errno, == EBADF);
ASSERT(&errno == main_errno);
CHECKr(pthread_create(&t1_tid, NULL, tmain, NULL));
ASSERTe(*main_errno, == EBADF);
ASSERT(&errno == main_errno);
ASSERT(pthread_equal(main_tid, pthread_self()));
set_state(T1_START);
ASSERTe(*main_errno, == EBADF);
ASSERT(&errno == main_errno);
wait_for_state(T1_SIGNAL);
ASSERTe(*main_errno, == EBADF);
ASSERT(&errno == main_errno);
ASSERT(pthread_equal(main_tid, pthread_self()));
handler_errno = &t1_errno;
CHECKe(pthread_kill(t1_tid, SIGUSR1));
ASSERT(&errno == main_errno);
CHECKr(sigwait(&sigusr2, &r));
ASSERTe(*main_errno, == EBADF);
ASSERT(&errno == main_errno);
ASSERT(pthread_equal(main_tid, pthread_self()));
set_state(T1_CHECK2);
wait_for_state(T1_EXIT);
CHECKe(pthread_join(t1_tid, NULL));
ASSERT(&errno == main_errno);
ASSERT(pthread_equal(main_tid, pthread_self()));
SUCCEED;
}
|