Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/shared_memory.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <errno.h> |
michael@0 | 8 | #include <fcntl.h> |
michael@0 | 9 | #include <sys/mman.h> |
michael@0 | 10 | #include <sys/stat.h> |
michael@0 | 11 | #include <unistd.h> |
michael@0 | 12 | |
michael@0 | 13 | #include "base/file_util.h" |
michael@0 | 14 | #include "base/logging.h" |
michael@0 | 15 | #include "base/platform_thread.h" |
michael@0 | 16 | #include "base/string_util.h" |
michael@0 | 17 | |
michael@0 | 18 | namespace base { |
michael@0 | 19 | |
michael@0 | 20 | SharedMemory::SharedMemory() |
michael@0 | 21 | : mapped_file_(-1), |
michael@0 | 22 | inode_(0), |
michael@0 | 23 | memory_(NULL), |
michael@0 | 24 | read_only_(false), |
michael@0 | 25 | max_size_(0) { |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
michael@0 | 29 | : mapped_file_(handle.fd), |
michael@0 | 30 | inode_(0), |
michael@0 | 31 | memory_(NULL), |
michael@0 | 32 | read_only_(read_only), |
michael@0 | 33 | max_size_(0) { |
michael@0 | 34 | struct stat st; |
michael@0 | 35 | if (fstat(handle.fd, &st) == 0) { |
michael@0 | 36 | // If fstat fails, then the file descriptor is invalid and we'll learn this |
michael@0 | 37 | // fact when Map() fails. |
michael@0 | 38 | inode_ = st.st_ino; |
michael@0 | 39 | } |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
michael@0 | 43 | ProcessHandle process) |
michael@0 | 44 | : mapped_file_(handle.fd), |
michael@0 | 45 | memory_(NULL), |
michael@0 | 46 | read_only_(read_only), |
michael@0 | 47 | max_size_(0) { |
michael@0 | 48 | // We don't handle this case yet (note the ignored parameter); let's die if |
michael@0 | 49 | // someone comes calling. |
michael@0 | 50 | NOTREACHED(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | SharedMemory::~SharedMemory() { |
michael@0 | 54 | Close(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // static |
michael@0 | 58 | bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
michael@0 | 59 | return handle.fd >= 0; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | // static |
michael@0 | 63 | SharedMemoryHandle SharedMemory::NULLHandle() { |
michael@0 | 64 | return SharedMemoryHandle(); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | bool SharedMemory::Create(const std::string &cname, bool read_only, |
michael@0 | 68 | bool open_existing, size_t size) { |
michael@0 | 69 | read_only_ = read_only; |
michael@0 | 70 | |
michael@0 | 71 | std::wstring name = UTF8ToWide(cname); |
michael@0 | 72 | |
michael@0 | 73 | int posix_flags = 0; |
michael@0 | 74 | posix_flags |= read_only ? O_RDONLY : O_RDWR; |
michael@0 | 75 | if (!open_existing || mapped_file_ <= 0) |
michael@0 | 76 | posix_flags |= O_CREAT; |
michael@0 | 77 | |
michael@0 | 78 | if (!CreateOrOpen(name, posix_flags, size)) |
michael@0 | 79 | return false; |
michael@0 | 80 | |
michael@0 | 81 | max_size_ = size; |
michael@0 | 82 | return true; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | // Our current implementation of shmem is with mmap()ing of files. |
michael@0 | 86 | // These files need to be deleted explicitly. |
michael@0 | 87 | // In practice this call is only needed for unit tests. |
michael@0 | 88 | bool SharedMemory::Delete(const std::wstring& name) { |
michael@0 | 89 | std::wstring mem_filename; |
michael@0 | 90 | if (FilenameForMemoryName(name, &mem_filename) == false) |
michael@0 | 91 | return false; |
michael@0 | 92 | |
michael@0 | 93 | FilePath path(WideToUTF8(mem_filename)); |
michael@0 | 94 | if (file_util::PathExists(path)) { |
michael@0 | 95 | return file_util::Delete(path, false); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | // Doesn't exist, so success. |
michael@0 | 99 | return true; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | bool SharedMemory::Open(const std::wstring &name, bool read_only) { |
michael@0 | 103 | read_only_ = read_only; |
michael@0 | 104 | |
michael@0 | 105 | int posix_flags = 0; |
michael@0 | 106 | posix_flags |= read_only ? O_RDONLY : O_RDWR; |
michael@0 | 107 | |
michael@0 | 108 | return CreateOrOpen(name, posix_flags, 0); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | // For the given shmem named |memname|, return a filename to mmap() |
michael@0 | 112 | // (and possibly create). Modifies |filename|. Return false on |
michael@0 | 113 | // error, or true of we are happy. |
michael@0 | 114 | bool SharedMemory::FilenameForMemoryName(const std::wstring &memname, |
michael@0 | 115 | std::wstring *filename) { |
michael@0 | 116 | std::wstring mem_filename; |
michael@0 | 117 | |
michael@0 | 118 | // mem_name will be used for a filename; make sure it doesn't |
michael@0 | 119 | // contain anything which will confuse us. |
michael@0 | 120 | DCHECK(memname.find_first_of(L"/") == std::string::npos); |
michael@0 | 121 | DCHECK(memname.find_first_of(L"\0") == std::string::npos); |
michael@0 | 122 | |
michael@0 | 123 | FilePath temp_dir; |
michael@0 | 124 | if (file_util::GetShmemTempDir(&temp_dir) == false) |
michael@0 | 125 | return false; |
michael@0 | 126 | |
michael@0 | 127 | mem_filename = UTF8ToWide(temp_dir.value()); |
michael@0 | 128 | file_util::AppendToPath(&mem_filename, L"com.google.chrome.shmem." + memname); |
michael@0 | 129 | *filename = mem_filename; |
michael@0 | 130 | return true; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | namespace { |
michael@0 | 134 | |
michael@0 | 135 | // A class to handle auto-closing of FILE*'s. |
michael@0 | 136 | class ScopedFILEClose { |
michael@0 | 137 | public: |
michael@0 | 138 | inline void operator()(FILE* x) const { |
michael@0 | 139 | if (x) { |
michael@0 | 140 | fclose(x); |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | }; |
michael@0 | 144 | |
michael@0 | 145 | typedef scoped_ptr_malloc<FILE, ScopedFILEClose> ScopedFILE; |
michael@0 | 146 | |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | // Chromium mostly only use the unique/private shmem as specified by |
michael@0 | 150 | // "name == L"". The exception is in the StatsTable. |
michael@0 | 151 | // TODO(jrg): there is no way to "clean up" all unused named shmem if |
michael@0 | 152 | // we restart from a crash. (That isn't a new problem, but it is a problem.) |
michael@0 | 153 | // In case we want to delete it later, it may be useful to save the value |
michael@0 | 154 | // of mem_filename after FilenameForMemoryName(). |
michael@0 | 155 | bool SharedMemory::CreateOrOpen(const std::wstring &name, |
michael@0 | 156 | int posix_flags, size_t size) { |
michael@0 | 157 | DCHECK(mapped_file_ == -1); |
michael@0 | 158 | |
michael@0 | 159 | ScopedFILE file_closer; |
michael@0 | 160 | FILE *fp; |
michael@0 | 161 | |
michael@0 | 162 | if (name == L"") { |
michael@0 | 163 | // It doesn't make sense to have a read-only private piece of shmem |
michael@0 | 164 | DCHECK(posix_flags & (O_RDWR | O_WRONLY)); |
michael@0 | 165 | |
michael@0 | 166 | FilePath path; |
michael@0 | 167 | fp = file_util::CreateAndOpenTemporaryShmemFile(&path); |
michael@0 | 168 | |
michael@0 | 169 | // Deleting the file prevents anyone else from mapping it in |
michael@0 | 170 | // (making it private), and prevents the need for cleanup (once |
michael@0 | 171 | // the last fd is closed, it is truly freed). |
michael@0 | 172 | file_util::Delete(path, false); |
michael@0 | 173 | } else { |
michael@0 | 174 | std::wstring mem_filename; |
michael@0 | 175 | if (FilenameForMemoryName(name, &mem_filename) == false) |
michael@0 | 176 | return false; |
michael@0 | 177 | |
michael@0 | 178 | std::string mode; |
michael@0 | 179 | switch (posix_flags) { |
michael@0 | 180 | case (O_RDWR | O_CREAT): |
michael@0 | 181 | // Careful: "w+" will truncate if it already exists. |
michael@0 | 182 | mode = "a+"; |
michael@0 | 183 | break; |
michael@0 | 184 | case O_RDWR: |
michael@0 | 185 | mode = "r+"; |
michael@0 | 186 | break; |
michael@0 | 187 | case O_RDONLY: |
michael@0 | 188 | mode = "r"; |
michael@0 | 189 | break; |
michael@0 | 190 | default: |
michael@0 | 191 | NOTIMPLEMENTED(); |
michael@0 | 192 | break; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | fp = file_util::OpenFile(mem_filename, mode.c_str()); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | if (fp == NULL) |
michael@0 | 199 | return false; |
michael@0 | 200 | file_closer.reset(fp); // close when we go out of scope |
michael@0 | 201 | |
michael@0 | 202 | // Make sure the (new) file is the right size. |
michael@0 | 203 | // According to the man page, "Use of truncate() to extend a file is |
michael@0 | 204 | // not portable." |
michael@0 | 205 | if (size && (posix_flags & (O_RDWR | O_CREAT))) { |
michael@0 | 206 | // Get current size. |
michael@0 | 207 | struct stat stat; |
michael@0 | 208 | if (fstat(fileno(fp), &stat) != 0) |
michael@0 | 209 | return false; |
michael@0 | 210 | size_t current_size = stat.st_size; |
michael@0 | 211 | if (current_size != size) { |
michael@0 | 212 | if (ftruncate(fileno(fp), size) != 0) |
michael@0 | 213 | return false; |
michael@0 | 214 | if (fseeko(fp, size, SEEK_SET) != 0) |
michael@0 | 215 | return false; |
michael@0 | 216 | } |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | mapped_file_ = dup(fileno(fp)); |
michael@0 | 220 | DCHECK(mapped_file_ >= 0); |
michael@0 | 221 | |
michael@0 | 222 | struct stat st; |
michael@0 | 223 | if (fstat(mapped_file_, &st)) |
michael@0 | 224 | NOTREACHED(); |
michael@0 | 225 | inode_ = st.st_ino; |
michael@0 | 226 | |
michael@0 | 227 | return true; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | bool SharedMemory::Map(size_t bytes) { |
michael@0 | 231 | if (mapped_file_ == -1) |
michael@0 | 232 | return false; |
michael@0 | 233 | |
michael@0 | 234 | memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
michael@0 | 235 | MAP_SHARED, mapped_file_, 0); |
michael@0 | 236 | |
michael@0 | 237 | if (memory_) |
michael@0 | 238 | max_size_ = bytes; |
michael@0 | 239 | |
michael@0 | 240 | bool mmap_succeeded = (memory_ != (void*)-1); |
michael@0 | 241 | DCHECK(mmap_succeeded) << "Call to mmap failed, errno=" << errno; |
michael@0 | 242 | return mmap_succeeded; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | bool SharedMemory::Unmap() { |
michael@0 | 246 | if (memory_ == NULL) |
michael@0 | 247 | return false; |
michael@0 | 248 | |
michael@0 | 249 | munmap(memory_, max_size_); |
michael@0 | 250 | memory_ = NULL; |
michael@0 | 251 | max_size_ = 0; |
michael@0 | 252 | return true; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
michael@0 | 256 | SharedMemoryHandle *new_handle, |
michael@0 | 257 | bool close_self) { |
michael@0 | 258 | const int new_fd = dup(mapped_file_); |
michael@0 | 259 | DCHECK(new_fd >= -1); |
michael@0 | 260 | new_handle->fd = new_fd; |
michael@0 | 261 | new_handle->auto_close = true; |
michael@0 | 262 | |
michael@0 | 263 | if (close_self) |
michael@0 | 264 | Close(); |
michael@0 | 265 | |
michael@0 | 266 | return true; |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | |
michael@0 | 270 | void SharedMemory::Close() { |
michael@0 | 271 | Unmap(); |
michael@0 | 272 | |
michael@0 | 273 | if (mapped_file_ > 0) { |
michael@0 | 274 | close(mapped_file_); |
michael@0 | 275 | mapped_file_ = -1; |
michael@0 | 276 | } |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | #ifdef ANDROID |
michael@0 | 280 | void SharedMemory::LockOrUnlockCommon(int function) { |
michael@0 | 281 | DCHECK(mapped_file_ >= 0); |
michael@0 | 282 | struct flock lockreq; |
michael@0 | 283 | lockreq.l_type = function; |
michael@0 | 284 | lockreq.l_whence = SEEK_SET; |
michael@0 | 285 | lockreq.l_start = 0; |
michael@0 | 286 | lockreq.l_len = 0; |
michael@0 | 287 | while (fcntl(mapped_file_, F_SETLKW, &lockreq) < 0) { |
michael@0 | 288 | if (errno == EINTR) { |
michael@0 | 289 | continue; |
michael@0 | 290 | } else if (errno == ENOLCK) { |
michael@0 | 291 | // temporary kernel resource exaustion |
michael@0 | 292 | PlatformThread::Sleep(500); |
michael@0 | 293 | continue; |
michael@0 | 294 | } else { |
michael@0 | 295 | NOTREACHED() << "lockf() failed." |
michael@0 | 296 | << " function:" << function |
michael@0 | 297 | << " fd:" << mapped_file_ |
michael@0 | 298 | << " errno:" << errno |
michael@0 | 299 | << " msg:" << strerror(errno); |
michael@0 | 300 | } |
michael@0 | 301 | } |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | void SharedMemory::Lock() { |
michael@0 | 305 | LockOrUnlockCommon(F_WRLCK); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | void SharedMemory::Unlock() { |
michael@0 | 309 | LockOrUnlockCommon(F_UNLCK); |
michael@0 | 310 | } |
michael@0 | 311 | #else |
michael@0 | 312 | void SharedMemory::LockOrUnlockCommon(int function) { |
michael@0 | 313 | DCHECK(mapped_file_ >= 0); |
michael@0 | 314 | while (lockf(mapped_file_, function, 0) < 0) { |
michael@0 | 315 | if (errno == EINTR) { |
michael@0 | 316 | continue; |
michael@0 | 317 | } else if (errno == ENOLCK) { |
michael@0 | 318 | // temporary kernel resource exaustion |
michael@0 | 319 | PlatformThread::Sleep(500); |
michael@0 | 320 | continue; |
michael@0 | 321 | } else { |
michael@0 | 322 | NOTREACHED() << "lockf() failed." |
michael@0 | 323 | << " function:" << function |
michael@0 | 324 | << " fd:" << mapped_file_ |
michael@0 | 325 | << " errno:" << errno |
michael@0 | 326 | << " msg:" << strerror(errno); |
michael@0 | 327 | } |
michael@0 | 328 | } |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | void SharedMemory::Lock() { |
michael@0 | 332 | LockOrUnlockCommon(F_LOCK); |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | void SharedMemory::Unlock() { |
michael@0 | 336 | LockOrUnlockCommon(F_ULOCK); |
michael@0 | 337 | } |
michael@0 | 338 | #endif |
michael@0 | 339 | |
michael@0 | 340 | SharedMemoryHandle SharedMemory::handle() const { |
michael@0 | 341 | return FileDescriptor(mapped_file_, false); |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | } // namespace base |