From fa2da63ca4d3f48793de5421f56c23c1c6d794ab Mon Sep 17 00:00:00 2001
From: Kurt Zeilenga <kurt@openldap.org>
Date: Mon, 17 Aug 1998 23:26:25 +0000
Subject: [PATCH] Merge from LAMBERT branch

---
 Make-common                               | 30 +++++++++++++----------
 build/platforms/freebsd-gcc/Make-platform | 29 +++++++++++++++++++++-
 clients/finger/main.c                     |  9 +++++++
 clients/gopher/go500.c                    |  9 +++++++
 clients/gopher/go500gw.c                  |  9 +++++++
 contrib/whois++/command.c                 |  9 +++++++
 libraries/liblber/decode.c                |  9 +++----
 libraries/liblber/encode.c                | 10 +++-----
 libraries/libldap/os-ip.c                 |  9 +++++++
 libraries/libldap/result.c                | 22 ++++++++++++++---
 libraries/msdos/winsock/wsockip.c         |  9 +++++++
 servers/slapd/connection.c                | 17 +++++++++++++
 servers/slapd/daemon.c                    | 11 ++++++---
 servers/slapd/main.c                      | 16 ++++++++++++
 servers/slurpd/re.c                       |  2 +-
 servers/slurpd/replica.c                  | 16 ++++++++++++
 servers/slurpd/rq.c                       |  4 +--
 17 files changed, 184 insertions(+), 36 deletions(-)

diff --git a/Make-common b/Make-common
index 800a743742..1869d320d9 100644
--- a/Make-common
+++ b/Make-common
@@ -39,14 +39,15 @@ BINDIR= $(INSTROOT)/bin
 # if you want things to run in a different directory from where they
 # are installed, set this accordingly (this path gets compiled into a
 # few binaries). otherwise, leave it alone.
-RUNTIMEETCDIR= $(ETCDIR)
+RUNTIMEETCDIR?= $(ETCDIR)
 
 #############################################################################
 ## General compiler options                                                ##
 #############################################################################
 # Passed to every compile (cc or gcc).  This is where you put -O or -g, etc.
 EXTRACFLAGS=-O -g
-# EXTRACFLAGS=-O
+#EXTRACFLAGS=-O
+#EXTRACFLAGS=-g
 # Passed to every link (ld).  Include -g here if you did in EXTRACFLAGS.
 EXTRALDFLAGS=-g
 
@@ -125,23 +126,26 @@ SLAPD_BACKENDS= -DLDAP_LDBM # -DLDAP_SHELL -DLDAP_PASSWD
 # You will also need to edit the include and lib strings appropriately.
 #
 # berkeley db btree package
-LDBMBACKEND=-DLDBM_USE_DBBTREE
-LDBMINCLUDE=-I/usr/include
 #LDBMLIB=-ldb
+#LDBMBACKEND?=-DLDBM_USE_DBBTREE
+#LDBMINCLUDE?=-I/usr/local/db/include
+#LDBMLIB?=-ldb
 # berkeley db hash package
-#LDBMBACKEND=-DLDBM_USE_DBHASH
-#LDBMINCLUDE=-I/usr/local/db/include
-#LDBMLIB=-ldb
+#LDBMBACKEND?=-DLDBM_USE_DBHASH
+#LDBMINCLUDE?=-I/usr/local/db/include
+#LDBMLIB?=-ldb
 # gnu dbm (gdbm)
-#LDBMBACKEND=-DLDBM_USE_GDBM
-#LDBMINCLUDE=-I/usr/local/gdbm/include
-#LDBMLIB=-lgdbm
+#LDBMBACKEND?=-DLDBM_USE_GDBM
+#LDBMINCLUDE?=-I/usr/local/gdbm/include
+#LDBMLIB?=-lgdbm
 # standard unix ndbm
-#LDBMBACKEND=-DLDBM_USE_NDBM
+LDBMBACKEND?=-DLDBM_USE_NDBM
+LDBMINCLUDE?=
+LDBMLIB?=
 #
 # if you want to use a non-default threads package change these lines
