NSFC_PR_Open(const char *name, PRIntn flags, PRIntn mode) { PRFileDesc *fd; #ifdef ESTALE // Retry PR_Open() up to NSFC_ESTALE_RETRIES times if it returns ESTALE. // Bug 545152 int retries = NSFC_ESTALE_RETRIES; do { fd = PR_Open(name, flags, mode); } while (!fd && PR_GetOSError() == ESTALE && retries--); #else fd = PR_Open(name, flags, mode); #endif #ifdef XP_WIN32 /* Work-around for NSPR error mapping deficiency */ if ((fd == NULL) && (PR_GetError() == PR_UNKNOWN_ERROR) && (PR_GetOSError() == ERROR_SHARING_VIOLATION)) PR_SetError(PR_FILE_IS_BUSY_ERROR, ERROR_SHARING_VIOLATION); #endif return fd; }
/* * Serve_TransmitFile_Client * Thread, started by the server, for serving a client connection. * Trasmits a small file, with a header, and a large file, without * a header */ static void Serve_TransmitFile_Client(void *arg) { Serve_Client_Param *scp = (Serve_Client_Param *) arg; PRFileDesc *sockfd; PRInt32 bytes; PRFileDesc *local_small_file_fd=NULL; PRFileDesc *local_large_file_fd=NULL; sockfd = scp->sockfd; local_small_file_fd = PR_Open(SMALL_FILE_NAME, PR_RDONLY,0); if (local_small_file_fd == NULL) { fprintf(stderr,"prsocket_test failed to open file for transmitting %s\n", SMALL_FILE_NAME); failed_already=1; goto done; } local_large_file_fd = PR_Open(LARGE_FILE_NAME, PR_RDONLY,0); if (local_large_file_fd == NULL) { fprintf(stderr,"prsocket_test failed to open file for transmitting %s\n", LARGE_FILE_NAME); failed_already=1; goto done; } bytes = PR_TransmitFile(sockfd, local_small_file_fd, small_file_header, SMALL_FILE_HEADER_SIZE, PR_TRANSMITFILE_KEEP_OPEN, PR_INTERVAL_NO_TIMEOUT); if (bytes != (SMALL_FILE_SIZE+ SMALL_FILE_HEADER_SIZE)) { fprintf(stderr, "prsocet_test: PR_TransmitFile failed: (%ld, %ld)\n", PR_GetError(), PR_GetOSError()); failed_already=1; } bytes = PR_TransmitFile(sockfd, local_large_file_fd, NULL, 0, PR_TRANSMITFILE_CLOSE_SOCKET, PR_INTERVAL_NO_TIMEOUT); if (bytes != LARGE_FILE_SIZE) { fprintf(stderr, "prsocket_test: PR_TransmitFile failed: (%ld, %ld)\n", PR_GetError(), PR_GetOSError()); failed_already=1; } done: if (local_small_file_fd != NULL) PR_Close(local_small_file_fd); if (local_large_file_fd != NULL) PR_Close(local_large_file_fd); }
static PRFileDesc * InitializeRecording( void ) { char *logFileName; PRFileDesc *logFile; /* Self initialize, if necessary */ if ( traceLock == NULL ) _PR_InitializeTrace(); PR_LOG( lm, PR_LOG_DEBUG, ("PR_RecordTraceEntries: begins")); logLostData = 0; /* reset at entry */ logState = LogReset; /* Get the filename for the logfile from the environment */ logFileName = PR_GetEnvSecure( "NSPR_TRACE_LOG" ); if ( logFileName == NULL ) { PR_LOG( lm, PR_LOG_ERROR, ("RecordTraceEntries: Environment variable not defined. Exiting")); return NULL; } /* Open the logfile */ logFile = PR_Open( logFileName, PR_WRONLY | PR_CREATE_FILE, 0666 ); if ( logFile == NULL ) { PR_LOG( lm, PR_LOG_ERROR, ("RecordTraceEntries: Cannot open %s as trace log file. OS error: %ld", logFileName, PR_GetOSError())); return NULL; } return logFile; } /* end InitializeRecording() */
extern PRStatus _MD_DeleteSharedMemory( const char *name ) { PRStatus rc = PR_SUCCESS; PRUintn urc; char ipcname[PR_IPC_NAME_SIZE]; rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm ); if ( PR_FAILURE == rc ) { PR_SetError( PR_UNKNOWN_ERROR , errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_OpenSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name )); return rc; } urc = shm_unlink( ipcname ); if ( -1 == urc ) { rc = PR_FAILURE; _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_DeleteSharedMemory(): shm_unlink failed: %s, errno: %d", ipcname, PR_GetOSError())); } else { PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_DeleteSharedMemory(): %s, success", ipcname)); } return rc; } /* end _MD_DeleteSharedMemory() */
int main (int argc, char **argv) { static PRIntervalTime thread_start_time; static PRThread *housekeeping_tid = NULL; PLOptStatus os; PLOptState *opt = PL_CreateOptState(argc, argv, "d"); while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { if (PL_OPT_BAD == os) continue; switch (opt->option) { case 'd': /* debug mode */ _debug_on = 1; break; default: break; } } PL_DestroyOptState(opt); if (( housekeeping_tid = PR_CreateThread (PR_USER_THREAD, housecleaning, (void*)&thread_start_time, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0)) == NULL ) { fprintf(stderr, "simple_test: Error - PR_CreateThread failed: (%ld, %ld)\n", PR_GetError(), PR_GetOSError()); exit( 1 ); } PR_Cleanup(); return(0); }
NSFC_TransmitEntryFile(PRFileDesc *socket, NSFCEntry entry, const void *headers, PRInt32 hdrlen, const void *trailers, PRInt32 tlrlen, PRIntervalTime timeout, NSFCCache cache, NSFCStatusInfo *statusInfo) { NSFCEntryImpl *nep = entry; NSFCFileInfo finfo; PRInt64 rv; NSFCSTATUSINFO_INIT(statusInfo); NSFC_RecordEntryHit(cache, nep); rv = NSFC_MD_TransmitFile(socket, nep, headers, hdrlen, trailers, tlrlen, timeout, cache, statusInfo); #ifdef XP_WIN32 /* Work-around for NSPR error mapping deficiency */ if ((rv < 0) && (PR_GetError() == PR_UNKNOWN_ERROR) && (PR_GetOSError() == ERROR_NETNAME_DELETED)) { PR_SetError(PR_CONNECT_RESET_ERROR, ERROR_NETNAME_DELETED); } #endif /* XP_WIN32 */ return rv; }
NSFC_PR_GetFileInfo(const char *filename, PRFileInfo64 *finfo) { PRStatus rv; #ifdef ESTALE // Retry PR_GetFileInfo64() up to NSFC_ESTALE_RETRIES times if it returns // ESTALE. Bug 545152 int retries = NSFC_ESTALE_RETRIES; do { rv = PR_GetFileInfo64(filename, finfo); } while (rv == PR_FAILURE && PR_GetOSError() == ESTALE && retries--); #else rv = PR_GetFileInfo64(filename, finfo); #endif // Reject requests for "/index.html/" if (rv == PR_SUCCESS && finfo->type == PR_FILE_FILE) { int len = strlen(filename); if (len > 0) { #ifdef XP_WIN32 if (filename[len - 1] == '/' || filename[len - 1] == '\\') #else if (filename[len - 1] == '/') #endif { PR_SetError(PR_NOT_DIRECTORY_ERROR, 0); rv = PR_FAILURE; } } } return rv; }
static void Test(void) { int i; PRInt32 rv; PRIntervalTime timeout; timeout = PR_MillisecondsToInterval(10); /* cause syspoll_list to grow */ for (i = 1; i <= POLL_DESC_COUNT; i++) { rv = PR_Poll(pd, i, timeout); if (rv != 0) { fprintf(stderr, "PR_Poll should time out but returns %d (%d, %d)\n", rv, PR_GetError(), PR_GetOSError()); exit(1); } } /* syspoll_list should be large enough for all these */ for (i = POLL_DESC_COUNT; i >= 1; i--) { rv = PR_Poll(pd, i, timeout); if (rv != 0) { fprintf(stderr, "PR_Poll should time out but returns %d\n", rv); exit(1); } } }
NSFC_GetNonCacheFileInfo(const char *filename, NSFCFileInfo *finfo) { PRStatus rv; PRIntervalTime now; PR_ASSERT(filename != NULL); PR_ASSERT(finfo != NULL); now = ft_timeIntervalNow(); finfo->lastUpdate = now; finfo->fileid[0] = 0; finfo->fileid[1] = 0; finfo->prerr = 0; finfo->oserr = 0; rv = NSFC_PR_GetFileInfo(filename, &finfo->pr); if (rv == PR_FAILURE) { /* Save the details of the failure */ finfo->prerr = PR_GetError(); finfo->oserr = PR_GetOSError(); finfo->pr.type = PR_FILE_OTHER; finfo->pr.size = 0; finfo->pr.creationTime = 0; finfo->pr.modifyTime = 0; } return rv; }
extern void * _MD_AttachSharedMemory( PRSharedMemory *shm, PRIntn flags ) { void *addr; PRIntn prot = (PROT_READ | PROT_WRITE); PR_ASSERT( shm->ident == _PR_SHM_IDENT ); if ( PR_SHM_READONLY == flags) prot ^= PROT_WRITE; addr = mmap( (void*)0, shm->size, prot, MAP_SHARED, shm->id, 0 ); if ((void*)-1 == addr ) { _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_AttachSharedMemory(): mmap failed: %s, errno: %d", shm->ipcname, PR_GetOSError())); addr = NULL; } else { PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_AttachSharedMemory(): name: %s, attached at: %p", shm->ipcname, addr)); } return addr; }
PRBool NTStatsServer::init(void) { PRBool retval = PR_FALSE; //Sleep(15000); if ((retval = createStatsChannel()) != PR_TRUE) { PR_ASSERT(0); ereport(LOG_FAILURE, XP_GetAdminStr(DBT_NTStatsServer_ErrorChannelCreation)); return retval; } // We need to create a global bound thread as this thread // doesn't wait on PR APIs. It waits on MsgWaitForMultipleObjects // By default, it seems PR_CreateThread creates a fibre which // hangs the StatsPollThread. if (start(PR_SYSTEM_THREAD, PR_UNJOINABLE_THREAD, 0, PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD) == PR_FAILURE) { PRInt32 err = PR_GetError(); PRInt32 oserr = PR_GetOSError(); ereport(LOG_FAILURE, XP_GetAdminStr(DBT_StatsManager_FailedToCreateThread), system_errmsg()); } return PR_TRUE; }
PRFileDesc * ServerSetup(void) { PRFileDesc *listenSocket; PRNetAddr serverAddr; PRThread *WorkerThread; if ( (listenSocket = PR_NewTCPSocket()) == NULL) { if (debug_mode) printf("\tServer error creating listen socket\n"); else failed_already=1; return NULL; } memset(&serverAddr, 0, sizeof(PRNetAddr)); serverAddr.inet.family = AF_INET; serverAddr.inet.port = PR_htons(PORT); serverAddr.inet.ip = PR_htonl(INADDR_ANY); if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { if (debug_mode) printf("\tServer error binding to server address: OS error %d\n", PR_GetOSError()); else failed_already=1; PR_Close(listenSocket); return NULL; } if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { if (debug_mode) printf("\tServer error listening to server socket\n"); else failed_already=1; PR_Close(listenSocket); return NULL; } /* Create Clients */ workerThreads = 0; workerThreadsBusy = 0; workerThreadsLock = PR_NewLock(); WorkerThread = PR_CreateThread( PR_SYSTEM_THREAD, WorkerThreadFunc, listenSocket, PR_PRIORITY_NORMAL, ServerScope, PR_UNJOINABLE_THREAD, STACKSIZE); if (!WorkerThread) { if (debug_mode) printf("error creating working thread\n"); PR_Close(listenSocket); return NULL; } PR_AtomicIncrement(&workerThreads); if (debug_mode) DPRINTF("\tServer created primordial worker thread\n"); return listenSocket; }
static void errWarn(char * funcString) { PRErrorCode perr = PR_GetError(); PRInt32 oserr = PR_GetOSError(); const char * errString = SECU_Strerror(perr); fprintf(stderr, "strsclnt: %s returned error %d, OS error %d: %s\n", funcString, perr, oserr, errString); }
PRStatus FastFetchFile(PRFileDesc *in, PRFileDesc *out, PRUint32 size) { PRInt32 nBytes; PRFileMap *outfMap; void *addr; char *start; PRUint32 rem; PRUint32 bytesToRead; PRStatus rv; PRInt64 sz64; LL_UI2L(sz64, size); outfMap = PR_CreateFileMap(out, sz64, PR_PROT_READWRITE); PR_ASSERT(outfMap); addr = PR_MemMap(outfMap, LL_ZERO, size); if (addr == (void *) -1) { fprintf(stderr, "cannot memory-map file: (%d, %d)\n", PR_GetError(), PR_GetOSError()); PR_CloseFileMap(outfMap); return PR_FAILURE; } PR_ASSERT(addr != (void *) -1); start = (char *) addr; rem = size; while ((nBytes = DrainInputBuffer(start, rem)) > 0) { start += nBytes; rem -= nBytes; } if (nBytes < 0) { /* Input buffer is empty and end of stream */ return PR_SUCCESS; } bytesToRead = (rem < FCOPY_BUFFER_SIZE) ? rem : FCOPY_BUFFER_SIZE; while (rem > 0 && (nBytes = PR_Read(in, start, bytesToRead)) > 0) { start += nBytes; rem -= nBytes; bytesToRead = (rem < FCOPY_BUFFER_SIZE) ? rem : FCOPY_BUFFER_SIZE; } if (nBytes < 0) { fprintf(stderr, "httpget: cannot read from socket\n"); return PR_FAILURE; } rv = PR_MemUnmap(addr, size); PR_ASSERT(rv == PR_SUCCESS); rv = PR_CloseFileMap(outfMap); PR_ASSERT(rv == PR_SUCCESS); return PR_SUCCESS; }
static void _NSFC_PR_NT_CancelIo(PRFileDesc *fd) { #ifdef XP_WIN32 PRErrorCode prerr = PR_GetError(); PRInt32 oserr = PR_GetOSError(); /* Attempt to load PR_NT_CancelIo() from the DLL that contains PR_Send() */ static PRStatus (*pfnPR_NT_CancelIo)(PRFileDesc *fd) = NULL; if (pfnPR_NT_CancelIo == NULL) { MEMORY_BASIC_INFORMATION mbi; VirtualQuery(&PR_Send, &mbi, sizeof(mbi)); pfnPR_NT_CancelIo = (PRStatus (*)(PRFileDesc *fd))GetProcAddress((HINSTANCE)mbi.AllocationBase, "PR_NT_CancelIo"); } /* If we couldn't find PR_NT_CancelIo(), just use the dummy */ if (pfnPR_NT_CancelIo == NULL) pfnPR_NT_CancelIo = &_NSFC_Dummy_NT_CancelIo; /* VB: _NSFC_PR_NT_CancelIo - calls PR_NT_CancelIo when an I/O timed out * or was interrupted. */ if (prerr == PR_IO_TIMEOUT_ERROR || prerr == PR_PENDING_INTERRUPT_ERROR) { // Need to cancel the i/o if (pfnPR_NT_CancelIo(fd) != PR_SUCCESS) { // VB: This should not happen. Assert when this happens // Get the error codes to make debugging a bit easier PRErrorCode cancelErr = PR_GetError(); PRInt32 cancelOSErr = PR_GetOSError(); PR_ASSERT(0); } } PR_SetError(prerr, oserr); #endif }
PR_IMPLEMENT(void) PL_FPrintError(PRFileDesc *fd, const char *msg) { PRErrorCode error = PR_GetError(); PRInt32 oserror = PR_GetOSError(); const char *name = PR_ErrorToName(error); if (NULL != msg) PR_fprintf(fd, "%s: ", msg); if (NULL == name) PR_fprintf( fd, " (%d)OUT OF RANGE, oserror = %d\n", error, oserror); else PR_fprintf( fd, "%s(%d), oserror = %d\n", name, error, oserror); } /* PL_FPrintError */
extern PRStatus _MD_CloseSharedMemory( PRSharedMemory *shm ) { int urc; PR_ASSERT( shm->ident == _PR_SHM_IDENT ); urc = close( shm->id ); if ( -1 == urc ) { _PR_MD_MAP_CLOSE_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_CloseSharedMemory(): close() failed, error: %d", PR_GetOSError())); return(PR_FAILURE); } PR_DELETE( shm->ipcname ); PR_DELETE( shm ); return PR_SUCCESS; }
extern PRStatus _MD_DetachSharedMemory( PRSharedMemory *shm, void *addr ) { PRStatus rc = PR_SUCCESS; PRIntn urc; PR_ASSERT( shm->ident == _PR_SHM_IDENT ); urc = munmap( addr, shm->size ); if ( -1 == urc ) { rc = PR_FAILURE; _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_DetachSharedMemory(): munmap failed: %s, errno: %d", shm->ipcname, PR_GetOSError())); } return rc; }
/* static */ void WebCL_LibCL::unload (WebCL_LibCL* aInstance) { D_METHOD_START; if (!(aInstance && aInstance->m_libHandle)) return; if (PR_UnloadLibrary (aInstance->m_libHandle) == PR_FAILURE) { char *errText = getPRErrorText (); D_LOG (LOG_LEVEL_WARNING, "Failed to unload library %s: %s", aInstance->m_libName, errText); D_LOG (LOG_LEVEL_DEBUG, " (prerr: %d oserr: %d)", PR_GetError(), PR_GetOSError()); PR_FREEIF (errText); } else { D_LOG (LOG_LEVEL_DEBUG, "Unloaded library %s", aInstance->m_libName); } }
extern void * _MD_AttachSharedMemory( PRSharedMemory *shm, PRIntn flags ) { void *addr; PRUint32 aFlags = shm->mode; PR_ASSERT( shm->ident == _PR_SHM_IDENT ); aFlags |= (flags & PR_SHM_READONLY )? SHM_RDONLY : 0; addr = shmat( shm->id, NULL, aFlags ); if ( (void*)-1 == addr ) { _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_AttachSharedMemory(): shmat() failed on name: %s, OsError: %d", shm->ipcname, PR_GetOSError() )); addr = NULL; } return addr; }
static PRInt32 PR_CALLBACK SocketRecv(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout) { PRInt32 rv; PRThread *me = _PR_MD_CURRENT_THREAD(); if (_PR_PENDING_INTERRUPT(me)) { me->flags &= ~_PR_INTERRUPT; PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); return -1; } if (_PR_IO_PENDING(me)) { PR_SetError(PR_IO_PENDING_ERROR, 0); return -1; } PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv: fd=%p osfd=%d buf=%p amount=%d", fd, fd->secret->md.osfd, buf, amount)); rv = _PR_MD_RECV(fd, buf, amount, flags, timeout); PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv -> %d, error = %d, os error = %d", rv, PR_GetError(), PR_GetOSError())); return rv; }
static NSFCStatus _NSFC_RejuvenateEntry(NSFCCache cache, NSFCEntryImpl *nep) { PRFileInfo64 finfo; PRIntervalTime now; NSFCStatus rfc = NSFC_DELETED; if (nep->flags & NSFCENTRY_HASINFO) { now = ft_timeIntervalNow(); if (NSFC_PR_GetFileInfo(nep->filename, &finfo) == PR_FAILURE) { if ((nep->flags & NSFCENTRY_ERRORINFO) && (PR_GetError() == nep->finfo.prerr) && (PR_GetOSError() == nep->finfo.oserr)) { nep->finfo.lastUpdate = now; rfc = NSFC_OK; } } else { if ((finfo.size == nep->finfo.pr.size) && (finfo.creationTime == nep->finfo.pr.creationTime) && (finfo.modifyTime == nep->finfo.pr.modifyTime)) { nep->finfo.lastUpdate = now; rfc = NSFC_OK; } } } if (rfc != NSFC_OK) { /* Couldn't rejuvenate, so mark this outdated entry for deletion */ _NSFC_DeleteEntry(cache, nep, PR_FALSE, PR_TRUE, PR_TRUE); } return rfc; }
extern PRSharedMemory * _MD_OpenSharedMemory( const char *name, PRSize size, PRIntn flags, PRIntn mode ) { PRStatus rc = PR_SUCCESS; PRInt32 end; PRSharedMemory *shm; char ipcname[PR_IPC_NAME_SIZE]; rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm ); if ( PR_FAILURE == rc ) { PR_SetError( PR_UNKNOWN_ERROR , errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_OpenSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name )); return( NULL ); } shm = PR_NEWZAP( PRSharedMemory ); if ( NULL == shm ) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 ); PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New PRSharedMemory out of memory")); return( NULL ); } shm->ipcname = PR_MALLOC( strlen( ipcname ) + 1 ); if ( NULL == shm->ipcname ) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 ); PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New shm->ipcname out of memory")); return( NULL ); } /* copy args to struct */ strcpy( shm->ipcname, ipcname ); shm->size = size; shm->mode = mode; shm->flags = flags; shm->ident = _PR_SHM_IDENT; /* ** Create the shared memory */ if ( flags & PR_SHM_CREATE ) { int oflag = (O_CREAT | O_RDWR); if ( flags & PR_SHM_EXCL ) oflag |= O_EXCL; shm->id = shm_open( shm->ipcname, oflag, shm->mode ); } else { shm->id = shm_open( shm->ipcname, O_RDWR, shm->mode ); } if ( -1 == shm->id ) { _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ("_MD_OpenSharedMemory(): shm_open failed: %s, OSError: %d", shm->ipcname, PR_GetOSError())); PR_DELETE( shm->ipcname ); PR_DELETE( shm ); return(NULL); } end = ftruncate( shm->id, shm->size ); if ( -1 == end ) { _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ("_MD_OpenSharedMemory(): ftruncate failed, OSError: %d", PR_GetOSError())); PR_DELETE( shm->ipcname ); PR_DELETE( shm ); return(NULL); } return(shm); } /* end _MD_OpenSharedMemory() */
PR_IMPLEMENT(void) PL_FPrintError(PRFileDesc *fd, const char *msg) { static const char *tags[] = { "PR_OUT_OF_MEMORY_ERROR", "PR_BAD_DESCRIPTOR_ERROR", "PR_WOULD_BLOCK_ERROR", "PR_ACCESS_FAULT_ERROR", "PR_INVALID_METHOD_ERROR", "PR_ILLEGAL_ACCESS_ERROR", "PR_UNKNOWN_ERROR", "PR_PENDING_INTERRUPT_ERROR", "PR_NOT_IMPLEMENTED_ERROR", "PR_IO_ERROR", "PR_IO_TIMEOUT_ERROR", "PR_IO_PENDING_ERROR", "PR_DIRECTORY_OPEN_ERROR", "PR_INVALID_ARGUMENT_ERROR", "PR_ADDRESS_NOT_AVAILABLE_ERROR", "PR_ADDRESS_NOT_SUPPORTED_ERROR", "PR_IS_CONNECTED_ERROR", "PR_BAD_ADDRESS_ERROR", "PR_ADDRESS_IN_USE_ERROR", "PR_CONNECT_REFUSED_ERROR", "PR_NETWORK_UNREACHABLE_ERROR", "PR_CONNECT_TIMEOUT_ERROR", "PR_NOT_CONNECTED_ERROR", "PR_LOAD_LIBRARY_ERROR", "PR_UNLOAD_LIBRARY_ERROR", "PR_FIND_SYMBOL_ERROR", "PR_INSUFFICIENT_RESOURCES_ERROR", "PR_DIRECTORY_LOOKUP_ERROR", "PR_TPD_RANGE_ERROR", "PR_PROC_DESC_TABLE_FULL_ERROR", "PR_SYS_DESC_TABLE_FULL_ERROR", "PR_NOT_SOCKET_ERROR", "PR_NOT_TCP_SOCKET_ERROR", "PR_SOCKET_ADDRESS_IS_BOUND_ERROR", "PR_NO_ACCESS_RIGHTS_ERROR", "PR_OPERATION_NOT_SUPPORTED_ERROR", "PR_PROTOCOL_NOT_SUPPORTED_ERROR", "PR_REMOTE_FILE_ERROR", "PR_BUFFER_OVERFLOW_ERROR", "PR_CONNECT_RESET_ERROR", "PR_RANGE_ERROR", "PR_DEADLOCK_ERROR", "PR_FILE_IS_LOCKED_ERROR", "PR_FILE_TOO_BIG_ERROR", "PR_NO_DEVICE_SPACE_ERROR", "PR_PIPE_ERROR", "PR_NO_SEEK_DEVICE_ERROR", "PR_IS_DIRECTORY_ERROR", "PR_LOOP_ERROR", "PR_NAME_TOO_LONG_ERROR", "PR_FILE_NOT_FOUND_ERROR", "PR_NOT_DIRECTORY_ERROR", "PR_READ_ONLY_FILESYSTEM_ERROR", "PR_DIRECTORY_NOT_EMPTY_ERROR", "PR_FILESYSTEM_MOUNTED_ERROR", "PR_NOT_SAME_DEVICE_ERROR", "PR_DIRECTORY_CORRUPTED_ERROR", "PR_FILE_EXISTS_ERROR", "PR_MAX_DIRECTORY_ENTRIES_ERROR", "PR_INVALID_DEVICE_STATE_ERROR", "PR_DEVICE_IS_LOCKED_ERROR", "PR_NO_MORE_FILES_ERROR", "PR_END_OF_FILE_ERROR", "PR_FILE_SEEK_ERROR", "PR_FILE_IS_BUSY_ERROR", "PR_IN_PROGRESS_ERROR", "PR_ALREADY_INITIATED_ERROR", "PR_GROUP_EMPTY_ERROR", "PR_INVALID_STATE_ERROR", "PR_MAX_ERROR" }; PRErrorCode error = PR_GetError(); PRInt32 oserror = PR_GetOSError(); PRIntn thoseIKnowAbout = sizeof(tags) / sizeof(char*); PRIntn lastError = PR_NSPR_ERROR_BASE + thoseIKnowAbout; if (NULL != msg) PR_fprintf(fd, "%s: ", msg); if ((error < PR_NSPR_ERROR_BASE) || (error > lastError)) PR_fprintf( fd, " (%d)OUT OF RANGE, oserror = %d\n", error, oserror); else PR_fprintf( fd, "%s(%d), oserror = %d\n", tags[error - PR_NSPR_ERROR_BASE], error, oserror); } /* PL_FPrintError */
static PRInt32 PR_CALLBACK SocketRecv(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout) { PRInt32 rv; PRThread *me = _PR_MD_CURRENT_THREAD(); if ((flags != 0) && (flags != PR_MSG_PEEK)) { PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); return -1; } if (_PR_PENDING_INTERRUPT(me)) { me->flags &= ~_PR_INTERRUPT; PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); return -1; } if (_PR_IO_PENDING(me)) { PR_SetError(PR_IO_PENDING_ERROR, 0); return -1; } PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv: fd=%p osfd=%" PR_PRIdOSFD " buf=%p amount=%d flags=%d", fd, fd->secret->md.osfd, buf, amount, flags)); #ifdef _PR_HAVE_PEEK_BUFFER if (fd->secret->peekBytes != 0) { rv = (amount < fd->secret->peekBytes) ? amount : fd->secret->peekBytes; memcpy(buf, fd->secret->peekBuffer, rv); if (flags == 0) { /* consume the bytes in the peek buffer */ fd->secret->peekBytes -= rv; if (fd->secret->peekBytes != 0) { memmove(fd->secret->peekBuffer, fd->secret->peekBuffer + rv, fd->secret->peekBytes); } } return rv; } /* allocate peek buffer, if necessary */ if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) { PR_ASSERT(0 == fd->secret->peekBytes); /* impose a max size on the peek buffer */ if (amount > _PR_PEEK_BUFFER_MAX) { amount = _PR_PEEK_BUFFER_MAX; } if (fd->secret->peekBufSize < amount) { if (fd->secret->peekBuffer) { PR_Free(fd->secret->peekBuffer); } fd->secret->peekBufSize = amount; fd->secret->peekBuffer = PR_Malloc(amount); if (NULL == fd->secret->peekBuffer) { fd->secret->peekBufSize = 0; PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return -1; } } } #endif rv = _PR_MD_RECV(fd, buf, amount, flags, timeout); PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv -> %d, error = %d, os error = %d", rv, PR_GetError(), PR_GetOSError())); #ifdef _PR_HAVE_PEEK_BUFFER if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) { if (rv > 0) { memcpy(fd->secret->peekBuffer, buf, rv); fd->secret->peekBytes = rv; } } #endif return rv; }
static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv ) { PRFileDesc *listenSock, *sock; PRUint16 listenPort; PRNetAddr addr; char buf[CHUNK_SIZE]; PRThread *clientThread; PRInt32 retVal; PRIntn optval = 1; PRIntn i; PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME); #ifdef XP_MAC SetupMacPrintfLog("nonblock.log"); #endif /* Create a listening socket */ if ((listenSock = PR_NewTCPSocket()) == NULL) { fprintf(stderr, "Can't create a new TCP socket\n"); exit(1); } addr.inet.family = AF_INET; addr.inet.ip = PR_htonl(INADDR_ANY); addr.inet.port = PR_htons(0); if (PR_Bind(listenSock, &addr) == PR_FAILURE) { fprintf(stderr, "Can't bind socket\n"); exit(1); } if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) { fprintf(stderr, "PR_GetSockName failed\n"); exit(1); } listenPort = PR_ntohs(addr.inet.port); if (PR_Listen(listenSock, 5) == PR_FAILURE) { fprintf(stderr, "Can't listen on a socket\n"); exit(1); } PR_snprintf(buf, sizeof(buf), "The server thread is listening on port %hu\n\n", listenPort); printf("%s", buf); clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc, (void *) listenPort, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (clientThread == NULL) { fprintf(stderr, "can't create thread\n"); exit(1); } printf("client thread created.\n"); PR_SetSockOpt(listenSock, PR_SockOpt_Nonblocking, &optval, sizeof(PRIntn)); /* time 0 */ sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); if (sock != NULL || PR_GetError() != PR_WOULD_BLOCK_ERROR) { PL_PrintError("First Accept\n"); fprintf(stderr, "First PR_Accept() xxx\n" ); exit(1); } printf("accept: EWOULDBLOCK, good\n"); fflush(stdout); /* time 2 */ PR_Sleep(2 * unitTime); sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); if (sock == NULL) { PL_PrintError("Second Accept\n"); fprintf(stderr, "Second PR_Accept() failed: (%d, %d)\n", PR_GetError(), PR_GetOSError()); exit(1); } printf("accept: succeeded, good\n"); fflush(stdout); PR_Close(listenSock); PR_SetSockOpt(sock, PR_SockOpt_Nonblocking, &optval, sizeof(PRIntn)); /* time 3, 5, 6, 8, etc. */ for (i = 0; i < NUMBER_ROUNDS; i++) { PR_Sleep(unitTime); retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT); if (retVal != -1 || PR_GetError() != PR_WOULD_BLOCK_ERROR) { PL_PrintError("First Receive:\n"); fprintf(stderr, "First PR_Recv: retVal: %ld, Error: %ld\n", retVal, PR_GetError()); exit(1); } printf("read: EWOULDBLOCK, good\n"); fflush(stdout); PR_Sleep(2 * unitTime); retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT); if (retVal != CHUNK_SIZE) { PL_PrintError("Second Receive:\n"); fprintf(stderr, "Second PR_Recv: retVal: %ld, Error: %ld\n", retVal, PR_GetError()); exit(1); } printf("read: %d bytes, good\n", retVal); fflush(stdout); } PR_Close(sock); printf("All tests finished\n"); printf("PASS\n"); return 0; }
int prldap_get_system_errno( void ) { return( PR_GetOSError()); }
int rmain ( int argc, char *argv[] ) { char *argv0 = argv[0]; PRLibrary *lib; CK_C_GetFunctionList gfl; CK_FUNCTION_LIST_PTR epv = (CK_FUNCTION_LIST_PTR)NULL; CK_RV ck_rv; CK_INFO info; CK_ULONG nSlots; CK_SLOT_ID *pSlots; CK_ULONG i; CK_C_INITIALIZE_ARGS ia, *iap; (void)memset(&ia, 0, sizeof(CK_C_INITIALIZE_ARGS)); iap = (CK_C_INITIALIZE_ARGS *)NULL; while( argv++, --argc ) { if( '-' == argv[0][0] ) { switch( argv[0][1] ) { case 'i': iap = &ia; if( ((char *)NULL != argv[1]) && ('-' != argv[1][0]) ) { #ifdef WITH_NSS ia.pConfig = argv[1]; ia.ulConfigLen = strlen(argv[1]); argv++, --argc; #else return usage(argv0); #endif /* WITH_NSS */ } break; case '-': argv++, --argc; goto endargs; default: return usage(argv0); } } else { break; } } endargs:; if( 1 != argc ) { return usage(argv0); } lib = PR_LoadLibrary(argv[0]); if( (PRLibrary *)NULL == lib ) { PR_fprintf(PR_STDERR, "Can't load %s: %ld, %ld\n", argv[1], PR_GetError(), PR_GetOSError()); return 1; } gfl = (CK_C_GetFunctionList)PR_FindSymbol(lib, "C_GetFunctionList"); if( (CK_C_GetFunctionList)NULL == gfl ) { PR_fprintf(PR_STDERR, "Can't find C_GetFunctionList in %s: %ld, %ld\n", argv[1], PR_GetError(), PR_GetOSError()); return 1; } ck_rv = (*gfl)(&epv); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "CK_GetFunctionList returned 0x%08x\n", ck_rv); return 1; } PR_fprintf(PR_STDOUT, "Module %s loaded, epv = 0x%08x.\n\n", argv[1], (CK_ULONG)epv); /* C_Initialize */ ck_rv = epv->C_Initialize(iap); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_Initialize returned 0x%08x\n", ck_rv); return 1; } /* C_GetInfo */ (void)memset(&info, 0, sizeof(CK_INFO)); ck_rv = epv->C_GetInfo(&info); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_GetInfo returned 0x%08x\n", ck_rv); return 1; } PR_fprintf(PR_STDOUT, "Module Info:\n"); PR_fprintf(PR_STDOUT, " cryptokiVersion = %lu.%02lu\n", (PRUint32)info.cryptokiVersion.major, (PRUint32)info.cryptokiVersion.minor); PR_fprintf(PR_STDOUT, " manufacturerID = \"%.32s\"\n", info.manufacturerID); PR_fprintf(PR_STDOUT, " flags = 0x%08lx\n", info.flags); PR_fprintf(PR_STDOUT, " libraryDescription = \"%.32s\"\n", info.libraryDescription); PR_fprintf(PR_STDOUT, " libraryVersion = %lu.%02lu\n", (PRUint32)info.libraryVersion.major, (PRUint32)info.libraryVersion.minor); PR_fprintf(PR_STDOUT, "\n"); /* C_GetSlotList */ nSlots = 0; ck_rv = epv->C_GetSlotList(CK_FALSE, (CK_SLOT_ID_PTR)CK_NULL_PTR, &nSlots); switch( ck_rv ) { case CKR_BUFFER_TOO_SMALL: case CKR_OK: break; default: PR_fprintf(PR_STDERR, "C_GetSlotList(FALSE, NULL, ) returned 0x%08x\n", ck_rv); return 1; } PR_fprintf(PR_STDOUT, "There are %lu slots.\n", nSlots); pSlots = (CK_SLOT_ID_PTR)PR_Calloc(nSlots, sizeof(CK_SLOT_ID)); if( (CK_SLOT_ID_PTR)NULL == pSlots ) { PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", nSlots * sizeof(CK_SLOT_ID)); return 1; } ck_rv = epv->C_GetSlotList(CK_FALSE, pSlots, &nSlots); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_GetSlotList(FALSE, , ) returned 0x%08x\n", ck_rv); return 1; } for( i = 0; i < nSlots; i++ ) { PR_fprintf(PR_STDOUT, " [%lu]: CK_SLOT_ID = %lu\n", (i+1), pSlots[i]); } PR_fprintf(PR_STDOUT, "\n"); /* C_GetSlotInfo */ for( i = 0; i < nSlots; i++ ) { CK_SLOT_INFO sinfo; PR_fprintf(PR_STDOUT, "[%lu]: CK_SLOT_ID = %lu\n", (i+1), pSlots[i]); (void)memset(&sinfo, 0, sizeof(CK_SLOT_INFO)); ck_rv = epv->C_GetSlotInfo(pSlots[i], &sinfo); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_GetSlotInfo(%lu, ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Slot Info:\n"); PR_fprintf(PR_STDOUT, " slotDescription = \"%.64s\"\n", sinfo.slotDescription); PR_fprintf(PR_STDOUT, " manufacturerID = \"%.32s\"\n", sinfo.manufacturerID); PR_fprintf(PR_STDOUT, " flags = 0x%08lx\n", sinfo.flags); PR_fprintf(PR_STDOUT, " -> TOKEN PRESENT = %s\n", sinfo.flags & CKF_TOKEN_PRESENT ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> REMOVABLE DEVICE = %s\n", sinfo.flags & CKF_REMOVABLE_DEVICE ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> HW SLOT = %s\n", sinfo.flags & CKF_HW_SLOT ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " hardwareVersion = %lu.%02lu\n", (PRUint32)sinfo.hardwareVersion.major, (PRUint32)sinfo.hardwareVersion.minor); PR_fprintf(PR_STDOUT, " firmwareVersion = %lu.%02lu\n", (PRUint32)sinfo.firmwareVersion.major, (PRUint32)sinfo.firmwareVersion.minor); if( sinfo.flags & CKF_TOKEN_PRESENT ) { CK_TOKEN_INFO tinfo; CK_MECHANISM_TYPE *pMechanismList; CK_ULONG nMechanisms = 0; CK_ULONG j; (void)memset(&tinfo, 0, sizeof(CK_TOKEN_INFO)); ck_rv = epv->C_GetTokenInfo(pSlots[i], &tinfo); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_GetTokenInfo(%lu, ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Token Info:\n"); PR_fprintf(PR_STDOUT, " label = \"%.32s\"\n", tinfo.label); PR_fprintf(PR_STDOUT, " manufacturerID = \"%.32s\"\n", tinfo.manufacturerID); PR_fprintf(PR_STDOUT, " model = \"%.16s\"\n", tinfo.model); PR_fprintf(PR_STDOUT, " serialNumber = \"%.16s\"\n", tinfo.serialNumber); PR_fprintf(PR_STDOUT, " flags = 0x%08lx\n", tinfo.flags); PR_fprintf(PR_STDOUT, " -> RNG = %s\n", tinfo.flags & CKF_RNG ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> WRITE PROTECTED = %s\n", tinfo.flags & CKF_WRITE_PROTECTED ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> LOGIN REQUIRED = %s\n", tinfo.flags & CKF_LOGIN_REQUIRED ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> USER PIN INITIALIZED = %s\n", tinfo.flags & CKF_USER_PIN_INITIALIZED ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> RESTORE KEY NOT NEEDED = %s\n", tinfo.flags & CKF_RESTORE_KEY_NOT_NEEDED ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> CLOCK ON TOKEN = %s\n", tinfo.flags & CKF_CLOCK_ON_TOKEN ? "TRUE" : "FALSE"); #ifdef CKF_SUPPORTS_PARALLEL PR_fprintf(PR_STDOUT, " -> SUPPORTS PARALLEL = %s\n", tinfo.flags & CKF_SUPPORTS_PARALLEL ? "TRUE" : "FALSE"); #endif /* CKF_SUPPORTS_PARALLEL */ PR_fprintf(PR_STDOUT, " -> PROTECTED AUTHENTICATION PATH = %s\n", tinfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> DUAL_CRYPTO_OPERATIONS = %s\n", tinfo.flags & CKF_DUAL_CRYPTO_OPERATIONS ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " ulMaxSessionCount = %lu\n", tinfo.ulMaxSessionCount); PR_fprintf(PR_STDOUT, " ulSessionCount = %lu\n", tinfo.ulSessionCount); PR_fprintf(PR_STDOUT, " ulMaxRwSessionCount = %lu\n", tinfo.ulMaxRwSessionCount); PR_fprintf(PR_STDOUT, " ulRwSessionCount = %lu\n", tinfo.ulRwSessionCount); PR_fprintf(PR_STDOUT, " ulMaxPinLen = %lu\n", tinfo.ulMaxPinLen); PR_fprintf(PR_STDOUT, " ulMinPinLen = %lu\n", tinfo.ulMinPinLen); PR_fprintf(PR_STDOUT, " ulTotalPublicMemory = %lu\n", tinfo.ulTotalPublicMemory); PR_fprintf(PR_STDOUT, " ulFreePublicMemory = %lu\n", tinfo.ulFreePublicMemory); PR_fprintf(PR_STDOUT, " ulTotalPrivateMemory = %lu\n", tinfo.ulTotalPrivateMemory); PR_fprintf(PR_STDOUT, " ulFreePrivateMemory = %lu\n", tinfo.ulFreePrivateMemory); PR_fprintf(PR_STDOUT, " hardwareVersion = %lu.%02lu\n", (PRUint32)tinfo.hardwareVersion.major, (PRUint32)tinfo.hardwareVersion.minor); PR_fprintf(PR_STDOUT, " firmwareVersion = %lu.%02lu\n", (PRUint32)tinfo.firmwareVersion.major, (PRUint32)tinfo.firmwareVersion.minor); PR_fprintf(PR_STDOUT, " utcTime = \"%.16s\"\n", tinfo.utcTime); ck_rv = epv->C_GetMechanismList(pSlots[i], (CK_MECHANISM_TYPE_PTR)CK_NULL_PTR, &nMechanisms); switch( ck_rv ) { case CKR_BUFFER_TOO_SMALL: case CKR_OK: break; default: PR_fprintf(PR_STDERR, "C_GetMechanismList(%lu, NULL, ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " %lu mechanisms:\n", nMechanisms); pMechanismList = (CK_MECHANISM_TYPE_PTR)PR_Calloc(nMechanisms, sizeof(CK_MECHANISM_TYPE)); if( (CK_MECHANISM_TYPE_PTR)NULL == pMechanismList ) { PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", nMechanisms * sizeof(CK_MECHANISM_TYPE)); return 1; } ck_rv = epv->C_GetMechanismList(pSlots[i], pMechanismList, &nMechanisms); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_GetMechanismList(%lu, , ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } for( j = 0; j < nMechanisms; j++ ) { PR_fprintf(PR_STDOUT, " {%lu}: CK_MECHANISM_TYPE = %lu\n", (j+1), pMechanismList[j]); } PR_fprintf(PR_STDOUT, "\n"); for( j = 0; j < nMechanisms; j++ ) { CK_MECHANISM_INFO minfo; (void)memset(&minfo, 0, sizeof(CK_MECHANISM_INFO)); ck_rv = epv->C_GetMechanismInfo(pSlots[i], pMechanismList[j], &minfo); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_GetMechanismInfo(%lu, %lu, ) returned 0x%08x\n", pSlots[i], pMechanismList[j]); return 1; } PR_fprintf(PR_STDOUT, " [%lu]: CK_MECHANISM_TYPE = %lu\n", (j+1), pMechanismList[j]); PR_fprintf(PR_STDOUT, " ulMinKeySize = %lu\n", minfo.ulMinKeySize); PR_fprintf(PR_STDOUT, " ulMaxKeySize = %lu\n", minfo.ulMaxKeySize); PR_fprintf(PR_STDOUT, " flags = 0x%08x\n", minfo.flags); PR_fprintf(PR_STDOUT, " -> HW = %s\n", minfo.flags & CKF_HW ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> ENCRYPT = %s\n", minfo.flags & CKF_ENCRYPT ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> DECRYPT = %s\n", minfo.flags & CKF_DECRYPT ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> DIGEST = %s\n", minfo.flags & CKF_DIGEST ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> SIGN = %s\n", minfo.flags & CKF_SIGN ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> SIGN_RECOVER = %s\n", minfo.flags & CKF_SIGN_RECOVER ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> VERIFY = %s\n", minfo.flags & CKF_VERIFY ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> VERIFY_RECOVER = %s\n", minfo.flags & CKF_VERIFY_RECOVER ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> GENERATE = %s\n", minfo.flags & CKF_GENERATE ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> GENERATE_KEY_PAIR = %s\n", minfo.flags & CKF_GENERATE_KEY_PAIR ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> WRAP = %s\n", minfo.flags & CKF_WRAP ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> UNWRAP = %s\n", minfo.flags & CKF_UNWRAP ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> DERIVE = %s\n", minfo.flags & CKF_DERIVE ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> EXTENSION = %s\n", minfo.flags & CKF_EXTENSION ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, "\n"); } if( tinfo.flags & CKF_LOGIN_REQUIRED ) { PR_fprintf(PR_STDERR, "*** LOGIN REQUIRED but not yet implemented ***\n"); /* all the stuff about logging in as SO and setting the user pin if needed, etc. */ return 2; } /* session to find objects */ { CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0; CK_SESSION_INFO sinfo; CK_ATTRIBUTE_PTR pTemplate; CK_ULONG tnObjects = 0; ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Opened a session: handle = 0x%08x\n", h); (void)memset(&sinfo, 0, sizeof(CK_SESSION_INFO)); ck_rv = epv->C_GetSessionInfo(h, &sinfo); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDOUT, "C_GetSessionInfo(%lu, ) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " SESSION INFO:\n"); PR_fprintf(PR_STDOUT, " slotID = %lu\n", sinfo.slotID); PR_fprintf(PR_STDOUT, " state = %lu\n", sinfo.state); PR_fprintf(PR_STDOUT, " flags = 0x%08x\n", sinfo.flags); #ifdef CKF_EXCLUSIVE_SESSION PR_fprintf(PR_STDOUT, " -> EXCLUSIVE SESSION = %s\n", sinfo.flags & CKF_EXCLUSIVE_SESSION ? "TRUE" : "FALSE"); #endif /* CKF_EXCLUSIVE_SESSION */ PR_fprintf(PR_STDOUT, " -> RW SESSION = %s\n", sinfo.flags & CKF_RW_SESSION ? "TRUE" : "FALSE"); PR_fprintf(PR_STDOUT, " -> SERIAL SESSION = %s\n", sinfo.flags & CKF_SERIAL_SESSION ? "TRUE" : "FALSE"); #ifdef CKF_INSERTION_CALLBACK PR_fprintf(PR_STDOUT, " -> INSERTION CALLBACK = %s\n", sinfo.flags & CKF_INSERTION_CALLBACK ? "TRUE" : "FALSE"); #endif /* CKF_INSERTION_CALLBACK */ PR_fprintf(PR_STDOUT, " ulDeviceError = %lu\n", sinfo.ulDeviceError); PR_fprintf(PR_STDOUT, "\n"); ck_rv = epv->C_FindObjectsInit(h, (CK_ATTRIBUTE_PTR)CK_NULL_PTR, 0); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDOUT, "C_FindObjectsInit(%lu, NULL_PTR, 0) returned 0x%08x\n", h, ck_rv); return 1; } pTemplate = (CK_ATTRIBUTE_PTR)PR_Calloc(number_of_all_known_attribute_types, sizeof(CK_ATTRIBUTE)); if( (CK_ATTRIBUTE_PTR)NULL == pTemplate ) { PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", number_of_all_known_attribute_types * sizeof(CK_ATTRIBUTE)); return 1; } PR_fprintf(PR_STDOUT, " All objects:\n"); while(1) { CK_OBJECT_HANDLE o = (CK_OBJECT_HANDLE)0; CK_ULONG nObjects = 0; CK_ULONG k; CK_ULONG nAttributes = 0; CK_ATTRIBUTE_PTR pT2; CK_ULONG l; ck_rv = epv->C_FindObjects(h, &o, 1, &nObjects); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjects(%lu, , 1, ) returned 0x%08x\n", h, ck_rv); return 1; } if( 0 == nObjects ) { PR_fprintf(PR_STDOUT, "\n"); break; } tnObjects++; PR_fprintf(PR_STDOUT, " OBJECT HANDLE %lu:\n", o); for( k = 0; k < number_of_all_known_attribute_types; k++ ) { pTemplate[k].type = all_known_attribute_types[k]; pTemplate[k].pValue = (CK_VOID_PTR)CK_NULL_PTR; pTemplate[k].ulValueLen = 0; } ck_rv = epv->C_GetAttributeValue(h, o, pTemplate, number_of_all_known_attribute_types); switch( ck_rv ) { case CKR_OK: case CKR_ATTRIBUTE_SENSITIVE: case CKR_ATTRIBUTE_TYPE_INVALID: case CKR_BUFFER_TOO_SMALL: break; default: PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, {all attribute types}, %lu) returned 0x%08x\n", h, o, number_of_all_known_attribute_types, ck_rv); return 1; } for( k = 0; k < number_of_all_known_attribute_types; k++ ) { if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) { nAttributes++; } } if( 1 ) { PR_fprintf(PR_STDOUT, " %lu attributes:\n", nAttributes); for( k = 0; k < number_of_all_known_attribute_types; k++ ) { if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) { PR_fprintf(PR_STDOUT, " 0x%08x (len = %lu)\n", pTemplate[k].type, pTemplate[k].ulValueLen); } } PR_fprintf(PR_STDOUT, "\n"); } pT2 = (CK_ATTRIBUTE_PTR)PR_Calloc(nAttributes, sizeof(CK_ATTRIBUTE)); if( (CK_ATTRIBUTE_PTR)NULL == pT2 ) { PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", nAttributes * sizeof(CK_ATTRIBUTE)); return 1; } for( l = 0, k = 0; k < number_of_all_known_attribute_types; k++ ) { if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) { pT2[l].type = pTemplate[k].type; pT2[l].ulValueLen = pTemplate[k].ulValueLen; pT2[l].pValue = (CK_VOID_PTR)PR_Malloc(pT2[l].ulValueLen); if( (CK_VOID_PTR)NULL == pT2[l].pValue ) { PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", pT2[l].ulValueLen); return 1; } l++; } } PR_ASSERT( l == nAttributes ); ck_rv = epv->C_GetAttributeValue(h, o, pT2, nAttributes); switch( ck_rv ) { case CKR_OK: case CKR_ATTRIBUTE_SENSITIVE: case CKR_ATTRIBUTE_TYPE_INVALID: case CKR_BUFFER_TOO_SMALL: break; default: PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, {existent attribute types}, %lu) returned 0x%08x\n", h, o, nAttributes, ck_rv); return 1; } for( l = 0; l < nAttributes; l++ ) { PR_fprintf(PR_STDOUT, " type = 0x%08x, len = %ld", pT2[l].type, (CK_LONG)pT2[l].ulValueLen); if( -1 == (CK_LONG)pT2[l].ulValueLen ) { ; } else { CK_ULONG m; if( pT2[l].ulValueLen <= 8 ) { PR_fprintf(PR_STDOUT, ", value = "); } else { PR_fprintf(PR_STDOUT, ", value = \n "); } for( m = 0; (m < pT2[l].ulValueLen) && (m < 20); m++ ) { PR_fprintf(PR_STDOUT, "%02x", (CK_ULONG)(0xff & ((CK_CHAR_PTR)pT2[l].pValue)[m])); } PR_fprintf(PR_STDOUT, " "); for( m = 0; (m < pT2[l].ulValueLen) && (m < 20); m++ ) { CK_CHAR c = ((CK_CHAR_PTR)pT2[l].pValue)[m]; if( (c < 0x20) || (c >= 0x7f) ) { c = '.'; } PR_fprintf(PR_STDOUT, "%c", c); } } PR_fprintf(PR_STDOUT, "\n"); } PR_fprintf(PR_STDOUT, "\n"); for( l = 0; l < nAttributes; l++ ) { PR_Free(pT2[l].pValue); } PR_Free(pT2); } /* while(1) */ ck_rv = epv->C_FindObjectsFinal(h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " (%lu objects total)\n", tnObjects); ck_rv = epv->C_CloseSession(h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CloseSession(%lu) returned 0x%08x\n", h, ck_rv); return 1; } } /* session to find objects */ /* session to create, find, and delete a couple session objects */ { CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0; CK_ATTRIBUTE one[7], two[7], three[7], delta[1], mask[1]; CK_OBJECT_CLASS cko_data = CKO_DATA; CK_BBOOL false = CK_FALSE, true = CK_TRUE; char *key = "TEST PROGRAM"; CK_ULONG key_len = strlen(key); CK_OBJECT_HANDLE hOneIn = (CK_OBJECT_HANDLE)0, hTwoIn = (CK_OBJECT_HANDLE)0, hThreeIn = (CK_OBJECT_HANDLE)0, hDeltaIn = (CK_OBJECT_HANDLE)0; CK_OBJECT_HANDLE found[10]; CK_ULONG nFound; ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Opened a session: handle = 0x%08x\n", h); one[0].type = CKA_CLASS; one[0].pValue = &cko_data; one[0].ulValueLen = sizeof(CK_OBJECT_CLASS); one[1].type = CKA_TOKEN; one[1].pValue = &false; one[1].ulValueLen = sizeof(CK_BBOOL); one[2].type = CKA_PRIVATE; one[2].pValue = &false; one[2].ulValueLen = sizeof(CK_BBOOL); one[3].type = CKA_MODIFIABLE; one[3].pValue = &true; one[3].ulValueLen = sizeof(CK_BBOOL); one[4].type = CKA_LABEL; one[4].pValue = "Test data object one"; one[4].ulValueLen = strlen(one[4].pValue); one[5].type = CKA_APPLICATION; one[5].pValue = key; one[5].ulValueLen = key_len; one[6].type = CKA_VALUE; one[6].pValue = "Object one"; one[6].ulValueLen = strlen(one[6].pValue); two[0].type = CKA_CLASS; two[0].pValue = &cko_data; two[0].ulValueLen = sizeof(CK_OBJECT_CLASS); two[1].type = CKA_TOKEN; two[1].pValue = &false; two[1].ulValueLen = sizeof(CK_BBOOL); two[2].type = CKA_PRIVATE; two[2].pValue = &false; two[2].ulValueLen = sizeof(CK_BBOOL); two[3].type = CKA_MODIFIABLE; two[3].pValue = &true; two[3].ulValueLen = sizeof(CK_BBOOL); two[4].type = CKA_LABEL; two[4].pValue = "Test data object two"; two[4].ulValueLen = strlen(two[4].pValue); two[5].type = CKA_APPLICATION; two[5].pValue = key; two[5].ulValueLen = key_len; two[6].type = CKA_VALUE; two[6].pValue = "Object two"; two[6].ulValueLen = strlen(two[6].pValue); three[0].type = CKA_CLASS; three[0].pValue = &cko_data; three[0].ulValueLen = sizeof(CK_OBJECT_CLASS); three[1].type = CKA_TOKEN; three[1].pValue = &false; three[1].ulValueLen = sizeof(CK_BBOOL); three[2].type = CKA_PRIVATE; three[2].pValue = &false; three[2].ulValueLen = sizeof(CK_BBOOL); three[3].type = CKA_MODIFIABLE; three[3].pValue = &true; three[3].ulValueLen = sizeof(CK_BBOOL); three[4].type = CKA_LABEL; three[4].pValue = "Test data object three"; three[4].ulValueLen = strlen(three[4].pValue); three[5].type = CKA_APPLICATION; three[5].pValue = key; three[5].ulValueLen = key_len; three[6].type = CKA_VALUE; three[6].pValue = "Object three"; three[6].ulValueLen = strlen(three[6].pValue); ck_rv = epv->C_CreateObject(h, one, 7, &hOneIn); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CreateObject(%lu, one, 7, ) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Created object one: handle = %lu\n", hOneIn); ck_rv = epv->C_CreateObject(h, two, 7, &hTwoIn); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CreateObject(%lu, two, 7, ) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Created object two: handle = %lu\n", hTwoIn); ck_rv = epv->C_CreateObject(h, three, 7, &hThreeIn); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CreateObject(%lu, three, 7, ) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Created object three: handle = %lu\n", hThreeIn); delta[0].type = CKA_VALUE; delta[0].pValue = "Copied object"; delta[0].ulValueLen = strlen(delta[0].pValue); ck_rv = epv->C_CopyObject(h, hThreeIn, delta, 1, &hDeltaIn); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CopyObject(%lu, %lu, delta, 1, ) returned 0x%08x\n", h, hThreeIn, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Copied object three: new handle = %lu\n", hDeltaIn); mask[0].type = CKA_APPLICATION; mask[0].pValue = key; mask[0].ulValueLen = key_len; ck_rv = epv->C_FindObjectsInit(h, mask, 1); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n", h, ck_rv); return 1; } (void)memset(&found, 0, sizeof(found)); nFound = 0; ck_rv = epv->C_FindObjects(h, found, 10, &nFound); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", h, ck_rv); return 1; } if( 4 != nFound ) { PR_fprintf(PR_STDERR, "Found %lu objects, not 4.\n", nFound); return 1; } PR_fprintf(PR_STDOUT, " Found 4 objects: %lu, %lu, %lu, %lu\n", found[0], found[1], found[2], found[3]); ck_rv = epv->C_FindObjectsFinal(h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv); return 1; } ck_rv = epv->C_DestroyObject(h, hThreeIn); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, hThreeIn, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Destroyed object three (handle = %lu)\n", hThreeIn); delta[0].type = CKA_APPLICATION; delta[0].pValue = "Changed application"; delta[0].ulValueLen = strlen(delta[0].pValue); ck_rv = epv->C_SetAttributeValue(h, hTwoIn, delta, 1); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n", h, hTwoIn, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Changed object two (handle = %lu).\n", hTwoIn); /* Can another session find these session objects? */ { CK_SESSION_HANDLE h2 = (CK_SESSION_HANDLE)0; ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h2); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Opened a second session: handle = 0x%08x\n", h2); /* mask is still the same */ ck_rv = epv->C_FindObjectsInit(h2, mask, 1); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n", h2, ck_rv); return 1; } (void)memset(&found, 0, sizeof(found)); nFound = 0; ck_rv = epv->C_FindObjects(h2, found, 10, &nFound); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", h2, ck_rv); return 1; } if( 2 != nFound ) { PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound); return 1; } PR_fprintf(PR_STDOUT, " Found 2 objects: %lu, %lu\n", found[0], found[1]); ck_rv = epv->C_FindObjectsFinal(h2); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h2, ck_rv); return 1; } /* Leave the session hanging open, we'll CloseAllSessions later */ } /* Can another session find these session objects? */ ck_rv = epv->C_CloseAllSessions(pSlots[i]); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CloseAllSessions(%lu) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } } /* session to create, find, and delete a couple session objects */ /* Might be interesting to do a find here to verify that all session objects are gone. */ if( tinfo.flags & CKF_WRITE_PROTECTED ) { PR_fprintf(PR_STDOUT, "Token is write protected, skipping token-object tests.\n"); } else { CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0; CK_ATTRIBUTE tobj[7], tsobj[7], stobj[7], delta[1], mask[2]; CK_OBJECT_CLASS cko_data = CKO_DATA; CK_BBOOL false = CK_FALSE, true = CK_TRUE; char *key = "TEST PROGRAM"; CK_ULONG key_len = strlen(key); CK_OBJECT_HANDLE hTIn = (CK_OBJECT_HANDLE)0, hTSIn = (CK_OBJECT_HANDLE)0, hSTIn = (CK_OBJECT_HANDLE)0, hDeltaIn = (CK_OBJECT_HANDLE)0; CK_OBJECT_HANDLE found[10]; CK_ULONG nFound; ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Opened a session: handle = 0x%08x\n", h); tobj[0].type = CKA_CLASS; tobj[0].pValue = &cko_data; tobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS); tobj[1].type = CKA_TOKEN; tobj[1].pValue = &true; tobj[1].ulValueLen = sizeof(CK_BBOOL); tobj[2].type = CKA_PRIVATE; tobj[2].pValue = &false; tobj[2].ulValueLen = sizeof(CK_BBOOL); tobj[3].type = CKA_MODIFIABLE; tobj[3].pValue = &true; tobj[3].ulValueLen = sizeof(CK_BBOOL); tobj[4].type = CKA_LABEL; tobj[4].pValue = "Test data object token"; tobj[4].ulValueLen = strlen(tobj[4].pValue); tobj[5].type = CKA_APPLICATION; tobj[5].pValue = key; tobj[5].ulValueLen = key_len; tobj[6].type = CKA_VALUE; tobj[6].pValue = "Object token"; tobj[6].ulValueLen = strlen(tobj[6].pValue); tsobj[0].type = CKA_CLASS; tsobj[0].pValue = &cko_data; tsobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS); tsobj[1].type = CKA_TOKEN; tsobj[1].pValue = &true; tsobj[1].ulValueLen = sizeof(CK_BBOOL); tsobj[2].type = CKA_PRIVATE; tsobj[2].pValue = &false; tsobj[2].ulValueLen = sizeof(CK_BBOOL); tsobj[3].type = CKA_MODIFIABLE; tsobj[3].pValue = &true; tsobj[3].ulValueLen = sizeof(CK_BBOOL); tsobj[4].type = CKA_LABEL; tsobj[4].pValue = "Test data object token->session"; tsobj[4].ulValueLen = strlen(tsobj[4].pValue); tsobj[5].type = CKA_APPLICATION; tsobj[5].pValue = key; tsobj[5].ulValueLen = key_len; tsobj[6].type = CKA_VALUE; tsobj[6].pValue = "Object token->session"; tsobj[6].ulValueLen = strlen(tsobj[6].pValue); stobj[0].type = CKA_CLASS; stobj[0].pValue = &cko_data; stobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS); stobj[1].type = CKA_TOKEN; stobj[1].pValue = &false; stobj[1].ulValueLen = sizeof(CK_BBOOL); stobj[2].type = CKA_PRIVATE; stobj[2].pValue = &false; stobj[2].ulValueLen = sizeof(CK_BBOOL); stobj[3].type = CKA_MODIFIABLE; stobj[3].pValue = &true; stobj[3].ulValueLen = sizeof(CK_BBOOL); stobj[4].type = CKA_LABEL; stobj[4].pValue = "Test data object session->token"; stobj[4].ulValueLen = strlen(stobj[4].pValue); stobj[5].type = CKA_APPLICATION; stobj[5].pValue = key; stobj[5].ulValueLen = key_len; stobj[6].type = CKA_VALUE; stobj[6].pValue = "Object session->token"; stobj[6].ulValueLen = strlen(stobj[6].pValue); ck_rv = epv->C_CreateObject(h, tobj, 7, &hTIn); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Created object token: handle = %lu\n", hTIn); ck_rv = epv->C_CreateObject(h, tsobj, 7, &hTSIn); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Created object token->session: handle = %lu\n", hTSIn); ck_rv = epv->C_CreateObject(h, stobj, 7, &hSTIn); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Created object session->token: handle = %lu\n", hSTIn); /* I've created two token objects and one session object; find the two */ mask[0].type = CKA_APPLICATION; mask[0].pValue = key; mask[0].ulValueLen = key_len; mask[1].type = CKA_TOKEN; mask[1].pValue = &true; mask[1].ulValueLen = sizeof(CK_BBOOL); ck_rv = epv->C_FindObjectsInit(h, mask, 2); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n", h, ck_rv); return 1; } (void)memset(&found, 0, sizeof(found)); nFound = 0; ck_rv = epv->C_FindObjects(h, found, 10, &nFound); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", h, ck_rv); return 1; } if( 2 != nFound ) { PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound); return 1; } PR_fprintf(PR_STDOUT, " Found 2 objects: %lu, %lu\n", found[0], found[1]); ck_rv = epv->C_FindObjectsFinal(h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv); return 1; } /* Convert a token to session object */ delta[0].type = CKA_TOKEN; delta[0].pValue = &false; delta[0].ulValueLen = sizeof(CK_BBOOL); ck_rv = epv->C_SetAttributeValue(h, hTSIn, delta, 1); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n", h, hTSIn, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Changed object from token to session (handle = %lu).\n", hTSIn); /* Now find again; there should be one */ mask[0].type = CKA_APPLICATION; mask[0].pValue = key; mask[0].ulValueLen = key_len; mask[1].type = CKA_TOKEN; mask[1].pValue = &true; mask[1].ulValueLen = sizeof(CK_BBOOL); ck_rv = epv->C_FindObjectsInit(h, mask, 2); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n", h, ck_rv); return 1; } (void)memset(&found, 0, sizeof(found)); nFound = 0; ck_rv = epv->C_FindObjects(h, found, 10, &nFound); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", h, ck_rv); return 1; } if( 1 != nFound ) { PR_fprintf(PR_STDERR, "Found %lu objects, not 1.\n", nFound); return 1; } PR_fprintf(PR_STDOUT, " Found 1 objects: %lu\n", found[0]); ck_rv = epv->C_FindObjectsFinal(h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv); return 1; } /* Convert a session to a token object */ delta[0].type = CKA_TOKEN; delta[0].pValue = &true; delta[0].ulValueLen = sizeof(CK_BBOOL); ck_rv = epv->C_SetAttributeValue(h, hSTIn, delta, 1); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n", h, hSTIn, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Changed object from session to token (handle = %lu).\n", hSTIn); /* Now find again; there should be two again */ mask[0].type = CKA_APPLICATION; mask[0].pValue = key; mask[0].ulValueLen = key_len; mask[1].type = CKA_TOKEN; mask[1].pValue = &true; mask[1].ulValueLen = sizeof(CK_BBOOL); ck_rv = epv->C_FindObjectsInit(h, mask, 2); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n", h, ck_rv); return 1; } (void)memset(&found, 0, sizeof(found)); nFound = 0; ck_rv = epv->C_FindObjects(h, found, 10, &nFound); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", h, ck_rv); return 1; } if( 2 != nFound ) { PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound); return 1; } PR_fprintf(PR_STDOUT, " Found 2 objects: %lu, %lu\n", found[0], found[1]); ck_rv = epv->C_FindObjectsFinal(h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv); return 1; } /* Delete the two (found) token objects to clean up */ ck_rv = epv->C_DestroyObject(h, found[0]); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, found[0], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Destroyed token object (handle = %lu)\n", found[0]); ck_rv = epv->C_DestroyObject(h, found[1]); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, found[1], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Destroyed token object (handle = %lu)\n", found[1]); /* Close the session and all objects should be gone */ ck_rv = epv->C_CloseSession(h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CloseSession(%lu) returned 0x%08x\n", h, ck_rv); return 1; } } /* if( tinfo.flags & CKF_WRITE_PROTECTED ) */ if( tinfo.flags & CKF_WRITE_PROTECTED ) { PR_fprintf(PR_STDOUT, "Token is write protected, skipping leaving a record.\n"); } else { CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0; CK_ATTRIBUTE record[7], mask[2]; CK_OBJECT_CLASS cko_data = CKO_DATA; CK_BBOOL false = CK_FALSE, true = CK_TRUE; char *key = "TEST RECORD"; CK_ULONG key_len = strlen(key); CK_OBJECT_HANDLE hin = (CK_OBJECT_HANDLE)0; char timebuffer[256]; ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Opened a session: handle = 0x%08x\n", h); /* I can't believe how hard NSPR makes this operation */ { time_t now = 0; struct tm *tm; time(&now); tm = localtime(&now); strftime(timebuffer, sizeof(timebuffer), "%Y-%m-%d %T %Z", tm); } record[0].type = CKA_CLASS; record[0].pValue = &cko_data; record[0].ulValueLen = sizeof(CK_OBJECT_CLASS); record[1].type = CKA_TOKEN; record[1].pValue = &true; record[1].ulValueLen = sizeof(CK_BBOOL); record[2].type = CKA_PRIVATE; record[2].pValue = &false; record[2].ulValueLen = sizeof(CK_BBOOL); record[3].type = CKA_MODIFIABLE; record[3].pValue = &true; record[3].ulValueLen = sizeof(CK_BBOOL); record[4].type = CKA_LABEL; record[4].pValue = "Test record"; record[4].ulValueLen = strlen(record[4].pValue); record[5].type = CKA_APPLICATION; record[5].pValue = key; record[5].ulValueLen = key_len; record[6].type = CKA_VALUE; record[6].pValue = timebuffer; record[6].ulValueLen = strlen(timebuffer)+1; PR_fprintf(PR_STDOUT, " Timestamping with \"%s\"\n", timebuffer); ck_rv = epv->C_CreateObject(h, record, 7, &hin); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv); return 1; } PR_fprintf(PR_STDOUT, " Created record object: handle = %lu\n", hin); PR_fprintf(PR_STDOUT, " == All test timestamps ==\n"); mask[0].type = CKA_CLASS; mask[0].pValue = &cko_data; mask[0].ulValueLen = sizeof(CK_OBJECT_CLASS); mask[1].type = CKA_APPLICATION; mask[1].pValue = key; mask[1].ulValueLen = key_len; ck_rv = epv->C_FindObjectsInit(h, mask, 2); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n", h, ck_rv); return 1; } while( 1 ) { CK_OBJECT_HANDLE o = (CK_OBJECT_HANDLE)0; CK_ULONG nObjects = 0; CK_ATTRIBUTE value[1]; char buffer[1024]; ck_rv = epv->C_FindObjects(h, &o, 1, &nObjects); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjects(%lu, , 1, ) returned 0x%08x\n", h, ck_rv); return 1; } if( 0 == nObjects ) { PR_fprintf(PR_STDOUT, "\n"); break; } value[0].type = CKA_VALUE; value[0].pValue = buffer; value[0].ulValueLen = sizeof(buffer); ck_rv = epv->C_GetAttributeValue(h, o, value, 1); switch( ck_rv ) { case CKR_OK: PR_fprintf(PR_STDOUT, " %s\n", value[0].pValue); break; case CKR_ATTRIBUTE_SENSITIVE: PR_fprintf(PR_STDOUT, " [Sensitive???]\n"); break; case CKR_ATTRIBUTE_TYPE_INVALID: PR_fprintf(PR_STDOUT, " [Invalid attribute???]\n"); break; case CKR_BUFFER_TOO_SMALL: PR_fprintf(PR_STDOUT, " (result > 1k (%lu))\n", value[0].ulValueLen); break; default: PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, CKA_VALUE, 1) returned 0x%08x\n", h, o); return 1; } } /* while */ ck_rv = epv->C_FindObjectsFinal(h); if( CKR_OK != ck_rv ) { PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv); return 1; } } /* "leaving a record" else clause */ } PR_fprintf(PR_STDOUT, "\n"); } return 0; }
extern PRFileMap* _md_OpenAnonFileMap( const char *dirName, PRSize size, PRFileMapProtect prot ) { PRFileMap *fm = NULL; PRFileDesc *fd; int osfd; PRIntn urc; PRIntn mode = 0600; char *genName; pid_t pid = getpid(); /* for generating filename */ PRThread *tid = PR_GetCurrentThread(); /* for generating filename */ int incr; /* for generating filename */ const int maxTries = 20; /* maximum # attempts at a unique filename */ PRInt64 size64; /* 64-bit version of 'size' */ /* ** generate a filename from input and runtime environment ** open the file, unlink the file. ** make maxTries number of attempts at uniqueness in the filename */ for ( incr = 0; incr < maxTries ; incr++ ) { #ifdef SYMBIAN genName = PR_smprintf( "%s\\NSPR-AFM-%d-%p.%d", #else genName = PR_smprintf( "%s/.NSPR-AFM-%d-%p.%d", #endif dirName, (int) pid, tid, incr ); if ( NULL == genName ) { PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): PR_snprintf(): failed, generating filename")); goto Finished; } /* create the file */ osfd = open( genName, (O_CREAT | O_EXCL | O_RDWR), mode ); if ( -1 == osfd ) { if ( EEXIST == errno ) { PR_smprintf_free( genName ); continue; /* name exists, try again */ } else { _PR_MD_MAP_OPEN_ERROR( errno ); PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): open(): failed, filename: %s, errno: %d", genName, PR_GetOSError())); PR_smprintf_free( genName ); goto Finished; } } break; /* name generation and open successful, break; */ } /* end for() */ if ( incr == maxTries ) { PR_ASSERT( -1 == osfd ); PR_ASSERT( EEXIST == errno ); _PR_MD_MAP_OPEN_ERROR( errno ); goto Finished; } urc = unlink( genName ); #if defined(__WINS__) /* If it is being used by the system or another process, Symbian OS Emulator(WINS) considers this an error. */ if ( -1 == urc && EACCES != errno ) { #else if ( -1 == urc ) { #endif _PR_MD_MAP_UNLINK_ERROR( errno ); PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): failed on unlink(), errno: %d", errno)); PR_smprintf_free( genName ); close( osfd ); goto Finished; } PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): unlink(): %s", genName )); PR_smprintf_free( genName ); fd = PR_ImportFile( osfd ); if ( NULL == fd ) { PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): PR_ImportFile(): failed")); goto Finished; } PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): fd: %p", fd )); urc = ftruncate( fd->secret->md.osfd, size ); if ( -1 == urc ) { _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): failed on ftruncate(), errno: %d", errno)); PR_Close( fd ); goto Finished; } PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): ftruncate(): size: %d", size )); LL_UI2L(size64, size); /* PRSize (size_t) is unsigned */ fm = PR_CreateFileMap( fd, size64, prot ); if ( NULL == fm ) { PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("PR_OpenAnonFileMap(): failed")); PR_Close( fd ); goto Finished; } fm->md.isAnonFM = PR_TRUE; /* set fd close */ PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_OpenAnonFileMap(): PR_CreateFileMap(): fm: %p", fm )); Finished: return(fm); } /* end md_OpenAnonFileMap() */ /* ** _md_ExportFileMapAsString() ** ** */ extern PRStatus _md_ExportFileMapAsString( PRFileMap *fm, PRSize bufSize, char *buf ) { PRIntn written; PRIntn prot = (PRIntn)fm->prot; written = PR_snprintf( buf, bufSize, "%ld:%d", fm->fd->secret->md.osfd, prot ); return((written == -1)? PR_FAILURE : PR_SUCCESS); } /* end _md_ExportFileMapAsString() */
/* static */ bool WebCL_LibCL::load (char const* aLibName, WebCL_LibCL** aInstanceOut, nsCString* aErrorMessageOut) { D_METHOD_START; if (!aLibName) { D_LOG (LOG_LEVEL_ERROR, "Invalid arguments: aLibName = NULL."); if (aErrorMessageOut) *aErrorMessageOut = "Invalid library name: NULL"; return false; } // Front strip lib name size_t libPathLen = strlen (aLibName); char const* cleanedLibName = aLibName; while (cleanedLibName < aLibName + libPathLen && isspace(*cleanedLibName)) ++cleanedLibName; if (cleanedLibName[0] == '\0') { // Empty name now allowed here, handle default library on a higher level D_LOG (LOG_LEVEL_ERROR, "Invalid arguments: no library name given."); if (aErrorMessageOut) *aErrorMessageOut = "Invalid library name: \"\""; return false; } char* systemName = PR_GetLibraryName (NULL, cleanedLibName); D_LOG (LOG_LEVEL_DEBUG, "system name for library %s: %s", cleanedLibName, systemName); nsCOMPtr<WebCL_LibCL> instance (new (std::nothrow) WebCL_LibCL); instance->m_libName = strdup (systemName); PRLibrary* libHndl = PR_LoadLibrary (systemName); PR_FreeLibraryName (systemName); char* errText = 0; if (!libHndl) { // Perhaps PR_GetLibraryName failed? errText = getPRErrorText (); D_LOG (LOG_LEVEL_ERROR, "Failed to load library by system name %s: %s", instance->m_libName, errText); D_LOG (LOG_LEVEL_DEBUG, " (prerr: %d oserr: %d)", PR_GetError(), PR_GetOSError()); PR_FREEIF (errText); errText = 0; // TODO: check if this is even sane? libHndl = PR_LoadLibrary (cleanedLibName); if (!libHndl) { // Failed to load the library. errText = getPRErrorText (); D_LOG (LOG_LEVEL_ERROR, "Failed to load library %s: %s", cleanedLibName, errText); D_LOG (LOG_LEVEL_DEBUG, " (prerr: %d oserr: %d)", PR_GetError(), PR_GetOSError()); if (aErrorMessageOut) *aErrorMessageOut = errText; PR_FREEIF (errText); return false; } instance->m_libName = strdup (cleanedLibName); } instance->m_libHandle = libHndl; if (!loadCLLibrary (instance, libHndl)) { D_LOG (LOG_LEVEL_ERROR, "Failed to load library %s: required symbols missing.", instance->m_libName); if (aErrorMessageOut) *aErrorMessageOut = "Required symbols not found."; return false; } if (aInstanceOut) { NS_ADDREF (*aInstanceOut = instance); } return true; }