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 #ifdef __native_client__
36 #include <nacl/nacl_imc.h>
38 #include "native_client/intermodule_comm/nacl_imc.h"
39 #endif // __native_client__
41 #include <bits/mman.h>
45 #include <sys/nacl_syscalls.h>
52 return (errno
== EAGAIN
) ? true : false;
55 int GetLastErrorString(char* buffer
, size_t length
) {
56 // Note newlib provides only GNU version of strerror_r().
57 if (buffer
== NULL
|| length
== 0) {
61 char* message
= strerror_r(errno
, buffer
, length
);
62 if (message
!= buffer
) {
63 size_t message_bytes
= strlen(message
) + 1;
64 length
= std::min(message_bytes
, length
);
65 memmove(buffer
, message
, length
);
66 buffer
[length
- 1] = '\0';
71 Handle
BoundSocket(const SocketAddress
* address
) {
72 // TODO: Switch to the following once the make_bound_sock() prototype
74 // return make_bound_sock(address);
78 int SocketPair(Handle pair
[2]) {
79 return imc_socketpair(pair
);
82 int Close(Handle handle
) {
86 int SendDatagram(Handle handle
, const MessageHeader
* message
, int flags
) {
87 return imc_sendmsg(handle
, reinterpret_cast<const NaClImcMsgHdr
*>(message
),
91 int SendDatagramTo(Handle handle
, const MessageHeader
* message
, int flags
,
92 const SocketAddress
* name
) {
93 return -1; // TODO: how to implement this for NaCl?
96 int ReceiveDatagram(Handle handle
, MessageHeader
* message
, int flags
) {
97 return imc_recvmsg(handle
, reinterpret_cast<NaClImcMsgHdr
*>(message
), flags
);
100 Handle
CreateMemoryObject(size_t length
) {
101 return imc_mem_obj_create(length
);
104 void* Map(void* start
, size_t length
, int prot
, int flags
,
105 Handle memory
, off_t offset
) {
106 static int posix_prot
[4] = {
110 PROT_READ
| PROT_WRITE
114 if (flags
& kMapShared
) {
115 adjusted
|= MAP_SHARED
;
117 if (flags
& kMapPrivate
) {
118 adjusted
|= MAP_PRIVATE
;
120 if (flags
& kMapFixed
) {
121 adjusted
|= MAP_FIXED
;
123 return mmap(start
, length
, posix_prot
[prot
& 3], adjusted
, memory
, offset
);
126 int Unmap(void* start
, size_t length
) {
127 return munmap(start
, length
);