From b9109a9f203c4f5be78253139783112ffbac5163 Mon Sep 17 00:00:00 2001
From: Kurt Zeilenga <kurt@openldap.org>
Date: Mon, 29 Mar 1999 09:04:35 +0000
Subject: [PATCH] add wait macros.  add LDAP_SIGCHLD.  and other misc NT
 cleanups.

---
 include/ac/signal.h             |  8 +++++++
 include/ac/wait.h               | 23 +++++++++++++++---
 libraries/liblber/sockbuf.c     | 42 +++++++++++++++++----------------
 libraries/liblutil/detach.c     | 12 ++++++++--
 libraries/liblutil/liblutil.dsp | 20 ++++++++++++++++
 libraries/liblutil/tempnam.c    |  2 +-
 servers/slapd/main.c            | 10 ++++----
 7 files changed, 85 insertions(+), 32 deletions(-)

diff --git a/include/ac/signal.h b/include/ac/signal.h
index 3ab614f581..4ce0deb7b6 100644
--- a/include/ac/signal.h
+++ b/include/ac/signal.h
@@ -52,4 +52,12 @@
 #	endif
 #endif
 
+#ifndef LDAP_SIGCHLD
+#ifdef SIGCHLD
+#define LDAP_SIGCHLD SIGCHLD
+#elif SIGCLD
+#define LDAP_SIGCHLD SIGCLD
+#endif
+#endif
+
 #endif /* _AC_SIGNAL_H */
diff --git a/include/ac/wait.h b/include/ac/wait.h
index 30da21643c..471c6a16f9 100644
--- a/include/ac/wait.h
+++ b/include/ac/wait.h
@@ -18,11 +18,28 @@
 # include <sys/wait.h>
 #endif
 
+#define LDAP_HI(s)	(((s) >> 8) & 0x377)
+#define LDAP_LO(s)	((s) & 0377)
+
+/* These should work on non-POSIX UNIX platforms,
+	all bets on off on non-POSIX non-UNIX platforms... */
+#ifndef WIFEXITED
+# define WIFEXITED(s)	(LDAP_LO(s) == 0)
+#endif
 #ifndef WEXITSTATUS
-# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
+# define WEXITSTATUS(s) LDAP_HI(s)
 #endif
-#ifndef WIFEXITED
-# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
+#ifndef WIFSIGNALED
+# define WIFSIGNALED(s) (LDAP_LO(s) > 0 && LDAP_HI(s) == 0)
+#endif
+#ifndef WTERMSIG
+# define WTERMSIG(s)	(LDAP_LO(s) & 0177)
+#endif
+#ifndef WIFSTOPPED
+# define WIFSTOPPED(s)	(LDAP_LO(s) == 0177 && LDAP_HI(s) != 0)
+#endif
+#ifndef WSTOPSIG
+# define WSTOPSIG(s)	LDAP_HI(s)
 #endif
 
 #ifdef WCONTINUED
diff --git a/libraries/liblber/sockbuf.c b/libraries/liblber/sockbuf.c
index f34f457112..8eb9bb3797 100644
--- a/libraries/liblber/sockbuf.c
+++ b/libraries/liblber/sockbuf.c
@@ -577,15 +577,18 @@ int lber_pvt_sb_set_nonblock( Sockbuf *sb, int nb )
       sb->sb_read_ahead = 0;
 #endif
    }
-#ifdef FIONBIO
    if (lber_pvt_sb_in_use(sb)) {
+#if HAVE_FCNTL
+		int flags = fcntl(lber_pvt_sb_get_desc(sb), F_GETFL);
+		flags |= O_NONBLOCK;
+		return fcntl(lber_pvt_sb_get_desc(sb), F_SETFL, flags);
+		
+#elif defined( FIONBIO )
 	   /* WINSOCK requires the status to be a long */
 		ioctl_t status = (nb!=0);
-		if (ioctl( lber_pvt_sb_get_desc(sb), FIONBIO, &status ) == -1 ) {
-	 return -1;
-      }
-   }
+		return ioctl( lber_pvt_sb_get_desc(sb), FIONBIO, &status );
 #endif /* FIONBIO */