-#THREADS=-DPOSIX_THREADS
-#THREADSLIB= -pthread
+#THREADS?=-DNO_THREADS
+#THREADSLIB?=
 
 #############################################################################
 ## The following options are used by the xax500 client.  If you haven't    ##
diff --git a/build/platforms/freebsd-gcc/Make-platform b/build/platforms/freebsd-gcc/Make-platform
index 1b8dae7517..66a943256f 100644
--- a/build/platforms/freebsd-gcc/Make-platform
+++ b/build/platforms/freebsd-gcc/Make-platform
@@ -6,12 +6,39 @@
 #
 # add any platform-specific overrides below here
 #
+PREFIX?=/usr/local
+INSTROOT=${PREFIX}
+ETCDIR= $(INSTROOT)/etc/ldap
+EXTRACFLAGS=-O -DLDAP_DEBUG
+LDBMBACKEND=-DLDBM_USE_DBBTREE
+LDBMINCLUDE=-I/usr/include
+#
+# LDAP has a problem with the idea of implicit vs. explicit yields
+# in call conversion threading packages, like the MIT pthreads
+# package.  Rather than resolve this globally, I have marked the
+# threading as "preeemptive", even though it is technically not.
+#
+# As far as I have been able to determine, there are not buzz-loop
+# based races in the LDAP code (at least not now that I've fixed
+# the bogus FD_SETSIZE assumptions about the select(2) system call
+# implementation being in any way related to getdtablesize(2) --
+# if it were, sys/types.h would define FD_SETSIZE in terms of the
+# getdtablesize(2) call, and there would be no implicit limits).
+#
+# This means that the implicit-yield threading is topologically
+# equivalent to preemptive threading.
+#
+THREADS= -D_THREAD_SAFE -DPOSIX_THREADS -DPTHREAD_PREEMPTIVE
+THREADSLIB= -pthread
+
+# we need to link in the V3 library to get sigset()
+PLATFORMLIBS= -lcrypt
 
 #
 # -------------------------------------------------------------------------
 # you will probably not need to edit anything below this point
 # -------------------------------------------------------------------------
-CC	= cc
+CC	= gcc
 
 PLATFORMCFLAGS=	-Dfreebsd
 
diff --git a/clients/finger/main.c b/clients/finger/main.c
index 38c8dfc769..64640543a4 100644
--- a/clients/finger/main.c
+++ b/clients/finger/main.c
@@ -172,11 +172,20 @@ static do_query()
 		exit( 1 );
 	}
 
+#ifdef FD_SETSIZE
+	/*
+	 * It is invalid to use a set size in excess of the type
+	 * scope, as defined for the fd_set in sys/types.h.  This
+	 * is true for any OS.
+	 */
+	tblsize = FD_SETSIZE;
+#else	/* !FD_SETSIZE*/
 #ifdef USE_SYSCONF
 	tblsize = sysconf( _SC_OPEN_MAX );
 #else /* USE_SYSCONF */
 	tblsize = getdtablesize();
 #endif /* USE_SYSCONF */
+#endif	/* !FD_SETSIZE*/
 
 	timeout.tv_sec = FINGER_TIMEOUT;
 	timeout.tv_usec = 0;
diff --git a/clients/gopher/go500.c b/clients/gopher/go500.c
index 333787677a..484c39c556 100644
--- a/clients/gopher/go500.c
+++ b/clients/gopher/go500.c
@@ -140,11 +140,20 @@ char	**argv;
 	}
 #endif
 
+#ifdef FD_SETSIZE
+	/*
+	 * It is invalid to use a set size in excess of the type
+	 * scope, as defined for the fd_set in sys/types.h.  This
+	 * is true for any OS.
+	 */
+	dtblsize = FD_SETSIZE;
+#else	/* !FD_SETSIZE*/
 #ifdef USE_SYSCONF
 	dtblsize = sysconf( _SC_OPEN_MAX );
 #else /* USE_SYSCONF */
 	dtblsize = getdtablesize();
 #endif /* USE_SYSCONF */
