1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include <android/log.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <sys/ioctl.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include "base/process_util.h"
18
19 #include "SharedMemoryBasic.h"
20
21 #include "mozilla/Ashmem.h"
22
23 namespace mozilla {
24 namespace ipc {
25
LogError(const char * what)26 static void LogError(const char* what) {
27 __android_log_print(ANDROID_LOG_ERROR, "Gecko", "%s: %s (%d)", what,
28 strerror(errno), errno);
29 }
30
SharedMemoryBasic()31 SharedMemoryBasic::SharedMemoryBasic()
32 : mShmFd(-1), mMemory(nullptr), mOpenRights(RightsReadWrite) {}
33
~SharedMemoryBasic()34 SharedMemoryBasic::~SharedMemoryBasic() {
35 Unmap();
36 CloseHandle();
37 }
38
SetHandle(const Handle & aHandle,OpenRights aRights)39 bool SharedMemoryBasic::SetHandle(const Handle& aHandle, OpenRights aRights) {
40 MOZ_ASSERT(-1 == mShmFd, "Already Create()d");
41 mShmFd = aHandle.fd;
42 mOpenRights = aRights;
43 return true;
44 }
45
Create(size_t aNbytes)46 bool SharedMemoryBasic::Create(size_t aNbytes) {
47 MOZ_ASSERT(-1 == mShmFd, "Already Create()d");
48
49 // Carve a new instance off of /dev/ashmem
50 int shmfd = mozilla::android::ashmem_create(nullptr, aNbytes);
51 if (-1 == shmfd) {
52 LogError("ShmemAndroid::Create():open");
53 return false;
54 }
55
56 mShmFd = shmfd;
57 Created(aNbytes);
58 return true;
59 }
60
Map(size_t nBytes,void * fixed_address)61 bool SharedMemoryBasic::Map(size_t nBytes, void* fixed_address) {
62 MOZ_ASSERT(nullptr == mMemory, "Already Map()d");
63
64 int prot = PROT_READ;
65 if (mOpenRights == RightsReadWrite) {
66 prot |= PROT_WRITE;
67 }
68
69 // Don't use MAP_FIXED when a fixed_address was specified, since that can
70 // replace pages that are alread mapped at that address.
71 mMemory = mmap(fixed_address, nBytes, prot, MAP_SHARED, mShmFd, 0);
72
73 if (MAP_FAILED == mMemory) {
74 if (!fixed_address) {
75 LogError("ShmemAndroid::Map()");
76 }
77 mMemory = nullptr;
78 return false;
79 }
80
81 if (fixed_address && mMemory != fixed_address) {
82 if (munmap(mMemory, nBytes)) {
83 LogError("ShmemAndroid::Map():unmap");
84 mMemory = nullptr;
85 return false;
86 }
87 }
88
89 Mapped(nBytes);
90 return true;
91 }
92
FindFreeAddressSpace(size_t size)93 void* SharedMemoryBasic::FindFreeAddressSpace(size_t size) {
94 void* memory =
95 mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
96 munmap(memory, size);
97 return memory != (void*)-1 ? memory : NULL;
98 }
99
ShareToProcess(base::ProcessId,Handle * aNewHandle)100 bool SharedMemoryBasic::ShareToProcess(base::ProcessId /*unused*/,
101 Handle* aNewHandle) {
102 MOZ_ASSERT(mShmFd >= 0, "Should have been Create()d by now");
103
104 int shmfdDup = dup(mShmFd);
105 if (-1 == shmfdDup) {
106 LogError("ShmemAndroid::ShareToProcess()");
107 return false;
108 }
109
110 aNewHandle->fd = shmfdDup;
111 aNewHandle->auto_close = true;
112 return true;
113 }
114
Unmap()115 void SharedMemoryBasic::Unmap() {
116 if (!mMemory) {
117 return;
118 }
119
120 if (munmap(mMemory, Size())) {
121 LogError("ShmemAndroid::Unmap()");
122 }
123 mMemory = nullptr;
124 }
125
CloseHandle()126 void SharedMemoryBasic::CloseHandle() {
127 if (mShmFd != -1) {
128 close(mShmFd);
129 mShmFd = -1;
130 mOpenRights = RightsReadWrite;
131 }
132 }
133
134 } // namespace ipc
135 } // namespace mozilla
136