+   }
    return 0;
 }
 #endif
@@ -750,8 +753,9 @@ stream_read( Sockbuf *sb, void *buf, long len )
  */
    return tcpread( lber_pvt_sb_get_desc(sb), 0, (unsigned char *)buf, 
 		   len, NULL );
-#elif (defined(DOS) && (defined(PCNFS) || defined( WINSOCK))) \
-	|| defined( _WIN32) || defined ( __BEOS__ )
+
+#elif defined( HAVE_PCNFS ) || \
+   defined( HAVE_WINSOCK ) || defined ( __BEOS__ )
 /*
  * PCNFS (under DOS)
  */
@@ -762,11 +766,13 @@ stream_read( Sockbuf *sb, void *buf, long len )
  * 32-bit Windows Socket API (under Windows NT or Windows 95)
  */
    return recv( lber_pvt_sb_get_desc(sb), buf, len, 0 );
-#elif (defined(DOS) && defined( NCSA ))
+
+#elif defined( HAVE_NCSA )
 /*
  * NCSA Telnet TCP/IP stack (under DOS)
  */
    return nread( lber_pvt_sb_get_desc(sb), buf, len );
+
 #else
    return read( lber_pvt_sb_get_desc(sb), buf, len );
 #endif
@@ -783,8 +789,9 @@ stream_write( Sockbuf *sb, void *buf, long len )
    return tcpwrite( lber_pvt_sb_get_desc(sb),
 		    (unsigned char *)(buf), 
 		    (len<MAX_WRITE)? len : MAX_WRITE );
-#elif (defined(DOS) && (defined(PCNFS) || defined( WINSOCK))) \
-	|| defined( _WIN32 ) || defined ( __BEOS__ )
+
+#elif defined( HAVE_PCNFS) \
+   || defined( HAVE_WINSOCK) || defined ( __BEOS__ )
 /*
  * PCNFS (under DOS)
  */
@@ -795,8 +802,10 @@ stream_write( Sockbuf *sb, void *buf, long len )
  * 32-bit Windows Socket API (under Windows NT or Windows 95)
  */
    return send( lber_pvt_sb_get_desc(sb), buf, len, 0 );
-#elif defined(NCSA)
+
+#elif defined(HAVE_NCSA)
    return netwrite( lber_pvt_sb_get_desc(sb), buf, len );
+
 #elif defined(VMS)
 /*
  * VMS -- each write must be 64K or smaller
@@ -862,12 +871,8 @@ dgram_read( Sockbuf *sb, void *buf, long len )
    
    dd = (struct dgram_data *)(sb->sb_iodata);
    
-# if !defined( MACOS) && !defined(DOS) && !defined( _WIN32)
    addrlen = sizeof( struct sockaddr );
    rc=recvfrom( lber_pvt_sb_get_desc(sb), buf, len, 0, &(dd->src), &addrlen );
-# else
-   UDP not supported
-# endif
    
    if ( sb->sb_debug ) {
       lber_log_printf( LDAP_DEBUG_ANY, sb->sb_debug,
@@ -892,13 +897,10 @@ dgram_write( Sockbuf *sb, void *buf, long len )
    
    dd = (struct dgram_data *)(sb->sb_iodata);
    
-# if !defined( MACOS) && !defined(DOS) && !defined( _WIN32)
    rc=sendto( lber_pvt_sb_get_desc(sb), buf, len, 0, &(dd->dst),
 	     sizeof( struct sockaddr ) );
-# else
-   UDP not supported
-# endif
-     if ( rc <= 0 )
+
+   if ( rc <= 0 )
        return( -1 );
    
    /* fake error if write was not atomic */
