1243730Srwatson/*- 2243730Srwatson * Copyright (c) 2012 The FreeBSD Foundation 3243730Srwatson * All rights reserved. 4243730Srwatson * 5243730Srwatson * This software was developed by Pawel Jakub Dawidek under sponsorship from 6243730Srwatson * the FreeBSD Foundation. 7243730Srwatson * 8243730Srwatson * Redistribution and use in source and binary forms, with or without 9243730Srwatson * modification, are permitted provided that the following conditions 10243730Srwatson * are met: 11243730Srwatson * 1. Redistributions of source code must retain the above copyright 12243730Srwatson * notice, this list of conditions and the following disclaimer. 13243730Srwatson * 2. Redistributions in binary form must reproduce the above copyright 14243730Srwatson * notice, this list of conditions and the following disclaimer in the 15243730Srwatson * documentation and/or other materials provided with the distribution. 16243730Srwatson * 17243730Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18243730Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19243730Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20243730Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21243730Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22243730Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23243730Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24243730Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25243730Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26243730Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27243730Srwatson * SUCH DAMAGE. 28243730Srwatson * 29243734Srwatson * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/sigtimedwait.h#2 $ 30243730Srwatson */ 31243730Srwatson 32243730Srwatson#ifndef _SIGTIMEDWAIT_H_ 33243730Srwatson#define _SIGTIMEDWAIT_H_ 34243730Srwatson 35243730Srwatson#include <sys/types.h> 36243730Srwatson#include <sys/time.h> 37243730Srwatson 38243730Srwatson#include <assert.h> 39243730Srwatson#include <errno.h> 40243730Srwatson#include <signal.h> 41243730Srwatson#include <stdio.h> 42243730Srwatson#include <string.h> 43243730Srwatson#include <unistd.h> 44243730Srwatson 45243734Srwatson#include "pjdlog.h" 46243730Srwatson 47243730Srwatsonstatic int 48243730Srwatsonsigtimedwait(const sigset_t *set, siginfo_t *info, 49243730Srwatson const struct timespec *timeout) 50243730Srwatson{ 51243730Srwatson struct itimerval it; 52243730Srwatson sigset_t mask; 53243730Srwatson int error, signo; 54243730Srwatson 55243730Srwatson PJDLOG_ASSERT(info == NULL); 56243730Srwatson 57243730Srwatson PJDLOG_VERIFY(sigemptyset(&mask) == 0); 58243730Srwatson PJDLOG_VERIFY(sigaddset(&mask, SIGALRM) == 0); 59243730Srwatson PJDLOG_VERIFY(sigprocmask(SIG_BLOCK, &mask, NULL) == 0); 60243730Srwatson 61243730Srwatson timerclear(&it.it_interval); 62243730Srwatson it.it_value.tv_sec = timeout->tv_sec; 63243730Srwatson it.it_value.tv_usec = timeout->tv_nsec / 1000; 64243730Srwatson if (it.it_value.tv_sec == 0 && it.it_value.tv_usec == 0) 65243730Srwatson it.it_value.tv_usec = 1; 66243730Srwatson PJDLOG_VERIFY(setitimer(ITIMER_REAL, &it, NULL) == 0); 67243730Srwatson 68243730Srwatson bcopy(set, &mask, sizeof(mask)); 69243730Srwatson PJDLOG_VERIFY(sigaddset(&mask, SIGALRM) == 0); 70243730Srwatson 71243730Srwatson PJDLOG_VERIFY(sigwait(&mask, &signo) == 0); 72243730Srwatson error = errno; 73243730Srwatson 74243730Srwatson timerclear(&it.it_interval); 75243730Srwatson timerclear(&it.it_value); 76243730Srwatson PJDLOG_VERIFY(setitimer(ITIMER_REAL, &it, NULL) == 0); 77243730Srwatson 78243730Srwatson PJDLOG_VERIFY(sigemptyset(&mask) == 0); 79243730Srwatson PJDLOG_VERIFY(sigaddset(&mask, SIGALRM) == 0); 80243730Srwatson PJDLOG_VERIFY(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0); 81243730Srwatson 82243730Srwatson if (signo == SIGALRM) { 83243730Srwatson errno = EAGAIN; 84243730Srwatson signo = -1; 85243730Srwatson } else { 86243730Srwatson errno = error; 87243730Srwatson } 88243730Srwatson 89243730Srwatson return (signo); 90243730Srwatson} 91243730Srwatson 92243730Srwatson#endif /* !_SIGTIMEDWAIT_H_ */ 93