andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** uxshm.c -- Unix Implementations NSPR Named Shared Memory andre@0: ** andre@0: ** andre@0: ** lth. Jul-1999. andre@0: ** andre@0: */ andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include "primpl.h" andre@0: #include andre@0: andre@0: extern PRLogModuleInfo *_pr_shm_lm; andre@0: andre@0: andre@0: #define NSPR_IPC_SHM_KEY 'b' andre@0: /* andre@0: ** Implementation for System V andre@0: */ andre@0: #if defined PR_HAVE_SYSV_NAMED_SHARED_MEMORY andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: andre@0: #define _MD_OPEN_SHARED_MEMORY _MD_OpenSharedMemory andre@0: #define _MD_ATTACH_SHARED_MEMORY _MD_AttachSharedMemory andre@0: #define _MD_DETACH_SHARED_MEMORY _MD_DetachSharedMemory andre@0: #define _MD_CLOSE_SHARED_MEMORY _MD_CloseSharedMemory andre@0: #define _MD_DELETE_SHARED_MEMORY _MD_DeleteSharedMemory andre@0: andre@0: extern PRSharedMemory * _MD_OpenSharedMemory( andre@0: const char *name, andre@0: PRSize size, andre@0: PRIntn flags, andre@0: PRIntn mode andre@0: ) andre@0: { andre@0: PRStatus rc = PR_SUCCESS; andre@0: key_t key; andre@0: PRSharedMemory *shm; andre@0: char ipcname[PR_IPC_NAME_SIZE]; andre@0: andre@0: rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm ); andre@0: if ( PR_FAILURE == rc ) andre@0: { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_OpenSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name )); andre@0: return( NULL ); andre@0: } andre@0: andre@0: shm = PR_NEWZAP( PRSharedMemory ); andre@0: if ( NULL == shm ) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 ); andre@0: PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New PRSharedMemory out of memory")); andre@0: return( NULL ); andre@0: } andre@0: andre@0: shm->ipcname = (char*)PR_MALLOC( strlen( ipcname ) + 1 ); andre@0: if ( NULL == shm->ipcname ) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 ); andre@0: PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New shm->ipcname out of memory")); andre@0: PR_DELETE( shm ); andre@0: return( NULL ); andre@0: } andre@0: andre@0: /* copy args to struct */ andre@0: strcpy( shm->ipcname, ipcname ); andre@0: shm->size = size; andre@0: shm->mode = mode; andre@0: shm->flags = flags; andre@0: shm->ident = _PR_SHM_IDENT; andre@0: andre@0: /* create the file first */ andre@0: if ( flags & PR_SHM_CREATE ) { andre@0: int osfd = open( shm->ipcname, (O_RDWR | O_CREAT), shm->mode ); andre@0: if ( -1 == osfd ) { andre@0: _PR_MD_MAP_OPEN_ERROR( errno ); andre@0: PR_FREEIF( shm->ipcname ); andre@0: PR_DELETE( shm ); andre@0: return( NULL ); andre@0: } andre@0: if ( close(osfd) == -1 ) { andre@0: _PR_MD_MAP_CLOSE_ERROR( errno ); andre@0: PR_FREEIF( shm->ipcname ); andre@0: PR_DELETE( shm ); andre@0: return( NULL ); andre@0: } andre@0: } andre@0: andre@0: /* hash the shm.name to an ID */ andre@0: key = ftok( shm->ipcname, NSPR_IPC_SHM_KEY ); andre@0: if ( -1 == key ) andre@0: { andre@0: rc = PR_FAILURE; andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_OpenSharedMemory(): ftok() failed on name: %s", shm->ipcname)); andre@0: PR_FREEIF( shm->ipcname ); andre@0: PR_DELETE( shm ); andre@0: return( NULL ); andre@0: } andre@0: andre@0: /* get the shared memory */ andre@0: if ( flags & PR_SHM_CREATE ) { andre@0: shm->id = shmget( key, shm->size, ( shm->mode | IPC_CREAT|IPC_EXCL)); andre@0: if ( shm->id >= 0 ) { andre@0: return( shm ); andre@0: } andre@0: if ((errno == EEXIST) && (flags & PR_SHM_EXCL)) { andre@0: PR_SetError( PR_FILE_EXISTS_ERROR, errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_OpenSharedMemory(): shmget() exclusive failed, errno: %d", errno)); andre@0: PR_FREEIF(shm->ipcname); andre@0: PR_DELETE(shm); andre@0: return(NULL); andre@0: } andre@0: } andre@0: andre@0: shm->id = shmget( key, shm->size, shm->mode ); andre@0: if ( -1 == shm->id ) { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_OpenSharedMemory(): shmget() failed, errno: %d", errno)); andre@0: PR_FREEIF(shm->ipcname); andre@0: PR_DELETE(shm); andre@0: return(NULL); andre@0: } andre@0: andre@0: return( shm ); andre@0: } /* end _MD_OpenSharedMemory() */ andre@0: andre@0: extern void * _MD_AttachSharedMemory( PRSharedMemory *shm, PRIntn flags ) andre@0: { andre@0: void *addr; andre@0: PRUint32 aFlags = shm->mode; andre@0: andre@0: PR_ASSERT( shm->ident == _PR_SHM_IDENT ); andre@0: andre@0: aFlags |= (flags & PR_SHM_READONLY )? SHM_RDONLY : 0; andre@0: andre@0: addr = shmat( shm->id, NULL, aFlags ); andre@0: if ( (void*)-1 == addr ) andre@0: { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_AttachSharedMemory(): shmat() failed on name: %s, OsError: %d", andre@0: shm->ipcname, PR_GetOSError() )); andre@0: addr = NULL; andre@0: } andre@0: andre@0: return addr; andre@0: } andre@0: andre@0: extern PRStatus _MD_DetachSharedMemory( PRSharedMemory *shm, void *addr ) andre@0: { andre@0: PRStatus rc = PR_SUCCESS; andre@0: PRIntn urc; andre@0: andre@0: PR_ASSERT( shm->ident == _PR_SHM_IDENT ); andre@0: andre@0: urc = shmdt( addr ); andre@0: if ( -1 == urc ) andre@0: { andre@0: rc = PR_FAILURE; andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DetachSharedMemory(): shmdt() failed on name: %s", shm->ipcname )); andre@0: } andre@0: andre@0: return rc; andre@0: } andre@0: andre@0: extern PRStatus _MD_CloseSharedMemory( PRSharedMemory *shm ) andre@0: { andre@0: PR_ASSERT( shm->ident == _PR_SHM_IDENT ); andre@0: andre@0: PR_FREEIF(shm->ipcname); andre@0: PR_DELETE(shm); andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: extern PRStatus _MD_DeleteSharedMemory( const char *name ) andre@0: { andre@0: PRStatus rc = PR_SUCCESS; andre@0: key_t key; andre@0: int id; andre@0: PRIntn urc; andre@0: char ipcname[PR_IPC_NAME_SIZE]; andre@0: andre@0: rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm ); andre@0: if ( PR_FAILURE == rc ) andre@0: { andre@0: PR_SetError( PR_UNKNOWN_ERROR , errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DeleteSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name )); andre@0: return(PR_FAILURE); andre@0: } andre@0: andre@0: /* create the file first */ andre@0: { andre@0: int osfd = open( ipcname, (O_RDWR | O_CREAT), 0666 ); andre@0: if ( -1 == osfd ) { andre@0: _PR_MD_MAP_OPEN_ERROR( errno ); andre@0: return( PR_FAILURE ); andre@0: } andre@0: if ( close(osfd) == -1 ) { andre@0: _PR_MD_MAP_CLOSE_ERROR( errno ); andre@0: return( PR_FAILURE ); andre@0: } andre@0: } andre@0: andre@0: /* hash the shm.name to an ID */ andre@0: key = ftok( ipcname, NSPR_IPC_SHM_KEY ); andre@0: if ( -1 == key ) andre@0: { andre@0: rc = PR_FAILURE; andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DeleteSharedMemory(): ftok() failed on name: %s", ipcname)); andre@0: } andre@0: andre@0: #ifdef SYMBIAN andre@0: /* In Symbian OS the system imposed minimum is 1 byte, instead of ZERO */ andre@0: id = shmget( key, 1, 0 ); andre@0: #else andre@0: id = shmget( key, 0, 0 ); andre@0: #endif andre@0: if ( -1 == id ) { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DeleteSharedMemory(): shmget() failed, errno: %d", errno)); andre@0: return(PR_FAILURE); andre@0: } andre@0: andre@0: urc = shmctl( id, IPC_RMID, NULL ); andre@0: if ( -1 == urc ) andre@0: { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DeleteSharedMemory(): shmctl() failed on name: %s", ipcname )); andre@0: return(PR_FAILURE); andre@0: } andre@0: andre@0: urc = unlink( ipcname ); andre@0: if ( -1 == urc ) { andre@0: _PR_MD_MAP_UNLINK_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DeleteSharedMemory(): unlink() failed: %s", ipcname )); andre@0: return(PR_FAILURE); andre@0: } andre@0: andre@0: return rc; andre@0: } /* end _MD_DeleteSharedMemory() */ andre@0: andre@0: /* andre@0: ** Implementation for Posix andre@0: */ andre@0: #elif defined PR_HAVE_POSIX_NAMED_SHARED_MEMORY andre@0: #include andre@0: andre@0: #define _MD_OPEN_SHARED_MEMORY _MD_OpenSharedMemory andre@0: #define _MD_ATTACH_SHARED_MEMORY _MD_AttachSharedMemory andre@0: #define _MD_DETACH_SHARED_MEMORY _MD_DetachSharedMemory andre@0: #define _MD_CLOSE_SHARED_MEMORY _MD_CloseSharedMemory andre@0: #define _MD_DELETE_SHARED_MEMORY _MD_DeleteSharedMemory andre@0: andre@0: struct _MDSharedMemory { andre@0: int handle; andre@0: }; andre@0: andre@0: extern PRSharedMemory * _MD_OpenSharedMemory( andre@0: const char *name, andre@0: PRSize size, andre@0: PRIntn flags, andre@0: PRIntn mode andre@0: ) andre@0: { andre@0: PRStatus rc = PR_SUCCESS; andre@0: PRInt32 end; andre@0: PRSharedMemory *shm; andre@0: char ipcname[PR_IPC_NAME_SIZE]; andre@0: andre@0: rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm ); andre@0: if ( PR_FAILURE == rc ) andre@0: { andre@0: PR_SetError( PR_UNKNOWN_ERROR , errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_OpenSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name )); andre@0: return( NULL ); andre@0: } andre@0: andre@0: shm = PR_NEWZAP( PRSharedMemory ); andre@0: if ( NULL == shm ) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 ); andre@0: PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New PRSharedMemory out of memory")); andre@0: return( NULL ); andre@0: } andre@0: andre@0: shm->ipcname = PR_MALLOC( strlen( ipcname ) + 1 ); andre@0: if ( NULL == shm->ipcname ) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 ); andre@0: PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New shm->ipcname out of memory")); andre@0: return( NULL ); andre@0: } andre@0: andre@0: /* copy args to struct */ andre@0: strcpy( shm->ipcname, ipcname ); andre@0: shm->size = size; andre@0: shm->mode = mode; andre@0: shm->flags = flags; andre@0: shm->ident = _PR_SHM_IDENT; andre@0: andre@0: /* andre@0: ** Create the shared memory andre@0: */ andre@0: if ( flags & PR_SHM_CREATE ) { andre@0: int oflag = (O_CREAT | O_RDWR); andre@0: andre@0: if ( flags & PR_SHM_EXCL ) andre@0: oflag |= O_EXCL; andre@0: shm->id = shm_open( shm->ipcname, oflag, shm->mode ); andre@0: } else { andre@0: shm->id = shm_open( shm->ipcname, O_RDWR, shm->mode ); andre@0: } andre@0: andre@0: if ( -1 == shm->id ) { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_OpenSharedMemory(): shm_open failed: %s, OSError: %d", andre@0: shm->ipcname, PR_GetOSError())); andre@0: PR_DELETE( shm->ipcname ); andre@0: PR_DELETE( shm ); andre@0: return(NULL); andre@0: } andre@0: andre@0: end = ftruncate( shm->id, shm->size ); andre@0: if ( -1 == end ) { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_OpenSharedMemory(): ftruncate failed, OSError: %d", andre@0: PR_GetOSError())); andre@0: PR_DELETE( shm->ipcname ); andre@0: PR_DELETE( shm ); andre@0: return(NULL); andre@0: } andre@0: andre@0: return(shm); andre@0: } /* end _MD_OpenSharedMemory() */ andre@0: andre@0: extern void * _MD_AttachSharedMemory( PRSharedMemory *shm, PRIntn flags ) andre@0: { andre@0: void *addr; andre@0: PRIntn prot = (PROT_READ | PROT_WRITE); andre@0: andre@0: PR_ASSERT( shm->ident == _PR_SHM_IDENT ); andre@0: andre@0: if ( PR_SHM_READONLY == flags) andre@0: prot ^= PROT_WRITE; andre@0: andre@0: addr = mmap( (void*)0, shm->size, prot, MAP_SHARED, shm->id, 0 ); andre@0: if ((void*)-1 == addr ) andre@0: { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_AttachSharedMemory(): mmap failed: %s, errno: %d", andre@0: shm->ipcname, PR_GetOSError())); andre@0: addr = NULL; andre@0: } else { andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_AttachSharedMemory(): name: %s, attached at: %p", shm->ipcname, addr)); andre@0: } andre@0: andre@0: return addr; andre@0: } andre@0: andre@0: extern PRStatus _MD_DetachSharedMemory( PRSharedMemory *shm, void *addr ) andre@0: { andre@0: PRStatus rc = PR_SUCCESS; andre@0: PRIntn urc; andre@0: andre@0: PR_ASSERT( shm->ident == _PR_SHM_IDENT ); andre@0: andre@0: urc = munmap( addr, shm->size ); andre@0: if ( -1 == urc ) andre@0: { andre@0: rc = PR_FAILURE; andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DetachSharedMemory(): munmap failed: %s, errno: %d", andre@0: shm->ipcname, PR_GetOSError())); andre@0: } andre@0: return rc; andre@0: } andre@0: andre@0: extern PRStatus _MD_CloseSharedMemory( PRSharedMemory *shm ) andre@0: { andre@0: int urc; andre@0: andre@0: PR_ASSERT( shm->ident == _PR_SHM_IDENT ); andre@0: andre@0: urc = close( shm->id ); andre@0: if ( -1 == urc ) { andre@0: _PR_MD_MAP_CLOSE_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_CloseSharedMemory(): close() failed, error: %d", PR_GetOSError())); andre@0: return(PR_FAILURE); andre@0: } andre@0: PR_DELETE( shm->ipcname ); andre@0: PR_DELETE( shm ); andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: extern PRStatus _MD_DeleteSharedMemory( const char *name ) andre@0: { andre@0: PRStatus rc = PR_SUCCESS; andre@0: PRUintn urc; andre@0: char ipcname[PR_IPC_NAME_SIZE]; andre@0: andre@0: rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm ); andre@0: if ( PR_FAILURE == rc ) andre@0: { andre@0: PR_SetError( PR_UNKNOWN_ERROR , errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_OpenSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name )); andre@0: return rc; andre@0: } andre@0: andre@0: urc = shm_unlink( ipcname ); andre@0: if ( -1 == urc ) { andre@0: rc = PR_FAILURE; andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DeleteSharedMemory(): shm_unlink failed: %s, errno: %d", andre@0: ipcname, PR_GetOSError())); andre@0: } else { andre@0: PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, andre@0: ("_MD_DeleteSharedMemory(): %s, success", ipcname)); andre@0: } andre@0: andre@0: return rc; andre@0: } /* end _MD_DeleteSharedMemory() */ andre@0: #endif andre@0: andre@0: andre@0: andre@0: /* andre@0: ** Unix implementation for anonymous memory (file) mapping andre@0: */ andre@0: extern PRLogModuleInfo *_pr_shma_lm; andre@0: andre@0: #include andre@0: andre@0: extern PRFileMap* _md_OpenAnonFileMap( andre@0: const char *dirName, andre@0: PRSize size, andre@0: PRFileMapProtect prot andre@0: ) andre@0: { andre@0: PRFileMap *fm = NULL; andre@0: PRFileDesc *fd; andre@0: int osfd; andre@0: PRIntn urc; andre@0: PRIntn mode = 0600; andre@0: char *genName; andre@0: pid_t pid = getpid(); /* for generating filename */ andre@0: PRThread *tid = PR_GetCurrentThread(); /* for generating filename */ andre@0: int incr; /* for generating filename */ andre@0: const int maxTries = 20; /* maximum # attempts at a unique filename */ andre@0: PRInt64 size64; /* 64-bit version of 'size' */ andre@0: andre@0: /* andre@0: ** generate a filename from input and runtime environment andre@0: ** open the file, unlink the file. andre@0: ** make maxTries number of attempts at uniqueness in the filename andre@0: */ andre@0: for ( incr = 0; incr < maxTries ; incr++ ) { andre@0: #if defined(SYMBIAN) andre@0: #define NSPR_AFM_FILENAME "%s\\NSPR-AFM-%d-%p.%d" andre@0: #else andre@0: #define NSPR_AFM_FILENAME "%s/.NSPR-AFM-%d-%p.%d" andre@0: #endif andre@0: genName = PR_smprintf( NSPR_AFM_FILENAME, andre@0: dirName, (int) pid, tid, incr ); andre@0: if ( NULL == genName ) { andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): PR_snprintf(): failed, generating filename")); andre@0: goto Finished; andre@0: } andre@0: andre@0: /* create the file */ andre@0: osfd = open( genName, (O_CREAT | O_EXCL | O_RDWR), mode ); andre@0: if ( -1 == osfd ) { andre@0: if ( EEXIST == errno ) { andre@0: PR_smprintf_free( genName ); andre@0: continue; /* name exists, try again */ andre@0: } else { andre@0: _PR_MD_MAP_OPEN_ERROR( errno ); andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): open(): failed, filename: %s, errno: %d", andre@0: genName, PR_GetOSError())); andre@0: PR_smprintf_free( genName ); andre@0: goto Finished; andre@0: } andre@0: } andre@0: break; /* name generation and open successful, break; */ andre@0: } /* end for() */ andre@0: andre@0: if ( incr == maxTries ) { andre@0: PR_ASSERT( -1 == osfd ); andre@0: PR_ASSERT( EEXIST == errno ); andre@0: _PR_MD_MAP_OPEN_ERROR( errno ); andre@0: goto Finished; andre@0: } andre@0: andre@0: urc = unlink( genName ); andre@0: #if defined(SYMBIAN) && defined(__WINS__) andre@0: /* If it is being used by the system or another process, Symbian OS andre@0: * Emulator(WINS) considers this an error. */ andre@0: if ( -1 == urc && EACCES != errno ) { andre@0: #else andre@0: if ( -1 == urc ) { andre@0: #endif andre@0: _PR_MD_MAP_UNLINK_ERROR( errno ); andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): failed on unlink(), errno: %d", errno)); andre@0: PR_smprintf_free( genName ); andre@0: close( osfd ); andre@0: goto Finished; andre@0: } andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): unlink(): %s", genName )); andre@0: andre@0: PR_smprintf_free( genName ); andre@0: andre@0: fd = PR_ImportFile( osfd ); andre@0: if ( NULL == fd ) { andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): PR_ImportFile(): failed")); andre@0: goto Finished; andre@0: } andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): fd: %p", fd )); andre@0: andre@0: urc = ftruncate( fd->secret->md.osfd, size ); andre@0: if ( -1 == urc ) { andre@0: _PR_MD_MAP_DEFAULT_ERROR( errno ); andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): failed on ftruncate(), errno: %d", errno)); andre@0: PR_Close( fd ); andre@0: goto Finished; andre@0: } andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): ftruncate(): size: %d", size )); andre@0: andre@0: LL_UI2L(size64, size); /* PRSize (size_t) is unsigned */ andre@0: fm = PR_CreateFileMap( fd, size64, prot ); andre@0: if ( NULL == fm ) { andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("PR_OpenAnonFileMap(): failed")); andre@0: PR_Close( fd ); andre@0: goto Finished; andre@0: } andre@0: fm->md.isAnonFM = PR_TRUE; /* set fd close */ andre@0: andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_OpenAnonFileMap(): PR_CreateFileMap(): fm: %p", fm )); andre@0: andre@0: Finished: andre@0: return(fm); andre@0: } /* end md_OpenAnonFileMap() */ andre@0: andre@0: /* andre@0: ** _md_ExportFileMapAsString() andre@0: ** andre@0: ** andre@0: */ andre@0: extern PRStatus _md_ExportFileMapAsString( andre@0: PRFileMap *fm, andre@0: PRSize bufSize, andre@0: char *buf andre@0: ) andre@0: { andre@0: PRIntn written; andre@0: PRIntn prot = (PRIntn)fm->prot; andre@0: andre@0: written = PR_snprintf( buf, bufSize, "%ld:%d", andre@0: fm->fd->secret->md.osfd, prot ); andre@0: andre@0: return((written == -1)? PR_FAILURE : PR_SUCCESS); andre@0: } /* end _md_ExportFileMapAsString() */ andre@0: andre@0: andre@0: extern PRFileMap * _md_ImportFileMapFromString( andre@0: const char *fmstring andre@0: ) andre@0: { andre@0: PRStatus rc; andre@0: PRInt32 osfd; andre@0: PRIntn prot; /* really: a PRFileMapProtect */ andre@0: PRFileDesc *fd; andre@0: PRFileMap *fm = NULL; /* default return value */ andre@0: PRFileInfo64 info; andre@0: andre@0: PR_sscanf( fmstring, "%ld:%d", &osfd, &prot ); andre@0: andre@0: /* import the os file descriptor */ andre@0: fd = PR_ImportFile( osfd ); andre@0: if ( NULL == fd ) { andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_ImportFileMapFromString(): PR_ImportFile() failed")); andre@0: goto Finished; andre@0: } andre@0: andre@0: rc = PR_GetOpenFileInfo64( fd, &info ); andre@0: if ( PR_FAILURE == rc ) { andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_ImportFileMapFromString(): PR_GetOpenFileInfo64() failed")); andre@0: goto Finished; andre@0: } andre@0: andre@0: fm = PR_CreateFileMap( fd, info.size, (PRFileMapProtect)prot ); andre@0: if ( NULL == fm ) { andre@0: PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, andre@0: ("_md_ImportFileMapFromString(): PR_CreateFileMap() failed")); andre@0: } andre@0: andre@0: Finished: andre@0: return(fm); andre@0: } /* end _md_ImportFileMapFromString() */