/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
/*
* Thread callback functions for libldap that use the NSPR (Netscape
* Portable Runtime) thread API.
*
*/
#ifdef _SOLARIS_SDK
#include <thread.h>
#include <synch.h>
#include <prinit.h>
#include <prthread.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
extern int errno;
#endif /* _SOLARIS_SDK */
#include "ldappr-int.h"
#ifndef _SOLARIS_SDK
/*
* Macros:
*/
/*
* Grow thread private data arrays 10 elements at a time.
*/
/*
* Structures and types:
*/
/*
* Structure used by libldap thread callbacks to maintain error information.
*/
typedef struct prldap_errorinfo {
int plei_lderrno;
char *plei_matched;
char *plei_errmsg;
/*
* Structure used to maintain thread-private data. At the present time,
* only error info. is thread-private. One of these structures is allocated
* for each thread.
*/
typedef struct prldap_tpd_header {
/*
* Structure used by associate a PRLDAP thread-private data index with an
* LDAP session handle. One of these exists for each active LDAP session
* handle.
*/
typedef struct prldap_tpd_map {
#ifdef _SOLARIS_SDK
extern mutex_t inited_mutex;
#endif /* _SOLARIS_SDK */
/*
* Static Variables:
*/
/*
* prldap_map_list points to all of the PRLDAP_TPDMap structures
* we have ever allocated. We recycle them as we open and close LDAP
* sessions.
*/
/*
* The prldap_map_mutex is used to protect access to the prldap_map_list.
*/
/*
* The prldap_tpd_maxindex value is used to track the largest TPD array
* index we have used.
*/
/*
* prldap_tpdindex is an NSPR thread private data index we use to
* maintain our own thread-private data. It is initialized inside
* prldap_init_tpd().
*/
/*
* The prldap_callonce_init_tpd structure is used by NSPR to ensure
* that prldap_init_tpd() is called at most once.
*/
/*
* Private function prototypes:
*/
void *errorarg );
void *errorarg );
#endif
static void *prldap_mutex_alloc( void );
static void prldap_mutex_free( void *mutex );
static int prldap_mutex_lock( void *mutex );
static int prldap_mutex_unlock( void *mutex );
static void *prldap_get_thread_id( void );
#ifndef _SOLARIS_SDK
static PRStatus prldap_init_tpd( void );
static PRUintn prldap_new_tpdindex( void );
int maxindex );
static void prldap_tsd_destroy( void *priv );
#endif
/*
* Install NSPR thread functions into ld (if ld is NULL, they are installed
* as the default functions for new LDAP * handles).
*
* Returns 0 if all goes well and -1 if not.
*/
int
{
#ifndef _SOLARIS_SDK
!= PR_SUCCESS ) {
return( -1 );
}
#endif /* _SOLARIS_SDK */
/* set thread function pointers */
if ( shared ) {
#ifdef _SOLARIS_SDK
#else
/*
* If this is a real ld (i.e., we are not setting the global
* defaults) allocate thread private data for error information.
* If ld is NULL we do not do this here but it is done in
* prldap_thread_new_handle().
*/
== NULL ) {
return( -1 );
}
}
#endif
}
(void *)&tfns ) != 0 ) {
#ifndef _SOLARIS_SDK
#endif
return( -1 );
}
/* set extended thread function pointers */
(void *)&xtfns ) != 0 ) {
return( -1 );
}
return( 0 );
}
static void *
prldap_mutex_alloc( void )
{
return( (void *)PR_NewLock());
}
static void
{
}
static int
{
return( 0 );
}
static int
{
return( -1 );
}
return( 0 );
}
static void *
prldap_get_thread_id( void )
{
#ifdef _SOLARIS_SDK
return ((void *)thr_self());
#else
return( (void *)PR_GetCurrentThread());
#endif
}
#ifndef _SOLARIS_SDK
static int
{
}
}
return( eip->plei_lderrno );
} else {
}
}
return( LDAP_LOCAL_ERROR ); /* punt */
}
}
static void
{
/*
* Error info. has not yet been allocated for this thread.
* Do so now. Note that we free this memory only for the
* thread that calls prldap_thread_dispose_handle(), which
* should be the one that called ldap_unbind() -- see
* prldap_return_map(). Not freeing the memory used by
* other threads is deemed acceptable since it will be
* recycled and used by other LDAP sessions. All of the
* thread-private memory is freed when a thread exits
* (inside the prldap_tsd_destroy() function).
*/
sizeof( PRLDAP_ErrorInfo ));
return; /* punt */
}
}
}
}
}
}
#endif
/*
* Called when a new LDAP * session handle is allocated.
* Allocate thread-private data for error information, but only if
* it has not already been allocated and the get_ld_error callback has
* been installed. If ld is not NULL when prldap_install_thread_functions()
* is called, we will have already allocated the thread-private data there.
*/
int
{
#ifndef _SOLARIS_SDK
return( LDAP_LOCAL_ERROR );
}
(void *)&tfns ) != 0 ) {
return( LDAP_LOCAL_ERROR );
}
}
#endif
return( LDAP_SUCCESS );
}
/*
* Called when an LDAP * session handle is being destroyed.
* Clean up our thread private data map.
*/
void
{
#ifndef _SOLARIS_SDK
(void *)&tfns ) == 0 &&
}
#endif
}
#ifndef _SOLARIS_SDK
static PRStatus
prldap_init_tpd( void )
{
return( PR_FAILURE );
}
return( PR_SUCCESS );
}
/*
* Function: prldap_allocate_map()
* Description: allocate a thread-private data map to use for a new
* LDAP session handle.
* Returns: a pointer to the TPD map or NULL if none available.
*/
static PRLDAP_TPDMap *
{
/*
* first look for a map that is already allocated but free to be re-used
*/
break;
}
}
/*
* if none we found (map == NULL), try to allocate a new one and add it
* to the end of our global list.
*/
} else {
}
}
}
/* since we are reusing...reset */
/* to initial state */
}
return( map );
}
/*
* Function: prldap_return_map()
* Description: return a thread-private data map to the pool of ones
* available for re-use.
*/
static void
{
/*
* Dispose of thread-private LDAP error information. Note that this
* only disposes of the memory consumed on THIS thread, but that is
* okay. See the comment in prldap_set_ld_error() for the reason why.
*/
}
}
}
/* mark map as available for re-use */
}
/*
* Function: prldap_new_tpdindex()
* Description: allocate a thread-private data index.
* Returns: the new index.
*/
static PRUintn
prldap_new_tpdindex( void )
{
return( tpdindex );
}
/*
* Function: prldap_set_thread_private()
* Description: store a piece of thread-private data.
* Returns: 0 if successful and -1 if not.
*/
static int
{
if ( tpdindex > prldap_tpd_maxindex ) {
return( -1 ); /* bad index */
}
return( -1 ); /* realloc failed */
}
}
return( 0 );
}
/*
* Function: prldap_get_thread_private()
* Description: retrieve a piece of thread-private data. If not set,
* NULL is returned.
* Returns: 0 if successful and -1 if not.
*/
static void *
{
return( NULL ); /* no thread private data */
}
return( NULL ); /* fewer data items than requested index */
}
}
/*
* Function: prldap_tsd_realloc()
* Description: enlarge the thread-private data array.
* Returns: the new PRLDAP_TPDHeader value (non-NULL if successful).
* Note: tsdhdr can be NULL (allocates a new PRLDAP_TPDHeader).
*/
static PRLDAP_TPDHeader *
{
int count;
/* allocate a new thread private data header */
return( NULL );
}
}
/*
* Make the size of the new array the next highest multiple of
* the array increment value that is greater than maxindex.
*/
/* increase the size of the data item array if necessary */
if ( newdataitems == NULL ) {
return( NULL );
}
tsdhdr->ptpdh_tpd_count * sizeof( void * ));
}
}
return( tsdhdr );
}
/*
* Function: prldap_tsd_destroy()
* Description: Free a thread-private data array. Installed as an NSPR TPD
* destructor function
* Returns: nothing.
* Note: this function assumes that each TPD item installed at the PRLDAP
* level can be freed with a call to PR_Free().
*/
static void
{
int i;
for ( i = 0; i < tsdhdr->ptpdh_tpd_count; ++i ) {
}
}
}
}
}
#endif
#ifdef _SOLARIS_SDK
#pragma init(prldap_nspr_init)
static int nspr_initialized = 0;
/*
* Initialize NSPR once
*
*/
void
prldap_nspr_init(void) {
/*
* For performance reason, test it here first
*/
if (nspr_initialized != 0)
return;
(void) mutex_lock(&nspr_init_lock);
/* Make sure PR_Init() is executed only once */
if (nspr_initialized == 0) {
/*
* PR_Init changes the signal handler of SIGPIPE to SIG_IGN.
* Save the original and restore it after PR_Init.
*/
if (PR_Initialized() == PR_FALSE) {
/*
* PR_Init() changes the current thread's
* priority. Save and restore the priority.
*/
int priority;
}
nspr_initialized = 1;
/*
* Restore signal handling attributes of SIGPIPE
*/
}
(void) mutex_unlock(&nspr_init_lock);
}
#endif