Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: mojo/system/raw_shared_buffer_posix.cc

Issue 304233005: Mojo: Implement passing of shared buffers across processes on POSIX. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/system/raw_shared_buffer.cc ('k') | mojo/system/raw_shared_buffer_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/system/raw_shared_buffer.h" 5 #include "mojo/system/raw_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/types.h> // For |off_t|. 11 #include <sys/types.h> // For |off_t|.
11 #include <unistd.h> 12 #include <unistd.h>
12 13
13 #include <limits> 14 #include <limits>
14 15
15 #include "base/file_util.h" 16 #include "base/file_util.h"
16 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
17 #include "base/files/scoped_file.h" 18 #include "base/files/scoped_file.h"
18 #include "base/logging.h" 19 #include "base/logging.h"
19 #include "base/macros.h" 20 #include "base/macros.h"
20 #include "base/posix/eintr_wrapper.h" 21 #include "base/posix/eintr_wrapper.h"
21 #include "base/sys_info.h" 22 #include "base/sys_info.h"
22 #include "base/threading/thread_restrictions.h" 23 #include "base/threading/thread_restrictions.h"
23 #include "mojo/embedder/platform_handle.h" 24 #include "mojo/embedder/platform_handle.h"
24 25
25 // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a 26 // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a
26 // |uint64_t|. 27 // |uint64_t|.
27 COMPILE_ASSERT(sizeof(size_t) <= sizeof(uint64_t), size_t_too_big); 28 COMPILE_ASSERT(sizeof(size_t) <= sizeof(uint64_t), size_t_too_big);
28 COMPILE_ASSERT(sizeof(off_t) <= sizeof(uint64_t), off_t_too_big); 29 COMPILE_ASSERT(sizeof(off_t) <= sizeof(uint64_t), off_t_too_big);
29 30
30 namespace mojo { 31 namespace mojo {
31 namespace system { 32 namespace system {
32 33
33 // RawSharedBuffer ------------------------------------------------------------- 34 // RawSharedBuffer -------------------------------------------------------------
34 35
35 bool RawSharedBuffer::InitNoLock() { 36 bool RawSharedBuffer::Init() {
36 DCHECK(!handle_.is_valid()); 37 DCHECK(!handle_.is_valid());
37 38
38 base::ThreadRestrictions::ScopedAllowIO allow_io; 39 base::ThreadRestrictions::ScopedAllowIO allow_io;
39 40
40 if (static_cast<uint64_t>(num_bytes_) > 41 if (static_cast<uint64_t>(num_bytes_) >
41 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) { 42 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) {
42 return false; 43 return false;
43 } 44 }
44 45
45 // TODO(vtl): This is stupid. The implementation of 46 // TODO(vtl): This is stupid. The implementation of
(...skipping 29 matching lines...) Expand all
75 76
76 if (HANDLE_EINTR(ftruncate(fd.get(), static_cast<off_t>(num_bytes_))) != 0) { 77 if (HANDLE_EINTR(ftruncate(fd.get(), static_cast<off_t>(num_bytes_))) != 0) {
77 PLOG(ERROR) << "ftruncate"; 78 PLOG(ERROR) << "ftruncate";
78 return false; 79 return false;
79 } 80 }
80 81
81 handle_.reset(embedder::PlatformHandle(fd.release())); 82 handle_.reset(embedder::PlatformHandle(fd.release()));
82 return true; 83 return true;
83 } 84 }
84 85
85 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImplNoLock( 86 bool RawSharedBuffer::InitFromPlatformHandle(
86 size_t offset, 87 embedder::ScopedPlatformHandle platform_handle) {
87 size_t length) { 88 DCHECK(!handle_.is_valid());
88 lock_.AssertAcquired();
89 89
90 if (static_cast<uint64_t>(num_bytes_) >
91 static_cast<uint64_t>(std::numeric_limits<off_t>::max())) {
92 return false;
93 }
94
95 struct stat sb = {};
96 // Note: |fstat()| isn't interruptible.
97 if (fstat(platform_handle.get().fd, &sb) != 0) {
98 PLOG(ERROR) << "fstat";
99 return false;
100 }
101
102 if (!S_ISREG(sb.st_mode)) {
103 LOG(ERROR) << "Platform handle not to a regular file";
104 return false;
105 }
106
107 if (sb.st_size != static_cast<off_t>(num_bytes_)) {
108 LOG(ERROR) << "Shared memory file has the wrong size";
109 return false;
110 }
111
112 // TODO(vtl): More checks?
113
114 handle_ = platform_handle.Pass();
115 return true;
116 }
117
118 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImpl(size_t offset,
119 size_t length) {
90 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); 120 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity();
91 size_t real_offset = offset - offset_rounding; 121 size_t real_offset = offset - offset_rounding;
92 size_t real_length = length + offset_rounding; 122 size_t real_length = length + offset_rounding;
93 123
94 // This should hold (since we checked |num_bytes| versus the maximum value of 124 // This should hold (since we checked |num_bytes| versus the maximum value of
95 // |off_t| on creation, but it never hurts to be paranoid. 125 // |off_t| on creation, but it never hurts to be paranoid.
96 DCHECK_LE(static_cast<uint64_t>(real_offset), 126 DCHECK_LE(static_cast<uint64_t>(real_offset),
97 static_cast<uint64_t>(std::numeric_limits<off_t>::max())); 127 static_cast<uint64_t>(std::numeric_limits<off_t>::max()));
98 128
99 void* real_base = mmap(NULL, real_length, PROT_READ | PROT_WRITE, MAP_SHARED, 129 void* real_base = mmap(NULL, real_length, PROT_READ | PROT_WRITE, MAP_SHARED,
(...skipping 12 matching lines...) Expand all
112 142
113 // RawSharedBufferMapping ------------------------------------------------------ 143 // RawSharedBufferMapping ------------------------------------------------------
114 144
115 void RawSharedBufferMapping::Unmap() { 145 void RawSharedBufferMapping::Unmap() {
116 int result = munmap(real_base_, real_length_); 146 int result = munmap(real_base_, real_length_);
117 PLOG_IF(ERROR, result != 0) << "munmap"; 147 PLOG_IF(ERROR, result != 0) << "munmap";
118 } 148 }
119 149
120 } // namespace system 150 } // namespace system
121 } // namespace mojo 151 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/raw_shared_buffer.cc ('k') | mojo/system/raw_shared_buffer_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698