+#endif	/* !FD_SETSIZE*/
 
 	/* detach if stderr is redirected or no debugging */
 	if ( inetd == 0 )
diff --git a/clients/gopher/go500gw.c b/clients/gopher/go500gw.c
index 7f74fbf57c..01d8b0cbc4 100644
--- a/clients/gopher/go500gw.c
+++ b/clients/gopher/go500gw.c
@@ -150,11 +150,20 @@ char	**argv;
 		}
 	}
 
+#ifdef FD_SETSIZE
+	/*
+	 * It is invalid to use a set size in excess of the type
+	 * scope, as defined for the fd_set in sys/types.h.  This
+	 * is true for any OS.
+	 */
+	dtblsize = FD_SETSIZE;
+#else	/* !FD_SETSIZE*/
 #ifdef USE_SYSCONF
 	dtblsize = sysconf( _SC_OPEN_MAX );
 #else /* USE_SYSCONF */
 	dtblsize = getdtablesize();
 #endif /* USE_SYSCONF */
+#endif	/* !FD_SETSIZE*/
 
 #ifdef GO500GW_HOSTNAME
 	strcpy( myhost, GO500GW_HOSTNAME );
diff --git a/contrib/whois++/command.c b/contrib/whois++/command.c
index 7c57b8b83c..f9688b37e5 100644
--- a/contrib/whois++/command.c
+++ b/contrib/whois++/command.c
@@ -50,7 +50,16 @@ char	*token;
 	int		i, status, tablesize;
 
 	if ( buffer == NULL ) {
+#ifdef FD_SETSIZE
+		/*
+		 * It is invalid to use a set size in excess of the type
+		 * scope, as defined for the fd_set in sys/types.h.  This
+		 * is true for any OS.
+		 */
+		tablesize = FD_SETSIZE;
+#else	/* !FD_SETSIZE*/
 		tablesize = getdtablesize();
+#endif	/* !FD_SETSIZE*/
 		timeout.tv_sec = 60;
 		timeout.tv_usec = 0;
 		FD_ZERO( &readfds );
diff --git a/libraries/liblber/decode.c b/libraries/liblber/decode.c
index a4fe65ae81..2fd2a6252b 100644
--- a/libraries/liblber/decode.c
+++ b/libraries/liblber/decode.c
@@ -17,13 +17,12 @@
 #include <stdarg.h>
 #include "macos.h"
 #else /* MACOS */
-#if defined(NeXT) || defined(VMS)
+
+#if defined(NeXT) || defined(VMS) || defined(__FreeBSD__)
 #include <stdlib.h>
-#else /* next || vms */
-#ifndef __FreeBSD__
+#else /* next || vms || freebsd */
 #include <malloc.h>
-#endif
-#endif /* next || vms */
+#endif /* next || vms || freebsd */
 #if defined(BC31) || defined(_WIN32)
 #include <stdarg.h>
 #else /* BC31 || _WIN32 */
diff --git a/libraries/liblber/encode.c b/libraries/liblber/encode.c
index cb9e30ffa0..c8a95e2a19 100644
--- a/libraries/liblber/encode.c
+++ b/libraries/liblber/encode.c
@@ -17,14 +17,12 @@
 #include <stdarg.h>
 #include "macos.h"
 #else /* MACOS */
-#if defined(NeXT) || defined(VMS)
+#if defined(NeXT) || defined(VMS) || defined(__FreeBSD__)
 #include <stdlib.h>
-#else /* next || vms */
-#ifndef __FreeBSD__
+#else /* next || vms || freebsd */
 #include <malloc.h>
-#endif
-#endif /* next || vms */
-#if defined( BC31 ) || defined( _WIN32 ) 
+#endif /* next || vms || freebsd */
+#if defined( BC31 ) || defined( _WIN32 )
 #include <stdarg.h>
 #else /* BC31 || _WIN32 */
 #include <varargs.h>
diff --git a/libraries/libldap/os-ip.c b/libraries/libldap/os-ip.c
index 955a5fab52..d7ae408a82 100644
--- a/libraries/libldap/os-ip.c
+++ b/libraries/libldap/os-ip.c
@@ -314,11 +314,20 @@ do_ldap_select( LDAP *ld, struct timeval *timeout )
 	Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
 
 	if ( tblsize == 0 ) {
+#ifdef FD_SETSIZE
+		/*
+		 * It is invalid to use a set size in excess of the type
+		 * scope, as defined for the fd_set in sys/types.h.  This
+		 * is true for any OS.
+		 */
+		tblsize = FD_SETSIZE;
+#else	/* !FD_SETSIZE*/
 #ifdef USE_SYSCONF
 		tblsize = sysconf( _SC_OPEN_MAX );
 #else /* USE_SYSCONF */
 		tblsize = getdtablesize();
 #endif /* USE_SYSCONF */
+#endif	/* !FD_SETSIZE*/
 	}
 
 	sip = (struct selectinfo *)ld->ld_selectinfo;
diff --git a/libraries/libldap/result.c b/libraries/libldap/result.c
index 39f26522c2..77929120eb 100644
--- a/libraries/libldap/result.c
+++ b/libraries/libldap/result.c
@@ -543,12 +543,17 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 		*result = l;
 		ld->ld_errno = LDAP_SUCCESS;
 #ifdef LDAP_WORLD_P16
-		/* inclusion of this patch causes searchs to hang on
-			multiple platforms */
+		/*
+		 * XXX questionable fix; see text for [P16] on
+		 * http://www.critical-angle.com/ldapworld/patch/
+		 *
+		 * inclusion of this patch causes searchs to hang on
+		 * multiple platforms
+		 */
 		return( l->lm_msgtype );
-#else
+#else	/* LDAP_WORLD_P16 */
 		return( tag );
-#endif
+#endif	/* !LDAP_WORLD_P16 */
 	}
 
 	return( -2 );	/* continue looking */
