afs_int32 FreeAllLocks(struct rx_call *call, afs_uint32 instanceId) { db_lockP startPtr, endPtr; struct ubik_trans *ut; afs_int32 code; if (callPermitted(call) == 0) return (BUDB_NOTPERMITTED); code = InitRPC(&ut, LOCKWRITE, 1); if (code) return (code); startPtr = &db.h.textLocks[0]; endPtr = &db.h.textLocks[TB_NUM - 1]; while (startPtr <= endPtr) { if ((ntohl(startPtr->lockState) == 1) && (ntohl(startPtr->instanceId) == instanceId) ) { /* release the lock */ startPtr->lockState = 0; /* unlock it */ startPtr->lockTime = 0; startPtr->expires = 0; startPtr->instanceId = 0; dbwrite(ut, DBH_POS(startPtr), (char *)startPtr, sizeof(db_lockT)); } startPtr++; } code = ubik_EndTrans(ut); return (code); }
void * setupDbDump(void *param) { int writeFid = (intptr_t)param; afs_int32 code = 0; code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1); if (code) goto error_exit; code = writeDatabase(dumpSyncPtr->ut, writeFid); if (code) LogError(code, "writeDatabase failed\n"); code = close(writeFid); if (code) LogError(code, "pipe writer close failed\n"); LogDebug(5, "writeDatabase complete\n"); error_exit: if (dumpSyncPtr->ut) ubik_EndTrans(dumpSyncPtr->ut); return (void *)(intptr_t)(code); }
afs_int32 FreeLock(struct rx_call *call, afs_uint32 lockHandle) { db_lockP lockPtr = 0; struct ubik_trans *ut; afs_int32 code; if (callPermitted(call) == 0) return (BUDB_NOTPERMITTED); code = InitRPC(&ut, LOCKWRITE, 1); if (code) return (code); if (checkLockHandle(ut, lockHandle) == 0) ABORT(BUDB_BADARGUMENT); lockPtr = &db.h.textLocks[lockHandle - 1]; lockPtr->lockState = 0; /* unlock it */ lockPtr->lockTime = 0; lockPtr->expires = 0; lockPtr->instanceId = 0; dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT)); code = ubik_EndTrans(ut); return (code); abort_exit: ubik_AbortTrans(ut); return (code); }
afs_int32 GetInstanceId(struct rx_call *call, afs_uint32 *instanceId) { struct ubik_trans *ut; afs_int32 code; afs_int32 instanceValue; LogDebug(4, "GetInstanceId:\n"); /* *** Allow anyone to get the instance id *** * if ( callPermitted(call) == 0 ) * return(BUDB_NOTPERMITTED); */ code = InitRPC(&ut, LOCKWRITE, 1); if (code) return (code); instanceValue = ntohl(db.h.lastInstanceId) + 1; set_header_word(ut, lastInstanceId, htonl(instanceValue)); code = ubik_EndTrans(ut); return (code); }
afs_int32 GetLock(struct rx_call *call, afs_uint32 instanceId, afs_int32 lockName, afs_int32 expiration, afs_uint32 *lockHandle) { struct timeval tv; db_lockP lockPtr; struct ubik_trans *ut; afs_int32 code; if (callPermitted(call) == 0) return (BUDB_NOTPERMITTED); if ((lockName < 0) || (lockName >= TB_NUM)) return (BUDB_BADARGUMENT); /* get the current time */ gettimeofday(&tv, 0); code = InitRPC(&ut, LOCKWRITE, 1); if (code) return (code); lockPtr = &db.h.textLocks[lockName]; if ((ntohl(lockPtr->lockState) != 0) /* lock set */ &&(ntohl(lockPtr->expires) > tv.tv_sec) /* not expired */ ) { if (ntohl(lockPtr->instanceId) == instanceId) code = BUDB_SELFLOCKED; else code = BUDB_LOCKED; goto abort_exit; } lockPtr->lockState = htonl(1); /* lock it */ lockPtr->lockTime = htonl(tv.tv_sec); /* when locked */ lockPtr->expires = htonl(tv.tv_sec + expiration); lockPtr->instanceId = htonl(instanceId); code = dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT)); if (code) ABORT(code); *lockHandle = (afs_uint32) (lockName + 1); code = ubik_EndTrans(ut); return (code); abort_exit: ubik_AbortTrans(ut); return (code); }
// Initialize communication related components void CQ2RTApplication::InitCommComponents(void) { CAppParams *ParamsMgr = CAppParams::Instance(); // Create all the protocol engines except for the terminal port and the tester for(int i = 0; i < TOTAL_COM_NUM; i++) if(i != (ParamsMgr->TerminalComNum - 1)) { // Create a protocl engine instance for each com port in the system m_ProtocolEngines[i] = new CEdenProtocolEngine(ComPortFactory(i + 1)); } else m_ProtocolEngines[i] = NULL; // Attach the communication clients to the engines // ----------------------------------------------- // Initialize the OCB client if(m_ProtocolEngines[ParamsMgr->OCBComNum - 1] != NULL) COCBProtocolClient::Init(m_ProtocolEngines[ParamsMgr->OCBComNum - 1]); else throw EQ2RTApplication("Invalid OCB com port number"); // Initialize the MCB client if(m_ProtocolEngines[ParamsMgr->MCBComNum - 1] != NULL) CMCBProtocolClient::Init(m_ProtocolEngines[ParamsMgr->MCBComNum - 1]); else throw EQ2RTApplication("Invalid MCB com port number"); // Initialize the OHDB client if(m_ProtocolEngines[ParamsMgr->OHDBComNum - 1] != NULL) COHDBProtocolClient::Init(m_ProtocolEngines[ParamsMgr->OHDBComNum - 1]); else throw EQ2RTApplication("Invalid OHDB com port number"); // COCBProtocolClient *OCBClient = COCBProtocolClient::Instance(); // OCBClient->m_PrintLogMessage = &PrintLogMessage; // CMCBProtocolClient *MCBClient =CMCBProtocolClient::Instance(); // MCBClient->m_PrintLogMessage = &PrintLogMessage; // COHDBProtocolClient *OHDBClient = COHDBProtocolClient::Instance(); // OHDBClient->m_PrintLogMessage = &PrintLogMessage; InitRPC(); }
afs_int32 GetTextVersion(struct rx_call *call, afs_int32 textType, afs_uint32 *tversion) { afs_int32 code; struct ubik_trans *ut; if (callPermitted(call) == 0) return (BUDB_NOTPERMITTED); if ((textType < 0) || (textType >= TB_NUM)) return (BUDB_BADARGUMENT); code = InitRPC(&ut, LOCKREAD, 1); if (code) return (code); *tversion = ntohl(db.h.textBlock[textType].version); code = ubik_EndTrans(ut); return (code); }
afs_int32 RestoreDbHeader(struct rx_call *call, struct DbHeader *header) { struct ubik_trans *ut = 0; afs_int32 code = 0; extern struct memoryDB db; if (callPermitted(call) == 0) ERROR(BUDB_NOTPERMITTED); code = InitRPC(&ut, LOCKWRITE, 1); if (code) goto error_exit; if (header->dbversion != ntohl(db.h.version)) ERROR(BUDB_VERSIONMISMATCH); /* merge rather than replace the header information */ if (db.h.lastDumpId < htonl(header->lastDumpId)) db.h.lastDumpId = htonl(header->lastDumpId); if (db.h.lastTapeId < htonl(header->lastTapeId)) db.h.lastTapeId = htonl(header->lastTapeId); if (db.h.lastInstanceId < htonl(header->lastInstanceId)) db.h.lastInstanceId = htonl(header->lastInstanceId); code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h)); if (code) code = BUDB_IO; error_exit: if (ut) ubik_EndTrans(ut); return (code); }
int main(void) { long go; GetRoot(); /* Also creates a child process for transcribing stdout */ GetParms(); MakeFiles(); /* in test directory */ InitRPC(); MakeWorkers(); GetConns(); GetVar(&go, "Say when: "); DoBindings(); MakeClients(); /* wait for all clients to get ready */ while (ClientsReady < Clients) LWP_DispatchProcess(); LWP_NoYieldSignal((char *)&ClientsReady); LWP_WaitProcess((char *)main); /* infinite wait */ return 0; /* make compiler happy */ }
afs_int32 GetText(struct rx_call *call, afs_uint32 lockHandle, afs_int32 textType, afs_int32 maxLength, afs_int32 offset, afs_int32 *nextOffset, charListT *charListPtr) { struct ubik_trans *ut = 0; struct block block; afs_int32 transferSize, chunkSize; afs_int32 blockOffset; dbadr lastBlockAddr; afs_int32 nblocks; struct textBlock *tbPtr; afs_int32 textRemaining; char *textPtr; afs_int32 code; LogDebug(5, "GetText: type %d, offset %d, nextOffset %"AFS_PTR_FMT ", maxLength %d\n", textType, offset, nextOffset, maxLength); if (callPermitted(call) == 0) { code = BUDB_NOTPERMITTED; goto no_xfer_abort; } /* check parameters */ if ((offset < 0) || (textType < 0) || (textType >= TB_NUM) ) { code = BUDB_BADARGUMENT; goto no_xfer_abort; } /* start the transaction */ code = InitRPC(&ut, LOCKWRITE, 1); if (code) goto no_xfer_abort; /* fetch the lock state */ if (checkLockHandle(ut, lockHandle) == 0) { code = BUDB_NOTLOCKED; goto no_xfer_abort; } tbPtr = &db.h.textBlock[textType]; if ((ntohl(tbPtr->size) > 0) && (offset >= ntohl(tbPtr->size)) ) { code = BUDB_BADARGUMENT; goto no_xfer_abort; } LogDebug(5, "GetText: store size is %d\n", ntohl(tbPtr->size)); /* compute minimum of remaining text or user buffer */ textRemaining = ntohl(tbPtr->size) - offset; transferSize = MIN(textRemaining, maxLength); /* allocate the transfer storage */ if (transferSize <= 0) { charListPtr->charListT_len = 0L; charListPtr->charListT_val = NULL; } else { charListPtr->charListT_len = transferSize; charListPtr->charListT_val = (char *)malloc(transferSize); if (charListPtr->charListT_val == 0) ABORT(BUDB_NOMEM); } textPtr = charListPtr->charListT_val; *nextOffset = offset + transferSize; /* setup the datablock. read and discard all blocks up to the one the * offset specifies */ nblocks = offset / BLOCK_DATA_SIZE; lastBlockAddr = ntohl(tbPtr->textAddr); while (nblocks--) { code = dbread(ut, lastBlockAddr, (char *)&block, sizeof(block)); if (code) ABORT(BUDB_IO); lastBlockAddr = ntohl(block.h.next); } while (transferSize > 0) { code = dbread(ut, lastBlockAddr, (char *)&block, sizeof(block)); if (code) ABORT(BUDB_IO); LogDebug(5, "fetched block %d\n", lastBlockAddr); /* compute the data size to extract */ blockOffset = offset % BLOCK_DATA_SIZE; textRemaining = BLOCK_DATA_SIZE - blockOffset; chunkSize = min(textRemaining, transferSize); memcpy(textPtr, &block.a[blockOffset], chunkSize); /* LogDebug(5, "transfering %d bytes: %s\n", chunkSize, textPtr); */ transferSize -= chunkSize; offset += chunkSize; textPtr += chunkSize; if (transferSize) { /* setup lastBlockAddr */ lastBlockAddr = ntohl(block.h.next); } } if (*nextOffset == ntohl(tbPtr->size)) { /* all done */ *nextOffset = -1; } /* error_exit: */ code = ubik_EndTrans(ut); /* printf("in error exit, code=%ld\n", code); */ return (code); no_xfer_abort: charListPtr->charListT_len = 0; charListPtr->charListT_val = (char *)malloc(0); abort_exit: if (ut) ubik_AbortTrans(ut); /* printf("in abort exit, code=%ld\n", code); */ return (code); }
afs_int32 SaveText(struct rx_call *call, afs_uint32 lockHandle, afs_int32 textType, afs_int32 offset, afs_int32 flags, charListT *charListPtr) { struct ubik_trans *ut; struct block diskBlock; dbadr diskBlockAddr; afs_int32 remainingInBlock, chunkSize; struct textBlock *tbPtr; afs_int32 textLength = charListPtr->charListT_len; char *textptr = charListPtr->charListT_val; afs_int32 code; LogDebug(5, "SaveText: type %d, offset %d, length %d\n", textType, offset, textLength); if (callPermitted(call) == 0) return (BUDB_NOTPERMITTED); if ((textLength > BLOCK_DATA_SIZE) || (offset < 0)) return (BUDB_BADARGUMENT); code = InitRPC(&ut, LOCKWRITE, 1); if (code) return (code); /* fetch the lock state */ if (checkLockHandle(ut, lockHandle) == 0) ABORT(BUDB_NOTLOCKED); if ((textType < 0) || (textType >= TB_NUM)) ABORT(BUDB_BADARGUMENT); tbPtr = &db.h.textBlock[textType]; LogDebug(5, "SaveText: lockHandle %d textType %d offset %d flags %d txtlength %d\n", lockHandle, textType, offset, flags, textLength); if (offset == 0) { /* release any blocks from previous transactions */ diskBlockAddr = ntohl(tbPtr->newTextAddr); freeOldBlockChain(ut, diskBlockAddr); if (textLength) { code = AllocBlock(ut, &diskBlock, &diskBlockAddr); if (code) ABORT(code); LogDebug(5, "allocated block %d\n", diskBlockAddr); /* set block type */ diskBlock.h.type = text_BLOCK; /* save it in the database header */ tbPtr->newsize = 0; tbPtr->newTextAddr = htonl(diskBlockAddr); dbwrite(ut, (char *)tbPtr - (char *)&db.h, (char *)tbPtr, sizeof(struct textBlock)); } else { tbPtr->newsize = 0; tbPtr->newTextAddr = 0; } } else { /* non-zero offset */ int nblocks; if (offset != ntohl(tbPtr->newsize)) ABORT(BUDB_BADARGUMENT); /* locate the block to which offset refers */ nblocks = offset / BLOCK_DATA_SIZE; diskBlockAddr = ntohl(tbPtr->newTextAddr); if (diskBlockAddr == 0) ABORT(BUDB_BADARGUMENT); code = dbread(ut, diskBlockAddr, (char *)&diskBlock, sizeof(diskBlock)); if (code) ABORT(code); while (nblocks--) { diskBlockAddr = ntohl(diskBlock.h.next); code = dbread(ut, diskBlockAddr, (char *)&diskBlock, sizeof(diskBlock)); if (code) ABORT(code); } } /* diskBlock and diskBlockAddr now point to the last block in the chain */ while (textLength) { /* compute the transfer size */ remainingInBlock = (BLOCK_DATA_SIZE - (offset % BLOCK_DATA_SIZE)); chunkSize = MIN(remainingInBlock, textLength); /* copy in the data */ memcpy(&diskBlock.a[offset % BLOCK_DATA_SIZE], textptr, chunkSize); /* LogDebug(5, "text is %s\n", textptr); */ textLength -= chunkSize; textptr += chunkSize; offset += chunkSize; tbPtr->newsize = htonl(ntohl(tbPtr->newsize) + chunkSize); if (textLength > 0) { afs_int32 prevBlockAddr; afs_int32 linkOffset; afs_int32 linkValue; /* have to add another block to the chain */ code = dbwrite(ut, diskBlockAddr, (char *)&diskBlock, sizeof(diskBlock)); if (code) ABORT(code); prevBlockAddr = (afs_int32) diskBlockAddr; code = AllocBlock(ut, &diskBlock, &diskBlockAddr); if (code) ABORT(code); LogDebug(5, "allocated block %d\n", diskBlockAddr); /* set block type */ diskBlock.h.type = text_BLOCK; /* now have to update the previous block's link */ linkOffset = (afs_int32) ((char*)& diskBlock.h.next - (char*)& diskBlock); linkValue = htonl(diskBlockAddr); code = dbwrite(ut, (afs_int32) prevBlockAddr + linkOffset, (char *)&linkValue, sizeof(afs_int32)); if (code) ABORT(code); } else { /* just write the old block */ code = dbwrite(ut, diskBlockAddr, (char *)&diskBlock, sizeof(diskBlock)); if (code) ABORT(code); } } if (flags & BUDB_TEXT_COMPLETE) { /* done */ /* this was the last chunk of text */ diskBlockAddr = ntohl(tbPtr->textAddr); freeOldBlockChain(ut, diskBlockAddr); tbPtr->textAddr = tbPtr->newTextAddr; tbPtr->newTextAddr = 0; tbPtr->size = tbPtr->newsize; tbPtr->newsize = 0; tbPtr->version = htonl(ntohl(tbPtr->version) + 1); /* saveTextToFile(ut, tbPtr); */ } /* update size and other text header info */ code = dbwrite(ut, (char *)tbPtr - (char *)&db.h, (char *)tbPtr, sizeof(struct textBlock)); if (code) ABORT(code); /*error_exit: */ code = ubik_EndTrans(ut); return (code); abort_exit: ubik_AbortTrans(ut); return (code); }