security/nss/lib/ssl/sslmutex.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/ssl/sslmutex.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,640 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "seccomon.h"
     1.9 +/* This ifdef should match the one in sslsnce.c */
    1.10 +#if defined(XP_UNIX) || defined(XP_WIN32) || defined (XP_OS2) || defined(XP_BEOS)
    1.11 +
    1.12 +#include "sslmutex.h"
    1.13 +#include "prerr.h"
    1.14 +
    1.15 +static SECStatus single_process_sslMutex_Init(sslMutex* pMutex)
    1.16 +{
    1.17 +    PR_ASSERT(pMutex != 0 && pMutex->u.sslLock == 0 );
    1.18 +    
    1.19 +    pMutex->u.sslLock = PR_NewLock();
    1.20 +    if (!pMutex->u.sslLock) {
    1.21 +        return SECFailure;
    1.22 +    }
    1.23 +    return SECSuccess;
    1.24 +}
    1.25 +
    1.26 +static SECStatus single_process_sslMutex_Destroy(sslMutex* pMutex)
    1.27 +{
    1.28 +    PR_ASSERT(pMutex != 0);
    1.29 +    PR_ASSERT(pMutex->u.sslLock!= 0);
    1.30 +    if (!pMutex->u.sslLock) {
    1.31 +        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
    1.32 +        return SECFailure;
    1.33 +    }
    1.34 +    PR_DestroyLock(pMutex->u.sslLock);
    1.35 +    return SECSuccess;
    1.36 +}
    1.37 +
    1.38 +static SECStatus single_process_sslMutex_Unlock(sslMutex* pMutex)
    1.39 +{
    1.40 +    PR_ASSERT(pMutex != 0 );
    1.41 +    PR_ASSERT(pMutex->u.sslLock !=0);
    1.42 +    if (!pMutex->u.sslLock) {
    1.43 +        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
    1.44 +        return SECFailure;
    1.45 +    }
    1.46 +    PR_Unlock(pMutex->u.sslLock);
    1.47 +    return SECSuccess;
    1.48 +}
    1.49 +
    1.50 +static SECStatus single_process_sslMutex_Lock(sslMutex* pMutex)
    1.51 +{
    1.52 +    PR_ASSERT(pMutex != 0);
    1.53 +    PR_ASSERT(pMutex->u.sslLock != 0 );
    1.54 +    if (!pMutex->u.sslLock) {
    1.55 +        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
    1.56 +        return SECFailure;
    1.57 +    }
    1.58 +    PR_Lock(pMutex->u.sslLock);
    1.59 +    return SECSuccess;
    1.60 +}
    1.61 +
    1.62 +#if defined(LINUX) || defined(AIX) || defined(BEOS) || defined(BSDI) || (defined(NETBSD) && __NetBSD_Version__ < 500000000) || defined(OPENBSD)
    1.63 +
    1.64 +#include <unistd.h>
    1.65 +#include <fcntl.h>
    1.66 +#include <string.h>
    1.67 +#include <errno.h>
    1.68 +#include "unix_err.h"
    1.69 +#include "pratom.h"
    1.70 +
    1.71 +#define SSL_MUTEX_MAGIC 0xfeedfd
    1.72 +#define NONBLOCKING_POSTS 1	/* maybe this is faster */
    1.73 +
    1.74 +#if NONBLOCKING_POSTS
    1.75 +
    1.76 +#ifndef FNONBLOCK
    1.77 +#define FNONBLOCK O_NONBLOCK
    1.78 +#endif
    1.79 +
    1.80 +static int
    1.81 +setNonBlocking(int fd, int nonBlocking)
    1.82 +{
    1.83 +    int flags;
    1.84 +    int err;
    1.85 +
    1.86 +    flags = fcntl(fd, F_GETFL, 0);
    1.87 +    if (0 > flags)
    1.88 +	return flags;
    1.89 +    if (nonBlocking)
    1.90 +	flags |= FNONBLOCK;
    1.91 +    else
    1.92 +	flags &= ~FNONBLOCK;
    1.93 +    err = fcntl(fd, F_SETFL, flags);
    1.94 +    return err;
    1.95 +}
    1.96 +#endif
    1.97 +
    1.98 +SECStatus
    1.99 +sslMutex_Init(sslMutex *pMutex, int shared)
   1.100 +{
   1.101 +    int  err;
   1.102 +    PR_ASSERT(pMutex);
   1.103 +    pMutex->isMultiProcess = (PRBool)(shared != 0);
   1.104 +    if (!shared) {
   1.105 +        return single_process_sslMutex_Init(pMutex);
   1.106 +    }
   1.107 +    pMutex->u.pipeStr.mPipes[0] = -1;
   1.108 +    pMutex->u.pipeStr.mPipes[1] = -1;
   1.109 +    pMutex->u.pipeStr.mPipes[2] = -1;
   1.110 +    pMutex->u.pipeStr.nWaiters  =  0;
   1.111 +
   1.112 +    err = pipe(pMutex->u.pipeStr.mPipes);
   1.113 +    if (err) {
   1.114 +	nss_MD_unix_map_default_error(errno);
   1.115 +	return err;
   1.116 +    }
   1.117 +#if NONBLOCKING_POSTS
   1.118 +    err = setNonBlocking(pMutex->u.pipeStr.mPipes[1], 1);
   1.119 +    if (err)
   1.120 +	goto loser;
   1.121 +#endif
   1.122 +
   1.123 +    pMutex->u.pipeStr.mPipes[2] = SSL_MUTEX_MAGIC;
   1.124 +
   1.125 +#if defined(LINUX) && defined(i386)
   1.126 +    /* Pipe starts out empty */
   1.127 +    return SECSuccess;
   1.128 +#else
   1.129 +    /* Pipe starts with one byte. */
   1.130 +    return sslMutex_Unlock(pMutex);
   1.131 +#endif
   1.132 +
   1.133 +loser:
   1.134 +    nss_MD_unix_map_default_error(errno);
   1.135 +    close(pMutex->u.pipeStr.mPipes[0]);
   1.136 +    close(pMutex->u.pipeStr.mPipes[1]);
   1.137 +    return SECFailure;
   1.138 +}
   1.139 +
   1.140 +SECStatus
   1.141 +sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal)
   1.142 +{
   1.143 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.144 +        return single_process_sslMutex_Destroy(pMutex);
   1.145 +    }
   1.146 +    if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
   1.147 +	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.148 +	return SECFailure;
   1.149 +    }
   1.150 +    close(pMutex->u.pipeStr.mPipes[0]);
   1.151 +    close(pMutex->u.pipeStr.mPipes[1]);
   1.152 +
   1.153 +    if (processLocal) {
   1.154 +	return SECSuccess;
   1.155 +    }
   1.156 +
   1.157 +    pMutex->u.pipeStr.mPipes[0] = -1;
   1.158 +    pMutex->u.pipeStr.mPipes[1] = -1;
   1.159 +    pMutex->u.pipeStr.mPipes[2] = -1;
   1.160 +    pMutex->u.pipeStr.nWaiters  =  0;
   1.161 +
   1.162 +    return SECSuccess;
   1.163 +}
   1.164 +
   1.165 +#if defined(LINUX) && defined(i386)
   1.166 +/* No memory barrier needed for this platform */
   1.167 +
   1.168 +/* nWaiters includes the holder of the lock (if any) and the number
   1.169 +** threads waiting for it.  After incrementing nWaiters, if the count
   1.170 +** is exactly 1, then you have the lock and may proceed.  If the 
   1.171 +** count is greater than 1, then you must wait on the pipe.
   1.172 +*/ 
   1.173 +
   1.174 +
   1.175 +SECStatus 
   1.176 +sslMutex_Unlock(sslMutex *pMutex)
   1.177 +{
   1.178 +    PRInt32 newValue;
   1.179 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.180 +        return single_process_sslMutex_Unlock(pMutex);
   1.181 +    }
   1.182 +
   1.183 +    if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
   1.184 +	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.185 +	return SECFailure;
   1.186 +    }
   1.187 +    /* Do Memory Barrier here. */
   1.188 +    newValue = PR_ATOMIC_DECREMENT(&pMutex->u.pipeStr.nWaiters);
   1.189 +    if (newValue > 0) {
   1.190 +	int  cc;
   1.191 +	char c  = 1;
   1.192 +	do {
   1.193 +	    cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1);
   1.194 +	} while (cc < 0 && (errno == EINTR || errno == EAGAIN));
   1.195 +	if (cc != 1) {
   1.196 +	    if (cc < 0)
   1.197 +		nss_MD_unix_map_default_error(errno);
   1.198 +	    else
   1.199 +		PORT_SetError(PR_UNKNOWN_ERROR);
   1.200 +	    return SECFailure;
   1.201 +	}
   1.202 +    }
   1.203 +    return SECSuccess;
   1.204 +}
   1.205 +
   1.206 +SECStatus 
   1.207 +sslMutex_Lock(sslMutex *pMutex)
   1.208 +{
   1.209 +    PRInt32 newValue;
   1.210 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.211 +        return single_process_sslMutex_Lock(pMutex);
   1.212 +    }
   1.213 +
   1.214 +    if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
   1.215 +	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.216 +	return SECFailure;
   1.217 +    }
   1.218 +    newValue = PR_ATOMIC_INCREMENT(&pMutex->u.pipeStr.nWaiters);
   1.219 +    /* Do Memory Barrier here. */
   1.220 +    if (newValue > 1) {
   1.221 +	int   cc;
   1.222 +	char  c;
   1.223 +	do {
   1.224 +	    cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1);
   1.225 +	} while (cc < 0 && errno == EINTR);
   1.226 +	if (cc != 1) {
   1.227 +	    if (cc < 0)
   1.228 +		nss_MD_unix_map_default_error(errno);
   1.229 +	    else
   1.230 +		PORT_SetError(PR_UNKNOWN_ERROR);
   1.231 +	    return SECFailure;
   1.232 +	}
   1.233 +    }
   1.234 +    return SECSuccess;
   1.235 +}
   1.236 +
   1.237 +#else
   1.238 +
   1.239 +/* Using Atomic operations requires the use of a memory barrier instruction 
   1.240 +** on PowerPC, Sparc, and Alpha.  NSPR's PR_Atomic functions do not perform
   1.241 +** them, and NSPR does not provide a function that does them (e.g. PR_Barrier).
   1.242 +** So, we don't use them on those platforms. 
   1.243 +*/
   1.244 +
   1.245 +SECStatus 
   1.246 +sslMutex_Unlock(sslMutex *pMutex)
   1.247 +{
   1.248 +    int  cc;
   1.249 +    char c  = 1;
   1.250 +
   1.251 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.252 +        return single_process_sslMutex_Unlock(pMutex);
   1.253 +    }
   1.254 +
   1.255 +    if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
   1.256 +	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.257 +	return SECFailure;
   1.258 +    }
   1.259 +    do {
   1.260 +	cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1);
   1.261 +    } while (cc < 0 && (errno == EINTR || errno == EAGAIN));
   1.262 +    if (cc != 1) {
   1.263 +	if (cc < 0)
   1.264 +	    nss_MD_unix_map_default_error(errno);
   1.265 +	else
   1.266 +	    PORT_SetError(PR_UNKNOWN_ERROR);
   1.267 +	return SECFailure;
   1.268 +    }
   1.269 +
   1.270 +    return SECSuccess;
   1.271 +}
   1.272 +
   1.273 +SECStatus 
   1.274 +sslMutex_Lock(sslMutex *pMutex)
   1.275 +{
   1.276 +    int   cc;
   1.277 +    char  c;
   1.278 +
   1.279 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.280 +        return single_process_sslMutex_Lock(pMutex);
   1.281 +    }
   1.282 + 
   1.283 +    if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
   1.284 +	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.285 +	return SECFailure;
   1.286 +    }
   1.287 +
   1.288 +    do {
   1.289 +	cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1);
   1.290 +    } while (cc < 0 && errno == EINTR);
   1.291 +    if (cc != 1) {
   1.292 +	if (cc < 0)
   1.293 +	    nss_MD_unix_map_default_error(errno);
   1.294 +	else
   1.295 +	    PORT_SetError(PR_UNKNOWN_ERROR);
   1.296 +	return SECFailure;
   1.297 +    }
   1.298 +
   1.299 +    return SECSuccess;
   1.300 +}
   1.301 +
   1.302 +#endif
   1.303 +
   1.304 +#elif defined(WIN32)
   1.305 +
   1.306 +#include "win32err.h"
   1.307 +
   1.308 +/* on Windows, we need to find the optimal type of locking mechanism to use
   1.309 + for the sslMutex.
   1.310 +
   1.311 + There are 3 cases :
   1.312 + 1) single-process, use a PRLock, as for all other platforms
   1.313 + 2) Win95 multi-process, use a Win32 mutex
   1.314 + 3) on WINNT multi-process, use a PRLock + a Win32 mutex
   1.315 +
   1.316 +*/
   1.317 +
   1.318 +#ifdef WINNT
   1.319 +
   1.320 +SECStatus sslMutex_2LevelInit(sslMutex *sem)
   1.321 +{
   1.322 +    /*  the following adds a PRLock to sslMutex . This is done in each
   1.323 +        process of a multi-process server and is only needed on WINNT, if
   1.324 +        using fibers. We can't tell if native threads or fibers are used, so
   1.325 +        we always do it on WINNT
   1.326 +    */
   1.327 +    PR_ASSERT(sem);
   1.328 +    if (sem) {
   1.329 +        /* we need to reset the sslLock in the children or the single_process init
   1.330 +           function below will assert */
   1.331 +        sem->u.sslLock = NULL;
   1.332 +    }
   1.333 +    return single_process_sslMutex_Init(sem);
   1.334 +}
   1.335 +
   1.336 +static SECStatus sslMutex_2LevelDestroy(sslMutex *sem)
   1.337 +{
   1.338 +    return single_process_sslMutex_Destroy(sem);
   1.339 +}
   1.340 +
   1.341 +#endif
   1.342 +
   1.343 +SECStatus
   1.344 +sslMutex_Init(sslMutex *pMutex, int shared)
   1.345 +{
   1.346 +#ifdef WINNT
   1.347 +    SECStatus retvalue;
   1.348 +#endif
   1.349 +    HANDLE hMutex;
   1.350 +    SECURITY_ATTRIBUTES attributes =
   1.351 +                                { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
   1.352 +
   1.353 +    PR_ASSERT(pMutex != 0 && (pMutex->u.sslMutx == 0 || 
   1.354 +              pMutex->u.sslMutx == INVALID_HANDLE_VALUE) );
   1.355 +    
   1.356 +    pMutex->isMultiProcess = (PRBool)(shared != 0);
   1.357 +    
   1.358 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.359 +        return single_process_sslMutex_Init(pMutex);
   1.360 +    }
   1.361 +    
   1.362 +#ifdef WINNT
   1.363 +    /*  we need a lock on WINNT for fibers in the parent process */
   1.364 +    retvalue = sslMutex_2LevelInit(pMutex);
   1.365 +    if (SECSuccess != retvalue)
   1.366 +        return SECFailure;
   1.367 +#endif
   1.368 +    
   1.369 +    if (!pMutex || ((hMutex = pMutex->u.sslMutx) != 0 && 
   1.370 +        hMutex != INVALID_HANDLE_VALUE)) {
   1.371 +        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.372 +        return SECFailure;
   1.373 +    }
   1.374 +    attributes.bInheritHandle = (shared ? TRUE : FALSE);
   1.375 +    hMutex = CreateMutex(&attributes, FALSE, NULL);
   1.376 +    if (hMutex == NULL) {
   1.377 +        hMutex = INVALID_HANDLE_VALUE;
   1.378 +        nss_MD_win32_map_default_error(GetLastError());
   1.379 +        return SECFailure;
   1.380 +    }
   1.381 +    pMutex->u.sslMutx = hMutex;
   1.382 +    return SECSuccess;
   1.383 +}
   1.384 +
   1.385 +SECStatus
   1.386 +sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal)
   1.387 +{
   1.388 +    HANDLE hMutex;
   1.389 +    int    rv;
   1.390 +    int retvalue = SECSuccess;
   1.391 +
   1.392 +    PR_ASSERT(pMutex != 0);
   1.393 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.394 +        return single_process_sslMutex_Destroy(pMutex);
   1.395 +    }
   1.396 +
   1.397 +    /*  multi-process mode */    
   1.398 +#ifdef WINNT
   1.399 +    /* on NT, get rid of the PRLock used for fibers within a process */
   1.400 +    retvalue = sslMutex_2LevelDestroy(pMutex);
   1.401 +#endif
   1.402 +    
   1.403 +    PR_ASSERT( pMutex->u.sslMutx != 0 && 
   1.404 +               pMutex->u.sslMutx != INVALID_HANDLE_VALUE);
   1.405 +    if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 
   1.406 +        || hMutex == INVALID_HANDLE_VALUE) {
   1.407 +        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.408 +        return SECFailure;
   1.409 +    }
   1.410 +    
   1.411 +    rv = CloseHandle(hMutex); /* ignore error */
   1.412 +    if (!processLocal && rv) {
   1.413 +        pMutex->u.sslMutx = hMutex = INVALID_HANDLE_VALUE;
   1.414 +    }
   1.415 +    if (!rv) {
   1.416 +        nss_MD_win32_map_default_error(GetLastError());
   1.417 +        retvalue = SECFailure;
   1.418 +    }
   1.419 +    return retvalue;
   1.420 +}
   1.421 +
   1.422 +int 
   1.423 +sslMutex_Unlock(sslMutex *pMutex)
   1.424 +{
   1.425 +    BOOL   success = FALSE;
   1.426 +    HANDLE hMutex;
   1.427 +
   1.428 +    PR_ASSERT(pMutex != 0 );
   1.429 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.430 +        return single_process_sslMutex_Unlock(pMutex);
   1.431 +    }
   1.432 +    
   1.433 +    PR_ASSERT(pMutex->u.sslMutx != 0 && 
   1.434 +              pMutex->u.sslMutx != INVALID_HANDLE_VALUE);
   1.435 +    if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 ||
   1.436 +        hMutex == INVALID_HANDLE_VALUE) {
   1.437 +        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.438 +        return SECFailure;
   1.439 +    }
   1.440 +    success = ReleaseMutex(hMutex);
   1.441 +    if (!success) {
   1.442 +        nss_MD_win32_map_default_error(GetLastError());
   1.443 +        return SECFailure;
   1.444 +    }
   1.445 +#ifdef WINNT
   1.446 +    return single_process_sslMutex_Unlock(pMutex);
   1.447 +    /* release PRLock for other fibers in the process */
   1.448 +#else
   1.449 +    return SECSuccess;
   1.450 +#endif
   1.451 +}
   1.452 +
   1.453 +int 
   1.454 +sslMutex_Lock(sslMutex *pMutex)
   1.455 +{
   1.456 +    HANDLE    hMutex;
   1.457 +    DWORD     event;
   1.458 +    DWORD     lastError;
   1.459 +    SECStatus rv;
   1.460 +    SECStatus retvalue = SECSuccess;
   1.461 +    PR_ASSERT(pMutex != 0);
   1.462 +
   1.463 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.464 +        return single_process_sslMutex_Lock(pMutex);
   1.465 +    }
   1.466 +#ifdef WINNT
   1.467 +    /* lock first to preserve from other threads/fibers
   1.468 +       in the same process */
   1.469 +    retvalue = single_process_sslMutex_Lock(pMutex);
   1.470 +#endif
   1.471 +    PR_ASSERT(pMutex->u.sslMutx != 0 && 
   1.472 +              pMutex->u.sslMutx != INVALID_HANDLE_VALUE);
   1.473 +    if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 || 
   1.474 +        hMutex == INVALID_HANDLE_VALUE) {
   1.475 +        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
   1.476 +        return SECFailure;      /* what else ? */
   1.477 +    }
   1.478 +    /* acquire the mutex to be the only owner accross all other processes */
   1.479 +    event = WaitForSingleObject(hMutex, INFINITE);
   1.480 +    switch (event) {
   1.481 +    case WAIT_OBJECT_0:
   1.482 +    case WAIT_ABANDONED:
   1.483 +        rv = SECSuccess;
   1.484 +        break;
   1.485 +
   1.486 +    case WAIT_TIMEOUT:
   1.487 +#if defined(WAIT_IO_COMPLETION)
   1.488 +    case WAIT_IO_COMPLETION:
   1.489 +#endif
   1.490 +    default:            /* should never happen. nothing we can do. */
   1.491 +        PR_ASSERT(!("WaitForSingleObject returned invalid value."));
   1.492 +	PORT_SetError(PR_UNKNOWN_ERROR);
   1.493 +	rv = SECFailure;
   1.494 +	break;
   1.495 +
   1.496 +    case WAIT_FAILED:           /* failure returns this */
   1.497 +        rv = SECFailure;
   1.498 +        lastError = GetLastError();     /* for debugging */
   1.499 +        nss_MD_win32_map_default_error(lastError);
   1.500 +        break;
   1.501 +    }
   1.502 +
   1.503 +    if (! (SECSuccess == retvalue && SECSuccess == rv)) {
   1.504 +        return SECFailure;
   1.505 +    }
   1.506 +    
   1.507 +    return SECSuccess;
   1.508 +}
   1.509 +
   1.510 +#elif defined(XP_UNIX)
   1.511 +
   1.512 +#include <errno.h>
   1.513 +#include "unix_err.h"
   1.514 +
   1.515 +SECStatus 
   1.516 +sslMutex_Init(sslMutex *pMutex, int shared)
   1.517 +{
   1.518 +    int rv;
   1.519 +    PR_ASSERT(pMutex);
   1.520 +    pMutex->isMultiProcess = (PRBool)(shared != 0);
   1.521 +    if (!shared) {
   1.522 +        return single_process_sslMutex_Init(pMutex);
   1.523 +    }
   1.524 +    do {
   1.525 +        rv = sem_init(&pMutex->u.sem, shared, 1);
   1.526 +    } while (rv < 0 && errno == EINTR);
   1.527 +    if (rv < 0) {
   1.528 +        nss_MD_unix_map_default_error(errno);
   1.529 +        return SECFailure;
   1.530 +    }
   1.531 +    return SECSuccess;
   1.532 +}
   1.533 +
   1.534 +SECStatus 
   1.535 +sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal)
   1.536 +{
   1.537 +    int rv;
   1.538 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.539 +        return single_process_sslMutex_Destroy(pMutex);
   1.540 +    }
   1.541 +
   1.542 +    /* semaphores are global resources. See SEM_DESTROY(3) man page */
   1.543 +    if (processLocal) {
   1.544 +	return SECSuccess;
   1.545 +    }
   1.546 +    do {
   1.547 +	rv = sem_destroy(&pMutex->u.sem);
   1.548 +    } while (rv < 0 && errno == EINTR);
   1.549 +    if (rv < 0) {
   1.550 +	nss_MD_unix_map_default_error(errno);
   1.551 +	return SECFailure;
   1.552 +    }
   1.553 +    return SECSuccess;
   1.554 +}
   1.555 +
   1.556 +SECStatus 
   1.557 +sslMutex_Unlock(sslMutex *pMutex)
   1.558 +{
   1.559 +    int rv;
   1.560 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.561 +        return single_process_sslMutex_Unlock(pMutex);
   1.562 +    }
   1.563 +    do {
   1.564 +	rv = sem_post(&pMutex->u.sem);
   1.565 +    } while (rv < 0 && errno == EINTR);
   1.566 +    if (rv < 0) {
   1.567 +	nss_MD_unix_map_default_error(errno);
   1.568 +	return SECFailure;
   1.569 +    }
   1.570 +    return SECSuccess;
   1.571 +}
   1.572 +
   1.573 +SECStatus 
   1.574 +sslMutex_Lock(sslMutex *pMutex)
   1.575 +{
   1.576 +    int rv;
   1.577 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.578 +        return single_process_sslMutex_Lock(pMutex);
   1.579 +    }
   1.580 +    do {
   1.581 +	rv = sem_wait(&pMutex->u.sem);
   1.582 +    } while (rv < 0 && errno == EINTR);
   1.583 +    if (rv < 0) {
   1.584 +	nss_MD_unix_map_default_error(errno);
   1.585 +	return SECFailure;
   1.586 +    }
   1.587 +    return SECSuccess;
   1.588 +}
   1.589 +
   1.590 +#else
   1.591 +
   1.592 +SECStatus 
   1.593 +sslMutex_Init(sslMutex *pMutex, int shared)
   1.594 +{
   1.595 +    PR_ASSERT(pMutex);
   1.596 +    pMutex->isMultiProcess = (PRBool)(shared != 0);
   1.597 +    if (!shared) {
   1.598 +        return single_process_sslMutex_Init(pMutex);
   1.599 +    }
   1.600 +    PORT_Assert(!("sslMutex_Init not implemented for multi-process applications !"));
   1.601 +    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
   1.602 +    return SECFailure;
   1.603 +}
   1.604 +
   1.605 +SECStatus 
   1.606 +sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal)
   1.607 +{
   1.608 +    PR_ASSERT(pMutex);
   1.609 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.610 +        return single_process_sslMutex_Destroy(pMutex);
   1.611 +    }
   1.612 +    PORT_Assert(!("sslMutex_Destroy not implemented for multi-process applications !"));
   1.613 +    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
   1.614 +    return SECFailure;
   1.615 +}
   1.616 +
   1.617 +SECStatus 
   1.618 +sslMutex_Unlock(sslMutex *pMutex)
   1.619 +{
   1.620 +    PR_ASSERT(pMutex);
   1.621 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.622 +        return single_process_sslMutex_Unlock(pMutex);
   1.623 +    }
   1.624 +    PORT_Assert(!("sslMutex_Unlock not implemented for multi-process applications !"));
   1.625 +    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
   1.626 +    return SECFailure;
   1.627 +}
   1.628 +
   1.629 +SECStatus 
   1.630 +sslMutex_Lock(sslMutex *pMutex)
   1.631 +{
   1.632 +    PR_ASSERT(pMutex);
   1.633 +    if (PR_FALSE == pMutex->isMultiProcess) {
   1.634 +        return single_process_sslMutex_Lock(pMutex);
   1.635 +    }
   1.636 +    PORT_Assert(!("sslMutex_Lock not implemented for multi-process applications !"));
   1.637 +    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
   1.638 +    return SECFailure;
   1.639 +}
   1.640 +
   1.641 +#endif
   1.642 +
   1.643 +#endif

mercurial