Update from 0.9.7-stable branch.
[openssl.git] / crypto / err / err.c
index 72e3f3a26c7c99c1f474d7b8149cbb1adedcc152..6812a9afed2c671dd3628f0e86d8e73c9103cd2b 100644 (file)
@@ -56,7 +56,7 @@
  * [including the GNU Public Licence.]
  */
 /* ====================================================================
- * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -147,6 +147,7 @@ static ERR_STRING_DATA ERR_str_libraries[]=
 {ERR_PACK(ERR_LIB_PKCS12,0,0)          ,"PKCS12 routines"},
 {ERR_PACK(ERR_LIB_RAND,0,0)            ,"random number generator"},
 {ERR_PACK(ERR_LIB_DSO,0,0)             ,"DSO support routines"},
+{ERR_PACK(ERR_LIB_TS,0,0)              ,"time stamp routines"},
 {ERR_PACK(ERR_LIB_ENGINE,0,0)          ,"engine routines"},
 {ERR_PACK(ERR_LIB_OCSP,0,0)            ,"OCSP routines"},
 {0,NULL},
@@ -195,6 +196,7 @@ static ERR_STRING_DATA ERR_str_reasons[]=
 {ERR_R_DSO_LIB                         ,"DSO lib"},
 {ERR_R_ENGINE_LIB                      ,"ENGINE lib"},
 {ERR_R_OCSP_LIB                                ,"OCSP lib"},
+{ERR_R_TS_LIB                          ,"TS lib"},
 
 {ERR_R_NESTED_ASN1_ERROR               ,"nested asn1 error"},
 {ERR_R_BAD_ASN1_OBJECT_HEADER          ,"bad asn1 object header"},
@@ -548,9 +550,20 @@ static void build_SYS_str_reasons(void)
        int i;
        static int init = 1;
 
-       if (!init) return;
-
+       CRYPTO_r_lock(CRYPTO_LOCK_ERR);
+       if (!init)
+               {
+               CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
+               return;
+               }
+       
+       CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
        CRYPTO_w_lock(CRYPTO_LOCK_ERR);
+       if (!init)
+               {
+               CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
+               return;
+               }
 
        for (i = 1; i <= NUM_SYS_STR_REASONS; i++)
                {
@@ -960,24 +973,32 @@ static int err_cmp(const void *a_void, const void *b_void)
 /* static unsigned long pid_hash(ERR_STATE *a) */
 static unsigned long pid_hash(const void *a_void)
        {
-       return(((const ERR_STATE *)a_void)->pid*13);
+       return((((const ERR_STATE *)a_void)->pid + (unsigned long)((const ERR_STATE *)a_void)->pidptr)*13);
        }
 
 /* static int pid_cmp(ERR_STATE *a, ERR_STATE *b) */
 static int pid_cmp(const void *a_void, const void *b_void)
        {
-       return((int)((long)((const ERR_STATE *)a_void)->pid -
-                       (long)((const ERR_STATE *)b_void)->pid));
+       return (((const ERR_STATE *)a_void)->pid != ((const ERR_STATE *)b_void)->pid)
+              || (((const ERR_STATE *)a_void)->pidptr != ((const ERR_STATE *)b_void)->pidptr);
        }
 
 void ERR_remove_state(unsigned long pid)
        {
        ERR_STATE tmp;
+       void *pidptr;
 
        err_fns_check();
-       if (pid == 0)
-               pid=(unsigned long)CRYPTO_thread_id();
+       if (pid != 0)
+               pidptr = &errno;
+       else
+               {
+               pid = CRYPTO_thread_id();
+               pidptr = CRYPTO_thread_idptr();
+               }
+       
        tmp.pid=pid;
+       tmp.pidptr=pidptr;
        /* thread_del_item automatically destroys the LHASH if the number of
         * items reaches zero. */
        ERRFN(thread_del_item)(&tmp);
@@ -989,10 +1010,13 @@ ERR_STATE *ERR_get_state(void)
        ERR_STATE *ret,tmp,*tmpp=NULL;
        int i;
        unsigned long pid;
+       void *pidptr;
 
        err_fns_check();
-       pid=(unsigned long)CRYPTO_thread_id();
-       tmp.pid=pid;
+       pid = CRYPTO_thread_id();
+       pidptr = CRYPTO_thread_idptr();
+       tmp.pid = pid;
+       tmp.pidptr = pidptr;
        ret=ERRFN(thread_get_item)(&tmp);
 
        /* ret == the error state, if NULL, make a new one */
@@ -1001,6 +1025,7 @@ ERR_STATE *ERR_get_state(void)
                ret=(ERR_STATE *)OPENSSL_malloc(sizeof(ERR_STATE));
                if (ret == NULL) return(&fallback);
                ret->pid=pid;
+               ret->pidptr=pidptr;
                ret->top=0;
                ret->bottom=0;
                for (i=0; i<ERR_NUM_ERRORS; i++)