074e084f68dd0b08686612bec695a0cfe249da6dml/*
074e084f68dd0b08686612bec695a0cfe249da6dml * Copyright (C) 2011 - 2015 Nominum, Inc.
074e084f68dd0b08686612bec695a0cfe249da6dml *
074e084f68dd0b08686612bec695a0cfe249da6dml * Permission to use, copy, modify, and distribute this software and its
074e084f68dd0b08686612bec695a0cfe249da6dml * documentation for any purpose with or without fee is hereby granted,
074e084f68dd0b08686612bec695a0cfe249da6dml * provided that the above copyright notice and this permission notice
074e084f68dd0b08686612bec695a0cfe249da6dml * appear in all copies.
074e084f68dd0b08686612bec695a0cfe249da6dml *
074e084f68dd0b08686612bec695a0cfe249da6dml * THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
074e084f68dd0b08686612bec695a0cfe249da6dml * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
074e084f68dd0b08686612bec695a0cfe249da6dml * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
074e084f68dd0b08686612bec695a0cfe249da6dml * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
074e084f68dd0b08686612bec695a0cfe249da6dml * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
074e084f68dd0b08686612bec695a0cfe249da6dml * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
074e084f68dd0b08686612bec695a0cfe249da6dml * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
074e084f68dd0b08686612bec695a0cfe249da6dml */
074e084f68dd0b08686612bec695a0cfe249da6dml
074e084f68dd0b08686612bec695a0cfe249da6dml#include <errno.h>
074e084f68dd0b08686612bec695a0cfe249da6dml#include <signal.h>
074e084f68dd0b08686612bec695a0cfe249da6dml#include <stdlib.h>
074e084f68dd0b08686612bec695a0cfe249da6dml#include <string.h>
074e084f68dd0b08686612bec695a0cfe249da6dml
074e084f68dd0b08686612bec695a0cfe249da6dml#include <sys/select.h>
074e084f68dd0b08686612bec695a0cfe249da6dml
074e084f68dd0b08686612bec695a0cfe249da6dml#include <isc/result.h>
074e084f68dd0b08686612bec695a0cfe249da6dml#include <isc/types.h>
074e084f68dd0b08686612bec695a0cfe249da6dml
074e084f68dd0b08686612bec695a0cfe249da6dml#include "log.h"
074e084f68dd0b08686612bec695a0cfe249da6dml#include "os.h"
074e084f68dd0b08686612bec695a0cfe249da6dml#include "util.h"
074e084f68dd0b08686612bec695a0cfe249da6dml
074e084f68dd0b08686612bec695a0cfe249da6dmlvoid
074e084f68dd0b08686612bec695a0cfe249da6dmlperf_os_blocksignal(int sig, isc_boolean_t block)
074e084f68dd0b08686612bec695a0cfe249da6dml{
074e084f68dd0b08686612bec695a0cfe249da6dml sigset_t sset;
074e084f68dd0b08686612bec695a0cfe249da6dml int op;
074e084f68dd0b08686612bec695a0cfe249da6dml
074e084f68dd0b08686612bec695a0cfe249da6dml op = block ? SIG_BLOCK : SIG_UNBLOCK;
074e084f68dd0b08686612bec695a0cfe249da6dml
074e084f68dd0b08686612bec695a0cfe249da6dml if (sigemptyset(&sset) < 0 ||
074e084f68dd0b08686612bec695a0cfe249da6dml sigaddset(&sset, sig) < 0 ||
pthread_sigmask(op, &sset, NULL) < 0)
perf_log_fatal("pthread_sigmask: %s", strerror(errno));
}
void
perf_os_handlesignal(int sig, void (*handler)(int))
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
if (sigfillset(&sa.sa_mask) < 0 ||
sigaction(sig, &sa, NULL) < 0)
perf_log_fatal("sigaction: %s", strerror(errno));
}
isc_result_t
perf_os_waituntilreadable(int fd, int pipe_fd, isc_int64_t timeout)
{
fd_set read_fds;
int maxfd;
struct timeval tv, *tvp;
int n;
FD_ZERO(&read_fds);
FD_SET(fd, &read_fds);
FD_SET(pipe_fd, &read_fds);
maxfd = pipe_fd > fd ? pipe_fd : fd;
if (timeout < 0) {
tvp = NULL;
} else {
tv.tv_sec = timeout / MILLION;
tv.tv_usec = timeout % MILLION;
tvp = &tv;
}
n = select(maxfd + 1, &read_fds, NULL, NULL, tvp);
if (n < 0) {
if (errno != EINTR)
perf_log_fatal("select() failed");
return (ISC_R_CANCELED);
} else if (FD_ISSET(fd, &read_fds)) {
return (ISC_R_SUCCESS);
} else if (FD_ISSET(pipe_fd, &read_fds)) {
return (ISC_R_CANCELED);
} else {
return (ISC_R_TIMEDOUT);
}
}
isc_result_t
perf_os_waituntilanyreadable(int *fds, unsigned int nfds, int pipe_fd,
isc_int64_t timeout)
{
fd_set read_fds;
unsigned int i;
int maxfd;
struct timeval tv, *tvp;
int n;
FD_ZERO(&read_fds);
maxfd = 0;
for (i = 0; i < nfds; i++) {
FD_SET(fds[i], &read_fds);
if (fds[i] > maxfd)
maxfd = fds[i];
}
FD_SET(pipe_fd, &read_fds);
if (pipe_fd > maxfd)
maxfd = pipe_fd;
if (timeout < 0) {
tvp = NULL;
} else {
tv.tv_sec = timeout / MILLION;
tv.tv_usec = timeout % MILLION;
tvp = &tv;
}
n = select(maxfd + 1, &read_fds, NULL, NULL, tvp);
if (n < 0) {
if (errno != EINTR)
perf_log_fatal("select() failed");
return (ISC_R_CANCELED);
} else if (n == 0) {
return (ISC_R_TIMEDOUT);
} else if (FD_ISSET(pipe_fd, &read_fds)) {
return (ISC_R_CANCELED);
} else {
return (ISC_R_SUCCESS);
}
}