summaryrefslogtreecommitdiff
path: root/test/pthread/cancellation-points.c
blob: 5453060f7f2c16940ffa7953513c73d4358e1aae (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Make sure functions marked as cancellation points actually are.
 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_09.html#tag_02_09_05
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <features.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/msg.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <mqueue.h>
#include <poll.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>

/* take care of optional things ... */
#define STUB(func, args) static void func args { sleep(0); }
#if defined(__UCLIBC_AIO__)
# include <aio.h>
#else
STUB(aio_suspend, (void *p, int n, const void *p2))
#endif
#if defined(__UCLIBC_STROPTS__)
# include <stropts.h>
#else
STUB(getmsg, (int f, void *p, void *p2, void *p3))
STUB(getpmsg, (int f, void *p, void *p2, void *p3, void *p4))
STUB(putmsg, (int f, void *p, void *p2, void *p3))
STUB(putpmsg, (int f, void *p, void *p2, void *p3, void *p4))
#endif
#if defined(__UCLIBC__)
STUB(clock_nanosleep, (int i, int f, const void *p, void *p2))
#endif

int cnt;
bool ready;

void cancel_timeout(int sig)
{
	ready = false;
}
void cancel_thread_cleanup(void *arg)
{
	ready = false;
}

/* some funcs need some help as they wont take NULL args ... */
const struct timespec zero_sec = { .tv_sec = 0, .tv_nsec = 0 };

sem_t sem;
void help_sem_setup(void)
{
	if (sem_init(&sem, 0, 1) == -1) {
		perror("sem_init() failed");
		exit(-1);
	}
}

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex;
void help_pthread_setup(void)
{
	pthread_mutex_init(&mutex, NULL);
	pthread_mutex_lock(&mutex);
}

/* the pthread function that will call the cancellable function over and over */
#define _MAKE_CANCEL_THREAD_FUNC_EX(func, sysfunc, args, setup) \
void *cancel_thread_##func(void *arg) \
{ \
	if (pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL)) { \
		perror("unable to set cancel type to deferred; something is seriously broken"); \
		exit(-1); \
	} \
	pthread_cleanup_push(cancel_thread_cleanup, NULL); \
	setup; \
	ready = true; \
	while (ready) \
		sysfunc args; \
	pthread_cleanup_pop(1); \
	return NULL; \
}
#define MAKE_CANCEL_THREAD_FUNC_RE(func, sysfunc, args) _MAKE_CANCEL_THREAD_FUNC_EX(func, sysfunc, args, (void)0)
#define MAKE_CANCEL_THREAD_FUNC_EX(func, args, setup)   _MAKE_CANCEL_THREAD_FUNC_EX(func, func, args, setup)
#define MAKE_CANCEL_THREAD_FUNC(func, args)             _MAKE_CANCEL_THREAD_FUNC_EX(func, func, args, (void)0)

