int32 psCoreOpen(void) { if (psCoreIsOpen) { return PS_CORE_IS_OPEN; } if (osdepTimeOpen() < 0) { psTraceCore("osdepTimeOpen failed\n"); return PS_FAILURE; } if (osdepEntropyOpen() < 0) { psTraceCore("osdepEntropyOpen failed\n"); osdepTimeClose(); return PS_FAILURE; } #ifdef USE_MULTITHREADING if (osdepMutexOpen() < 0) { psTraceCore("osdepMutexOpen failed\n"); osdepEntropyClose(); osdepTimeClose(); return PS_FAILURE; } #endif /* USE_MULTITHREADING */ psCoreIsOpen = PS_TRUE; return PS_SUCCESS; }
int32_t psCreateMutex(psMutex_t *mutex, uint32_t flags) { pthread_mutexattr_t attr; int rc; if (flags & ~PS_SHARED) { psErrorInt("psCreateMutex unsupported flag %u\n", flags); return PS_PLATFORM_FAIL; } if ((rc = pthread_mutexattr_init(&attr)) < 0) { psErrorInt("pthread_mutexattr_init failed %d\n", rc); return PS_PLATFORM_FAIL; } if ((flags & PS_SHARED) && (rc = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) < 0) { pthread_mutexattr_destroy(&attr); psErrorInt("pthread_mutexattr shared failed %d\n", rc); return PS_PLATFORM_FAIL; } if ((rc = pthread_mutex_init(mutex, &attr)) != 0) { pthread_mutexattr_destroy(&attr); psErrorInt("pthread_mutex_init failed %d\n", rc); return PS_PLATFORM_FAIL; } if (pthread_mutexattr_destroy(&attr) != 0) { psTraceCore("pthread_mutexattr_destroy failed\n"); } return PS_SUCCESS; }
void psUnlockMutex(psMutex_t *mutex) { if (pthread_mutex_unlock(mutex) != 0) { psTraceCore("pthread_mutex_unlock failed\n"); abort(); /* Catastrophic error: mutex does not work correctly. */ } }
int32 psUnlockMutex(psMutex_t *mutex) { if (pthread_mutex_unlock(mutex) != 0) { psTraceCore("pthread_mutex_unlock failed\n"); return PS_PLATFORM_FAIL; } return PS_SUCCESS; }
/* PScore Public API implementations */ int32 psGetEntropy(unsigned char *bytes, uint32 size, void *userPtr) { /* Read from /dev/random non-blocking first, then from urandom if it would block. Also, handle file closure case and re-open. */ int32 rc, sanity, retry, readBytes; unsigned char *where = bytes; sanity = retry = rc = readBytes = 0; while (size) { if ((rc = read(randfd, where, size)) < 0 || sanity > MAX_RAND_READS) { if (errno == EINTR) { if (sanity > MAX_RAND_READS) { psTraceCore("psGetEntropy failed randfd sanity\n"); return PS_PLATFORM_FAIL; } sanity++; continue; } else if (errno == EAGAIN) { break; } else if (errno == EBADF && retry == 0) { close(randfd); if ((randfd = open("/dev/random", O_RDONLY | O_NONBLOCK)) < 0) { break; } retry++; continue; } else { break; } } readBytes += rc; where += rc; size -= rc; } sanity = retry = 0; while (size) { if ((rc = read(urandfd, where, size)) < 0 || sanity > MAX_RAND_READS) { if (errno == EINTR) { if (sanity > MAX_RAND_READS) { psTraceCore("psGetEntropy failed urandfd sanity\n"); return PS_PLATFORM_FAIL; } sanity++; continue; } else if (errno == EBADF && retry == 0) { close(urandfd); if ((urandfd = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) < 0) { psTraceCore("psGetEntropy failed urandom open\n"); return PS_PLATFORM_FAIL; } retry++; continue; } else { psTraceIntCore("psGetEntropy fail errno %d\n", errno); return PS_PLATFORM_FAIL; } } readBytes += rc; where += rc; size -= rc; } return readBytes; }
void osdepMutexClose() { if (pthread_mutexattr_destroy(&attr) != 0) { psTraceCore("pthread_mutex_destroy failed\n"); } }