diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-01-04 14:24:24 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-01-04 14:24:24 +0000 |
commit | dc1565216a5d20ae0d75872151523252309a1292 (patch) | |
tree | d57454ba9a40386552179eddf60d28bd1e8f3d54 /nss/lib/ssl/sslmutex.c | |
parent | 26c046fbc57d53136b4fb3b5e0d18298318125d4 (diff) | |
download | nss-dc1565216a5d20ae0d75872151523252309a1292.tar.gz |
nss-3.28.1nss-3.28.1
Diffstat (limited to 'nss/lib/ssl/sslmutex.c')
-rw-r--r-- | nss/lib/ssl/sslmutex.c | 356 |
1 files changed, 187 insertions, 169 deletions
diff --git a/nss/lib/ssl/sslmutex.c b/nss/lib/ssl/sslmutex.c index af683da..560a9e8 100644 --- a/nss/lib/ssl/sslmutex.c +++ b/nss/lib/ssl/sslmutex.c @@ -4,15 +4,16 @@ #include "seccomon.h" /* This ifdef should match the one in sslsnce.c */ -#if defined(XP_UNIX) || defined(XP_WIN32) || defined (XP_OS2) || defined(XP_BEOS) +#if defined(XP_UNIX) || defined(XP_WIN32) || defined(XP_OS2) || defined(XP_BEOS) #include "sslmutex.h" #include "prerr.h" -static SECStatus single_process_sslMutex_Init(sslMutex* pMutex) +static SECStatus +single_process_sslMutex_Init(sslMutex* pMutex) { - PR_ASSERT(pMutex != 0 && pMutex->u.sslLock == 0 ); - + PR_ASSERT(pMutex != 0 && pMutex->u.sslLock == 0); + pMutex->u.sslLock = PR_NewLock(); if (!pMutex->u.sslLock) { return SECFailure; @@ -20,10 +21,11 @@ static SECStatus single_process_sslMutex_Init(sslMutex* pMutex) return SECSuccess; } -static SECStatus single_process_sslMutex_Destroy(sslMutex* pMutex) +static SECStatus +single_process_sslMutex_Destroy(sslMutex* pMutex) { PR_ASSERT(pMutex != 0); - PR_ASSERT(pMutex->u.sslLock!= 0); + PR_ASSERT(pMutex->u.sslLock != 0); if (!pMutex->u.sslLock) { PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure; @@ -32,10 +34,11 @@ static SECStatus single_process_sslMutex_Destroy(sslMutex* pMutex) return SECSuccess; } -static SECStatus single_process_sslMutex_Unlock(sslMutex* pMutex) +static SECStatus +single_process_sslMutex_Unlock(sslMutex* pMutex) { - PR_ASSERT(pMutex != 0 ); - PR_ASSERT(pMutex->u.sslLock !=0); + PR_ASSERT(pMutex != 0); + PR_ASSERT(pMutex->u.sslLock != 0); if (!pMutex->u.sslLock) { PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure; @@ -44,10 +47,11 @@ static SECStatus single_process_sslMutex_Unlock(sslMutex* pMutex) return SECSuccess; } -static SECStatus single_process_sslMutex_Lock(sslMutex* pMutex) +static SECStatus +single_process_sslMutex_Lock(sslMutex* pMutex) { PR_ASSERT(pMutex != 0); - PR_ASSERT(pMutex->u.sslLock != 0 ); + PR_ASSERT(pMutex->u.sslLock != 0); if (!pMutex->u.sslLock) { PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure; @@ -66,7 +70,7 @@ static SECStatus single_process_sslMutex_Lock(sslMutex* pMutex) #include "pratom.h" #define SSL_MUTEX_MAGIC 0xfeedfd -#define NONBLOCKING_POSTS 1 /* maybe this is faster */ +#define NONBLOCKING_POSTS 1 /* maybe this is faster */ #if NONBLOCKING_POSTS @@ -82,20 +86,20 @@ setNonBlocking(int fd, int nonBlocking) flags = fcntl(fd, F_GETFL, 0); if (0 > flags) - return flags; + return flags; if (nonBlocking) - flags |= FNONBLOCK; + flags |= FNONBLOCK; else - flags &= ~FNONBLOCK; + flags &= ~FNONBLOCK; err = fcntl(fd, F_SETFL, flags); return err; } #endif SECStatus -sslMutex_Init(sslMutex *pMutex, int shared) +sslMutex_Init(sslMutex* pMutex, int shared) { - int err; + int err; PR_ASSERT(pMutex); pMutex->isMultiProcess = (PRBool)(shared != 0); if (!shared) { @@ -104,17 +108,17 @@ sslMutex_Init(sslMutex *pMutex, int shared) pMutex->u.pipeStr.mPipes[0] = -1; pMutex->u.pipeStr.mPipes[1] = -1; pMutex->u.pipeStr.mPipes[2] = -1; - pMutex->u.pipeStr.nWaiters = 0; + pMutex->u.pipeStr.nWaiters = 0; err = pipe(pMutex->u.pipeStr.mPipes); if (err) { - nss_MD_unix_map_default_error(errno); - return err; + nss_MD_unix_map_default_error(errno); + return err; } #if NONBLOCKING_POSTS err = setNonBlocking(pMutex->u.pipeStr.mPipes[1], 1); if (err) - goto loser; + goto loser; #endif pMutex->u.pipeStr.mPipes[2] = SSL_MUTEX_MAGIC; @@ -135,26 +139,26 @@ loser: } SECStatus -sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) +sslMutex_Destroy(sslMutex* pMutex, PRBool processLocal) { if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Destroy(pMutex); } if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { - PORT_SetError(PR_INVALID_ARGUMENT_ERROR); - return SECFailure; + PORT_SetError(PR_INVALID_ARGUMENT_ERROR); + return SECFailure; } close(pMutex->u.pipeStr.mPipes[0]); close(pMutex->u.pipeStr.mPipes[1]); if (processLocal) { - return SECSuccess; + return SECSuccess; } pMutex->u.pipeStr.mPipes[0] = -1; pMutex->u.pipeStr.mPipes[1] = -1; pMutex->u.pipeStr.mPipes[2] = -1; - pMutex->u.pipeStr.nWaiters = 0; + pMutex->u.pipeStr.nWaiters = 0; return SECSuccess; } @@ -164,13 +168,12 @@ sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) /* nWaiters includes the holder of the lock (if any) and the number ** threads waiting for it. After incrementing nWaiters, if the count -** is exactly 1, then you have the lock and may proceed. If the +** is exactly 1, then you have the lock and may proceed. If the ** count is greater than 1, then you must wait on the pipe. -*/ - +*/ -SECStatus -sslMutex_Unlock(sslMutex *pMutex) +SECStatus +sslMutex_Unlock(sslMutex* pMutex) { PRInt32 newValue; if (PR_FALSE == pMutex->isMultiProcess) { @@ -178,30 +181,30 @@ sslMutex_Unlock(sslMutex *pMutex) } if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { - PORT_SetError(PR_INVALID_ARGUMENT_ERROR); - return SECFailure; + PORT_SetError(PR_INVALID_ARGUMENT_ERROR); + return SECFailure; } /* Do Memory Barrier here. */ newValue = PR_ATOMIC_DECREMENT(&pMutex->u.pipeStr.nWaiters); if (newValue > 0) { - int cc; - char c = 1; - do { - cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1); - } while (cc < 0 && (errno == EINTR || errno == EAGAIN)); - if (cc != 1) { - if (cc < 0) - nss_MD_unix_map_default_error(errno); - else - PORT_SetError(PR_UNKNOWN_ERROR); - return SECFailure; - } + int cc; + char c = 1; + do { + cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1); + } while (cc < 0 && (errno == EINTR || errno == EAGAIN)); + if (cc != 1) { + if (cc < 0) + nss_MD_unix_map_default_error(errno); + else + PORT_SetError(PR_UNKNOWN_ERROR); + return SECFailure; + } } return SECSuccess; } -SECStatus -sslMutex_Lock(sslMutex *pMutex) +SECStatus +sslMutex_Lock(sslMutex* pMutex) { PRInt32 newValue; if (PR_FALSE == pMutex->isMultiProcess) { @@ -209,88 +212,88 @@ sslMutex_Lock(sslMutex *pMutex) } if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { - PORT_SetError(PR_INVALID_ARGUMENT_ERROR); - return SECFailure; + PORT_SetError(PR_INVALID_ARGUMENT_ERROR); + return SECFailure; } newValue = PR_ATOMIC_INCREMENT(&pMutex->u.pipeStr.nWaiters); /* Do Memory Barrier here. */ if (newValue > 1) { - int cc; - char c; - do { - cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1); - } while (cc < 0 && errno == EINTR); - if (cc != 1) { - if (cc < 0) - nss_MD_unix_map_default_error(errno); - else - PORT_SetError(PR_UNKNOWN_ERROR); - return SECFailure; - } + int cc; + char c; + do { + cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1); + } while (cc < 0 && errno == EINTR); + if (cc != 1) { + if (cc < 0) + nss_MD_unix_map_default_error(errno); + else + PORT_SetError(PR_UNKNOWN_ERROR); + return SECFailure; + } } return SECSuccess; } #else -/* Using Atomic operations requires the use of a memory barrier instruction +/* Using Atomic operations requires the use of a memory barrier instruction ** on PowerPC, Sparc, and Alpha. NSPR's PR_Atomic functions do not perform ** them, and NSPR does not provide a function that does them (e.g. PR_Barrier). -** So, we don't use them on those platforms. +** So, we don't use them on those platforms. */ -SECStatus -sslMutex_Unlock(sslMutex *pMutex) +SECStatus +sslMutex_Unlock(sslMutex* pMutex) { - int cc; - char c = 1; + int cc; + char c = 1; if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Unlock(pMutex); } if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { - PORT_SetError(PR_INVALID_ARGUMENT_ERROR); - return SECFailure; + PORT_SetError(PR_INVALID_ARGUMENT_ERROR); + return SECFailure; } do { - cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1); + cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1); } while (cc < 0 && (errno == EINTR || errno == EAGAIN)); if (cc != 1) { - if (cc < 0) - nss_MD_unix_map_default_error(errno); - else - PORT_SetError(PR_UNKNOWN_ERROR); - return SECFailure; + if (cc < 0) + nss_MD_unix_map_default_error(errno); + else + PORT_SetError(PR_UNKNOWN_ERROR); + return SECFailure; } return SECSuccess; } -SECStatus -sslMutex_Lock(sslMutex *pMutex) +SECStatus +sslMutex_Lock(sslMutex* pMutex) { - int cc; - char c; + int cc; + char c; if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Lock(pMutex); } - + if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) { - PORT_SetError(PR_INVALID_ARGUMENT_ERROR); - return SECFailure; + PORT_SetError(PR_INVALID_ARGUMENT_ERROR); + return SECFailure; } do { - cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1); + cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1); } while (cc < 0 && errno == EINTR); if (cc != 1) { - if (cc < 0) - nss_MD_unix_map_default_error(errno); - else - PORT_SetError(PR_UNKNOWN_ERROR); - return SECFailure; + if (cc < 0) + nss_MD_unix_map_default_error(errno); + else + PORT_SetError(PR_UNKNOWN_ERROR); + return SECFailure; } return SECSuccess; @@ -314,7 +317,8 @@ sslMutex_Lock(sslMutex *pMutex) #ifdef WINNT -SECStatus sslMutex_2LevelInit(sslMutex *sem) +SECStatus +sslMutex_2LevelInit(sslMutex *sem) { /* the following adds a PRLock to sslMutex . This is done in each process of a multi-process server and is only needed on WINNT, if @@ -330,7 +334,8 @@ SECStatus sslMutex_2LevelInit(sslMutex *sem) return single_process_sslMutex_Init(sem); } -static SECStatus sslMutex_2LevelDestroy(sslMutex *sem) +static SECStatus +sslMutex_2LevelDestroy(sslMutex *sem) { return single_process_sslMutex_Destroy(sem); } @@ -345,26 +350,28 @@ sslMutex_Init(sslMutex *pMutex, int shared) #endif HANDLE hMutex; SECURITY_ATTRIBUTES attributes = - { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; + { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; + + PR_ASSERT(pMutex != 0 && (pMutex->u.sslMutx == 0 || + pMutex->u.sslMutx == + INVALID_HANDLE_VALUE)); - PR_ASSERT(pMutex != 0 && (pMutex->u.sslMutx == 0 || - pMutex->u.sslMutx == INVALID_HANDLE_VALUE) ); - pMutex->isMultiProcess = (PRBool)(shared != 0); - + if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Init(pMutex); } - + #ifdef WINNT /* we need a lock on WINNT for fibers in the parent process */ retvalue = sslMutex_2LevelInit(pMutex); if (SECSuccess != retvalue) return SECFailure; #endif - - if (!pMutex || ((hMutex = pMutex->u.sslMutx) != 0 && - hMutex != INVALID_HANDLE_VALUE)) { + + if (!pMutex || ((hMutex = pMutex->u.sslMutx) != 0 && + hMutex != + INVALID_HANDLE_VALUE)) { PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure; } @@ -383,28 +390,32 @@ SECStatus sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) { HANDLE hMutex; - int rv; + int rv; int retvalue = SECSuccess; PR_ASSERT(pMutex != 0); + if (!pMutex) { + PORT_SetError(PR_INVALID_ARGUMENT_ERROR); + return SECFailure; + } + if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Destroy(pMutex); } - /* multi-process mode */ +/* multi-process mode */ #ifdef WINNT /* on NT, get rid of the PRLock used for fibers within a process */ retvalue = sslMutex_2LevelDestroy(pMutex); #endif - - PR_ASSERT( pMutex->u.sslMutx != 0 && - pMutex->u.sslMutx != INVALID_HANDLE_VALUE); - if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 - || hMutex == INVALID_HANDLE_VALUE) { + + PR_ASSERT(pMutex->u.sslMutx != 0 && + pMutex->u.sslMutx != INVALID_HANDLE_VALUE); + if ((hMutex = pMutex->u.sslMutx) == 0 || hMutex == INVALID_HANDLE_VALUE) { PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure; } - + rv = CloseHandle(hMutex); /* ignore error */ if (!processLocal && rv) { pMutex->u.sslMutx = hMutex = INVALID_HANDLE_VALUE; @@ -416,21 +427,25 @@ sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) return retvalue; } -int +int sslMutex_Unlock(sslMutex *pMutex) { - BOOL success = FALSE; + BOOL success = FALSE; HANDLE hMutex; - PR_ASSERT(pMutex != 0 ); + PR_ASSERT(pMutex != 0); + if (!pMutex) { + PORT_SetError(PR_INVALID_ARGUMENT_ERROR); + return SECFailure; + } + if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Unlock(pMutex); } - - PR_ASSERT(pMutex->u.sslMutx != 0 && + + PR_ASSERT(pMutex->u.sslMutx != 0 && pMutex->u.sslMutx != INVALID_HANDLE_VALUE); - if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 || - hMutex == INVALID_HANDLE_VALUE) { + if ((hMutex = pMutex->u.sslMutx) == 0 || hMutex == INVALID_HANDLE_VALUE) { PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure; } @@ -441,66 +456,69 @@ sslMutex_Unlock(sslMutex *pMutex) } #ifdef WINNT return single_process_sslMutex_Unlock(pMutex); - /* release PRLock for other fibers in the process */ +/* release PRLock for other fibers in the process */ #else return SECSuccess; #endif } -int +int sslMutex_Lock(sslMutex *pMutex) { - HANDLE hMutex; - DWORD event; - DWORD lastError; + HANDLE hMutex; + DWORD event; + DWORD lastError; SECStatus rv; SECStatus retvalue = SECSuccess; + PR_ASSERT(pMutex != 0); + if (!pMutex) { + PORT_SetError(PR_INVALID_ARGUMENT_ERROR); + return SECFailure; + } if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Lock(pMutex); } #ifdef WINNT - /* lock first to preserve from other threads/fibers - in the same process */ + /* lock first to preserve from other threads/fibers in the same process */ retvalue = single_process_sslMutex_Lock(pMutex); #endif - PR_ASSERT(pMutex->u.sslMutx != 0 && + PR_ASSERT(pMutex->u.sslMutx != 0 && pMutex->u.sslMutx != INVALID_HANDLE_VALUE); - if (!pMutex || (hMutex = pMutex->u.sslMutx) == 0 || - hMutex == INVALID_HANDLE_VALUE) { + if ((hMutex = pMutex->u.sslMutx) == 0 || hMutex == INVALID_HANDLE_VALUE) { PORT_SetError(PR_INVALID_ARGUMENT_ERROR); - return SECFailure; /* what else ? */ + return SECFailure; /* what else ? */ } /* acquire the mutex to be the only owner accross all other processes */ event = WaitForSingleObject(hMutex, INFINITE); switch (event) { - case WAIT_OBJECT_0: - case WAIT_ABANDONED: - rv = SECSuccess; - break; + case WAIT_OBJECT_0: + case WAIT_ABANDONED: + rv = SECSuccess; + break; - case WAIT_TIMEOUT: + case WAIT_TIMEOUT: #if defined(WAIT_IO_COMPLETION) - case WAIT_IO_COMPLETION: + case WAIT_IO_COMPLETION: #endif - default: /* should never happen. nothing we can do. */ - PR_ASSERT(!("WaitForSingleObject returned invalid value.")); - PORT_SetError(PR_UNKNOWN_ERROR); - rv = SECFailure; - break; + default: /* should never happen. nothing we can do. */ + PR_ASSERT(!("WaitForSingleObject returned invalid value.")); + PORT_SetError(PR_UNKNOWN_ERROR); + rv = SECFailure; + break; - case WAIT_FAILED: /* failure returns this */ - rv = SECFailure; - lastError = GetLastError(); /* for debugging */ - nss_MD_win32_map_default_error(lastError); - break; + case WAIT_FAILED: /* failure returns this */ + rv = SECFailure; + lastError = GetLastError(); /* for debugging */ + nss_MD_win32_map_default_error(lastError); + break; } - if (! (SECSuccess == retvalue && SECSuccess == rv)) { + if (!(SECSuccess == retvalue && SECSuccess == rv)) { return SECFailure; } - + return SECSuccess; } @@ -509,8 +527,8 @@ sslMutex_Lock(sslMutex *pMutex) #include <errno.h> #include "unix_err.h" -SECStatus -sslMutex_Init(sslMutex *pMutex, int shared) +SECStatus +sslMutex_Init(sslMutex* pMutex, int shared) { int rv; PR_ASSERT(pMutex); @@ -528,8 +546,8 @@ sslMutex_Init(sslMutex *pMutex, int shared) return SECSuccess; } -SECStatus -sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) +SECStatus +sslMutex_Destroy(sslMutex* pMutex, PRBool processLocal) { int rv; if (PR_FALSE == pMutex->isMultiProcess) { @@ -538,56 +556,56 @@ sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) /* semaphores are global resources. See SEM_DESTROY(3) man page */ if (processLocal) { - return SECSuccess; + return SECSuccess; } do { - rv = sem_destroy(&pMutex->u.sem); + rv = sem_destroy(&pMutex->u.sem); } while (rv < 0 && errno == EINTR); if (rv < 0) { - nss_MD_unix_map_default_error(errno); - return SECFailure; + nss_MD_unix_map_default_error(errno); + return SECFailure; } return SECSuccess; } -SECStatus -sslMutex_Unlock(sslMutex *pMutex) +SECStatus +sslMutex_Unlock(sslMutex* pMutex) { int rv; if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Unlock(pMutex); } do { - rv = sem_post(&pMutex->u.sem); + rv = sem_post(&pMutex->u.sem); } while (rv < 0 && errno == EINTR); if (rv < 0) { - nss_MD_unix_map_default_error(errno); - return SECFailure; + nss_MD_unix_map_default_error(errno); + return SECFailure; } return SECSuccess; } -SECStatus -sslMutex_Lock(sslMutex *pMutex) +SECStatus +sslMutex_Lock(sslMutex* pMutex) { int rv; if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Lock(pMutex); } do { - rv = sem_wait(&pMutex->u.sem); + rv = sem_wait(&pMutex->u.sem); } while (rv < 0 && errno == EINTR); if (rv < 0) { - nss_MD_unix_map_default_error(errno); - return SECFailure; + nss_MD_unix_map_default_error(errno); + return SECFailure; } return SECSuccess; } #else -SECStatus -sslMutex_Init(sslMutex *pMutex, int shared) +SECStatus +sslMutex_Init(sslMutex* pMutex, int shared) { PR_ASSERT(pMutex); pMutex->isMultiProcess = (PRBool)(shared != 0); @@ -599,8 +617,8 @@ sslMutex_Init(sslMutex *pMutex, int shared) return SECFailure; } -SECStatus -sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) +SECStatus +sslMutex_Destroy(sslMutex* pMutex, PRBool processLocal) { PR_ASSERT(pMutex); if (PR_FALSE == pMutex->isMultiProcess) { @@ -611,8 +629,8 @@ sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal) return SECFailure; } -SECStatus -sslMutex_Unlock(sslMutex *pMutex) +SECStatus +sslMutex_Unlock(sslMutex* pMutex) { PR_ASSERT(pMutex); if (PR_FALSE == pMutex->isMultiProcess) { @@ -623,8 +641,8 @@ sslMutex_Unlock(sslMutex *pMutex) return SECFailure; } -SECStatus -sslMutex_Lock(sslMutex *pMutex) +SECStatus +sslMutex_Lock(sslMutex* pMutex) { PR_ASSERT(pMutex); if (PR_FALSE == pMutex->isMultiProcess) { |