MAKE_CANCEL_THREAD_FUNC(accept, (-1, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(aio_suspend, (NULL, 0, &zero_sec))
MAKE_CANCEL_THREAD_FUNC(clock_nanosleep, (0, 0, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(close, (-1))
MAKE_CANCEL_THREAD_FUNC(connect, (-1, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(creat, ("", 0))
MAKE_CANCEL_THREAD_FUNC(fcntl, (0, F_SETLKW, NULL))
MAKE_CANCEL_THREAD_FUNC(fdatasync, (-1))
MAKE_CANCEL_THREAD_FUNC(fsync, (0))
MAKE_CANCEL_THREAD_FUNC(getmsg, (-1, NULL, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(getpmsg, (-1, NULL, NULL, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(lockf, (-1, F_TEST, 0))
MAKE_CANCEL_THREAD_FUNC(mq_receive, (0, NULL, 0, NULL))
MAKE_CANCEL_THREAD_FUNC(mq_send, (0, NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(mq_timedreceive, (0, NULL, 0, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(mq_timedsend, (0, NULL, 0, 0, NULL))
MAKE_CANCEL_THREAD_FUNC(msgrcv, (-1, NULL, 0, 0, 0))
MAKE_CANCEL_THREAD_FUNC(msgsnd, (-1, NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(msync, (NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(nanosleep, (NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(open, ("", 0))
MAKE_CANCEL_THREAD_FUNC(pause, ())
MAKE_CANCEL_THREAD_FUNC(poll, (NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(pread, (-1, NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(pselect, (0, NULL, NULL, NULL, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC_EX(pthread_cond_timedwait, (&cond, &mutex, &zero_sec), help_pthread_setup())
MAKE_CANCEL_THREAD_FUNC_EX(pthread_cond_wait, (&cond, &mutex), help_pthread_setup())
/*MAKE_CANCEL_THREAD_FUNC_EX(pthread_join, (0, NULL))*/
MAKE_CANCEL_THREAD_FUNC(pthread_testcancel, ())
MAKE_CANCEL_THREAD_FUNC(putmsg, (-1, NULL, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(putpmsg, (-1, NULL, NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(pwrite, (-1, NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(read, (-1, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(readv, (-1, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(recv, (-1, NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(recvfrom, (-1, NULL, 0, 0, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(recvmsg, (-1, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(select, (0, NULL, NULL, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC_EX(sem_timedwait, (&sem, &zero_sec), help_sem_setup())
MAKE_CANCEL_THREAD_FUNC_EX(sem_wait, (&sem), help_sem_setup())
MAKE_CANCEL_THREAD_FUNC(send, (-1, NULL, 0, 0))
MAKE_CANCEL_THREAD_FUNC(sendmsg, (-1, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(sendto, (-1, NULL, 0, 0, NULL, 0))
#ifdef __UCLIBC_SUSV4_LEGACY__
MAKE_CANCEL_THREAD_FUNC(sigpause, (0))
#endif
MAKE_CANCEL_THREAD_FUNC(sigsuspend, (NULL))
MAKE_CANCEL_THREAD_FUNC(sigtimedwait, (NULL, NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(sigwait, (NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(sigwaitinfo, (NULL, NULL))
MAKE_CANCEL_THREAD_FUNC(sleep, (0))
MAKE_CANCEL_THREAD_FUNC(system, (""))
MAKE_CANCEL_THREAD_FUNC(tcdrain, (-1))
#ifdef __UCLIBC_SUSV3_LEGACY__
MAKE_CANCEL_THREAD_FUNC(usleep, (0))
#endif
MAKE_CANCEL_THREAD_FUNC(wait, (NULL))
MAKE_CANCEL_THREAD_FUNC(waitid, (0, 0, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(waitpid, (-1, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(write, (-1, NULL, 0))
MAKE_CANCEL_THREAD_FUNC(writev, (-1, NULL, 0))

/* test a few variations that should not cancel ... */
MAKE_CANCEL_THREAD_FUNC_RE(fcntl_another, fcntl, (0, F_GETFD))

/* main test that creates thread, cancels it, etc... */
int _test_func(const char *func_name, void *(*func)(void*), const int should_cancel)
{
	int ret;
	pthread_t cancel_thread_id;

	++cnt;

	printf("testing %-30s ", func_name);

	printf(".");
	if (signal(SIGALRM, cancel_timeout) == SIG_ERR) {
		perror("unable to bind SIGALRM");
		exit(-1);
	}

	printf(".");
	ready = false;
	pthread_create(&cancel_thread_id, NULL, func, NULL);

	printf(".");
	while (!ready)
		sched_yield();

	printf(".");
	if (pthread_cancel(cancel_thread_id)) {
		perror("unable to cancel thread");
		exit(-1);
	}

	printf(".");
	alarm(5);
	while (ready)
		sched_yield();

	printf(".");
	ret = (!!!alarm(0) == should_cancel);

	if (ret)
		printf(" failed ;(\n");
	else
		printf(" OK!\n");

	return ret;
}
#define TEST_FUNC(f)    _test_func(#f, cancel_thread_##f, 1)
#define TEST_FUNC_RE(f) _test_func(#f, cancel_thread_##f, 0)

int main(int argc, char *argv[])
{
	int ret = 0;
	setbuf(stdout, NULL);
	cnt = 0;

	ret += TEST_FUNC(accept);
	ret += TEST_FUNC(aio_suspend);
	ret += TEST_FUNC(clock_nanosleep);
	ret += TEST_FUNC(close);
	ret += TEST_FUNC(connect);
	ret += TEST_FUNC(creat);
	ret += TEST_FUNC(fcntl);
	ret += TEST_FUNC(fdatasync);
	ret += TEST_FUNC(fsync);
	ret += TEST_FUNC(getmsg);
	ret += TEST_FUNC(getpmsg);
	ret += TEST_FUNC(lockf);
	ret += TEST_FUNC(mq_receive);
	ret += TEST_FUNC(mq_send);
	ret += TEST_FUNC(mq_timedreceive);
	ret += TEST_FUNC(mq_timedsend);
	ret += TEST_FUNC(msgrcv);
	ret += TEST_FUNC(msgsnd);
	ret += TEST_FUNC(msync);
	ret += TEST_FUNC(nanosleep);
	ret += TEST_FUNC(open);
	ret += TEST_FUNC(pause);
	ret += TEST_FUNC(poll);
	ret += TEST_FUNC(pread);
	ret += TEST_FUNC(pselect);
	ret += TEST_FUNC(pthread_cond_timedwait);
	ret += TEST_FUNC(pthread_cond_wait);
	/*ret += TEST_FUNC(pthread_join);*/
	ret += TEST_FUNC(pthread_testcancel);
	ret += TEST_FUNC(putmsg);
	ret += TEST_FUNC(putpmsg);
	ret += TEST_FUNC(pwrite);
	ret += TEST_FUNC(read);
	ret += TEST_FUNC(readv);
	ret += TEST_FUNC(recv);
	ret += TEST_FUNC(recvfrom);
	ret += TEST_FUNC(recvmsg);
	ret += TEST_FUNC(select);
	ret += TEST_FUNC(sem_timedwait);
	ret += TEST_FUNC(sem_wait);
	ret += TEST_FUNC(send);
	ret += TEST_FUNC(sendmsg);
	ret += TEST_FUNC(sendto);
	ret += TEST_FUNC(sigpause);
	ret += TEST_FUNC(sigsuspend);
	ret += TEST_FUNC(sigtimedwait);
	ret += TEST_FUNC(sigwait);
	ret += TEST_FUNC(sigwaitinfo);
	ret += TEST_FUNC(sleep);
	ret += TEST_FUNC(system);
	ret += TEST_FUNC(tcdrain);
#ifdef __UCLIBC_SUSV3_LEGACY__
	ret += TEST_FUNC(usleep);
#endif
	ret += TEST_FUNC(wait);
	ret += TEST_FUNC(waitid);
	ret += TEST_FUNC(waitpid);
	ret += TEST_FUNC(write);
	ret += TEST_FUNC(writev);

	ret += TEST_FUNC_RE(fcntl_another);

	if (ret)
		printf("!!! %i / %i tests failed\n", ret, cnt);

	return ret;
}