bool nsTemporaryFileInputStream::Deserialize(const InputStreamParams& aParams, const FileDescriptorArray& aFileDescriptors) { const TemporaryFileInputStreamParams& params = aParams.get_TemporaryFileInputStreamParams(); uint32_t fileDescriptorIndex = params.fileDescriptorIndex(); FileDescriptor fd; if (fileDescriptorIndex < aFileDescriptors.Length()) { fd = aFileDescriptors[fileDescriptorIndex]; NS_WARN_IF_FALSE(fd.IsValid(), "Received an invalid file descriptor!"); } else { NS_WARNING("Received a bad file descriptor index!"); } if (fd.IsValid()) { auto rawFD = fd.ClonePlatformHandle(); PRFileDesc* fileDesc = PR_ImportFile(PROsfd(rawFD.release())); if (!fileDesc) { NS_WARNING("Failed to import file handle!"); return false; } mFileDescOwner = new FileDescOwner(fileDesc); } else { mClosed = true; } mStartPos = mCurPos = params.startPos(); mEndPos = params.endPos(); return true; }
mozilla::ipc::IPCResult TemporaryIPCBlobParent::RecvOperationDone(const nsCString& aContentType, const FileDescriptor& aFD) { MOZ_ASSERT(mActive); mActive = false; // We have received a file descriptor because in this way we have kept the // file locked on windows during the IPC communication. After the creation of // the TemporaryFileBlobImpl, this prfile can be closed. auto rawFD = aFD.ClonePlatformHandle(); PRFileDesc* prfile = PR_ImportFile(PROsfd(rawFD.release())); // Let's create the BlobImpl. nsCOMPtr<nsIFile> file = Move(mFile); RefPtr<TemporaryFileBlobImpl> blobImpl = new TemporaryFileBlobImpl(file, NS_ConvertUTF8toUTF16(aContentType)); PR_Close(prfile); IPCBlob ipcBlob; nsresult rv = IPCBlobUtils::Serialize(blobImpl, Manager(), ipcBlob); if (NS_WARN_IF(NS_FAILED(rv))) { Unused << Send__delete__(this, NS_ERROR_FAILURE); return IPC_OK(); } Unused << Send__delete__(this, ipcBlob); return IPC_OK(); }
/* static */ already_AddRefed<FileDescriptorOutputStream> FileDescriptorOutputStream::Create( const ipc::FileDescriptor& fileDescriptor) { if (NS_WARN_IF(!fileDescriptor.IsValid())) return nullptr; auto rawFD = fileDescriptor.ClonePlatformHandle(); PRFileDesc* prfd = PR_ImportFile(PROsfd(rawFD.release())); if (NS_WARN_IF(!prfd)) return nullptr; RefPtr<FileDescriptorOutputStream> stream = new FileDescriptorOutputStream(prfd); return stream.forget(); }
/* static */ already_AddRefed<FileDescriptorOutputStream> FileDescriptorOutputStream::Create(const ipc::FileDescriptor& fileDescriptor) { if (NS_WARN_IF(!fileDescriptor.IsValid())) return nullptr; PRFileDesc* prfd = PR_ImportFile(PROsfd(fileDescriptor.PlatformHandle())); if (NS_WARN_IF(!prfd)) return nullptr; nsRefPtr<FileDescriptorOutputStream> stream = new FileDescriptorOutputStream(prfd); return stream.forget(); }
NSAPI_PUBLIC SYS_NETFD net_dup2(SYS_NETFD prfd, int osfd) { SYS_NETFD newfd = NULL; if (prfd && PR_FileDesc2NativeHandle(prfd) != osfd) { if (dup2(PR_FileDesc2NativeHandle(prfd), osfd) != -1) { newfd = PR_ImportFile(osfd); if (!newfd) close(osfd); } else { NsprError::mapUnixErrno(); } } return newfd; }
//----------------------------------------------------------------------------- // FcgiServerChannel::connect //----------------------------------------------------------------------------- PRStatus FcgiServerChannel::connect(PRIntervalTime timeoutVal) { // Try to connect #ifdef XP_WIN32 if (config->udsName) { PRBool pipeBusy = PR_TRUE; while(pipeBusy) { HANDLE newFd = CreateFile(config->procInfo.bindPath, // pipe name GENERIC_READ | GENERIC_WRITE, // read and write access FILE_SHARE_WRITE | FILE_SHARE_READ, // sharing NULL, // default security attributes OPEN_ALWAYS, // opens existing pipe or creates a new one FILE_FLAG_OVERLAPPED, // default attributes NULL); // no template file // Break if the pipe handle is valid. if (newFd != INVALID_HANDLE_VALUE) { fd = PR_ImportFile((int)newFd); return PR_SUCCESS; } if (!WaitNamedPipe(config->procInfo.bindPath, PR_IntervalToMilliseconds(timeoutVal))) { return PR_FAILURE; } if (GetLastError() != ERROR_PIPE_BUSY) { pipeBusy = PR_FALSE; } } return PR_FAILURE; } else { #endif // XP_WIN32 if (!fd) return PR_FAILURE; PRStatus rv = PR_Connect(fd, &(config->procInfo.addr), timeoutVal); return rv; #ifdef XP_WIN32 } #endif // XP_WIN32 }
extern PRFileMap * _md_ImportFileMapFromString( const char *fmstring ) { PRStatus rc; PRInt32 osfd; PRIntn prot; /* really: a PRFileMapProtect */ PRFileDesc *fd; PRFileMap *fm = NULL; /* default return value */ PRFileInfo64 info; PR_sscanf( fmstring, "%ld:%d", &osfd, &prot ); /* import the os file descriptor */ fd = PR_ImportFile( osfd ); if ( NULL == fd ) { PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_ImportFileMapFromString(): PR_ImportFile() failed")); goto Finished; } rc = PR_GetOpenFileInfo64( fd, &info ); if ( PR_FAILURE == rc ) { PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_ImportFileMapFromString(): PR_GetOpenFileInfo64() failed")); goto Finished; } fm = PR_CreateFileMap( fd, info.size, (PRFileMapProtect)prot ); if ( NULL == fm ) { PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("_md_ImportFileMapFromString(): PR_CreateFileMap() failed")); } Finished: return(fm); } /* end _md_ImportFileMapFromString() */
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() */
void RemotePrintJobChild::SetNextPageFD(const mozilla::ipc::FileDescriptor& aFd) { auto handle = aFd.ClonePlatformHandle(); mNextPageFD = PR_ImportFile(PROsfd(handle.release())); }
PR_IMPLEMENT(PRFileDesc *) PR_GetInheritedFD( const char *name) { PRFileDesc *fd; const char *envVar; const char *ptr; int len = strlen(name); PROsfd osfd; int nColons; PRIntn fileType; envVar = PR_GetEnv("NSPR_INHERIT_FDS"); if (NULL == envVar || '\0' == envVar[0]) { PR_SetError(PR_UNKNOWN_ERROR, 0); return NULL; } ptr = envVar; while (1) { if ((ptr[len] == ':') && (strncmp(ptr, name, len) == 0)) { ptr += len + 1; PR_sscanf(ptr, "%d:0x%" PR_SCNxOSFD, &fileType, &osfd); switch ((PRDescType)fileType) { case PR_DESC_FILE: fd = PR_ImportFile(osfd); break; case PR_DESC_PIPE: fd = PR_ImportPipe(osfd); break; case PR_DESC_SOCKET_TCP: fd = PR_ImportTCPSocket(osfd); break; case PR_DESC_SOCKET_UDP: fd = PR_ImportUDPSocket(osfd); break; default: PR_ASSERT(0); PR_SetError(PR_UNKNOWN_ERROR, 0); fd = NULL; break; } if (fd) { /* * An inherited FD is inheritable by default. * The child process needs to call PR_SetFDInheritable * to make it non-inheritable if so desired. */ fd->secret->inheritable = _PR_TRI_TRUE; } return fd; } /* Skip three colons */ nColons = 0; while (*ptr) { if (*ptr == ':') { if (++nColons == 3) { break; } } ptr++; } if (*ptr == '\0') { PR_SetError(PR_UNKNOWN_ERROR, 0); return NULL; } ptr++; } }