1 /*-
2  * Copyright (c) 2012 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef	_SIGTIMEDWAIT_H_
31 #define	_SIGTIMEDWAIT_H_
32 
33 #include <sys/types.h>
34 #include <sys/time.h>
35 
36 #include <assert.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "pjdlog.h"
44 
45 static int
46 sigtimedwait(const sigset_t *set, siginfo_t *info,
47     const struct timespec *timeout)
48 {
49 	struct itimerval it;
50 	sigset_t mask;
51 	int error, signo;
52 
53 	PJDLOG_ASSERT(info == NULL);
54 
55 	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
56 	PJDLOG_VERIFY(sigaddset(&mask, SIGALRM) == 0);
57 	PJDLOG_VERIFY(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
58 
59 	timerclear(&it.it_interval);
60 	it.it_value.tv_sec = timeout->tv_sec;
61 	it.it_value.tv_usec = timeout->tv_nsec / 1000;
62 	if (it.it_value.tv_sec == 0 && it.it_value.tv_usec == 0)
63 		it.it_value.tv_usec = 1;
64 	PJDLOG_VERIFY(setitimer(ITIMER_REAL, &it, NULL) == 0);
65 
66 	bcopy(set, &mask, sizeof(mask));
67 	PJDLOG_VERIFY(sigaddset(&mask, SIGALRM) == 0);
68 
69 	PJDLOG_VERIFY(sigwait(&mask, &signo) == 0);
70 	error = errno;
71 
72 	timerclear(&it.it_interval);
73 	timerclear(&it.it_value);
74 	PJDLOG_VERIFY(setitimer(ITIMER_REAL, &it, NULL) == 0);
75 
76 	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
77 	PJDLOG_VERIFY(sigaddset(&mask, SIGALRM) == 0);
78 	PJDLOG_VERIFY(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0);
79 
80 	if (signo == SIGALRM) {
81 		errno = EAGAIN;
82 		signo = -1;
83 	} else {
84 		errno = error;
85 	}
86 
87 	return (signo);
88 }
89 
90 #endif	/* !_SIGTIMEDWAIT_H_ */
91