r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / lib / select.c
index f88ad52de65e5ce4ac40b9dcd84ad6ff1e66a9b7..d5e4ba68f29b59aaa74ff2950a96823bec9123de 100644 (file)
@@ -6,7 +6,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -15,8 +15,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -38,9 +37,8 @@ static VOLATILE unsigned pipe_written, pipe_read;
  nasty signal race condition.
 ********************************************************************/
 
-void sys_select_signal(void)
+void sys_select_signal(char c)
 {
-       char c = 1;
        if (!initialised) return;
 
        if (pipe_written > pipe_read+256) return;
@@ -72,9 +70,9 @@ int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, s
                 */
 
                if(set_blocking(select_pipe[0],0)==-1)
-                       smb_panic("select_pipe[0]: O_NONBLOCK failed.\n");
+                       smb_panic("select_pipe[0]: O_NONBLOCK failed");
                if(set_blocking(select_pipe[1],0)==-1)
-                       smb_panic("select_pipe[1]: O_NONBLOCK failed.\n");
+                       smb_panic("select_pipe[1]: O_NONBLOCK failed");
 
                initialised = sys_getpid();
        }
@@ -99,20 +97,27 @@ int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, s
                        FD_ZERO(writefds);
                if (errorfds)
                        FD_ZERO(errorfds);
-       }
-
-       if (FD_ISSET(select_pipe[0], readfds2)) {
+       } else if (FD_ISSET(select_pipe[0], readfds2)) {
                char c;
                saved_errno = errno;
                if (read(select_pipe[0], &c, 1) == 1) {
                        pipe_read++;
-               }
-               errno = saved_errno;
-               FD_CLR(select_pipe[0], readfds2);
-               ret--;
-               if (ret == 0) {
+                       /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
+                          fix to ensure we don't lose signals. We must always
+                          return -1 when the select pipe is set, otherwise if another
+                          fd is also ready (so ret == 2) then we used to eat the
+                          byte in the pipe and lose the signal. JRA.
+                       */
                        ret = -1;
+#if 0
+                       /* JRA - we can use this to debug the signal messaging... */
+                       DEBUG(0,("select got %u signal\n", (unsigned int)c));
+#endif
                        errno = EINTR;
+               } else {
+                       FD_CLR(select_pipe[0], readfds2);
+                       ret--;
+                       errno = saved_errno;
                }
        }
 
@@ -128,12 +133,23 @@ int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorf
 {
        int ret;
        fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
-       struct timeval tval2, *ptval;
+       struct timeval tval2, *ptval, end_time;
 
        readfds2 = (readfds ? &readfds_buf : NULL);
        writefds2 = (writefds ? &writefds_buf : NULL);
        errorfds2 = (errorfds ? &errorfds_buf : NULL);
-       ptval = (tval ? &tval2 : NULL);
+       if (tval) {
+               GetTimeOfDay(&end_time);
+               end_time.tv_sec += tval->tv_sec;
+               end_time.tv_usec += tval->tv_usec;
+               end_time.tv_sec += end_time.tv_usec / 1000000;
+               end_time.tv_usec %= 1000000;
+               errno = 0;
+               tval2 = *tval;
+               ptval = &tval2;
+       } else {
+               ptval = NULL;
+       }
 
        do {
                if (readfds)
@@ -142,10 +158,26 @@ int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorf
                        writefds_buf = *writefds;
                if (errorfds)
                        errorfds_buf = *errorfds;
-               if (tval)
-                       tval2 = *tval;
+               if (ptval && (errno == EINTR)) {
+                       struct timeval now_time;
+                       SMB_BIG_INT tdif;
+
+                       GetTimeOfDay(&now_time);
+                       tdif = usec_time_diff(&end_time, &now_time);
+                       if (tdif <= 0) {
+                               ret = 0; /* time expired. */
+                               break;
+                       }
+                       ptval->tv_sec = tdif / 1000000;
+                       ptval->tv_usec = tdif % 1000000;
+               }
 
-               ret = sys_select(maxfd, readfds2, writefds2, errorfds2, ptval);
+               /* We must use select and not sys_select here. If we use
+                  sys_select we'd lose the fact a signal occurred when sys_select
+                  read a byte from the pipe. Fix from Mark Weaver
+                  <mark-clist@npsl.co.uk>
+               */
+               ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
        } while (ret == -1 && errno == EINTR);
 
        if (readfds)