diff --git a/libraries/liblutil/detach.c b/libraries/liblutil/detach.c
index 5d27febf9c..682410d421 100644
--- a/libraries/liblutil/detach.c
+++ b/libraries/liblutil/detach.c
@@ -15,12 +15,18 @@
 #include <stdio.h>
 
 #include <ac/signal.h>
+#include <ac/socket.h>
 #include <ac/unistd.h>
 
 #include <sys/stat.h>
 #include <fcntl.h>
+
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
+#endif
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
+#endif
 
 #include "lutil.h"
 
@@ -82,13 +88,15 @@ lutil_detach( int debug, int do_close )
 
 #ifdef HAVE_SETSID
 		(void) setsid();
-#else /* HAVE_SETSID */
+#elif TIOCNOTTY
 		if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
 			(void) ioctl( sd, TIOCNOTTY, NULL );
 			(void) close( sd );
 		}
-#endif /* HAVE_SETSID */
+#endif
 	} 
 
+#ifdef SIGPIPE
 	(void) SIGNAL( SIGPIPE, SIG_IGN );
+#endif
 }
diff --git a/libraries/liblutil/liblutil.dsp b/libraries/liblutil/liblutil.dsp
index e3f1ff6fc6..e104adebd9 100644
--- a/libraries/liblutil/liblutil.dsp
+++ b/libraries/liblutil/liblutil.dsp
@@ -100,6 +100,10 @@ SOURCE=..\..\include\ldap_features.h
 # End Source File
 # Begin Source File
 
+SOURCE=.\lockf.c
+# End Source File
+# Begin Source File
+
 SOURCE=..\..\include\lutil.h
 # End Source File
 # Begin Source File
@@ -116,11 +120,27 @@ SOURCE=.\md5.c
 # End Source File
 # Begin Source File
 
+SOURCE=.\memcmp.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\passwd.c
+# End Source File
+# Begin Source File
+
 SOURCE=..\..\include\portable.h
 # End Source File
 # Begin Source File
 
+SOURCE=.\setproctitle.c
+# End Source File
+# Begin Source File
+
 SOURCE=.\sha1.c
 # End Source File
+# Begin Source File
+
+SOURCE=.\tempnam.c
+# End Source File
 # End Target
 # End Project
diff --git a/libraries/liblutil/tempnam.c b/libraries/liblutil/tempnam.c
index d4aa9cc239..358d4bddbc 100644
--- a/libraries/liblutil/tempnam.c
+++ b/libraries/liblutil/tempnam.c
@@ -10,7 +10,7 @@
 #include "lutil.h"
 
 char *
-tempnam( const char *dir, const char *pfx )
+(tempnam)( const char *dir, const char *pfx )
 {
     char	*s;
 
diff --git a/servers/slapd/main.c b/servers/slapd/main.c
index aaff83fe91..73a4e15ca8 100644
--- a/servers/slapd/main.c
+++ b/servers/slapd/main.c
@@ -15,7 +15,7 @@
 #include "slap.h"
 #include "lutil.h"			/* Get lutil_detach() */
 
-#if defined(SIGCHLD) || defined(SIGCLD)
+#ifdef LDAP_SIGCHLD
 static RETSIGTYPE wait4child( int sig );
 #endif
 
@@ -226,10 +226,8 @@ main( int argc, char **argv )
 #endif
 	(void) SIGNAL( SIGINT, slap_set_shutdown );
 	(void) SIGNAL( SIGTERM, slap_set_shutdown );
-#ifdef SIGCHLD
-	(void) SIGNAL( SIGCHLD, wait4child );
-#elif defined(SIGCLD)
-	(void) SIGNAL( SIGCLD, wait4child );
+#ifdef LDAP_SIGCHLD
+	(void) SIGNAL( LDAP_SIGCHLD, wait4child );
 #endif
 
 	if(!inetd) {
@@ -290,7 +288,7 @@ destroy:
 }
 
 
-#if defined(SIGCHLD) || defined(SIGCLD)
+#ifdef LDAP_SIGCHLD
 
 /*
  *  Catch and discard terminated child processes, to avoid zombies.
-- 
GitLab