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/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/posix/eintr_wrapper.h" |
18 #include "base/process/process_metrics.h" | 19 #include "base/process/process_metrics.h" |
19 #include "base/profiler/scoped_tracker.h" | 20 #include "base/profiler/scoped_tracker.h" |
20 #include "base/safe_strerror_posix.h" | 21 #include "base/safe_strerror_posix.h" |
21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
22 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
23 #include "base/threading/platform_thread.h" | 24 #include "base/threading/platform_thread.h" |
24 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
25 | 26 |
26 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
27 #include "base/mac/foundation_util.h" | 28 #include "base/mac/foundation_util.h" |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 if (readonly_fd.is_valid()) { | 396 if (readonly_fd.is_valid()) { |
396 struct stat readonly_st = {}; | 397 struct stat readonly_st = {}; |
397 if (fstat(readonly_fd.get(), &readonly_st)) | 398 if (fstat(readonly_fd.get(), &readonly_st)) |
398 NOTREACHED(); | 399 NOTREACHED(); |
399 if (st.st_dev != readonly_st.st_dev || st.st_ino != readonly_st.st_ino) { | 400 if (st.st_dev != readonly_st.st_dev || st.st_ino != readonly_st.st_ino) { |
400 LOG(ERROR) << "writable and read-only inodes don't match; bailing"; | 401 LOG(ERROR) << "writable and read-only inodes don't match; bailing"; |
401 return false; | 402 return false; |
402 } | 403 } |
403 } | 404 } |
404 | 405 |
405 mapped_file_ = dup(fileno(fp.get())); | 406 mapped_file_ = HANDLE_EINTR(dup(fileno(fp.get()))); |
406 if (mapped_file_ == -1) { | 407 if (mapped_file_ == -1) { |
407 if (errno == EMFILE) { | 408 if (errno == EMFILE) { |
408 LOG(WARNING) << "Shared memory creation failed; out of file descriptors"; | 409 LOG(WARNING) << "Shared memory creation failed; out of file descriptors"; |
409 return false; | 410 return false; |
410 } else { | 411 } else { |
411 NOTREACHED() << "Call to dup failed, errno=" << errno; | 412 NOTREACHED() << "Call to dup failed, errno=" << errno; |
412 } | 413 } |
413 } | 414 } |
414 inode_ = st.st_ino; | 415 inode_ = st.st_ino; |
415 readonly_mapped_file_ = readonly_fd.release(); | 416 readonly_mapped_file_ = readonly_fd.release(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 handle_to_dup = mapped_file_; | 475 handle_to_dup = mapped_file_; |
475 break; | 476 break; |
476 case SHARE_READONLY: | 477 case SHARE_READONLY: |
477 // We could imagine re-opening the file from /dev/fd, but that can't make | 478 // We could imagine re-opening the file from /dev/fd, but that can't make |
478 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 | 479 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 |
479 CHECK_GE(readonly_mapped_file_, 0); | 480 CHECK_GE(readonly_mapped_file_, 0); |
480 handle_to_dup = readonly_mapped_file_; | 481 handle_to_dup = readonly_mapped_file_; |
481 break; | 482 break; |
482 } | 483 } |
483 | 484 |
484 const int new_fd = dup(handle_to_dup); | 485 const int new_fd = HANDLE_EINTR(dup(handle_to_dup)); |
485 if (new_fd < 0) { | 486 if (new_fd < 0) { |
486 DPLOG(ERROR) << "dup() failed."; | 487 DPLOG(ERROR) << "dup() failed."; |
487 return false; | 488 return false; |
488 } | 489 } |
489 | 490 |
490 new_handle->fd = new_fd; | 491 new_handle->fd = new_fd; |
491 new_handle->auto_close = true; | 492 new_handle->auto_close = true; |
492 | 493 |
493 if (close_self) { | 494 if (close_self) { |
494 Unmap(); | 495 Unmap(); |
495 Close(); | 496 Close(); |
496 } | 497 } |
497 | 498 |
498 return true; | 499 return true; |
499 } | 500 } |
500 | 501 |
501 } // namespace base | 502 } // namespace base |
OLD | NEW |