2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // NaCl inter-module communication primitives.
35 // This file implements common parts of IMC for "unix like systems" (i.e. not
38 // TODO: Perhaps this file should go into a platform-specific directory
39 // (posix? unixlike?) We have a little convention going where mac/linux stuff
40 // goes in the linux directory and is referenced by the mac build but that's a
43 #include "native_client/intermodule_comm/nacl_imc.h"
54 #include <sys/types.h>
58 #include "native_client/include/atomic_ops.h"
64 // The pathname prefix for memory objects created by CreateMemoryObject().
65 const char kShmPrefix
[] = "/google-nacl-shm-";
70 return (errno
== EAGAIN
) ? true : false;
73 int GetLastErrorString(char* buffer
, size_t length
) {
75 // Note some Linux distributions provide only GNU version of strerror_r().
76 if (buffer
== NULL
|| length
== 0) {
80 char* message
= strerror_r(errno
, buffer
, length
);
81 if (message
!= buffer
) {
82 size_t message_bytes
= strlen(message
) + 1;
83 length
= std::min(message_bytes
, length
);
84 memmove(buffer
, message
, length
);
85 buffer
[length
- 1] = '\0';
89 return strerror_r(errno
, buffer
, length
);
93 Handle
CreateMemoryObject(size_t length
) {
94 static AtomicWord memory_object_count
;
98 snprintf(name
, sizeof name
, "%s-%u.%u", kShmPrefix
,
100 static_cast<uint32_t>(AtomicIncrement(&memory_object_count
, 1)));
101 int m
= shm_open(name
, O_RDWR
| O_CREAT
| O_EXCL
, 0666);
103 if (ftruncate(m
, length
) == -1) {
110 if (errno
!= EEXIST
) {
116 void* Map(void* start
, size_t length
, int prot
, int flags
,
117 Handle memory
, off_t offset
) {
118 static int posix_prot
[4] = {
122 PROT_READ
| PROT_WRITE
126 if (flags
& kMapShared
) {
127 adjusted
|= MAP_SHARED
;
129 if (flags
& kMapPrivate
) {
130 adjusted
|= MAP_PRIVATE
;
132 if (flags
& kMapFixed
) {
133 adjusted
|= MAP_FIXED
;
135 return mmap(start
, length
, posix_prot
[prot
& 3], adjusted
, memory
, offset
);
138 int Unmap(void* start
, size_t length
) {
139 return munmap(start
, length
);