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 <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <sys/types.h> | 11 #include <sys/types.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/process/process_metrics.h" | 17 #include "base/process/process_metrics.h" |
18 #include "base/safe_strerror_posix.h" | 18 #include "base/safe_strerror_posix.h" |
19 #include "base/strings/stringprintf.h" | |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
21 #include "base/threading/platform_thread.h" | 22 #include "base/threading/platform_thread.h" |
22 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
23 | 24 |
24 #if defined(OS_MACOSX) | 25 #if defined(OS_MACOSX) |
25 #include "base/mac/foundation_util.h" | 26 #include "base/mac/foundation_util.h" |
26 #endif // OS_MACOSX | 27 #endif // OS_MACOSX |
27 | 28 |
28 #if defined(OS_ANDROID) | 29 #if defined(OS_ANDROID) |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 NOTREACHED() << "lockf() failed." | 393 NOTREACHED() << "lockf() failed." |
393 << " function:" << function | 394 << " function:" << function |
394 << " fd:" << mapped_file_ | 395 << " fd:" << mapped_file_ |
395 << " errno:" << errno | 396 << " errno:" << errno |
396 << " msg:" << safe_strerror(errno); | 397 << " msg:" << safe_strerror(errno); |
397 } | 398 } |
398 } | 399 } |
399 } | 400 } |
400 | 401 |
401 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 402 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
402 SharedMemoryHandle *new_handle, | 403 SharedMemoryHandle* new_handle, |
403 bool close_self) { | 404 bool close_self, |
404 const int new_fd = dup(mapped_file_); | 405 ShareMode share_mode) { |
405 if (new_fd < 0) { | 406 int new_fd = -1; |
406 DPLOG(ERROR) << "dup() failed."; | 407 switch(share_mode) { |
407 return false; | 408 case SHARE_CURRENT_MODE: { |
409 new_fd = dup(mapped_file_); | |
410 if (new_fd < 0) { | |
411 DPLOG(ERROR) << "dup() failed."; | |
412 return false; | |
413 } | |
414 } break; | |
415 case SHARE_READONLY: { | |
416 const std::string writable_fd_path = StringPrintf( | |
417 #if defined(OS_ANDROID) | |
418 "/proc/self/fd/%d", | |
419 #else | |
420 "/dev/fd/%d", | |
421 #endif | |
422 mapped_file_); | |
423 new_fd = HANDLE_EINTR(open(writable_fd_path.c_str(), O_RDONLY)); | |
Jeffrey Yasskin
2013/10/17 03:00:25
This approach doesn't work on Apple OSen.
iOS: ht
| |
424 if (new_fd < 0) { | |
425 DPLOG(ERROR) << "open(\"" << writable_fd_path << "\", O_RDONLY) failed"; | |
426 return false; | |
427 } | |
428 } break; | |
408 } | 429 } |
409 | 430 |
410 new_handle->fd = new_fd; | 431 new_handle->fd = new_fd; |
411 new_handle->auto_close = true; | 432 new_handle->auto_close = true; |
412 | 433 |
413 if (close_self) | 434 if (close_self) |
414 Close(); | 435 Close(); |
415 | 436 |
416 return true; | 437 return true; |
417 } | 438 } |
418 | 439 |
419 } // namespace base | 440 } // namespace base |
OLD | NEW |