Don't delete or unpack binutils and gcc: we get them from Git
[nativeclient.git] / intermodule_comm / nacl_htp.cc
blob2531145c033ab19ef3fcf22a987020b2292efe69
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
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
14 * distribution.
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"
36 #include <assert.h>
37 #include <errno.h>
38 #include <string.h>
39 #if NACL_WINDOWS
40 #include <io.h>
41 #include <fcntl.h>
42 #endif
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"
54 namespace nacl {
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);
61 if (result < 0) {
62 return -1;
64 return result;
67 int ReceiveDatagram(HtpHandle socket, HtpHeader* message, int flags) {
68 int32_t result = NaClImcRecvTypedMessage(socket, NULL,
69 reinterpret_cast<struct NaClImcTypedMsgHdr*>(message), flags);
70 if (result < 0) {
71 return -1;
73 return result;
76 int Close(HtpHandle handle) {
77 if (handle) {
78 NaClDescUnref(handle);
80 return 0;
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) {
91 return NULL;
93 NaClHostDesc* nhdp = static_cast<NaClHostDesc*>(malloc(sizeof(*nhdp)));
94 if (!nhdp) {
95 return NULL;
97 NaClDescIoDesc* ndidp = static_cast<NaClDescIoDesc*>(malloc(sizeof(*ndidp)));
98 if (!ndidp) {
99 free(nhdp);
100 return NULL;
102 int fd;
103 #if NACL_WINDOWS
104 fd = _open_osfhandle(reinterpret_cast<intptr_t>(handle),
105 _O_RDWR | _O_BINARY);
106 if (fd == -1) {
107 free(ndidp);
108 free(nhdp);
109 return NULL;
111 #else
112 fd = handle;
113 #endif
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);
120 #if NACL_WINDOWS
121 close(fd);
122 #endif
123 free(ndidp);
124 free(nhdp);
125 return NULL;
128 HtpHandle CreateShmDesc(Handle handle, off_t length) {
129 if (handle == kInvalidHandle) {
130 return NULL;
132 NaClDescImcShm* desc = static_cast<NaClDescImcShm*>(malloc(sizeof(*desc)));
133 if (desc == NULL) {
134 return NULL;
136 if (!NaClDescImcShmCtor(desc, handle, length)) {
137 free(desc);
138 return NULL;
140 return reinterpret_cast<NaClDesc*>(desc);
143 HtpHandle CreateImcDesc(Handle handle) {
144 if (handle == kInvalidHandle) {
145 return NULL;
147 NaClDescImcDesc* desc = static_cast<NaClDescImcDesc*>(malloc(sizeof(*desc)));
148 if (desc == NULL) {
149 return NULL;
151 if (!NaClDescImcDescCtor(desc, handle)) {
152 free(desc);
153 return NULL;
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__
168 } // namespace nacl