Temporary fixes so we can continue to build in smock.
[fedora-mingw.git] / curl / curl-7.18.2-nss-thread-safety.patch
1 diff -u --recursive curl-7.18.2/lib/nss.c curl-7.18.2.new/lib/nss.c
2 --- curl-7.18.2/lib/nss.c       2008-09-16 11:13:00.000000000 -0400
3 +++ curl-7.18.2.new/lib/nss.c   2008-09-16 11:29:13.000000000 -0400
4 @@ -73,6 +73,8 @@
5  
6  PRFileDesc *PR_ImportTCPSocket(PRInt32 osfd);
7  
8 +PRLock * nss_initlock = NULL;
9 +
10  int initialized = 0;
11  
12  #define HANDSHAKE_TIMEOUT 30
13 @@ -719,8 +721,11 @@
14   */
15  int Curl_nss_init(void)
16  {
17 -  if(!initialized)
18 +  /* curl_global_init() is not thread-safe so this test is ok */
19 +  if (nss_initlock == NULL) {
20      PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
21 +    nss_initlock = PR_NewLock();
22 +  }
23  
24    /* We will actually initialize NSS later */
25  
26 @@ -730,7 +735,17 @@
27  /* Global cleanup */
28  void Curl_nss_cleanup(void)
29  {
30 -  NSS_Shutdown();
31 +  /* This function isn't required to be threadsafe and this is only done
32 +   * as a safety feature.
33 +   */
34 +  PR_Lock(nss_initlock);
35 +  if (initialized)
36 +    NSS_Shutdown();
37 +  PR_Unlock(nss_initlock);
38 +
39 +  PR_DestroyLock(nss_initlock);
40 +  nss_initlock = NULL;
41 +
42    initialized = 0;
43  }
44  
45 @@ -808,7 +823,8 @@
46      return CURLE_OK;
47  
48    /* FIXME. NSS doesn't support multiple databases open at the same time. */
49 -  if(!initialized) {
50 +  PR_Lock(nss_initlock);
51 +  if(!initialized && !NSS_IsInitialized()) {
52      initialized = 1;
53  
54      certDir = getenv("SSL_DIR"); /* Look in $SSL_DIR */
55 @@ -832,6 +848,8 @@
56      if(rv != SECSuccess) {
57        infof(conn->data, "Unable to initialize NSS database\n");
58        curlerr = CURLE_SSL_CACERT_BADFILE;
59 +      PR_Unlock(nss_initlock);
60 +      initialized = 0;
61        goto error;
62      }
63  
64 @@ -854,6 +872,7 @@
65      }
66  #endif
67    }
68 +  PR_Unlock(nss_initlock);
69  
70    model = PR_NewTCPSocket();
71    if(!model)
72 Only in curl-7.18.2.new/lib: nss.c.orig