| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" | 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" | 
| 6 | 6 | 
| 7 #include <stdint.h> | 7 #include <stdint.h> | 
| 8 #include <stdio.h>     // For |fileno()|. | 8 #include <stdio.h>     // For |fileno()|. | 
| 9 #include <sys/mman.h>  // For |mmap()|/|munmap()|. | 9 #include <sys/mman.h>  // For |mmap()|/|munmap()|. | 
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> | 
| 11 #include <sys/types.h>  // For |off_t|. | 11 #include <sys/types.h>  // For |off_t|. | 
| 12 #include <unistd.h> | 12 #include <unistd.h> | 
| 13 | 13 | 
| 14 #include <limits> | 14 #include <limits> | 
| 15 | 15 | 
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" | 
| 17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" | 
| 18 #include "base/files/scoped_file.h" |  | 
| 19 #include "base/logging.h" | 18 #include "base/logging.h" | 
| 20 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" | 
| 21 #include "base/sys_info.h" | 20 #include "base/sys_info.h" | 
| 22 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" | 
|  | 22 #include "mojo/edk/util/scoped_file.h" | 
| 23 | 23 | 
| 24 // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a | 24 // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a | 
| 25 // |uint64_t|. | 25 // |uint64_t|. | 
| 26 static_assert(sizeof(size_t) <= sizeof(uint64_t), "size_t too big"); | 26 static_assert(sizeof(size_t) <= sizeof(uint64_t), "size_t too big"); | 
| 27 static_assert(sizeof(off_t) <= sizeof(uint64_t), "off_t too big"); | 27 static_assert(sizeof(off_t) <= sizeof(uint64_t), "off_t too big"); | 
| 28 | 28 | 
| 29 namespace mojo { | 29 namespace mojo { | 
| 30 namespace embedder { | 30 namespace embedder { | 
| 31 | 31 | 
| 32 // SimplePlatformSharedBuffer -------------------------------------------------- | 32 // SimplePlatformSharedBuffer -------------------------------------------------- | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
| 49   // |CreateAndOpenTemporaryFileInDir()| starts with an FD, |fdopen()|s to get a | 49   // |CreateAndOpenTemporaryFileInDir()| starts with an FD, |fdopen()|s to get a | 
| 50   // |FILE*|, and then we have to |dup(fileno(fp))| to get back to an FD that we | 50   // |FILE*|, and then we have to |dup(fileno(fp))| to get back to an FD that we | 
| 51   // can own. (base/memory/shared_memory_posix.cc does this too, with more | 51   // can own. (base/memory/shared_memory_posix.cc does this too, with more | 
| 52   // |fstat()|s thrown in for good measure.) | 52   // |fstat()|s thrown in for good measure.) | 
| 53   base::FilePath shared_buffer_dir; | 53   base::FilePath shared_buffer_dir; | 
| 54   if (!base::GetShmemTempDir(false, &shared_buffer_dir)) { | 54   if (!base::GetShmemTempDir(false, &shared_buffer_dir)) { | 
| 55     LOG(ERROR) << "Failed to get temporary directory for shared memory"; | 55     LOG(ERROR) << "Failed to get temporary directory for shared memory"; | 
| 56     return false; | 56     return false; | 
| 57   } | 57   } | 
| 58   base::FilePath shared_buffer_file; | 58   base::FilePath shared_buffer_file; | 
| 59   base::ScopedFILE fp(base::CreateAndOpenTemporaryFileInDir( | 59   util::ScopedFILE fp(base::CreateAndOpenTemporaryFileInDir( | 
| 60       shared_buffer_dir, &shared_buffer_file)); | 60       shared_buffer_dir, &shared_buffer_file)); | 
| 61   if (!fp) { | 61   if (!fp) { | 
| 62     LOG(ERROR) << "Failed to create/open temporary file for shared memory"; | 62     LOG(ERROR) << "Failed to create/open temporary file for shared memory"; | 
| 63     return false; | 63     return false; | 
| 64   } | 64   } | 
| 65   // Note: |unlink()| is not interruptible. | 65   // Note: |unlink()| is not interruptible. | 
| 66   if (unlink(shared_buffer_file.value().c_str()) != 0) { | 66   if (unlink(shared_buffer_file.value().c_str()) != 0) { | 
| 67     PLOG(WARNING) << "unlink"; | 67     PLOG(WARNING) << "unlink"; | 
| 68     // This isn't "fatal" (e.g., someone else may have unlinked the file first), | 68     // This isn't "fatal" (e.g., someone else may have unlinked the file first), | 
| 69     // so we may as well continue. | 69     // so we may as well continue. | 
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 149 | 149 | 
| 150 // SimplePlatformSharedBufferMapping ------------------------------------------- | 150 // SimplePlatformSharedBufferMapping ------------------------------------------- | 
| 151 | 151 | 
| 152 void SimplePlatformSharedBufferMapping::Unmap() { | 152 void SimplePlatformSharedBufferMapping::Unmap() { | 
| 153   int result = munmap(real_base_, real_length_); | 153   int result = munmap(real_base_, real_length_); | 
| 154   PLOG_IF(ERROR, result != 0) << "munmap"; | 154   PLOG_IF(ERROR, result != 0) << "munmap"; | 
| 155 } | 155 } | 
| 156 | 156 | 
| 157 }  // namespace embedder | 157 }  // namespace embedder | 
| 158 }  // namespace mojo | 158 }  // namespace mojo | 
| OLD | NEW | 
|---|