17c478bd9Sstevel@tonic-gate /*
2d4204c85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
87c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
97c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
107c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
137c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
157c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
187c478bd9Sstevel@tonic-gate  * March 31, 1998.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
217c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
227c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bd9Sstevel@tonic-gate  * Rights Reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Contributor(s):
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Thread callback functions for libldap that use the NSPR (Netscape
307c478bd9Sstevel@tonic-gate  * Portable Runtime) thread API.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
357c478bd9Sstevel@tonic-gate #include <thread.h>
367c478bd9Sstevel@tonic-gate #include <synch.h>
377c478bd9Sstevel@tonic-gate #include <prinit.h>
387c478bd9Sstevel@tonic-gate #include <prthread.h>
397c478bd9Sstevel@tonic-gate #include <syslog.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate extern int	errno;
457c478bd9Sstevel@tonic-gate #endif	/* _SOLARIS_SDK */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include "ldappr-int.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Macros:
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Grow thread private data arrays 10 elements at a time.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate #define PRLDAP_TPD_ARRAY_INCREMENT	10
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * Structures and types:
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Structure used by libldap thread callbacks to maintain error information.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate typedef struct prldap_errorinfo {
657c478bd9Sstevel@tonic-gate     int		plei_lderrno;
667c478bd9Sstevel@tonic-gate     char	*plei_matched;
677c478bd9Sstevel@tonic-gate     char	*plei_errmsg;
687c478bd9Sstevel@tonic-gate } PRLDAP_ErrorInfo;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Structure used to maintain thread-private data. At the present time,
727c478bd9Sstevel@tonic-gate  * only error info. is thread-private.  One of these structures is allocated
737c478bd9Sstevel@tonic-gate  * for each thread.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate typedef struct prldap_tpd_header {
767c478bd9Sstevel@tonic-gate     int			ptpdh_tpd_count;	/* # of data items allocated */
777c478bd9Sstevel@tonic-gate     void		**ptpdh_dataitems;	/* array of data items */
787c478bd9Sstevel@tonic-gate } PRLDAP_TPDHeader;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Structure used by associate a PRLDAP thread-private data index with an
827c478bd9Sstevel@tonic-gate  * LDAP session handle. One of these exists for each active LDAP session
837c478bd9Sstevel@tonic-gate  * handle.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate typedef struct prldap_tpd_map {
867c478bd9Sstevel@tonic-gate     LDAP			*prtm_ld;	/* non-NULL if in use */
877c478bd9Sstevel@tonic-gate     PRUintn			prtm_index;	/* index into TPD array */
887c478bd9Sstevel@tonic-gate     struct prldap_tpd_map	*prtm_next;
897c478bd9Sstevel@tonic-gate } PRLDAP_TPDMap;
907c478bd9Sstevel@tonic-gate 
91*1da57d55SToomas Soome #ifdef _SOLARIS_SDK
92*1da57d55SToomas Soome extern  mutex_t         inited_mutex;
93*1da57d55SToomas Soome #endif  /* _SOLARIS_SDK */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Static Variables:
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * prldap_map_list points to all of the PRLDAP_TPDMap structures
1007c478bd9Sstevel@tonic-gate  * we have ever allocated.  We recycle them as we open and close LDAP
1017c478bd9Sstevel@tonic-gate  * sessions.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate static PRLDAP_TPDMap *prldap_map_list = NULL;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * The prldap_map_mutex is used to protect access to the prldap_map_list.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static PRLock	*prldap_map_mutex = NULL;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * The prldap_tpd_maxindex value is used to track the largest TPD array
1137c478bd9Sstevel@tonic-gate  * index we have used.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate static PRInt32	prldap_tpd_maxindex = -1;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * prldap_tpdindex is an NSPR thread private data index we use to
1197c478bd9Sstevel@tonic-gate  * maintain our own thread-private data. It is initialized inside
1207c478bd9Sstevel@tonic-gate  * prldap_init_tpd().
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static PRUintn	prldap_tpdindex = 0;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * The prldap_callonce_init_tpd structure is used by NSPR to ensure
1267c478bd9Sstevel@tonic-gate  * that prldap_init_tpd() is called at most once.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate static PRCallOnceType prldap_callonce_init_tpd = { 0, 0, 0 };
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Private function prototypes:
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate static void prldap_set_ld_error( int err, char *matched, char *errmsg,
1357c478bd9Sstevel@tonic-gate 	void *errorarg );
1367c478bd9Sstevel@tonic-gate static int prldap_get_ld_error( char **matchedp, char **errmsgp,
1377c478bd9Sstevel@tonic-gate 	void *errorarg );
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate static void *prldap_mutex_alloc( void );
1407c478bd9Sstevel@tonic-gate static void prldap_mutex_free( void *mutex );
1417c478bd9Sstevel@tonic-gate static int prldap_mutex_lock( void *mutex );
1427c478bd9Sstevel@tonic-gate static int prldap_mutex_unlock( void *mutex );
1437c478bd9Sstevel@tonic-gate static void *prldap_get_thread_id( void );
1447c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
1457c478bd9Sstevel@tonic-gate static PRStatus prldap_init_tpd( void );
1467c478bd9Sstevel@tonic-gate static PRLDAP_TPDMap *prldap_allocate_map( LDAP *ld );
1477c478bd9Sstevel@tonic-gate static void prldap_return_map( PRLDAP_TPDMap *map );
1487c478bd9Sstevel@tonic-gate static PRUintn prldap_new_tpdindex( void );
1497c478bd9Sstevel@tonic-gate static int prldap_set_thread_private( PRInt32 tpdindex, void *priv );
1507c478bd9Sstevel@tonic-gate static void *prldap_get_thread_private( PRInt32 tpdindex );
1517c478bd9Sstevel@tonic-gate static PRLDAP_TPDHeader *prldap_tsd_realloc( PRLDAP_TPDHeader *tsdhdr,
1527c478bd9Sstevel@tonic-gate 	int maxindex );
1537c478bd9Sstevel@tonic-gate static void prldap_tsd_destroy( void *priv );
1547c478bd9Sstevel@tonic-gate #endif
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Install NSPR thread functions into ld (if ld is NULL, they are installed
1597c478bd9Sstevel@tonic-gate  * as the default functions for new LDAP * handles).
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  * Returns 0 if all goes well and -1 if not.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate int
prldap_install_thread_functions(LDAP * ld,int shared)1647c478bd9Sstevel@tonic-gate prldap_install_thread_functions( LDAP *ld, int shared )
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate     struct ldap_thread_fns		tfns;
1677c478bd9Sstevel@tonic-gate     struct ldap_extra_thread_fns	xtfns;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
1707c478bd9Sstevel@tonic-gate     if ( PR_CallOnce( &prldap_callonce_init_tpd, prldap_init_tpd )
1717c478bd9Sstevel@tonic-gate 		!= PR_SUCCESS ) {
1727c478bd9Sstevel@tonic-gate 	ldap_set_lderrno( ld, LDAP_LOCAL_ERROR, NULL, NULL );
1737c478bd9Sstevel@tonic-gate 	return( -1 );
1747c478bd9Sstevel@tonic-gate     }
1757c478bd9Sstevel@tonic-gate #endif	/* _SOLARIS_SDK */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate     /* set thread function pointers */
1787c478bd9Sstevel@tonic-gate     memset( &tfns, '\0', sizeof(struct ldap_thread_fns) );
1797c478bd9Sstevel@tonic-gate     tfns.ltf_get_errno = prldap_get_system_errno;
1807c478bd9Sstevel@tonic-gate     tfns.ltf_set_errno = prldap_set_system_errno;
1817c478bd9Sstevel@tonic-gate     if ( shared ) {
1827c478bd9Sstevel@tonic-gate 	tfns.ltf_mutex_alloc = prldap_mutex_alloc;
1837c478bd9Sstevel@tonic-gate 	tfns.ltf_mutex_free = prldap_mutex_free;
1847c478bd9Sstevel@tonic-gate 	tfns.ltf_mutex_lock = prldap_mutex_lock;
1857c478bd9Sstevel@tonic-gate 	tfns.ltf_mutex_unlock = prldap_mutex_unlock;
1867c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
1877c478bd9Sstevel@tonic-gate 	tfns.ltf_get_lderrno = NULL;
1887c478bd9Sstevel@tonic-gate 	tfns.ltf_set_lderrno = NULL;
1897c478bd9Sstevel@tonic-gate #else
1907c478bd9Sstevel@tonic-gate 	tfns.ltf_get_lderrno = prldap_get_ld_error;
1917c478bd9Sstevel@tonic-gate 	tfns.ltf_set_lderrno = prldap_set_ld_error;
1927c478bd9Sstevel@tonic-gate 	if ( ld != NULL ) {
1937c478bd9Sstevel@tonic-gate 	    /*
1947c478bd9Sstevel@tonic-gate 	     * If this is a real ld (i.e., we are not setting the global
1957c478bd9Sstevel@tonic-gate 	     * defaults) allocate thread private data for error information.
1967c478bd9Sstevel@tonic-gate 	     * If ld is NULL we do not do this here but it is done in
1977c478bd9Sstevel@tonic-gate 	     * prldap_thread_new_handle().
1987c478bd9Sstevel@tonic-gate 	     */
1997c478bd9Sstevel@tonic-gate 	    if (( tfns.ltf_lderrno_arg = (void *)prldap_allocate_map( ld ))
2007c478bd9Sstevel@tonic-gate 		    == NULL ) {
2017c478bd9Sstevel@tonic-gate 		return( -1 );
2027c478bd9Sstevel@tonic-gate 	    }
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate #endif
2057c478bd9Sstevel@tonic-gate     }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate     if ( ldap_set_option( ld, LDAP_OPT_THREAD_FN_PTRS,
2087c478bd9Sstevel@tonic-gate 	    (void *)&tfns ) != 0 ) {
2097c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
2107c478bd9Sstevel@tonic-gate 	prldap_return_map( (PRLDAP_TPDMap *)tfns.ltf_lderrno_arg );
2117c478bd9Sstevel@tonic-gate #endif
2127c478bd9Sstevel@tonic-gate 	return( -1 );
2137c478bd9Sstevel@tonic-gate     }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate     /* set extended thread function pointers */
2167c478bd9Sstevel@tonic-gate     memset( &xtfns, '\0', sizeof(struct ldap_extra_thread_fns) );
2177c478bd9Sstevel@tonic-gate     xtfns.ltf_threadid_fn = prldap_get_thread_id;
2187c478bd9Sstevel@tonic-gate     if ( ldap_set_option( ld, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
2197c478bd9Sstevel@tonic-gate 	    (void *)&xtfns ) != 0 ) {
2207c478bd9Sstevel@tonic-gate 	return( -1 );
2217c478bd9Sstevel@tonic-gate     }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate     return( 0 );
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static void *
prldap_mutex_alloc(void)2287c478bd9Sstevel@tonic-gate prldap_mutex_alloc( void )
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate     return( (void *)PR_NewLock());
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static void
prldap_mutex_free(void * mutex)2357c478bd9Sstevel@tonic-gate prldap_mutex_free( void *mutex )
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate     PR_DestroyLock( (PRLock *)mutex );
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static int
prldap_mutex_lock(void * mutex)2427c478bd9Sstevel@tonic-gate prldap_mutex_lock( void *mutex )
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate     PR_Lock( (PRLock *)mutex );
2457c478bd9Sstevel@tonic-gate     return( 0 );
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static int
prldap_mutex_unlock(void * mutex)2507c478bd9Sstevel@tonic-gate prldap_mutex_unlock( void *mutex )
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate     if ( PR_Unlock( (PRLock *)mutex ) == PR_FAILURE ) {
2537c478bd9Sstevel@tonic-gate 	return( -1 );
2547c478bd9Sstevel@tonic-gate     }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate     return( 0 );
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static void *
prldap_get_thread_id(void)2617c478bd9Sstevel@tonic-gate prldap_get_thread_id( void )
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate #ifdef	_SOLARIS_SDK
2647c478bd9Sstevel@tonic-gate 	return ((void *)thr_self());
2657c478bd9Sstevel@tonic-gate #else
2667c478bd9Sstevel@tonic-gate     return( (void *)PR_GetCurrentThread());
2677c478bd9Sstevel@tonic-gate #endif
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate #ifndef	_SOLARIS_SDK
2717c478bd9Sstevel@tonic-gate static int
prldap_get_ld_error(char ** matchedp,char ** errmsgp,void * errorarg)2727c478bd9Sstevel@tonic-gate prldap_get_ld_error( char **matchedp, char **errmsgp, void *errorarg )
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate     PRLDAP_TPDMap	*map;
2757c478bd9Sstevel@tonic-gate     PRLDAP_ErrorInfo	*eip;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate     if (( map = (PRLDAP_TPDMap *)errorarg ) != NULL && ( eip =
2787c478bd9Sstevel@tonic-gate 	    (PRLDAP_ErrorInfo *)prldap_get_thread_private(
2797c478bd9Sstevel@tonic-gate 	    map->prtm_index )) != NULL ) {
2807c478bd9Sstevel@tonic-gate 	if ( matchedp != NULL ) {
2817c478bd9Sstevel@tonic-gate 	    *matchedp = eip->plei_matched;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	if ( errmsgp != NULL ) {
2847c478bd9Sstevel@tonic-gate 	    *errmsgp = eip->plei_errmsg;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	return( eip->plei_lderrno );
2877c478bd9Sstevel@tonic-gate     } else {
2887c478bd9Sstevel@tonic-gate 	if ( matchedp != NULL ) {
2897c478bd9Sstevel@tonic-gate 	    *matchedp = NULL;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	if ( errmsgp != NULL ) {
2927c478bd9Sstevel@tonic-gate 	    *errmsgp = NULL;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	return( LDAP_LOCAL_ERROR );	/* punt */
2957c478bd9Sstevel@tonic-gate     }
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate static void
prldap_set_ld_error(int err,char * matched,char * errmsg,void * errorarg)3007c478bd9Sstevel@tonic-gate prldap_set_ld_error( int err, char *matched, char *errmsg, void *errorarg )
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate     PRLDAP_TPDMap	*map;
3037c478bd9Sstevel@tonic-gate     PRLDAP_ErrorInfo	*eip;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate     if (( map = (PRLDAP_TPDMap *)errorarg ) != NULL ) {
3067c478bd9Sstevel@tonic-gate 	if (( eip = (PRLDAP_ErrorInfo *)prldap_get_thread_private(
3077c478bd9Sstevel@tonic-gate 		map->prtm_index )) == NULL ) {
3087c478bd9Sstevel@tonic-gate 	    /*
3097c478bd9Sstevel@tonic-gate 	     * Error info. has not yet been allocated for this thread.
3107c478bd9Sstevel@tonic-gate 	     * Do so now.  Note that we free this memory only for the
3117c478bd9Sstevel@tonic-gate 	     * thread that calls prldap_thread_dispose_handle(), which
3127c478bd9Sstevel@tonic-gate 	     * should be the one that called ldap_unbind() -- see
3137c478bd9Sstevel@tonic-gate 	     * prldap_return_map().  Not freeing the memory used by
3147c478bd9Sstevel@tonic-gate 	     * other threads is deemed acceptable since it will be
3157c478bd9Sstevel@tonic-gate 	     * recycled and used by other LDAP sessions.  All of the
3167c478bd9Sstevel@tonic-gate 	     * thread-private memory is freed when a thread exits
3177c478bd9Sstevel@tonic-gate 	     * (inside the prldap_tsd_destroy() function).
3187c478bd9Sstevel@tonic-gate 	     */
3197c478bd9Sstevel@tonic-gate 	    eip = (PRLDAP_ErrorInfo *)PR_Calloc( 1,
3207c478bd9Sstevel@tonic-gate 		    sizeof( PRLDAP_ErrorInfo ));
3217c478bd9Sstevel@tonic-gate 	    if ( eip == NULL ) {
3227c478bd9Sstevel@tonic-gate 		return;	/* punt */
3237c478bd9Sstevel@tonic-gate 	    }
3247c478bd9Sstevel@tonic-gate 	    (void)prldap_set_thread_private( map->prtm_index, eip );
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	eip->plei_lderrno = err;
3287c478bd9Sstevel@tonic-gate 	if ( eip->plei_matched != NULL ) {
3297c478bd9Sstevel@tonic-gate 	    ldap_memfree( eip->plei_matched );
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	eip->plei_matched = matched;
3327c478bd9Sstevel@tonic-gate 	if ( eip->plei_errmsg != NULL ) {
3337c478bd9Sstevel@tonic-gate 	    ldap_memfree( eip->plei_errmsg );
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	eip->plei_errmsg = errmsg;
3367c478bd9Sstevel@tonic-gate     }
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate #endif
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * Called when a new LDAP * session handle is allocated.
3437c478bd9Sstevel@tonic-gate  * Allocate thread-private data for error information, but only if
3447c478bd9Sstevel@tonic-gate  * it has not already been allocated and the get_ld_error callback has
3457c478bd9Sstevel@tonic-gate  * been installed.  If ld is not NULL when prldap_install_thread_functions()
3467c478bd9Sstevel@tonic-gate  * is called, we will have already allocated the thread-private data there.
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate int
prldap_thread_new_handle(LDAP * ld,void * sessionarg)3497c478bd9Sstevel@tonic-gate prldap_thread_new_handle( LDAP *ld, void *sessionarg )
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate     struct ldap_thread_fns	tfns;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
3547c478bd9Sstevel@tonic-gate     if ( ldap_get_option( ld, LDAP_OPT_THREAD_FN_PTRS, (void *)&tfns ) != 0 ) {
3557c478bd9Sstevel@tonic-gate 	return( LDAP_LOCAL_ERROR );
3567c478bd9Sstevel@tonic-gate     }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate     if ( tfns.ltf_lderrno_arg == NULL && tfns.ltf_get_lderrno != NULL ) {
3597c478bd9Sstevel@tonic-gate 	if (( tfns.ltf_lderrno_arg = (void *)prldap_allocate_map( ld )) == NULL
3607c478bd9Sstevel@tonic-gate 		|| ldap_set_option( ld, LDAP_OPT_THREAD_FN_PTRS,
3617c478bd9Sstevel@tonic-gate 		(void *)&tfns ) != 0 ) {
3627c478bd9Sstevel@tonic-gate 	    return( LDAP_LOCAL_ERROR );
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate     }
3657c478bd9Sstevel@tonic-gate #endif
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * Called when an LDAP * session handle is being destroyed.
3737c478bd9Sstevel@tonic-gate  * Clean up our thread private data map.
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate void
prldap_thread_dispose_handle(LDAP * ld,void * sessionarg)3767c478bd9Sstevel@tonic-gate prldap_thread_dispose_handle( LDAP *ld, void *sessionarg )
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
3797c478bd9Sstevel@tonic-gate     struct ldap_thread_fns	tfns;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate     if ( ldap_get_option( ld, LDAP_OPT_THREAD_FN_PTRS,
3827c478bd9Sstevel@tonic-gate 	    (void *)&tfns ) == 0 &&
3837c478bd9Sstevel@tonic-gate 	    tfns.ltf_lderrno_arg != NULL ) {
3847c478bd9Sstevel@tonic-gate 	prldap_return_map( (PRLDAP_TPDMap *)tfns.ltf_lderrno_arg );
3857c478bd9Sstevel@tonic-gate     }
3867c478bd9Sstevel@tonic-gate #endif
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
3917c478bd9Sstevel@tonic-gate static PRStatus
prldap_init_tpd(void)3927c478bd9Sstevel@tonic-gate prldap_init_tpd( void )
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate     if (( prldap_map_mutex = PR_NewLock()) == NULL || PR_NewThreadPrivateIndex(
3957c478bd9Sstevel@tonic-gate 		&prldap_tpdindex, prldap_tsd_destroy ) != PR_SUCCESS ) {
3967c478bd9Sstevel@tonic-gate 	return( PR_FAILURE );
3977c478bd9Sstevel@tonic-gate     }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate     prldap_map_list = NULL;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate     return( PR_SUCCESS );
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * Function: prldap_allocate_map()
4077c478bd9Sstevel@tonic-gate  * Description: allocate a thread-private data map to use for a new
4087c478bd9Sstevel@tonic-gate  *	LDAP session handle.
4097c478bd9Sstevel@tonic-gate  * Returns: a pointer to the TPD map or NULL if none available.
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate static PRLDAP_TPDMap *
prldap_allocate_map(LDAP * ld)4127c478bd9Sstevel@tonic-gate prldap_allocate_map( LDAP *ld )
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate     PRLDAP_TPDMap	*map, *prevmap;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate     PR_Lock( prldap_map_mutex );
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate     /*
4197c478bd9Sstevel@tonic-gate      * first look for a map that is already allocated but free to be re-used
4207c478bd9Sstevel@tonic-gate      */
4217c478bd9Sstevel@tonic-gate     prevmap = NULL;
4227c478bd9Sstevel@tonic-gate     for ( map = prldap_map_list; map != NULL; map = map->prtm_next ) {
4237c478bd9Sstevel@tonic-gate 	if ( map->prtm_ld == NULL ) {
4247c478bd9Sstevel@tonic-gate 	    break;
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 	prevmap = map;
4277c478bd9Sstevel@tonic-gate     }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate     /*
4307c478bd9Sstevel@tonic-gate      * if none we found (map == NULL), try to allocate a new one and add it
4317c478bd9Sstevel@tonic-gate      * to the end of our global list.
4327c478bd9Sstevel@tonic-gate      */
4337c478bd9Sstevel@tonic-gate     if ( map == NULL ) {
4347c478bd9Sstevel@tonic-gate 	PRUintn	tpdindex;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	tpdindex = prldap_new_tpdindex();
4377c478bd9Sstevel@tonic-gate 	map = (PRLDAP_TPDMap *)PR_Malloc( sizeof( PRLDAP_TPDMap ));
4387c478bd9Sstevel@tonic-gate 	if ( map != NULL ) {
4397c478bd9Sstevel@tonic-gate 	    map->prtm_index = tpdindex;
4407c478bd9Sstevel@tonic-gate 	    map->prtm_next = NULL;
4417c478bd9Sstevel@tonic-gate 	    if ( prevmap == NULL ) {
4427c478bd9Sstevel@tonic-gate 		prldap_map_list = map;
4437c478bd9Sstevel@tonic-gate 	    } else {
4447c478bd9Sstevel@tonic-gate 		prevmap->prtm_next = map;
4457c478bd9Sstevel@tonic-gate 	    }
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate     }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate     if ( map != NULL ) {
4507c478bd9Sstevel@tonic-gate 	map->prtm_ld = ld;	/* now marked as "in use" */
4517c478bd9Sstevel@tonic-gate 				/* since we are reusing...reset */
4527c478bd9Sstevel@tonic-gate 				/* to initial state */
4537c478bd9Sstevel@tonic-gate 	(void)prldap_set_thread_private( map->prtm_index, NULL );
4547c478bd9Sstevel@tonic-gate     }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate     PR_Unlock( prldap_map_mutex );
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate     return( map );
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * Function: prldap_return_map()
4647c478bd9Sstevel@tonic-gate  * Description: return a thread-private data map to the pool of ones
4657c478bd9Sstevel@tonic-gate  *	available for re-use.
4667c478bd9Sstevel@tonic-gate  */
4677c478bd9Sstevel@tonic-gate static void
prldap_return_map(PRLDAP_TPDMap * map)4687c478bd9Sstevel@tonic-gate prldap_return_map( PRLDAP_TPDMap *map )
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate     PRLDAP_ErrorInfo	*eip;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate     PR_Lock( prldap_map_mutex );
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate     /*
4757c478bd9Sstevel@tonic-gate      * Dispose of thread-private LDAP error information.  Note that this
4767c478bd9Sstevel@tonic-gate      * only disposes of the memory consumed on THIS thread, but that is
4777c478bd9Sstevel@tonic-gate      * okay.  See the comment in prldap_set_ld_error() for the reason why.
4787c478bd9Sstevel@tonic-gate      */
4797c478bd9Sstevel@tonic-gate     if (( eip = (PRLDAP_ErrorInfo *)prldap_get_thread_private(
4807c478bd9Sstevel@tonic-gate 		map->prtm_index )) != NULL &&
4817c478bd9Sstevel@tonic-gate 		prldap_set_thread_private( map->prtm_index, NULL ) == 0 ) {
4827c478bd9Sstevel@tonic-gate 	if ( eip->plei_matched != NULL ) {
4837c478bd9Sstevel@tonic-gate 	    ldap_memfree( eip->plei_matched );
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 	if ( eip->plei_errmsg != NULL ) {
4867c478bd9Sstevel@tonic-gate 	    ldap_memfree( eip->plei_errmsg );
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	PR_Free( eip );
4907c478bd9Sstevel@tonic-gate     }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate     /* mark map as available for re-use */
4937c478bd9Sstevel@tonic-gate     map->prtm_ld = NULL;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate     PR_Unlock( prldap_map_mutex );
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * Function: prldap_new_tpdindex()
5017c478bd9Sstevel@tonic-gate  * Description: allocate a thread-private data index.
5027c478bd9Sstevel@tonic-gate  * Returns: the new index.
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate static PRUintn
prldap_new_tpdindex(void)5057c478bd9Sstevel@tonic-gate prldap_new_tpdindex( void )
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate     PRUintn	tpdindex;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate     tpdindex = (PRUintn)PR_AtomicIncrement( &prldap_tpd_maxindex );
5107c478bd9Sstevel@tonic-gate     return( tpdindex );
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * Function: prldap_set_thread_private()
5167c478bd9Sstevel@tonic-gate  * Description: store a piece of thread-private data.
5177c478bd9Sstevel@tonic-gate  * Returns: 0 if successful and -1 if not.
5187c478bd9Sstevel@tonic-gate  */
5197c478bd9Sstevel@tonic-gate static int
prldap_set_thread_private(PRInt32 tpdindex,void * priv)5207c478bd9Sstevel@tonic-gate prldap_set_thread_private( PRInt32 tpdindex, void *priv )
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate     PRLDAP_TPDHeader	*tsdhdr;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate     if ( tpdindex > prldap_tpd_maxindex ) {
525*1da57d55SToomas Soome 	return( -1 );	/* bad index */
5267c478bd9Sstevel@tonic-gate     }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate     tsdhdr = (PRLDAP_TPDHeader *)PR_GetThreadPrivate( prldap_tpdindex );
5297c478bd9Sstevel@tonic-gate     if ( tsdhdr == NULL || tpdindex >= tsdhdr->ptpdh_tpd_count ) {
5307c478bd9Sstevel@tonic-gate 	tsdhdr = prldap_tsd_realloc( tsdhdr, tpdindex );
5317c478bd9Sstevel@tonic-gate 	if ( tsdhdr == NULL ) {
5327c478bd9Sstevel@tonic-gate 	    return( -1 );	/* realloc failed */
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate     }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate     tsdhdr->ptpdh_dataitems[ tpdindex ] = priv;
5377c478bd9Sstevel@tonic-gate     return( 0 );
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate  * Function: prldap_get_thread_private()
5437c478bd9Sstevel@tonic-gate  * Description: retrieve a piece of thread-private data.  If not set,
5447c478bd9Sstevel@tonic-gate  *	NULL is returned.
5457c478bd9Sstevel@tonic-gate  * Returns: 0 if successful and -1 if not.
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate static void *
prldap_get_thread_private(PRInt32 tpdindex)5487c478bd9Sstevel@tonic-gate prldap_get_thread_private( PRInt32 tpdindex )
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate     PRLDAP_TPDHeader	*tsdhdr;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate     tsdhdr = (PRLDAP_TPDHeader *)PR_GetThreadPrivate( prldap_tpdindex );
5537c478bd9Sstevel@tonic-gate     if ( tsdhdr == NULL ) {
5547c478bd9Sstevel@tonic-gate 	return( NULL );	/* no thread private data */
5557c478bd9Sstevel@tonic-gate     }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate     if ( tpdindex >= tsdhdr->ptpdh_tpd_count
5587c478bd9Sstevel@tonic-gate 		|| tsdhdr->ptpdh_dataitems == NULL ) {
5597c478bd9Sstevel@tonic-gate 	return( NULL );	/* fewer data items than requested index */
5607c478bd9Sstevel@tonic-gate     }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate     return( tsdhdr->ptpdh_dataitems[ tpdindex ] );
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * Function: prldap_tsd_realloc()
5687c478bd9Sstevel@tonic-gate  * Description: enlarge the thread-private data array.
5697c478bd9Sstevel@tonic-gate  * Returns: the new PRLDAP_TPDHeader value (non-NULL if successful).
5707c478bd9Sstevel@tonic-gate  * Note: tsdhdr can be NULL (allocates a new PRLDAP_TPDHeader).
5717c478bd9Sstevel@tonic-gate  */
5727c478bd9Sstevel@tonic-gate static PRLDAP_TPDHeader *
prldap_tsd_realloc(PRLDAP_TPDHeader * tsdhdr,int maxindex)5737c478bd9Sstevel@tonic-gate prldap_tsd_realloc( PRLDAP_TPDHeader *tsdhdr, int maxindex )
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate     void	*newdataitems = NULL;
5767c478bd9Sstevel@tonic-gate     int		count;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate     if ( tsdhdr == NULL ) {
5797c478bd9Sstevel@tonic-gate 	/* allocate a new thread private data header */
5807c478bd9Sstevel@tonic-gate 	if (( tsdhdr = PR_Calloc( 1, sizeof( PRLDAP_TPDHeader ))) == NULL ) {
5817c478bd9Sstevel@tonic-gate 	    return( NULL );
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	(void)PR_SetThreadPrivate( prldap_tpdindex, tsdhdr );
5847c478bd9Sstevel@tonic-gate     }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate     /*
5877c478bd9Sstevel@tonic-gate      * Make the size of the new array the next highest multiple of
5887c478bd9Sstevel@tonic-gate      * the array increment value that is greater than maxindex.
5897c478bd9Sstevel@tonic-gate      */
5907c478bd9Sstevel@tonic-gate     count = PRLDAP_TPD_ARRAY_INCREMENT *
5917c478bd9Sstevel@tonic-gate 		( 1 + ( maxindex / PRLDAP_TPD_ARRAY_INCREMENT ));
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate     /* increase the size of the data item array if necessary */
5947c478bd9Sstevel@tonic-gate     if ( count > tsdhdr->ptpdh_tpd_count  ) {
5957c478bd9Sstevel@tonic-gate 	newdataitems = (PRLDAP_ErrorInfo *)PR_Calloc( count, sizeof( void * ));
5967c478bd9Sstevel@tonic-gate 	if ( newdataitems == NULL ) {
5977c478bd9Sstevel@tonic-gate 	    return( NULL );
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 	if ( tsdhdr->ptpdh_dataitems != NULL ) {	/* preserve old data */
6007c478bd9Sstevel@tonic-gate 	    memcpy( newdataitems, tsdhdr->ptpdh_dataitems,
6017c478bd9Sstevel@tonic-gate 			tsdhdr->ptpdh_tpd_count * sizeof( void * ));
6027c478bd9Sstevel@tonic-gate 	    PR_Free( tsdhdr->ptpdh_dataitems );
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	tsdhdr->ptpdh_tpd_count = count;
6067c478bd9Sstevel@tonic-gate 	tsdhdr->ptpdh_dataitems = newdataitems;
6077c478bd9Sstevel@tonic-gate     }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate     return( tsdhdr );
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate  * Function: prldap_tsd_destroy()
6157c478bd9Sstevel@tonic-gate  * Description: Free a thread-private data array. Installed as an NSPR TPD
6167c478bd9Sstevel@tonic-gate  *	destructor function
6177c478bd9Sstevel@tonic-gate  * Returns: nothing.
6187c478bd9Sstevel@tonic-gate  * Note: this function assumes that each TPD item installed at the PRLDAP
6197c478bd9Sstevel@tonic-gate  *	level can be freed with a call to PR_Free().
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate static void
prldap_tsd_destroy(void * priv)6227c478bd9Sstevel@tonic-gate prldap_tsd_destroy( void *priv )
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate     PRLDAP_TPDHeader	*tsdhdr;
6257c478bd9Sstevel@tonic-gate     int			i;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate     tsdhdr = (PRLDAP_TPDHeader *)priv;
6287c478bd9Sstevel@tonic-gate     if ( tsdhdr != NULL ) {
6297c478bd9Sstevel@tonic-gate 	if ( tsdhdr->ptpdh_dataitems != NULL ) {
6307c478bd9Sstevel@tonic-gate 	    for ( i = 0; i < tsdhdr->ptpdh_tpd_count; ++i ) {
6317c478bd9Sstevel@tonic-gate 		if ( tsdhdr->ptpdh_dataitems[ i ] != NULL ) {
6327c478bd9Sstevel@tonic-gate 		    PR_Free( tsdhdr->ptpdh_dataitems[ i ] );
6337c478bd9Sstevel@tonic-gate 		    tsdhdr->ptpdh_dataitems[ i ] = NULL;
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate 	    }
6367c478bd9Sstevel@tonic-gate 	    PR_Free( tsdhdr->ptpdh_dataitems );
6377c478bd9Sstevel@tonic-gate 	    tsdhdr->ptpdh_dataitems = NULL;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	PR_Free( tsdhdr );
6407c478bd9Sstevel@tonic-gate     }
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate #endif
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate #ifdef	_SOLARIS_SDK
6457c478bd9Sstevel@tonic-gate #pragma	init(prldap_nspr_init)
6467c478bd9Sstevel@tonic-gate static mutex_t	nspr_init_lock = DEFAULTMUTEX;
6477c478bd9Sstevel@tonic-gate static int	nspr_initialized = 0;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate  * Initialize NSPR once
6517c478bd9Sstevel@tonic-gate  *
6527c478bd9Sstevel@tonic-gate  */
6537c478bd9Sstevel@tonic-gate void
prldap_nspr_init(void)6547c478bd9Sstevel@tonic-gate prldap_nspr_init(void) {
6557c478bd9Sstevel@tonic-gate 	struct sigaction	action;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * For performance reason, test it here first
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	if (nspr_initialized != 0)
6617c478bd9Sstevel@tonic-gate 		return;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&nspr_init_lock);
6647c478bd9Sstevel@tonic-gate 	/* Make sure PR_Init() is executed only once */
6657c478bd9Sstevel@tonic-gate 	if (nspr_initialized == 0) {
6667c478bd9Sstevel@tonic-gate 		/*
6677c478bd9Sstevel@tonic-gate 		 * PR_Init changes the signal handler of SIGPIPE to SIG_IGN.
6687c478bd9Sstevel@tonic-gate 		 * Save the original and restore it after PR_Init.
6697c478bd9Sstevel@tonic-gate 		 */
6707c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGPIPE, NULL, &action);
6717c478bd9Sstevel@tonic-gate 
6721d367f3fSth 		if (PR_Initialized() == PR_FALSE) {
6737c478bd9Sstevel@tonic-gate 			/*
6741d367f3fSth 			 * PR_Init() changes the current thread's
6751d367f3fSth 			 * priority.  Save and restore the priority.
6767c478bd9Sstevel@tonic-gate 			 */
6771d367f3fSth 			int priority;
6781d367f3fSth 			(void) thr_getprio(thr_self(), &priority);
6791d367f3fSth 			PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
6801d367f3fSth 			(void) thr_setprio(thr_self(), priority);
6817c478bd9Sstevel@tonic-gate 		}
6821d367f3fSth 		nspr_initialized = 1;
6837c478bd9Sstevel@tonic-gate 		/*
6847c478bd9Sstevel@tonic-gate 		 * Restore signal handling attributes of SIGPIPE
6857c478bd9Sstevel@tonic-gate 		 */
6867c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGPIPE, &action, NULL);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&nspr_init_lock);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate #endif
691