ipc/glue/SharedMemoryBasic_android.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial