bool JdbcUtil::getSemaphores(bool create) { setError(NULL); if (semaphoreId == -1) { int semid; if ((semid = semget(memoryKey, 0, 0666 ))==-1) { if ((errno==ENOENT) && create) { if ((semid = semget(memoryKey, TOTAL_SEM, IPC_CREAT | 0666 ))==-1) { setPerror("Cannot create semaphores"); return false; } } else { setPerror("Cannot get semaphores"); return false; } } semaphoreId = semid; } return true; }
bool JdbcUtil::removeMemory(void) { setError(NULL); if (!getMemory(false)) { // If not found, it does not exist if (errorCode == ENOENT) return true; return false; } // deactivate the old memory in case some still has it attached if (attachMemory()) { sharedMemory->active = false; detachMemory(); } // Remove the memory if (shmctl(sharedMemoryId,IPC_RMID,NULL)==-1) { setPerror("Cannot remove shared memory"); return false; } sharedMemoryId = -1; return true; }
bool JdbcUtil::attachMemory(void) { setError(NULL); // If we have not accessed the memory, get it // only if it has already been created. if ((sharedMemoryId==-1) && !getMemory(false)) return false; if (sharedMemory && !sharedMemory->active) { // We are attached to an old memory segment. // Detach it so we can attach the new one. if (!detachMemory()) return false; } if (sharedMemory==NULL) { // At this point we have an id to the memory, now we // can attach it. void *shm; if ((shm = shmat(sharedMemoryId, NULL, 0)) == (void *) -1) { setPerror("Cannot attach shared memory"); return false; } sharedMemory = (struct JdbcUtilMemoryStruct *) shm; } return true; }
/* setCreate() */ LS_BITSET_T * setCreate(const int size, int (*directFunction)(void *), void *(*inverseFunction)(int ), char *caller) { static char *fname = "setCreate()"; int sz; LS_BITSET_T *set; set = calloc(1, sizeof(LS_BITSET_T)); if (!set) { bitseterrno = LS_BITSET_ERR_NOMEM; ls_syslog(LOG_ERR,"%s %s", fname, setPerror(bitseterrno)); return NULL; } set->setDescription = putstr_(caller); /* Size is the number of elements the caller wants * to store in the set. */ sz = (size > 0) ? size : SET_DEFAULT_SIZE; /* Width is how many bytes we need to store * size number of elements. */ set->setWidth = (sz + WORDLENGTH - 1)/WORDLENGTH; /* This is the new size eventually rounded up by 1. */ set->setSize = (set->setWidth) * WORDLENGTH; set->setNumElements = 0; set->bitmask = calloc(set->setWidth, sizeof(unsigned int)); if (!set->bitmask) { bitseterrno = LS_BITSET_ERR_NOMEM; return NULL; } if (directFunction) set->getIndexByObject = directFunction; else set->getIndexByObject = NULL; if (inverseFunction) set->getObjectByIndex = inverseFunction; else set->getObjectByIndex = NULL; return (set); }
bool JdbcUtil::getMemory(bool create) { setError(NULL); if (sharedMemoryId==-1) { int shmid; // Try to get the memory if ((shmid = shmget( memoryKey, 0, 0666)) < 0) { if ((errno==ENOENT) && create) { // Does not exist. Try to create. if ((shmid = shmget( memoryKey, SHARED_MEMORY_SIZE, IPC_CREAT | 0666)) < 0) { setPerror("Cannot create shared memory"); return false; } } else { setPerror("Cannot open shared memory"); return false; } } sharedMemoryId = shmid; } return true; }
bool JdbcUtil::detachMemory(void) { setError(NULL); if (sharedMemory) { // Detach the memory segment if (shmdt(sharedMemory)==-1) { setPerror("Cannot detach memory"); return false; } sharedMemory = NULL; } return true; }
bool JdbcUtil::removeSemaphores(void) { setError(NULL); if (semaphoreId!=-1) { if (semctl(semaphoreId, 0, IPC_RMID) == -1) { setPerror("Cannot remove semaphore"); return false; } semaphoreId = -1; } return true; }
bool JdbcUtil::memoryInit(void) { setError(NULL); if (!removeMemory()) return false; if (!removeSemaphores()) return false; if (!getMemory(true)) return false; if (!attachMemory()) return false; memset(sharedMemory,0,SHARED_MEMORY_SIZE); sharedMemory->active = true; if (!getSemaphores(true)) return false; if (semctl(semaphoreId, 0, SETVAL, 1) == -1) { int err = errno; removeMemory(); removeSemaphores(); setPerror(err,"Cannot initialize semaphore value"); return false; } return true; }
void JdbcUtil::setPerror(const char *action) { setPerror(errno,action); }