1 /*	$OpenBSD: read.c,v 1.1 2011/09/13 23:50:17 fgsch Exp $	*/
2 /*
3  * Federico G. Schwindt <fgsch@openbsd.org>, 2011. Public Domain.
4  */
5 #include <sys/types.h>
6 #include <pthread.h>
7 #include <signal.h>
8 #include <unistd.h>
9 #include "test.h"
10 
11 volatile sig_atomic_t hits = 0;
12 
13 void
14 handler(int sig)
15 {
16 	hits++;
17 }
18 
19 void *
20 thr_read(void *arg)
21 {
22 	int fds[2];
23 	char buf;
24 
25 	CHECKe(pipe(fds));
26 	ASSERT(read(fds[0], &buf, 1) == -1);
27 	return ((caddr_t)NULL + errno);
28 }
29 
30 int
31 main(int argc, char **argv)
32 {
33 	struct sigaction sa;
34 	pthread_t tid;
35 	void *retval;
36 
37 	bzero(&sa, sizeof(sa));
38 	sa.sa_handler = handler;
39 	sa.sa_flags = SA_RESTART;
40 	CHECKe(sigaction(SIGUSR1, &sa, NULL));
41 	sa.sa_flags = 0;
42 	CHECKe(sigaction(SIGUSR2, &sa, NULL));
43 
44 	CHECKr(pthread_create(&tid, NULL, thr_read, NULL));
45 	sleep(2);
46 
47 	/* Should restart it. */
48 	CHECKr(pthread_kill(tid, SIGUSR1));
49 	sleep(1);
50 
51 	/* Should interrupt it. */
52 	CHECKr(pthread_kill(tid, SIGUSR2));
53 	sleep(1);
54 
55 	CHECKr(pthread_join(tid, &retval));
56 	ASSERT(retval == (void *)EINTR);
57 	ASSERT(hits == 2);
58 	SUCCEED;
59 }
60