• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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.
4 
5 #include "base/memory/shared_memory_handle.h"
6 
7 #include <sys/mman.h>
8 #include <unistd.h>
9 
10 #include "base/logging.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "base/posix/unix_domain_socket.h"
13 #include "base/unguessable_token.h"
14 #include "third_party/ashmem/ashmem.h"
15 
16 namespace base {
17 
GetAshmemRegionProtectionMask(int fd)18 static int GetAshmemRegionProtectionMask(int fd) {
19   int prot = ashmem_get_prot_region(fd);
20   if (prot < 0) {
21     DPLOG(ERROR) << "ashmem_get_prot_region";
22     return -1;
23   }
24   return prot;
25 }
26 
27 SharedMemoryHandle::SharedMemoryHandle() = default;
28 
SharedMemoryHandle(const base::FileDescriptor & file_descriptor,size_t size,const base::UnguessableToken & guid)29 SharedMemoryHandle::SharedMemoryHandle(
30     const base::FileDescriptor& file_descriptor,
31     size_t size,
32     const base::UnguessableToken& guid)
33     : guid_(guid), size_(size) {
34   DCHECK_GE(file_descriptor.fd, 0);
35   file_descriptor_ = file_descriptor;
36 }
37 
38 // static
ImportHandle(int fd,size_t size)39 SharedMemoryHandle SharedMemoryHandle::ImportHandle(int fd, size_t size) {
40   SharedMemoryHandle handle;
41   handle.file_descriptor_.fd = fd;
42   handle.file_descriptor_.auto_close = false;
43   handle.guid_ = UnguessableToken::Create();
44   handle.size_ = size;
45   return handle;
46 }
47 
GetHandle() const48 int SharedMemoryHandle::GetHandle() const {
49   DCHECK(IsValid());
50   return file_descriptor_.fd;
51 }
52 
IsValid() const53 bool SharedMemoryHandle::IsValid() const {
54   return file_descriptor_.fd >= 0;
55 }
56 
Close() const57 void SharedMemoryHandle::Close() const {
58   DCHECK(IsValid());
59   if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0)
60     PLOG(ERROR) << "close";
61 }
62 
Release()63 int SharedMemoryHandle::Release() {
64   int old_fd = file_descriptor_.fd;
65   file_descriptor_.fd = -1;
66   return old_fd;
67 }
68 
Duplicate() const69 SharedMemoryHandle SharedMemoryHandle::Duplicate() const {
70   DCHECK(IsValid());
71   SharedMemoryHandle result;
72   int duped_handle = HANDLE_EINTR(dup(file_descriptor_.fd));
73   if (duped_handle >= 0) {
74     result = SharedMemoryHandle(FileDescriptor(duped_handle, true), GetSize(),
75                                 GetGUID());
76     if (IsReadOnly())
77       result.SetReadOnly();
78   }
79   return result;
80 }
81 
SetOwnershipPassesToIPC(bool ownership_passes)82 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) {
83   file_descriptor_.auto_close = ownership_passes;
84 }
85 
OwnershipPassesToIPC() const86 bool SharedMemoryHandle::OwnershipPassesToIPC() const {
87   return file_descriptor_.auto_close;
88 }
89 
IsRegionReadOnly() const90 bool SharedMemoryHandle::IsRegionReadOnly() const {
91   int prot = GetAshmemRegionProtectionMask(file_descriptor_.fd);
92   return (prot >= 0 && (prot & PROT_WRITE) == 0);
93 }
94 
SetRegionReadOnly() const95 bool SharedMemoryHandle::SetRegionReadOnly() const {
96   int fd = file_descriptor_.fd;
97   int prot = GetAshmemRegionProtectionMask(fd);
98   if (prot < 0)
99     return false;
100 
101   if ((prot & PROT_WRITE) == 0) {
102     // Region is already read-only.
103     return true;
104   }
105 
106   prot &= ~PROT_WRITE;
107   int ret = ashmem_set_prot_region(fd, prot);
108   if (ret != 0) {
109     DPLOG(ERROR) << "ashmem_set_prot_region";
110     return false;
111   }
112   return true;
113 }
114 
115 }  // namespace base
116