@@ -633,11 +638,20 @@ ldap_select1( LDAP *ld, struct timeval *timeout )
 	static int	tblsize;
 
 	if ( tblsize == 0 ) {
+#ifdef FD_SETSIZE
+		/*
+		 * It is invalid to use a set size in excess of the type
+		 * scope, as defined for the fd_set in sys/types.h.  This
+		 * is true for any OS.
+		 */
+		tblsize = FD_SETSIZE;
+#else	/* !FD_SETSIZE*/
 #ifdef USE_SYSCONF
 		tblsize = sysconf( _SC_OPEN_MAX );
 #else /* USE_SYSCONF */
 		tblsize = getdtablesize();
 #endif /* USE_SYSCONF */
+#endif	/* !FD_SETSIZE*/
 	}
 
 	FD_ZERO( &readfds );
diff --git a/libraries/msdos/winsock/wsockip.c b/libraries/msdos/winsock/wsockip.c
index 307541cfc2..5c86cd771f 100644
--- a/libraries/msdos/winsock/wsockip.c
+++ b/libraries/msdos/winsock/wsockip.c
@@ -446,6 +446,14 @@ do_ldap_select( LDAP *ld, struct timeval *timeout )
 	Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
 
 	if ( tblsize == 0 ) {
+#ifdef FD_SETSIZE
+		/*
+		 * It is invalid to use a set size in excess of the type
+		 * scope, as defined for the fd_set in sys/types.h.  This
+		 * is true for any OS.
+		 */
+		tblsize = FD_SETSIZE;
+#else	/* !FD_SETSIZE*/
 #ifdef USE_SYSCONF
 		tblsize = sysconf( _SC_OPEN_MAX );
 #else /* USE_SYSCONF */
@@ -455,6 +463,7 @@ do_ldap_select( LDAP *ld, struct timeval *timeout )
 		tblsize = getdtablesize();
 #endif
 #endif /* USE_SYSCONF */
+#endif	/* !FD_SETSIZE*/
 	}
 
 	sip = (struct selectinfo *)ld->ld_selectinfo;
