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 Handle Transfer Protocol
35 #include "native_client/intermodule_comm/nacl_htp.h"
43 #include "native_client/service_runtime/include/sys/fcntl.h"
44 #include "native_client/service_runtime/include/sys/nacl_imc_api.h"
45 #include "native_client/service_runtime/nacl_desc_base.h"
46 #include "native_client/service_runtime/nacl_desc_imc.h"
47 #include "native_client/service_runtime/nacl_desc_imc_shm.h"
48 #include "native_client/service_runtime/nacl_desc_io.h"
49 #include "native_client/service_runtime/nrd_xfer_lib/nrd_all_modules.h"
50 #include "native_client/service_runtime/nrd_xfer_lib/nrd_xfer.h"
51 #include "native_client/service_runtime/nrd_xfer_lib/nrd_xfer_effector.h"
56 #ifndef __native_client__
58 int SendDatagram(HtpHandle socket
, const HtpHeader
* message
, int flags
) {
59 int32_t result
= NaClImcSendTypedMessage(socket
, NULL
,
60 reinterpret_cast<const struct NaClImcTypedMsgHdr
*>(message
), flags
);
67 int ReceiveDatagram(HtpHandle socket
, HtpHeader
* message
, int flags
) {
68 int32_t result
= NaClImcRecvTypedMessage(socket
, NULL
,
69 reinterpret_cast<struct NaClImcTypedMsgHdr
*>(message
), flags
);
76 int Close(HtpHandle handle
) {
78 NaClDescUnref(handle
);
83 void* Map(void* start
, size_t length
, int prot
, int flags
,
84 HtpHandle memory
, off_t offset
) {
85 NaClDescImcShm
* desc
= reinterpret_cast<NaClDescImcShm
*>(memory
);
86 return Map(start
, length
, prot
, flags
, desc
->h
, offset
);
89 HtpHandle
CreateIoDesc(Handle handle
) {
90 if (handle
== kInvalidHandle
) {
93 NaClHostDesc
* nhdp
= static_cast<NaClHostDesc
*>(malloc(sizeof(*nhdp
)));
97 NaClDescIoDesc
* ndidp
= static_cast<NaClDescIoDesc
*>(malloc(sizeof(*ndidp
)));
104 fd
= _open_osfhandle(reinterpret_cast<intptr_t>(handle
),
105 _O_RDWR
| _O_BINARY
);
114 // We mark it as read/write, but don't really know for sure until we
115 // try to make those syscalls (in which case we'd get EBADF).
116 if (0 <= NaClHostDescPosixTake(nhdp
, fd
, NACL_ABI_O_RDWR
) &&
117 NaClDescIoDescCtor(ndidp
, nhdp
)) {
118 return reinterpret_cast<NaClDesc
*>(ndidp
);
128 HtpHandle
CreateShmDesc(Handle handle
, off_t length
) {
129 if (handle
== kInvalidHandle
) {
132 NaClDescImcShm
* desc
= static_cast<NaClDescImcShm
*>(malloc(sizeof(*desc
)));
136 if (!NaClDescImcShmCtor(desc
, handle
, length
)) {
140 return reinterpret_cast<NaClDesc
*>(desc
);
143 HtpHandle
CreateImcDesc(Handle handle
) {
144 if (handle
== kInvalidHandle
) {
147 NaClDescImcDesc
* desc
= static_cast<NaClDescImcDesc
*>(malloc(sizeof(*desc
)));
151 if (!NaClDescImcDescCtor(desc
, handle
)) {
155 return reinterpret_cast<NaClDesc
*>(desc
);
158 int Read(HtpHandle handle
, void* buffer
, size_t count
) {
159 return NaClDescIoDescRead(handle
, NULL
, buffer
, count
);
162 int Write(HtpHandle handle
, const void* buffer
, size_t count
) {
163 return NaClDescIoDescWrite(handle
, NULL
, buffer
, count
);
166 #endif // __native_client__