timeout.tv_sec = 0;
timeout.tv_usec = 0;
- sys_select_intr(cli->fd+1,&fds,&timeout);
+ sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
/* We deliberately use receive_smb instead of
client_receive_smb as we want to receive
FD_ZERO(&fds);
FD_SET(fd,&fds);
- if (sys_select_intr(fd+1,&fds,&timeout) == 1) {
+ if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
ret = fgets(line, sizeof(line), stdin);
return ret;
}
/*
- Unix SMB/CIFS implementation.
+ Unix SMB/Netbios implementation.
+ Version 3.0
Samba select/poll implementation
Copyright (C) Andrew Tridgell 1992-1998
#include "includes.h"
-/* this is here because it allows us to avoid a nasty race in signal handling.
+/* This is here because it allows us to avoid a nasty race in signal handling.
We need to guarantee that when we get a signal we get out of a select immediately
but doing that involves a race condition. We can avoid the race by getting the
signal handler to write to a pipe that is in the select/poll list
- this means all Samba signal handlers should call sys_select_signal()
+ This means all Samba signal handlers should call sys_select_signal().
*/
+
static pid_t initialised;
static int select_pipe[2];
static VOLATILE unsigned pipe_written, pipe_read;
-
/*******************************************************************
-call this from all Samba signal handlers if you want to avoid a
-nasty signal race condition
+ Call this from all Samba signal handlers if you want to avoid a
+ nasty signal race condition.
********************************************************************/
+
void sys_select_signal(void)
{
char c = 1;
}
/*******************************************************************
-like select() but avoids the signal race using a pipe
-it also guarantees that fds on return only ever contains bits set
-for file descriptors that were readable
+ Like select() but avoids the signal race using a pipe
+ it also guuarantees that fds on return only ever contains bits set
+ for file descriptors that were readable.
********************************************************************/
-int sys_select(int maxfd, fd_set *fds,struct timeval *tval)
+
+int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
{
int ret, saved_errno;
+ fd_set *readfds2, readfds_buf;
if (initialised != sys_getpid()) {
pipe(select_pipe);
}
maxfd = MAX(select_pipe[0]+1, maxfd);
- FD_SET(select_pipe[0], fds);
+
+ /* If readfds is NULL we need to provide our own set. */
+ if (readfds) {
+ readfds2 = readfds;
+ } else {
+ readfds2 = &readfds_buf;
+ FD_ZERO(readfds2);
+ }
+ FD_SET(select_pipe[0], readfds2);
+
errno = 0;
- ret = select(maxfd,fds,NULL,NULL,tval);
+ ret = select(maxfd,readfds2,writefds,errorfds,tval);
if (ret <= 0) {
- FD_ZERO(fds);
+ FD_ZERO(readfds2);
+ if (writefds)
+ FD_ZERO(writefds);
+ if (errorfds)
+ FD_ZERO(errorfds);
}
- if (FD_ISSET(select_pipe[0], fds)) {
- FD_CLR(select_pipe[0], fds);
+ if (FD_ISSET(select_pipe[0], readfds2)) {
+ FD_CLR(select_pipe[0], readfds2);
ret--;
if (ret == 0) {
ret = -1;
}
/*******************************************************************
-similar to sys_select() but catch EINTR and continue
-this is what sys_select() used to do in Samba
+ Similar to sys_select() but catch EINTR and continue.
+ This is what sys_select() used to do in Samba.
********************************************************************/
-int sys_select_intr(int maxfd, fd_set *fds,struct timeval *tval)
+
+int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
{
int ret;
- fd_set fds2;
+ fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
+
+ readfds2 = (readfds ? &readfds_buf : NULL);
+ writefds2 = (writefds ? &writefds_buf : NULL);
+ errorfds2 = (errorfds ? &errorfds_buf : NULL);
do {
- fds2 = *fds;
- ret = sys_select(maxfd, &fds2, tval);
+ if (readfds)
+ readfds_buf = *readfds;
+ if (writefds)
+ writefds_buf = *writefds;
+ if (errorfds)
+ errorfds_buf = *errorfds;
+ ret = sys_select(maxfd, readfds2, writefds2, errorfds2, tval);
} while (ret == -1 && errno == EINTR);
- *fds = fds2;
+ if (readfds)
+ *readfds = readfds_buf;
+ if (writefds)
+ *writefds = writefds_buf;
+ if (errorfds)
+ *errorfds = errorfds_buf;
return ret;
}
FD_ZERO(&fds);
errno = 0;
- sys_select_intr(0,&fds,&tval);
+ sys_select_intr(0,&fds,NULL,NULL,&tval);
GetTimeOfDay(&t2);
tdiff = TvalDiff(&t1,&t2);
timeout.tv_sec = (time_t) (time_out / 1000);
timeout.tv_usec = (long)(1000 * (time_out % 1000));
- selrtn = sys_select_intr(fd + 1, &fds, &timeout);
+ selrtn = sys_select_intr(fd + 1, &fds, NULL, NULL, &timeout);
if (selrtn <= 0)
{
FD_ZERO(&fds);
FD_SET(fd,&fds);
- selrtn = sys_select_intr(fd+1,&fds,&timeout);
+ selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
/* Check if error */
if (selrtn == -1) {
timeout.tv_sec = t/1000;
timeout.tv_usec = 1000*(t%1000);
- if ((ret = sys_select_intr(fd+1,&fds,&timeout)) == -1) {
+ if ((ret = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout)) == -1) {
/* errno should be EBADF or EINVAL. */
DEBUG(0,("select returned -1, errno = %s (%d)\n", strerror(errno), errno));
return NULL;
BlockSignals(False, SIGTERM);
- selrtn = sys_select(FD_SETSIZE,&fds,&timeout);
+ selrtn = sys_select(FD_SETSIZE,&fds,NULL,NULL,&timeout);
/* We can only take signals when we are in the select - block them again here. */
static void termination_handler(int signum)
{
do_sigterm = True;
+ sys_select_signal();
}
static BOOL do_sigusr2;
static void sigusr2_handler(int signum)
{
do_sigusr2 = True;
+ sys_select_signal();
}
static BOOL do_sighup;
static void sighup_handler(int signum)
{
do_sighup = True;
+ sys_select_signal();
}
/* Create winbindd socket */
/* Call select */
- selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
+ selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
if (selret == 0)
continue;
to.tv_sec = timeout / 1000;
to.tv_usec = (timeout % 1000) * 1000;
- selrtn = sys_select(maxfd+1,fds,&to);
+ selrtn = sys_select(maxfd+1,fds,NULL,NULL,&to);
if (selrtn == -1 && errno == EINTR) {
/* could be a kernel oplock interrupt */
to.tv_sec = timeout / 1000;
to.tv_usec = (timeout % 1000) * 1000;
- selrtn = sys_select(MAX(maxfd,smbd_server_fd())+1,&fds,timeout>0?&to:NULL);
+ selrtn = sys_select(MAX(maxfd,smbd_server_fd())+1,&fds,NULL,NULL,timeout>0?&to:NULL);
/* if we get EINTR then maybe we have received an oplock
signal - treat this as select returning 1. This is ugly, but
memcpy((char *)&lfds, (char *)&listen_set,
sizeof(listen_set));
- num = sys_select(FD_SETSIZE,&lfds,NULL);
+ num = sys_select(FD_SETSIZE,&lfds,NULL,NULL,NULL);
if (num == -1 && errno == EINTR) {
extern VOLATILE sig_atomic_t reload_after_sighup;
if (s != -1) FD_SET(s, &fds);
if (c != -1) FD_SET(c, &fds);
- num = sys_select_intr(MAX(s+1, c+1),&fds,NULL);
+ num = sys_select_intr(MAX(s+1, c+1),&fds,NULL,NULL,NULL);
if (num <= 0) continue;
if (c != -1 && FD_ISSET(c, &fds)) {
FD_ZERO(&fds);
FD_SET(s, &fds);
- num = sys_select_intr(s+1,&fds,NULL);
+ num = sys_select_intr(s+1,&fds,NULL,NULL,NULL);
if (num > 0) {
c = accept(s, &addr, &in_addrlen);
if (c != -1) {
BlockSignals(False, SIGTERM);
- num = sys_select(FD_SETSIZE, &fds, &timeout);
+ num = sys_select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
/* We can only take signals when we are in the select - block them again here. */