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/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" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
86 bool RawSharedBuffer::InitFromPlatformHandleNoLock( | |
87 embedder::ScopedPlatformHandle platform_handle) { | |
88 DCHECK(!handle_.is_valid()); | |
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"; | |
yzshen1
2014/05/30 00:09:35
nit: maybe also output the errno?
viettrungluu
2014/05/30 00:23:42
That's what PLOG does.
yzshen1
2014/05/30 00:27:48
Right. Didn't notice that. Thanks.
| |
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 | |
85 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImplNoLock( | 118 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImplNoLock( |
86 size_t offset, | 119 size_t offset, |
87 size_t length) { | 120 size_t length) { |
88 lock_.AssertAcquired(); | 121 lock_.AssertAcquired(); |
89 | 122 |
90 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | 123 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); |
91 size_t real_offset = offset - offset_rounding; | 124 size_t real_offset = offset - offset_rounding; |
92 size_t real_length = length + offset_rounding; | 125 size_t real_length = length + offset_rounding; |
93 | 126 |
94 // This should hold (since we checked |num_bytes| versus the maximum value of | 127 // This should hold (since we checked |num_bytes| versus the maximum value of |
(...skipping 17 matching lines...) Expand all Loading... | |
112 | 145 |
113 // RawSharedBufferMapping ------------------------------------------------------ | 146 // RawSharedBufferMapping ------------------------------------------------------ |
114 | 147 |
115 void RawSharedBufferMapping::Unmap() { | 148 void RawSharedBufferMapping::Unmap() { |
116 int result = munmap(real_base_, real_length_); | 149 int result = munmap(real_base_, real_length_); |
117 PLOG_IF(ERROR, result != 0) << "munmap"; | 150 PLOG_IF(ERROR, result != 0) << "munmap"; |
118 } | 151 } |
119 | 152 |
120 } // namespace system | 153 } // namespace system |
121 } // namespace mojo | 154 } // namespace mojo |
OLD | NEW |