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