"Fossies" - the Fresh Open Source Software Archive

Member "nss_ldap-265/ltf.c" (6 Nov 2009, 8489 Bytes) of package /linux/privat/old/nss_ldap-265.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 1997-2005 Luke Howard.
    2    This file is part of the nss_ldap library.
    3    Contributed by Luke Howard, <lukeh@padl.com>, 1997.
    4 
    5    The nss_ldap library is free software; you can redistribute it and/or
    6    modify it under the terms of the GNU Library General Public License as
    7    published by the Free Software Foundation; either version 2 of the
    8    License, or (at your option) any later version.
    9 
   10    The nss_ldap library is distributed in the hope that it will be useful,
   11    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13    Library General Public License for more details.
   14 
   15    You should have received a copy of the GNU Library General Public
   16    License along with the nss_ldap library; see the file COPYING.LIB.  If not,
   17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   18    Boston, MA 02111-1307, USA.
   19  */
   20 
   21 static char rcsId[] = "$Id: ltf.c,v 2.29 2005/05/20 05:30:42 lukeh Exp $";
   22 
   23 #include "config.h"
   24 
   25 #ifdef HAVE_PORT_BEFORE_H
   26 #include <port_before.h>
   27 #endif
   28 
   29 #if defined(HAVE_THREAD_H) && !defined(_AIX)
   30 #include <thread.h>
   31 #elif defined(HAVE_PTHREAD_H)
   32 #include <pthread.h>
   33 #endif
   34 
   35 #include <stdlib.h>
   36 #include <string.h>
   37 #include <stdio.h>
   38 
   39 #ifdef HAVE_LBER_H
   40 #include <lber.h>
   41 #endif
   42 #ifdef HAVE_LDAP_H
   43 #include <ldap.h>
   44 #endif
   45 
   46 #include "ldap-nss.h"
   47 
   48 #if defined(LDAP_OPT_THREAD_FN_PTRS) && (defined(HAVE_THREAD_H) || defined(HAVE_PTHREAD_H))
   49 
   50 static void *ltf_mutex_alloc (void);
   51 static void ltf_mutex_free (void *m);
   52 static NSS_STATUS ltf_tsd_setup (void);
   53 static void ltf_set_ld_error (int err, char *matched, char *errmsg,
   54                   void *dummy);
   55 static int ltf_get_ld_error (char **matched, char **errmsg, void *dummy);
   56 static void ltf_set_errno (int err);
   57 static int ltf_get_errno (void);
   58 
   59 #ifndef HAVE_THREAD_H       /* thus, pthreads */
   60 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
   61 
   62  * The contents of this file are subject to the Netscape Public License
   63  * Version 1.0 (the "NPL"); you may not use this file except in
   64  * compliance with the NPL.  You may obtain a copy of the NPL at
   65  * http://www.mozilla.org/NPL/
   66  *
   67  * Software distributed under the NPL is distributed on an "AS IS" basis,
   68  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
   69  * for the specific language governing rights and limitations under the
   70  * NPL.
   71  *
   72  * The Initial Developer of this code under the NPL is Netscape
   73  * Communications Corporation.  Portions created by Netscape are
   74  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
   75  * Reserved.
   76  */
   77 
   78 static int ltf_mutex_lock (void *);
   79 static int ltf_mutex_unlock (void *);
   80 
   81 static pthread_key_t key;
   82 
   83 NSS_STATUS _nss_ldap_ltf_thread_init (LDAP * ld)
   84 {
   85   struct ldap_thread_fns tfns;
   86 
   87   /* set mutex pointers */
   88   memset (&tfns, '\0', sizeof (struct ldap_thread_fns));
   89   tfns.ltf_mutex_alloc = ltf_mutex_alloc;
   90   tfns.ltf_mutex_free = ltf_mutex_free;
   91   tfns.ltf_mutex_lock = ltf_mutex_lock;
   92   tfns.ltf_mutex_unlock = ltf_mutex_unlock;
   93   tfns.ltf_get_errno = ltf_get_errno;
   94   tfns.ltf_set_errno = ltf_set_errno;
   95   tfns.ltf_get_lderrno = ltf_get_ld_error;
   96   tfns.ltf_set_lderrno = ltf_set_ld_error;
   97   tfns.ltf_lderrno_arg = NULL;
   98   /* set ld_errno pointers */
   99   if (ldap_set_option (ld, LDAP_OPT_THREAD_FN_PTRS, (void *) &tfns) != 0)
  100     {
  101       return NSS_UNAVAIL;
  102     }
  103 
  104   return ltf_tsd_setup ();
  105 }
  106 
  107 static void *
  108 ltf_mutex_alloc (void)
  109 {
  110   pthread_mutex_t *mutexp;
  111 
  112   if ((mutexp = malloc (sizeof (pthread_mutex_t))) != NULL)
  113     {
  114       pthread_mutex_init (mutexp, NULL);
  115     }
  116 
  117   return (mutexp);
  118 }
  119 
  120 static void
  121 ltf_mutex_free (void *mutexp)
  122 {
  123   pthread_mutex_destroy ((pthread_mutex_t *) mutexp);
  124 }
  125 
  126 static int
  127 ltf_mutex_lock (void *mutexp)
  128 {
  129 #if defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
  130   return __libc_lock_lock (*(pthread_mutex_t *) mutexp);
  131 #elif defined(HPUX)
  132   return __thread_mutex_lock ((pthread_mutex_t *) mutexp);
  133 #else
  134 # ifdef _AIX
  135   if (__multi_threaded == 0)
  136     return 0;
  137 # endif
  138   return pthread_mutex_lock ((pthread_mutex_t *) mutexp);
  139 #endif /* HAVE_LIBC_LOCK_H || HAVE_BITS_LIBC_LOCK_H */
  140 }
  141 
  142 static int
  143 ltf_mutex_unlock (void *mutexp)
  144 {
  145 #if defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
  146   return __libc_lock_unlock (*(pthread_mutex_t *) mutexp);
  147 #elif defined(HPUX)
  148   return __thread_mutex_unlock ((pthread_mutex_t *) mutexp);
  149 #else
  150 # ifdef _AIX
  151   if (__multi_threaded == 0)
  152     return 0;
  153 # endif
  154   return pthread_mutex_unlock ((pthread_mutex_t *) mutexp);
  155 #endif /* HAVE_LIBC_LOCK_H || HAVE_BITS_LIBC_LOCK_H */
  156 }
  157 
  158 static NSS_STATUS
  159 ltf_tsd_setup (void)
  160 {
  161   void *tsd;
  162 
  163 #if defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
  164   if (__libc_key_create (&key, free) != 0)
  165     {
  166       return NSS_UNAVAIL;
  167     }
  168   tsd = (void *) calloc (1, sizeof (struct ldap_error));
  169   __libc_setspecific (key, tsd);
  170 #else
  171   if (pthread_key_create (&key, free) != 0)
  172     {
  173       return NSS_UNAVAIL;
  174     }
  175   tsd = pthread_getspecific (key);
  176   if (tsd != NULL)
  177     {
  178       pthread_exit (NULL);
  179     }
  180   tsd = (void *) calloc (1, sizeof (struct ldap_error));
  181   pthread_setspecific (key, tsd);
  182 #endif /* HAVE_LIBC_LOCK_H || HAVE_BITS_LIBC_LOCK_H */
  183 
  184   return NSS_SUCCESS;
  185 }
  186 
  187 static void
  188 ltf_set_ld_error (int err, char *matched, char *errmsg, void *dummy)
  189 {
  190   struct ldap_error *le;
  191 
  192 #if defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
  193   le = __libc_getspecific (key);
  194 #else
  195   le = pthread_getspecific (key);
  196 #endif /* HAVE_LIBC_LOCK_H || HAVE_BITS_LIBC_LOCK_H */
  197 
  198   le->le_errno = err;
  199 
  200   if (le->le_matched != NULL)
  201     ldap_memfree (le->le_matched);
  202   le->le_matched = matched;
  203 
  204   if (le->le_errmsg != NULL)
  205     ldap_memfree (le->le_errmsg);
  206   le->le_errmsg = errmsg;
  207 }
  208 
  209 static int
  210 ltf_get_ld_error (char **matched, char **errmsg, void *dummy)
  211 {
  212   struct ldap_error *le;
  213 
  214 #if defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
  215   le = __libc_getspecific (key);
  216 #else
  217   le = pthread_getspecific (key);
  218 #endif /* HAVE_LIBC_LOCK_H || HAVE_BITS_LIBC_LOCK_H */
  219   if (le == NULL)
  220     return LDAP_LOCAL_ERROR;
  221 
  222   if (matched != NULL)
  223     *matched = le->le_matched;
  224 
  225   if (errmsg != NULL)
  226     *errmsg = le->le_errmsg;
  227 
  228   return (le->le_errno);
  229 }
  230 
  231 static void
  232 ltf_set_errno (int err)
  233 {
  234   errno = err;
  235 }
  236 
  237 static int
  238 ltf_get_errno (void)
  239 {
  240   return (errno);
  241 }
  242 #else
  243 static thread_key_t ltf_key = 0;
  244 
  245 static void *
  246 ltf_mutex_alloc (void)
  247 {
  248   mutex_t *m;
  249 
  250   m = (mutex_t *) malloc (sizeof (*m));
  251   if (m == NULL)
  252     return NULL;
  253 
  254   if (mutex_init (m, USYNC_THREAD, NULL) < 0)
  255     return NULL;
  256 
  257   return m;
  258 }
  259 
  260 static void
  261 ltf_mutex_free (void *m)
  262 {
  263   mutex_destroy ((mutex_t *) m);
  264 /*      free(m); */
  265 }
  266 
  267 void
  268 ltf_destr (void *tsd)
  269 {
  270   free (tsd);
  271 }
  272 
  273 static NSS_STATUS
  274 ltf_tsd_setup (void)
  275 {
  276   void *tsd;
  277 
  278   (void) thr_keycreate (&ltf_key, ltf_destr);
  279   tsd = (void *) calloc (1, sizeof (ldap_error_t));
  280   thr_setspecific (ltf_key, tsd);
  281   return NSS_SUCCESS;
  282 }
  283 
  284 static void
  285 ltf_set_ld_error (int err, char *matched, char *errmsg, void *dummy)
  286 {
  287   ldap_error_t *le;
  288 
  289   (void) thr_getspecific (ltf_key, (void **) &le);
  290   if (le == NULL)
  291     return;
  292 
  293   le->le_errno = err;
  294 
  295   if (le->le_matched != NULL)
  296     ldap_memfree (le->le_matched);
  297   le->le_matched = matched;
  298 
  299   if (le->le_errmsg != NULL)
  300     ldap_memfree (le->le_errmsg);
  301   le->le_errmsg = errmsg;
  302 }
  303 
  304 static int
  305 ltf_get_ld_error (char **matched, char **errmsg, void *dummy)
  306 {
  307   ldap_error_t *le = NULL;
  308 
  309   (void) thr_getspecific (ltf_key, (void **) &le);
  310   if (le == NULL)
  311     return LDAP_LOCAL_ERROR;
  312 
  313   if (matched != NULL)
  314     *matched = le->le_matched;
  315 
  316   if (errmsg != NULL)
  317     *errmsg = le->le_errmsg;
  318 
  319   return le->le_errno;
  320 }
  321 
  322 static void
  323 ltf_set_errno (int err)
  324 {
  325   errno = err;
  326 }
  327 
  328 static int
  329 ltf_get_errno (void)
  330 {
  331   return errno;
  332 }
  333 
  334 NSS_STATUS _nss_ldap_ltf_thread_init (LDAP * ld)
  335 {
  336   struct ldap_thread_fns tfns;
  337 
  338   memset (&tfns, '\0', sizeof (tfns));
  339   tfns.ltf_mutex_alloc = ltf_mutex_alloc;
  340   tfns.ltf_mutex_free = ltf_mutex_free;
  341   tfns.ltf_mutex_lock = (int (*)(void *)) mutex_lock;
  342   tfns.ltf_mutex_unlock = (int (*)(void *)) mutex_unlock;
  343   tfns.ltf_get_errno = ltf_get_errno;
  344   tfns.ltf_set_errno = ltf_set_errno;
  345   tfns.ltf_get_lderrno = ltf_get_ld_error;
  346   tfns.ltf_set_lderrno = ltf_set_ld_error;
  347   tfns.ltf_lderrno_arg = NULL;
  348 
  349   if (ldap_set_option (ld, LDAP_OPT_THREAD_FN_PTRS, (void *) &tfns) != 0)
  350     return NSS_UNAVAIL;
  351 
  352   return ltf_tsd_setup ();
  353 }
  354 #endif /* !HAVE_THREAD_H */
  355 #endif /* LDAP_OPT_THREAD_FN_PTRS */