OLD | NEW |
---|---|
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 <fcntl.h> | |
9 #include <sys/mman.h> | 10 #include <sys/mman.h> |
10 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <sys/stat.h> | |
13 #include <sys/types.h> | |
11 #include <unistd.h> | 14 #include <unistd.h> |
12 | 15 |
13 #include "base/file_util.h" | 16 #include "base/file_util.h" |
14 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 18 #include "base/logging.h" |
16 #include "base/process_util.h" | 19 #include "base/process_util.h" |
17 #include "base/safe_strerror_posix.h" | 20 #include "base/safe_strerror_posix.h" |
18 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
19 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
20 #include "base/threading/platform_thread.h" | 23 #include "base/threading/platform_thread.h" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 // private), and prevents the need for cleanup (once the last fd is closed, | 145 // private), and prevents the need for cleanup (once the last fd is closed, |
143 // it is truly freed). | 146 // it is truly freed). |
144 if (fp) { | 147 if (fp) { |
145 if (unlink(path.value().c_str())) | 148 if (unlink(path.value().c_str())) |
146 PLOG(WARNING) << "unlink"; | 149 PLOG(WARNING) << "unlink"; |
147 } | 150 } |
148 } else { | 151 } else { |
149 if (!FilePathForMemoryName(*options.name, &path)) | 152 if (!FilePathForMemoryName(*options.name, &path)) |
150 return false; | 153 return false; |
151 | 154 |
152 fp = file_util::OpenFile(path, "w+x"); | 155 // Make sure that we don't give permissions to access this file |
153 if (fp == NULL && options.open_existing) { | 156 // to other users on the system. |
154 // "w+" will truncate if it already exists. | 157 const mode_t file_mode = S_IRUSR | S_IWUSR; |
155 fp = file_util::OpenFile(path, "a+"); | 158 int fd; |
159 // First, try to create the file. | |
160 fd = open(path.value().c_str(), O_RDWR | O_CREAT | O_EXCL, file_mode); | |
161 if (fd == -1 && options.open_existing) { | |
162 // If this doesn't work, try again in append mode. | |
163 fd = open(path.value().c_str(), O_RDWR | O_APPEND, file_mode); | |
Markus (顧孟勤)
2013/06/26 12:28:22
Do not pass in file_mode. That's just misleading y
jln (very slow on Chromium)
2013/07/02 02:37:06
Done.
| |
156 fix_size = false; | 164 fix_size = false; |
157 } | 165 } |
166 fp = NULL; | |
167 if (fd >= 0) { | |
168 // "a+" is always appropriate: if it's a new file, a+ is similar to w+. | |
169 fp = fdopen(fd, "a+"); | |
170 } | |
158 } | 171 } |
159 if (fp && fix_size) { | 172 if (fp && fix_size) { |
160 // Get current size. | 173 // Get current size. |
161 struct stat stat; | 174 struct stat stat; |
162 if (fstat(fileno(fp), &stat) != 0) { | 175 if (fstat(fileno(fp), &stat) != 0) { |
163 file_util::CloseFile(fp); | 176 file_util::CloseFile(fp); |
164 return false; | 177 return false; |
165 } | 178 } |
166 const size_t current_size = stat.st_size; | 179 const size_t current_size = stat.st_size; |
167 if (current_size != options.size) { | 180 if (current_size != options.size) { |
Markus (顧孟勤)
2013/06/26 12:28:22
I realize this isn't code that you wrote, but over
jln (very slow on Chromium)
2013/07/02 02:37:06
Unfortunately no, CreateAndOpenTemporaryShmemFile(
| |
168 if (HANDLE_EINTR(ftruncate(fileno(fp), options.size)) != 0) { | 181 if (HANDLE_EINTR(ftruncate(fileno(fp), options.size)) != 0) { |
169 file_util::CloseFile(fp); | 182 file_util::CloseFile(fp); |
170 return false; | 183 return false; |
171 } | 184 } |
172 } | 185 } |
173 requested_size_ = options.size; | 186 requested_size_ = options.size; |
174 } | 187 } |
175 if (fp == NULL) { | 188 if (fp == NULL) { |
176 #if !defined(OS_MACOSX) | 189 #if !defined(OS_MACOSX) |
177 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; | 190 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 new_handle->fd = new_fd; | 392 new_handle->fd = new_fd; |
380 new_handle->auto_close = true; | 393 new_handle->auto_close = true; |
381 | 394 |
382 if (close_self) | 395 if (close_self) |
383 Close(); | 396 Close(); |
384 | 397 |
385 return true; | 398 return true; |
386 } | 399 } |
387 | 400 |
388 } // namespace base | 401 } // namespace base |
OLD | NEW |