diff --git a/servers/slapd/connection.c b/servers/slapd/connection.c
index df5618ad70..275e644af8 100644
--- a/servers/slapd/connection.c
+++ b/servers/slapd/connection.c
@@ -203,6 +203,22 @@ connection_activity(
 
 	pthread_attr_init( &attr );
 	pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
+#ifdef PTHREAD_MUTEX_INITIALIZER
+	/*
+	 * This is a draft 10 or standard pthreads implementation
+	 */
+	if ( pthread_create( &arg->co_op->o_tid, attr,
+	    (void *) connection_operation, (void *) arg ) != 0 ) {
+		Debug( LDAP_DEBUG_ANY, "pthread_create failed\n", 0, 0, 0 );
+	} else {
+		pthread_mutex_lock( &active_threads_mutex );
+		active_threads++;
+		pthread_mutex_unlock( &active_threads_mutex );
+	}
+#else	/* !PTHREAD_MUTEX_INITIALIZER*/
+	/*
+	 * This is a draft 4 or earlier pthreads implementation
+	 */
 	if ( pthread_create( &arg->co_op->o_tid, &attr,
 	    (void *) connection_operation, (void *) arg ) != 0 ) {
 		Debug( LDAP_DEBUG_ANY, "pthread_create failed\n", 0, 0, 0 );
@@ -211,5 +227,6 @@ connection_activity(
 		active_threads++;
 		pthread_mutex_unlock( &active_threads_mutex );
 	}
+#endif	/* !PTHREAD_MUTEX_INITIALIZER*/
 	pthread_attr_destroy( &attr );
 }
diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c
index 38062d9b2b..c88ab7ecc9 100644
--- a/servers/slapd/daemon.c
+++ b/servers/slapd/daemon.c
@@ -78,16 +78,19 @@ slapd_daemon(
 	int			on = 1;
 
 #ifdef USE_SYSCONF
-        dtblsize = sysconf( _SC_OPEN_MAX );
+	dtblsize = sysconf( _SC_OPEN_MAX );
 #else /* USE_SYSCONF */
-        dtblsize = getdtablesize();
+	dtblsize = getdtablesize();
 #endif /* USE_SYSCONF */
 	/*
 	 * Add greg@greg.rim.or.jp
 	 */
+#ifdef FD_SETSIZE
 	if(dtblsize > FD_SETSIZE) {
 		dtblsize = FD_SETSIZE;
 	}
+#endif	/* !FD_SETSIZE*/
+
 	c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
 
 	for ( i = 0; i < dtblsize; i++ ) {
@@ -222,7 +225,7 @@ slapd_daemon(
 #endif
 		pthread_mutex_unlock( &active_threads_mutex );
 
-		switch ( select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
+		switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
 		case -1:	/* failure - try again */
 			Debug( LDAP_DEBUG_CONNS,
 			    "select failed errno %d (%s)\n",
@@ -237,7 +240,7 @@ slapd_daemon(
 			continue;
 
 		default:	/* something happened - deal with it */
-			Debug( LDAP_DEBUG_CONNS, "select activity\n", 0, 0, 0 );
+			Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
 			;	/* FALL */
 		}
 		pthread_mutex_lock( &currenttime_mutex );
diff --git a/servers/slapd/main.c b/servers/slapd/main.c
index 70b21821b0..b2777d544b 100644
--- a/servers/slapd/main.c
+++ b/servers/slapd/main.c
@@ -15,6 +15,7 @@ extern int	lber_debug;
 
 extern char Versionstr[];
 
+
 /*
  * read-only global variables or variables only written by the listener
  * thread (after they are initialized) - no need to protect them with a mutex.
@@ -184,12 +185,27 @@ main( argc, argv )
 		pthread_attr_init( &attr );
 		pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
 
+#ifdef PTHREAD_MUTEX_INITIALIZER
+		/*
+		 * This is a draft 10 or standard pthreads implementation
+		 */
+		if ( pthread_create( &listener_tid, attr, (void *) slapd_daemon,
+		    (void *) port ) != 0 ) {
+			Debug( LDAP_DEBUG_ANY,
+			    "listener pthread_create failed\n", 0, 0, 0 );
+			exit( 1 );
+		}
+#else	/* !PTHREAD_MUTEX_INITIALIZER */
+		/*
+		 * This is a draft 4 or earlier pthreads implementation
+		 */
 		if ( pthread_create( &listener_tid, &attr, (void *) slapd_daemon,
 		    (void *) port ) != 0 ) {
 			Debug( LDAP_DEBUG_ANY,
 			    "listener pthread_create failed\n", 0, 0, 0 );
 			exit( 1 );
 		}
+#endif	/* !PTHREAD_MUTEX_INITIALIZER */
 		pthread_attr_destroy( &attr );
 		pthread_join( listener_tid, (void *) &status );
 		pthread_exit( 0 );
diff --git a/servers/slurpd/re.c b/servers/slurpd/re.c
index 18043d98f8..401630d7de 100644
--- a/servers/slurpd/re.c
+++ b/servers/slurpd/re.c
@@ -33,7 +33,7 @@ extern void ch_free( char *p );
 
 #ifndef	SYSERRLIST_IN_STDIO
 extern char *sys_errlist[];
-#endif /* SYSERRLIST_IN_STDIO
+#endif /* SYSERRLIST_IN_STDIO */
 
 /* Forward references */
 static Rh 	*get_repl_hosts( char *, int *, char ** );
diff --git a/servers/slurpd/replica.c b/servers/slurpd/replica.c
index ce878390fd..59c2f72672 100644
--- a/servers/slurpd/replica.c
+++ b/servers/slurpd/replica.c
@@ -59,6 +59,21 @@ start_replica_thread(
     pthread_attr_init( &attr );
     pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
 
+#ifdef PTHREAD_MUTEX_INITIALIZER
+    /*
+     * This is a draft 10 or standard pthreads implementation
+     */
+    if ( pthread_create( &(ri->ri_tid), attr, (void *) replicate,
+	    (void *) ri ) != 0 ) {
+	Debug( LDAP_DEBUG_ANY, "replica \"%s:%d\" pthread_create failed\n",
+		ri->ri_hostname, ri->ri_port, 0 );
+	pthread_attr_destroy( &attr );
+	return -1;
+    }
+#else	/* !PTHREAD_MUTEX_INITIALIZER */
+    /*
+     * This is a draft 4 or earlier pthreads implementation
+     */
     if ( pthread_create( &(ri->ri_tid), &attr, (void *) replicate,
 	    (void *) ri ) != 0 ) {
 	Debug( LDAP_DEBUG_ANY, "replica \"%s:%d\" pthread_create failed\n",
@@ -66,6 +81,7 @@ start_replica_thread(
 	pthread_attr_destroy( &attr );
 	return -1;
     }
+#endif	/* !PTHREAD_MUTEX_INITIALIZER */
     pthread_attr_destroy( &attr );
     return 0;
 }
diff --git a/servers/slurpd/rq.c b/servers/slurpd/rq.c
index ef94cfa935..3fb842d48e 100644
--- a/servers/slurpd/rq.c
+++ b/servers/slurpd/rq.c
@@ -44,9 +44,9 @@ extern void Re_dump( Re *re );
 extern void Re_dump();
 #endif /* NEEDPROTOS */
 
-#ifndef	SYSERRLIST_IN_STDIO
+#ifndef SYSERRLIST_IN_STDIO
 extern char *sys_errlist[];
-#endif /* SYSERRLIST_IN_STDIO
+#endif /* SYSERRLIST_IN_STDIO */
 
 /*
  * Lock the replication queue.
-- 
GitLab