#include "includes.h"
#ifndef HAVE_PSELECT
#include <sys/types.h>
#include <sys/time.h>
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "log.h"
#ifndef HAVE_SIGHANDLER_T
typedef void (*sighandler_t)(int);
#endif
static sighandler_t saved_sighandler[_NSIG];
static int notify_pipe[2];
static int suppress_debug;
static void
pselect_set_nonblock(int fd)
{
int val;
if ((val = fcntl(fd, F_GETFL)) == -1 ||
fcntl(fd, F_SETFL, val|O_NONBLOCK) == -1)
error_f("fcntl: %s", strerror(errno));
}
static int
pselect_notify_setup(void)
{
if (pipe(notify_pipe) == -1) {
error("pipe(notify_pipe) failed %s", strerror(errno));
notify_pipe[0] = notify_pipe[1] = -1;
return -1;
}
pselect_set_nonblock(notify_pipe[0]);
pselect_set_nonblock(notify_pipe[1]);
if (!suppress_debug)
debug3_f("pipe0 %d pipe1 %d", notify_pipe[0], notify_pipe[1]);
return 0;
}
static void
pselect_notify_parent(void)
{
if (notify_pipe[1] != -1)
(void)write(notify_pipe[1], "", 1);
}
static void
pselect_notify_prepare(fd_set *readset)
{
if (notify_pipe[0] != -1)
FD_SET(notify_pipe[0], readset);
}
static void
pselect_notify_done(fd_set *readset)
{
char c;
if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset)) {
while (read(notify_pipe[0], &c, 1) != -1)
debug2_f("reading");
FD_CLR(notify_pipe[0], readset);
}
(void)close(notify_pipe[0]);
(void)close(notify_pipe[1]);
}
static void
pselect_sig_handler(int sig)
{
int save_errno = errno;
pselect_notify_parent();
if (saved_sighandler[sig] != NULL)
(*saved_sighandler[sig])(sig);
errno = save_errno;
}
int
pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
const struct timespec *timeout, const sigset_t *mask)
{
int ret, sig, saved_errno, unmasked = 0;
sigset_t osig;
struct sigaction sa, osa;
struct timeval tv, *tvp = NULL;
if (timeout != NULL) {
tv.tv_sec = timeout->tv_sec;
tv.tv_usec = timeout->tv_nsec / 1000;
tvp = &tv;
}
if (mask == NULL)
return select(nfds, readfds, writefds, exceptfds, tvp);
for (sig = 0; sig < _NSIG; sig++) {
saved_sighandler[sig] = NULL;
if (sig == SIGKILL || sig == SIGSTOP || sigismember(mask, sig))
continue;
if (sigaction(sig, NULL, &sa) == 0 &&
sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) {
unmasked = 1;
sa.sa_handler = pselect_sig_handler;
if (sigaction(sig, &sa, &osa) == 0) {
if (!suppress_debug)
debug3_f("installed signal handler for"
" %s, previous 0x%p",
strsignal(sig), osa.sa_handler);
saved_sighandler[sig] = osa.sa_handler;
}
}
}
if (unmasked) {
if ((ret = pselect_notify_setup()) == -1) {
saved_errno = ENOMEM;
goto out;
}
pselect_notify_prepare(readfds);
nfds = MAX(nfds, notify_pipe[0] + 1);
}
sigprocmask(SIG_SETMASK, mask, &osig);
ret = select(nfds, readfds, writefds, exceptfds, tvp);
saved_errno = errno;
sigprocmask(SIG_SETMASK, &osig, NULL);
if (unmasked)
pselect_notify_done(readfds);
out:
for (sig = 0; sig < _NSIG; sig++) {
if (saved_sighandler[sig] == NULL)
continue;
if (sigaction(sig, NULL, &sa) == 0) {
sa.sa_handler = saved_sighandler[sig];
if (sigaction(sig, &sa, NULL) == 0) {
if (!suppress_debug)
debug3_f("restored signal handler for "
"%s", strsignal(sig));
} else {
error_f("failed to restore signal handler for "
"%s: %s", strsignal(sig), strerror(errno));
}
}
}
suppress_debug = 1;
errno = saved_errno;
return ret;
}
#endif