PRStatus _PR_MD_CLOSE_SEMAPHORE(PRSem *sem) { if (CloseHandle(sem->sem) == FALSE) { _PR_MD_MAP_CLOSE_ERROR(GetLastError()); return PR_FAILURE; } PR_DELETE(sem); return PR_SUCCESS; }
PRInt32 _MD_CloseFile(PRInt32 osfd) { PRInt32 rv; rv = DosClose((HFILE)osfd); if (rv != NO_ERROR) _PR_MD_MAP_CLOSE_ERROR(rv); return rv; }
PRInt32 _MD_CloseFile(PROsfd osfd) { PRInt32 rv; rv = (CloseHandle((HANDLE)osfd))?0:-1; if (rv == -1) _PR_MD_MAP_CLOSE_ERROR(GetLastError()); return rv; }
/* ** _MD_CloseSocket() -- Close a socket ** */ PRInt32 _MD_CloseSocket(PROsfd osfd) { PRInt32 rv; rv = closesocket((SOCKET) osfd ); if (rv < 0) _PR_MD_MAP_CLOSE_ERROR(WSAGetLastError()); return rv; }
PR_IMPLEMENT(PRInt32) _amigaos_CloseFile(PRInt32 osfd) { PRInt32 rv; rv = close(osfd); if (rv == -1) { _PR_MD_MAP_CLOSE_ERROR(_MD_ERRNO()); } return rv; }
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; }
PR_IMPLEMENT(PRSem *) PR_OpenSemaphore( const char *name, PRIntn flags, PRIntn mode, PRUintn value) { PRSem *sem; key_t key; union semun arg; struct sembuf sop; struct semid_ds seminfo; #define MAX_TRIES 60 PRIntn i; char osname[PR_IPC_NAME_SIZE]; if (_PR_MakeNativeIPCName(name, osname, sizeof(osname), _PRIPCSem) == PR_FAILURE) { return NULL; } /* Make sure the file exists before calling ftok. */ if (flags & PR_SEM_CREATE) { int osfd = open(osname, O_RDWR|O_CREAT, mode); if (-1 == osfd) { _PR_MD_MAP_OPEN_ERROR(errno); return NULL; } if (close(osfd) == -1) { _PR_MD_MAP_CLOSE_ERROR(errno); return NULL; } } key = ftok(osname, NSPR_IPC_KEY_ID); if ((key_t)-1 == key) { _PR_MD_MAP_DEFAULT_ERROR(errno); return NULL; } sem = PR_NEW(PRSem); if (NULL == sem) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } if (flags & PR_SEM_CREATE) { sem->semid = semget(key, 1, mode|IPC_CREAT|IPC_EXCL); if (sem->semid >= 0) { /* creator of a semaphore is responsible for initializing it */ arg.val = 0; if (semctl(sem->semid, 0, SETVAL, arg) == -1) { _PR_MD_MAP_DEFAULT_ERROR(errno); PR_Free(sem); return NULL; } /* call semop to set sem_otime to nonzero */ sop.sem_num = 0; sop.sem_op = value; sop.sem_flg = 0; if (semop(sem->semid, &sop, 1) == -1) { _PR_MD_MAP_DEFAULT_ERROR(errno); PR_Free(sem); return NULL; } return sem; } if (errno != EEXIST || flags & PR_SEM_EXCL) { _PR_MD_MAP_DEFAULT_ERROR(errno); PR_Free(sem); return NULL; } } sem->semid = semget(key, 1, NSPR_SEM_MODE); if (sem->semid == -1) { _PR_MD_MAP_DEFAULT_ERROR(errno); PR_Free(sem); return NULL; } for (i = 0; i < MAX_TRIES; i++) { arg.buf = &seminfo; semctl(sem->semid, 0, IPC_STAT, arg); if (seminfo.sem_otime != 0) break; sleep(1); } if (i == MAX_TRIES) { PR_SetError(PR_IO_TIMEOUT_ERROR, 0); PR_Free(sem); return NULL; } return sem; }
extern PRSharedMemory * _MD_OpenSharedMemory( const char *name, PRSize size, PRIntn flags, PRIntn mode ) { PRStatus rc = PR_SUCCESS; key_t key; PRSharedMemory *shm; char ipcname[PR_IPC_NAME_SIZE]; rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm ); if ( PR_FAILURE == rc ) { _PR_MD_MAP_DEFAULT_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 = (char*)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")); PR_DELETE( shm ); 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 file first */ if ( flags & PR_SHM_CREATE ) { int osfd = open( shm->ipcname, (O_RDWR | O_CREAT), shm->mode ); if ( -1 == osfd ) { _PR_MD_MAP_OPEN_ERROR( errno ); PR_FREEIF( shm->ipcname ); PR_DELETE( shm ); return( NULL ); } if ( close(osfd) == -1 ) { _PR_MD_MAP_CLOSE_ERROR( errno ); PR_FREEIF( shm->ipcname ); PR_DELETE( shm ); return( NULL ); } } /* hash the shm.name to an ID */ key = ftok( shm->ipcname, NSPR_IPC_SHM_KEY ); if ( -1 == key ) { rc = PR_FAILURE; _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_OpenSharedMemory(): ftok() failed on name: %s", shm->ipcname)); PR_FREEIF( shm->ipcname ); PR_DELETE( shm ); return( NULL ); } /* get the shared memory */ if ( flags & PR_SHM_CREATE ) { shm->id = shmget( key, shm->size, ( shm->mode | IPC_CREAT|IPC_EXCL)); if ( shm->id >= 0 ) { return( shm ); } if ((errno == EEXIST) && (flags & PR_SHM_EXCL)) { PR_SetError( PR_FILE_EXISTS_ERROR, errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_OpenSharedMemory(): shmget() exclusive failed, errno: %d", errno)); PR_FREEIF(shm->ipcname); PR_DELETE(shm); return(NULL); } } shm->id = shmget( key, shm->size, shm->mode ); if ( -1 == shm->id ) { _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_OpenSharedMemory(): shmget() failed, errno: %d", errno)); PR_FREEIF(shm->ipcname); PR_DELETE(shm); return(NULL); } return( shm ); } /* end _MD_OpenSharedMemory() */
extern PRStatus _MD_DeleteSharedMemory( const char *name ) { PRStatus rc = PR_SUCCESS; key_t key; int id; PRIntn 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_DeleteSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name )); return(PR_FAILURE); } /* create the file first */ { int osfd = open( ipcname, (O_RDWR | O_CREAT), 0666 ); if ( -1 == osfd ) { _PR_MD_MAP_OPEN_ERROR( errno ); return( PR_FAILURE ); } if ( close(osfd) == -1 ) { _PR_MD_MAP_CLOSE_ERROR( errno ); return( PR_FAILURE ); } } /* hash the shm.name to an ID */ key = ftok( ipcname, NSPR_IPC_SHM_KEY ); if ( -1 == key ) { rc = PR_FAILURE; _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_DeleteSharedMemory(): ftok() failed on name: %s", ipcname)); } #ifdef SYMBIAN /* In Symbian OS the system imposed minimum is 1 byte, instead of ZERO */ id = shmget( key, 1, 0 ); #else id = shmget( key, 0, 0 ); #endif if ( -1 == id ) { _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_DeleteSharedMemory(): shmget() failed, errno: %d", errno)); return(PR_FAILURE); } urc = shmctl( id, IPC_RMID, NULL ); if ( -1 == urc ) { _PR_MD_MAP_DEFAULT_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_DeleteSharedMemory(): shmctl() failed on name: %s", ipcname )); return(PR_FAILURE); } urc = unlink( ipcname ); if ( -1 == urc ) { _PR_MD_MAP_UNLINK_ERROR( errno ); PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, ("_MD_DeleteSharedMemory(): unlink() failed: %s", ipcname )); return(PR_FAILURE); } return rc; } /* end _MD_DeleteSharedMemory() */