#ifdef _SOLARIS_SDK
#include <thread.h>
#include <synch.h>
#include <prinit.h>
#include <prthread.h>
#include <syslog.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
extern int errno;
#endif
#include "ldappr-int.h"
#ifndef _SOLARIS_SDK
#define PRLDAP_TPD_ARRAY_INCREMENT 10
typedef struct prldap_errorinfo {
int plei_lderrno;
char *plei_matched;
char *plei_errmsg;
} PRLDAP_ErrorInfo;
typedef struct prldap_tpd_header {
int ptpdh_tpd_count;
void **ptpdh_dataitems;
} PRLDAP_TPDHeader;
typedef struct prldap_tpd_map {
LDAP *prtm_ld;
PRUintn prtm_index;
struct prldap_tpd_map *prtm_next;
} PRLDAP_TPDMap;
#ifdef _SOLARIS_SDK
extern mutex_t inited_mutex;
#endif
static PRLDAP_TPDMap *prldap_map_list = NULL;
static PRLock *prldap_map_mutex = NULL;
static PRInt32 prldap_tpd_maxindex = -1;
static PRUintn prldap_tpdindex = 0;
static PRCallOnceType prldap_callonce_init_tpd = { 0, 0, 0 };
static void prldap_set_ld_error( int err, char *matched, char *errmsg,
void *errorarg );
static int prldap_get_ld_error( char **matchedp, char **errmsgp,
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 PRLDAP_TPDMap *prldap_allocate_map( LDAP *ld );
static void prldap_return_map( PRLDAP_TPDMap *map );
static PRUintn prldap_new_tpdindex( void );
static int prldap_set_thread_private( PRInt32 tpdindex, void *priv );
static void *prldap_get_thread_private( PRInt32 tpdindex );
static PRLDAP_TPDHeader *prldap_tsd_realloc( PRLDAP_TPDHeader *tsdhdr,
int maxindex );
static void prldap_tsd_destroy( void *priv );
#endif
int
prldap_install_thread_functions( LDAP *ld, int shared )
{
struct ldap_thread_fns tfns;
struct ldap_extra_thread_fns xtfns;
#ifndef _SOLARIS_SDK
if ( PR_CallOnce( &prldap_callonce_init_tpd, prldap_init_tpd )
!= PR_SUCCESS ) {
ldap_set_lderrno( ld, LDAP_LOCAL_ERROR, NULL, NULL );
return( -1 );
}
#endif
memset( &tfns, '\0', sizeof(struct ldap_thread_fns) );
tfns.ltf_get_errno = prldap_get_system_errno;
tfns.ltf_set_errno = prldap_set_system_errno;
if ( shared ) {
tfns.ltf_mutex_alloc = prldap_mutex_alloc;
tfns.ltf_mutex_free = prldap_mutex_free;
tfns.ltf_mutex_lock = prldap_mutex_lock;
tfns.ltf_mutex_unlock = prldap_mutex_unlock;
#ifdef _SOLARIS_SDK
tfns.ltf_get_lderrno = NULL;
tfns.ltf_set_lderrno = NULL;
#else
tfns.ltf_get_lderrno = prldap_get_ld_error;
tfns.ltf_set_lderrno = prldap_set_ld_error;
if ( ld != NULL ) {
if (( tfns.ltf_lderrno_arg = (void *)prldap_allocate_map( ld ))
== NULL ) {
return( -1 );
}
}
#endif
}
if ( ldap_set_option( ld, LDAP_OPT_THREAD_FN_PTRS,
(void *)&tfns ) != 0 ) {
#ifndef _SOLARIS_SDK
prldap_return_map( (PRLDAP_TPDMap *)tfns.ltf_lderrno_arg );
#endif
return( -1 );
}
memset( &xtfns, '\0', sizeof(struct ldap_extra_thread_fns) );
xtfns.ltf_threadid_fn = prldap_get_thread_id;
if ( ldap_set_option( ld, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
(void *)&xtfns ) != 0 ) {
return( -1 );
}
return( 0 );
}
static void *
prldap_mutex_alloc( void )
{
return( (void *)PR_NewLock());
}
static void
prldap_mutex_free( void *mutex )
{
PR_DestroyLock( (PRLock *)mutex );
}
static int
prldap_mutex_lock( void *mutex )
{
PR_Lock( (PRLock *)mutex );
return( 0 );
}
static int
prldap_mutex_unlock( void *mutex )
{
if ( PR_Unlock( (PRLock *)mutex ) == PR_FAILURE ) {
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
prldap_get_ld_error( char **matchedp, char **errmsgp, void *errorarg )
{
PRLDAP_TPDMap *map;
PRLDAP_ErrorInfo *eip;
if (( map = (PRLDAP_TPDMap *)errorarg ) != NULL && ( eip =
(PRLDAP_ErrorInfo *)prldap_get_thread_private(
map->prtm_index )) != NULL ) {
if ( matchedp != NULL ) {
*matchedp = eip->plei_matched;
}
if ( errmsgp != NULL ) {
*errmsgp = eip->plei_errmsg;
}
return( eip->plei_lderrno );
} else {
if ( matchedp != NULL ) {
*matchedp = NULL;
}
if ( errmsgp != NULL ) {
*errmsgp = NULL;
}
return( LDAP_LOCAL_ERROR );
}
}
static void
prldap_set_ld_error( int err, char *matched, char *errmsg, void *errorarg )
{
PRLDAP_TPDMap *map;
PRLDAP_ErrorInfo *eip;
if (( map = (PRLDAP_TPDMap *)errorarg ) != NULL ) {
if (( eip = (PRLDAP_ErrorInfo *)prldap_get_thread_private(
map->prtm_index )) == NULL ) {
eip = (PRLDAP_ErrorInfo *)PR_Calloc( 1,
sizeof( PRLDAP_ErrorInfo ));
if ( eip == NULL ) {
return;
}
(void)prldap_set_thread_private( map->prtm_index, eip );
}
eip->plei_lderrno = err;
if ( eip->plei_matched != NULL ) {
ldap_memfree( eip->plei_matched );
}
eip->plei_matched = matched;
if ( eip->plei_errmsg != NULL ) {
ldap_memfree( eip->plei_errmsg );
}
eip->plei_errmsg = errmsg;
}
}
#endif
int
prldap_thread_new_handle( LDAP *ld, void *sessionarg )
{
struct ldap_thread_fns tfns;
#ifndef _SOLARIS_SDK
if ( ldap_get_option( ld, LDAP_OPT_THREAD_FN_PTRS, (void *)&tfns ) != 0 ) {
return( LDAP_LOCAL_ERROR );
}
if ( tfns.ltf_lderrno_arg == NULL && tfns.ltf_get_lderrno != NULL ) {
if (( tfns.ltf_lderrno_arg = (void *)prldap_allocate_map( ld )) == NULL
|| ldap_set_option( ld, LDAP_OPT_THREAD_FN_PTRS,
(void *)&tfns ) != 0 ) {
return( LDAP_LOCAL_ERROR );
}
}
#endif
return( LDAP_SUCCESS );
}
void
prldap_thread_dispose_handle( LDAP *ld, void *sessionarg )
{
#ifndef _SOLARIS_SDK
struct ldap_thread_fns tfns;
if ( ldap_get_option( ld, LDAP_OPT_THREAD_FN_PTRS,
(void *)&tfns ) == 0 &&
tfns.ltf_lderrno_arg != NULL ) {
prldap_return_map( (PRLDAP_TPDMap *)tfns.ltf_lderrno_arg );
}
#endif
}
#ifndef _SOLARIS_SDK
static PRStatus
prldap_init_tpd( void )
{
if (( prldap_map_mutex = PR_NewLock()) == NULL || PR_NewThreadPrivateIndex(
&prldap_tpdindex, prldap_tsd_destroy ) != PR_SUCCESS ) {
return( PR_FAILURE );
}
prldap_map_list = NULL;
return( PR_SUCCESS );
}
static PRLDAP_TPDMap *
prldap_allocate_map( LDAP *ld )
{
PRLDAP_TPDMap *map, *prevmap;
PR_Lock( prldap_map_mutex );
prevmap = NULL;
for ( map = prldap_map_list; map != NULL; map = map->prtm_next ) {
if ( map->prtm_ld == NULL ) {
break;
}
prevmap = map;
}
if ( map == NULL ) {
PRUintn tpdindex;
tpdindex = prldap_new_tpdindex();
map = (PRLDAP_TPDMap *)PR_Malloc( sizeof( PRLDAP_TPDMap ));
if ( map != NULL ) {
map->prtm_index = tpdindex;
map->prtm_next = NULL;
if ( prevmap == NULL ) {
prldap_map_list = map;
} else {
prevmap->prtm_next = map;
}
}
}
if ( map != NULL ) {
map->prtm_ld = ld;
(void)prldap_set_thread_private( map->prtm_index, NULL );
}
PR_Unlock( prldap_map_mutex );
return( map );
}
static void
prldap_return_map( PRLDAP_TPDMap *map )
{
PRLDAP_ErrorInfo *eip;
PR_Lock( prldap_map_mutex );
if (( eip = (PRLDAP_ErrorInfo *)prldap_get_thread_private(
map->prtm_index )) != NULL &&
prldap_set_thread_private( map->prtm_index, NULL ) == 0 ) {
if ( eip->plei_matched != NULL ) {
ldap_memfree( eip->plei_matched );
}
if ( eip->plei_errmsg != NULL ) {
ldap_memfree( eip->plei_errmsg );
}
PR_Free( eip );
}
map->prtm_ld = NULL;
PR_Unlock( prldap_map_mutex );
}
static PRUintn
prldap_new_tpdindex( void )
{
PRUintn tpdindex;
tpdindex = (PRUintn)PR_AtomicIncrement( &prldap_tpd_maxindex );
return( tpdindex );
}
static int
prldap_set_thread_private( PRInt32 tpdindex, void *priv )
{
PRLDAP_TPDHeader *tsdhdr;
if ( tpdindex > prldap_tpd_maxindex ) {
return( -1 );
}
tsdhdr = (PRLDAP_TPDHeader *)PR_GetThreadPrivate( prldap_tpdindex );
if ( tsdhdr == NULL || tpdindex >= tsdhdr->ptpdh_tpd_count ) {
tsdhdr = prldap_tsd_realloc( tsdhdr, tpdindex );
if ( tsdhdr == NULL ) {
return( -1 );
}
}
tsdhdr->ptpdh_dataitems[ tpdindex ] = priv;
return( 0 );
}
static void *
prldap_get_thread_private( PRInt32 tpdindex )
{
PRLDAP_TPDHeader *tsdhdr;
tsdhdr = (PRLDAP_TPDHeader *)PR_GetThreadPrivate( prldap_tpdindex );
if ( tsdhdr == NULL ) {
return( NULL );
}
if ( tpdindex >= tsdhdr->ptpdh_tpd_count
|| tsdhdr->ptpdh_dataitems == NULL ) {
return( NULL );
}
return( tsdhdr->ptpdh_dataitems[ tpdindex ] );
}
static PRLDAP_TPDHeader *
prldap_tsd_realloc( PRLDAP_TPDHeader *tsdhdr, int maxindex )
{
void *newdataitems = NULL;
int count;
if ( tsdhdr == NULL ) {
if (( tsdhdr = PR_Calloc( 1, sizeof( PRLDAP_TPDHeader ))) == NULL ) {
return( NULL );
}
(void)PR_SetThreadPrivate( prldap_tpdindex, tsdhdr );
}
count = PRLDAP_TPD_ARRAY_INCREMENT *
( 1 + ( maxindex / PRLDAP_TPD_ARRAY_INCREMENT ));
if ( count > tsdhdr->ptpdh_tpd_count ) {
newdataitems = (PRLDAP_ErrorInfo *)PR_Calloc( count, sizeof( void * ));
if ( newdataitems == NULL ) {
return( NULL );
}
if ( tsdhdr->ptpdh_dataitems != NULL ) {
memcpy( newdataitems, tsdhdr->ptpdh_dataitems,
tsdhdr->ptpdh_tpd_count * sizeof( void * ));
PR_Free( tsdhdr->ptpdh_dataitems );
}
tsdhdr->ptpdh_tpd_count = count;
tsdhdr->ptpdh_dataitems = newdataitems;
}
return( tsdhdr );
}
static void
prldap_tsd_destroy( void *priv )
{
PRLDAP_TPDHeader *tsdhdr;
int i;
tsdhdr = (PRLDAP_TPDHeader *)priv;
if ( tsdhdr != NULL ) {
if ( tsdhdr->ptpdh_dataitems != NULL ) {
for ( i = 0; i < tsdhdr->ptpdh_tpd_count; ++i ) {
if ( tsdhdr->ptpdh_dataitems[ i ] != NULL ) {
PR_Free( tsdhdr->ptpdh_dataitems[ i ] );
tsdhdr->ptpdh_dataitems[ i ] = NULL;
}
}
PR_Free( tsdhdr->ptpdh_dataitems );
tsdhdr->ptpdh_dataitems = NULL;
}
PR_Free( tsdhdr );
}
}
#endif
#ifdef _SOLARIS_SDK
#pragma init(prldap_nspr_init)
static mutex_t nspr_init_lock = DEFAULTMUTEX;
static int nspr_initialized = 0;
void
prldap_nspr_init(void) {
struct sigaction action;
if (nspr_initialized != 0)
return;
(void) mutex_lock(&nspr_init_lock);
if (nspr_initialized == 0) {
(void) sigaction(SIGPIPE, NULL, &action);
if (PR_Initialized() == PR_FALSE) {
int priority;
(void) thr_getprio(thr_self(), &priority);
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
(void) thr_setprio(thr_self(), priority);
}
nspr_initialized = 1;
(void) sigaction(SIGPIPE, &action, NULL);
}
(void) mutex_unlock(&nspr_init_lock);
}
#endif