license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 4 | |
| 5 | #include "base/shared_memory.h" |
| 6 | |
tc@google.com | 7e1fb1d | 2008-08-13 07:40:21 +0900 | [diff] [blame] | 7 | #include <fcntl.h> |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 8 | #include <sys/mman.h> |
| 9 | |
| 10 | #include "base/logging.h" |
| 11 | #include "base/string_util.h" |
| 12 | |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 13 | namespace base { |
| 14 | |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 15 | namespace { |
avi@google.com | 9e0cc15 | 2008-08-14 04:59:07 +0900 | [diff] [blame] | 16 | // Paranoia. Semaphores and shared memory segments should live in different |
| 17 | // namespaces, but who knows what's out there. |
| 18 | const char kSemaphoreSuffix[] = "-sem"; |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | SharedMemory::SharedMemory() |
| 22 | : mapped_file_(-1), |
| 23 | memory_(NULL), |
| 24 | read_only_(false), |
| 25 | max_size_(0), |
| 26 | lock_(NULL) { |
| 27 | } |
| 28 | |
| 29 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 30 | : mapped_file_(handle), |
| 31 | memory_(NULL), |
| 32 | read_only_(read_only), |
| 33 | max_size_(0), |
| 34 | lock_(NULL) { |
| 35 | } |
| 36 | |
| 37 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 38 | ProcessHandle process) |
| 39 | : mapped_file_(handle), |
| 40 | memory_(NULL), |
| 41 | read_only_(read_only), |
| 42 | max_size_(0), |
| 43 | lock_(NULL) { |
| 44 | // We don't handle this case yet (note the ignored parameter); let's die if |
| 45 | // someone comes calling. |
| 46 | NOTREACHED(); |
| 47 | } |
| 48 | |
| 49 | SharedMemory::~SharedMemory() { |
| 50 | Close(); |
| 51 | if (lock_ != NULL) |
| 52 | sem_close(lock_); |
| 53 | } |
| 54 | |
| 55 | bool SharedMemory::Create(const std::wstring &name, bool read_only, |
| 56 | bool open_existing, size_t size) { |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 57 | read_only_ = read_only; |
| 58 | |
| 59 | int posix_flags = 0; |
| 60 | posix_flags |= read_only ? O_RDONLY : O_RDWR; |
pinkerton@google.com | 38e8681 | 2008-09-24 00:29:16 +0900 | [diff] [blame] | 61 | if (!open_existing || mapped_file_ <= 0) |
avi@google.com | 9e0cc15 | 2008-08-14 04:59:07 +0900 | [diff] [blame] | 62 | posix_flags |= O_CREAT; |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 63 | |
avi@google.com | 9e0cc15 | 2008-08-14 04:59:07 +0900 | [diff] [blame] | 64 | if (CreateOrOpen(name, posix_flags)) { |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 65 | if (0 == ftruncate(mapped_file_, size)) { |
| 66 | max_size_ = size; |
| 67 | return true; |
| 68 | } else { |
| 69 | Close(); |
| 70 | return false; |
| 71 | } |
avi@google.com | 9e0cc15 | 2008-08-14 04:59:07 +0900 | [diff] [blame] | 72 | } |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 73 | |
avi@google.com | 9e0cc15 | 2008-08-14 04:59:07 +0900 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
| 77 | bool SharedMemory::Open(const std::wstring &name, bool read_only) { |
| 78 | read_only_ = read_only; |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 79 | |
avi@google.com | 9e0cc15 | 2008-08-14 04:59:07 +0900 | [diff] [blame] | 80 | int posix_flags = 0; |
| 81 | posix_flags |= read_only ? O_RDONLY : O_RDWR; |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 82 | |
avi@google.com | 9e0cc15 | 2008-08-14 04:59:07 +0900 | [diff] [blame] | 83 | return CreateOrOpen(name, posix_flags); |
| 84 | } |
| 85 | |
| 86 | bool SharedMemory::CreateOrOpen(const std::wstring &name, int posix_flags) { |
| 87 | DCHECK(mapped_file_ == -1); |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 88 | |
avi@google.com | 9e0cc15 | 2008-08-14 04:59:07 +0900 | [diff] [blame] | 89 | name_ = L"/" + name; |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 90 | |
brettw@google.com | 4a6795c | 2008-10-30 02:01:22 +0900 | [diff] [blame] | 91 | mode_t posix_mode = S_IRUSR | S_IWUSR; // owner read/write |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 92 | std::string posix_name(WideToUTF8(name_)); |
| 93 | mapped_file_ = shm_open(posix_name.c_str(), posix_flags, posix_mode); |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 94 | if (mapped_file_ < 0) |
| 95 | return false; |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 96 | |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 97 | posix_name += kSemaphoreSuffix; |
| 98 | lock_ = sem_open(posix_name.c_str(), O_CREAT, posix_mode, 1); |
| 99 | if (lock_ == SEM_FAILED) { |
| 100 | close(mapped_file_); |
| 101 | mapped_file_ = -1; |
| 102 | shm_unlink(posix_name.c_str()); |
| 103 | lock_ = NULL; |
| 104 | return false; |
| 105 | } |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 106 | |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 110 | bool SharedMemory::Map(size_t bytes) { |
| 111 | if (mapped_file_ == -1) |
| 112 | return false; |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 113 | |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 114 | memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
| 115 | MAP_SHARED, mapped_file_, 0); |
| 116 | |
| 117 | if (memory_) |
| 118 | max_size_ = bytes; |
| 119 | |
| 120 | return (memory_ != NULL); |
| 121 | } |
| 122 | |
| 123 | bool SharedMemory::Unmap() { |
| 124 | if (memory_ == NULL) |
| 125 | return false; |
| 126 | |
| 127 | munmap(memory_, max_size_); |
| 128 | memory_ = NULL; |
| 129 | max_size_ = 0; |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 134 | SharedMemoryHandle *new_handle, |
| 135 | bool close_self) { |
| 136 | *new_handle = 0; |
| 137 | // TODO(awalker): figure out if we need this, and do the appropriate |
| 138 | // VM magic if so. |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | void SharedMemory::Close() { |
| 144 | if (memory_ != NULL) { |
| 145 | munmap(memory_, max_size_); |
| 146 | |
| 147 | memory_ = NULL; |
| 148 | max_size_ = 0; |
| 149 | } |
| 150 | |
| 151 | std::string posix_name(WideToUTF8(name_)); |
brettw@google.com | 4a6795c | 2008-10-30 02:01:22 +0900 | [diff] [blame] | 152 | if (mapped_file_ > 0) { |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 153 | close(mapped_file_); |
| 154 | shm_unlink(posix_name.c_str()); |
| 155 | mapped_file_ = -1; |
| 156 | } |
agl@chromium.org | 69e0a49 | 2008-11-18 09:21:05 +0900 | [diff] [blame^] | 157 | |
avi@google.com | 3a20898 | 2008-08-13 06:06:40 +0900 | [diff] [blame] | 158 | if (lock_) { |
| 159 | posix_name += kSemaphoreSuffix; |
| 160 | sem_unlink(posix_name.c_str()); |
| 161 | lock_ = NULL; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void SharedMemory::Lock() { |
| 166 | DCHECK(lock_ != NULL); |
| 167 | sem_wait(lock_); |
| 168 | } |
| 169 | |
| 170 | void SharedMemory::Unlock() { |
| 171 | DCHECK(lock_ != NULL); |
| 172 | sem_post(lock_); |
| 173 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 174 | |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 175 | } // namespace base |