[Boost-bugs] [Boost C++ Libraries] #12947: Failed to use memory-mapped file to interprocess communications

Subject: [Boost-bugs] [Boost C++ Libraries] #12947: Failed to use memory-mapped file to interprocess communications
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2017-04-04 14:46:04


#12947: Failed to use memory-mapped file to interprocess communications
---------------------------------------------+-----------------------
 Reporter: Valeriy N. Sifurov <vsifurov@…> | Owner: turkanis
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: iostreams
  Version: Boost 1.63.0 | Severity: Problem
 Keywords: |
---------------------------------------------+-----------------------
 In first process we create and write the memory-mapped file through
 boost::iostreams::mapped_file_sink with
 boost::iostreams::mapped_file::readwrite flag. In seconde process we
 attempt to open the same memory-mapped file for read through
 boost::iostreams::mapped_file_source with
 boost::iostreams::mapped_file::readonly flag. The aptempt has failed for
 second process. Windows error code 32 (The process cannot access the file
 because it is being used by another process).
 In source code of iostream library we discover the next code that attempt
 to open file (mapped_file.cpp, void mapped_file_impl::open_file(param_type
 p)):

 DWORD dwDesiredAccess =
         readonly ?
             GENERIC_READ :
             (GENERIC_READ | GENERIC_WRITE);
     DWORD dwCreationDisposition = (p.new_file_size != 0 && !readonly) ?
         CREATE_ALWAYS :
         OPEN_EXISTING;
     DWORD dwFlagsandAttributes =
         readonly ?
             FILE_ATTRIBUTE_READONLY :
             FILE_ATTRIBUTE_TEMPORARY;
     handle_ = p.path.is_wide() ?
         ::CreateFileW(
             p.path.c_wstr(),
             dwDesiredAccess,
             FILE_SHARE_READ,
             NULL,
             dwCreationDisposition,
             dwFlagsandAttributes,
             NULL ) :
         ::CreateFileA(
             p.path.c_str(),
             dwDesiredAccess,
             FILE_SHARE_READ,
             NULL,
             dwCreationDisposition,
             dwFlagsandAttributes,
             NULL );
     if (handle_ == INVALID_HANDLE_VALUE)
         cleanup_and_throw("failed opening file");


 In both cases (opening the file for write of for read) the dwShareMode
 flag has FILE_SHARE_READ value. This is wrong!
 Using the rules that dуscraybed on page https://msdn.microsoft.com/en-
 us/library/windows/desktop/aa363874(v=vs.85).aspx we corrected the code of
 function:

 DWORD dwDesiredAccess =
         readonly ?
             GENERIC_READ :
             (GENERIC_READ | GENERIC_WRITE);
     DWORD dwCreationDisposition = (p.new_file_size != 0 && !readonly) ?
         CREATE_ALWAYS :
         OPEN_EXISTING;
     DWORD dwFlagsandAttributes =
         readonly ?
             FILE_ATTRIBUTE_READONLY :
             FILE_ATTRIBUTE_TEMPORARY;
     handle_ = p.path.is_wide() ?
         ::CreateFileW(
             p.path.c_wstr(),
             dwDesiredAccess,
             !readonly ? FILE_SHARE_READ : FILE_SHARE_READ |
 FILE_SHARE_WRITE | FILE_SHARE_DELETE,
             NULL,
             dwCreationDisposition,
             dwFlagsandAttributes,
             NULL ) :
         ::CreateFileA(
             p.path.c_str(),
             dwDesiredAccess,
             !readonly ? FILE_SHARE_READ : FILE_SHARE_READ |
 FILE_SHARE_WRITE | FILE_SHARE_DELETE,
             NULL,
             dwCreationDisposition,
             dwFlagsandAttributes,
             NULL );
     if (handle_ == INVALID_HANDLE_VALUE)
         cleanup_and_throw("failed opening file");

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12947>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-04-04 14:51:13 UTC