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

Side by Side Diff: base/memory/shared_memory_posix.cc

Issue 27265002: Implement SharedMemory::NewAnonymousReadOnly(contents). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix signedness on Mac Created 7 years, 2 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/memory/shared_memory.h" 5 #include "base/memory/shared_memory.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/mman.h> 9 #include <sys/mman.h>
10 #include <sys/stat.h> 10 #include <sys/stat.h>
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 DCHECK_GE(handle.fd, 0); 94 DCHECK_GE(handle.fd, 0);
95 if (HANDLE_EINTR(close(handle.fd)) < 0) 95 if (HANDLE_EINTR(close(handle.fd)) < 0)
96 DPLOG(ERROR) << "close"; 96 DPLOG(ERROR) << "close";
97 } 97 }
98 98
99 // static 99 // static
100 size_t SharedMemory::GetHandleLimit() { 100 size_t SharedMemory::GetHandleLimit() {
101 return base::GetMaxFds(); 101 return base::GetMaxFds();
102 } 102 }
103 103
104 // static
105 scoped_ptr<SharedMemory> SharedMemory::NewAnonymousReadOnly(
106 StringPiece contents) {
107 // This function theoretically can block on the disk, but realistically
108 // the temporary files we create will just go into the buffer cache
109 // and be deleted before they ever make it out to disk.
110 base::ThreadRestrictions::ScopedAllowIO allow_io;
111
112 FilePath path;
113 file_util::ScopedFILE writable_fp(
114 file_util::CreateAndOpenTemporaryShmemFile(&path, /*executable=*/false));
115 if (!writable_fp) {
116 LOG(ERROR) << "Failed to create temp shmem file.";
117 return scoped_ptr<SharedMemory>();
118 }
119
120 int readonly_fd = HANDLE_EINTR(open(path.value().c_str(), O_RDONLY));
121 if (readonly_fd < 0) {
122 PLOG(ERROR) << "Failed to reopen temp shmem file readonly.";
123 return scoped_ptr<SharedMemory>();
124 }
125 file_util::ScopedFD readonly_fd_closer(&readonly_fd);
126
127 if (unlink(path.value().c_str()))
128 PLOG(WARNING) << "unlink";
129
130 if (fwrite(contents.data(), 1, contents.size(), writable_fp.get()) !=
131 contents.size()) {
132 LOG(ERROR) << "Failed to write " << contents.size()
133 << " bytes to memory block.";
134 return scoped_ptr<SharedMemory>();
135 }
136
137 if (fclose(writable_fp.release()) != 0) {
138 PLOG(ERROR) << "Failed to flush contents of memory block.";
139 return scoped_ptr<SharedMemory>();
140 }
141
142 // Protect against people accidentally reopening as read/write. It's still
143 // possible to intentionally fchmod() the FD back and then open it from /dev,
144 // but the renderer can't open from a filesystem path.
145 if (0 != fchmod(readonly_fd, S_IRUSR))
146 PLOG(DFATAL) << "Failed to make FD " << readonly_fd << " read-only";
147
148 return make_scoped_ptr(new SharedMemory(
149 FileDescriptor(*readonly_fd_closer.release(), /*iauto_close=*/false),
150 /*read_only=*/true));
151 }
152
104 bool SharedMemory::CreateAndMapAnonymous(size_t size) { 153 bool SharedMemory::CreateAndMapAnonymous(size_t size) {
105 return CreateAnonymous(size) && Map(size); 154 return CreateAnonymous(size) && Map(size);
106 } 155 }
107 156
108 #if !defined(OS_ANDROID) 157 #if !defined(OS_ANDROID)
109 // Chromium mostly only uses the unique/private shmem as specified by 158 // Chromium mostly only uses the unique/private shmem as specified by
110 // "name == L"". The exception is in the StatsTable. 159 // "name == L"". The exception is in the StatsTable.
111 // TODO(jrg): there is no way to "clean up" all unused named shmem if 160 // TODO(jrg): there is no way to "clean up" all unused named shmem if
112 // we restart from a crash. (That isn't a new problem, but it is a problem.) 161 // we restart from a crash. (That isn't a new problem, but it is a problem.)
113 // In case we want to delete it later, it may be useful to save the value 162 // In case we want to delete it later, it may be useful to save the value
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 new_handle->fd = new_fd; 459 new_handle->fd = new_fd;
411 new_handle->auto_close = true; 460 new_handle->auto_close = true;
412 461
413 if (close_self) 462 if (close_self)
414 Close(); 463 Close();
415 464
416 return true; 465 return true;
417 } 466 }
418 467
419 } // namespace base 468 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698