📄 shmem.c
字号:
return FALSE; } // SAB Get the group information for the PKCS#11 group... fail if // it does not exist grp = getgrnam("pkcs11"); if ( !grp ) { ErrLog("Group PKCS#11 does not exist "); return FALSE; // Group does not exist... setup is wrong.. } tok = ftok(Path ,'b'); // Allocate the shared memory... Fail if the memory is already // allocated since the slot mgr is the owner of it. // Is this some attempt at exclusivity, or is that just a side effect? - SCM 9/1 shmid = shmget( tok, sizeof( Slot_Mgr_Shr_t ), IPC_CREAT | IPC_EXCL | S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP ); // Explanation of options to shmget(): /* * IPC_CREAT Creates the data structure if it does not already exist. * IPC_EXCL Causes the shmget subroutine to be unsuccessful if the * IPC_CREAT flag is also set, and the data structure already exists. * S_IRUSR Permits the process that owns the data structure to read it. * S_IWUSR Permits the process that owns the data structure to modify it. * S_IRGRP Permits the group associated with the data structure to read it. * S_IWGRP Permits the group associated with the data structure to modify it. * * * WE DON"T WANT OTHERS * S_IROTH Permits others to read the data structure. * S_IWOTH Permits others to modify the data structure. */ if ( shmid < 0 ) { #pragma info(nopro) ErrLog(SLOTD_MSG(SHMEMCR, "Shared memory creation failed (0x%X)\n"), errno); #pragma info(restore) ErrLog(SLOTD_MSG(IPCRM, "perform ipcrm -M 0x%X\n"), tok); return FALSE; } // SAB Set the group ownership of the shared mem segment.. // we already have the group structure.. if ( shmctl(shmid, IPC_STAT,&shm_info) == 0 ) { shm_info.shm_perm.gid = grp->gr_gid; if (shmctl(shmid,IPC_SET,&shm_info) == -1) { ErrLog("Failed to set group ownership for shm \n"); shmctl(shmid, IPC_RMID,NULL); } } else { ErrLog("Can't get status of shared memory %d\n",errno); // we know it was created... we need to destroy it... shmctl(shmid, IPC_RMID,NULL); } return TRUE;#else {#warning "EXPERIMENTAL" int fd; int i; char *buffer; fd = open(MAPFILENAME,O_RDWR,MODE); if (fd < 0 ){ // File does not exist... this is cool, we creat it here fd = open(MAPFILENAME,O_RDWR|O_CREAT,MODE); // Create the file if (fd < 0 ){ // We are really hosed here, since we should be able // to create the file now return FALSE; } else { // Create a buffer and make the file the right length i = sizeof(Slot_Mgr_Shr_t); buffer = malloc(sizeof(Slot_Mgr_Shr_t)); memset(buffer,'\0',i); write(fd,buffer,i); free(buffer); close(fd); } } else { close(fd); return FALSE; } return TRUE;}#endif}/*********************************************************************** * * AttachToSharedMemeory - * * Called after creating the shared memory file * Basically allows us to have access to the memory we've just created * ***********************************************************************/int AttachToSharedMemeory ( void ) {#if !MMAP shmp = NULL; shmp = (Slot_Mgr_Shr_t *) shmat( shmid, NULL, 0 ); if ( !shmp ) { #pragma info(nopro) ErrLog(SLOTD_MSG(SHMEMAT, "Shared memory attach failed (0x%X)\n"), errno); #pragma info(restore) return FALSE; } /* Initizalize the memory to 0 */ memset ( shmp, '\0', sizeof(*shmp) ); return TRUE;#else{#warning "EXPERIMENTAL" int fd; int i; char *buffer; fd = open(MAPFILENAME,O_RDWR,MODE); if (fd < 0 ){ return FALSE; //Failed } shmp = (Slot_Mgr_Shr_t *)mmap(NULL,sizeof(Slot_Mgr_Shr_t),PROT_READ|PROT_WRITE, MAP_SHARED,fd,0); close(fd); if ( ! shmp ) { return FALSE; } return TRUE;}#endif}/*********************************************************************** * * DetachFromSharedMemory - * * Un-does AttachToSharedMemory() :) * ***********************************************************************/void DetachFromSharedMemory ( void ) {#if !MMAP if ( shmp == NULL ) return; if ( shmdt(shmp) != 0 ) { ErrLog(SLOTD_MSG(SHMEMDE, "Attempted to detach from an invalid shared memory pointer")); } shmp = NULL; return;#else if ( shmp == NULL ) return; munmap((void *)shmp,sizeof(*shmp)); unlink(MAPFILENAME);#endif}/*********************************************************************** * * DestroySharedMemory - * * Closes (destroys) the shared memory file we created with CreateSharedMemory() * * We should make sure that everyone else has detached before we do this * if we manage to exit before this gets called, you have to call ipcrm * to clean things up... * ***********************************************************************/void DestroySharedMemory ( void ) { if (shmctl (shmid, IPC_RMID, 0) != 0) { perror ("error in closing shared memory segment"); } return;}/*********************************************************************** * * InitSharedMemory - * * Set up our newly allocated shared memory segment * * ***********************************************************************/int InitSharedMemory ( Slot_Mgr_Shr_t *sp ) {#ifdef PKCS64 CK_INFO_PTR_64 ckinf = NULL;#else CK_INFO_PTR ckinf = NULL;#endif CK_VERSION_PTR ckver; CK_SLOT_INFO_PTR ckslot; CK_SLOT_ID id; uint16 procindex; ckinf = &(sp->ck_info); ckver = &(ckinf->cryptokiVersion); ckver->major = CRYPTOKI_API_MAJOR_V; ckver->minor = CRYPTOKI_API_MINOR_V; sp->ck_info.cryptokiVersion.major = CRYPTOKI_API_MAJOR_V; memset ( ckinf->manufacturerID, ' ', sizeof(ckinf->manufacturerID) ); memset ( ckinf->libraryDescription, ' ', sizeof(ckinf->libraryDescription) ); memcpy ( ckinf->manufacturerID, MFG, strlen(MFG) ); memcpy ( ckinf->libraryDescription, LIB, strlen(LIB) ); ckver = &(ckinf->libraryVersion); ckver->major = LIB_MAJOR_V; ckver->minor = LIB_MINOR_V; /* * populate the Slot entries... */ sp->num_slots = NumberSlotsInDB; /* FIXME: Change NUMBER_SLOTS_MANAGED in loop condition to NumberSlotsInDB? */ for ( id=0; ( (id < NUMBER_SLOTS_MANAGED) && (sinfo[id].present == TRUE ) ) ; id++ ) { /* Was sinfo[id].pk_slot.slotDescription[0] != '0' - assume typo 9/1 SCM */#ifdef PKCS64 CK_SLOT_INFO_64 *dest = &(sp->slot_info[id].pk_slot); CK_SLOT_INFO_64 *src = &(sinfo[id].pk_slot); #else CK_SLOT_INFO *dest = &(sp->slot_info[id].pk_slot); CK_SLOT_INFO *src = &(sinfo[id].pk_slot);#endif sp->slot_info[id].slot_number = sinfo[id].slot_number; sp->slot_info[id].present = sinfo[id].present; memset ( &(dest->slotDescription[0]), ' ', sizeof(dest->slotDescription) ); memset ( &(dest->manufacturerID[0]), ' ', sizeof(dest->manufacturerID) ); memcpy ( &(dest->slotDescription[0]), &(src->slotDescription[0]), sizeof( src->slotDescription ) ); memcpy ( &(dest->manufacturerID[0]), &(src->manufacturerID[0]), sizeof( src->manufacturerID ) ); dest->flags = src->flags; memcpy ( &(dest->hardwareVersion), &(src->hardwareVersion), sizeof(src->hardwareVersion) ); memcpy ( &(dest->firmwareVersion), &(src->firmwareVersion), sizeof(src->firmwareVersion) ); /* FIXME: We really should check the length of the strings before copying it */ strcpy( sp->slot_info[id].dll_location, sinfo[id].dll_location ); strcpy( sp->slot_info[id].slot_init_fcn, sinfo[id].slot_init_fcn ); strcpy( sp->slot_info[id].correlator, sinfo[id].correlator ); } /* end for id */ for( ; id < NUMBER_SLOTS_MANAGED; id++ ) { sp->slot_info[id].slot_number = id; } /* Initialize the process side of things. */ /* for now don't worry about the condition variables */ for ( procindex=0; procindex < NUMBER_PROCESSES_ALLOWED; procindex++ ) { /* Initialize the mutex variables. */#if FIXME#error "No usage of the per process mutexes..."#ifdef PKCS64#if (AIX) msem_init( &(sp->proc_table[procindex].proc_mutex), 0 );#else msem_init( &(sp->proc_table[procindex].proc_mutex), 1 );#endif#else pthread_mutex_init( &(sp->proc_table[procindex].proc_mutex), &mtxattr );#endif#endif sp->proc_table[procindex].inuse = FALSE; } return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -