1 : // Copyright (c) 2011 The Native Client Authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef NATIVE_CLIENT_SRC_TRUSTED_DESC_NACL_DESC_WRAPPER_H_
6 : #define NATIVE_CLIENT_SRC_TRUSTED_DESC_NACL_DESC_WRAPPER_H_
7 :
8 : #include "native_client/src/include/nacl_base.h"
9 : #include "native_client/src/trusted/desc/nacl_desc_base.h"
10 : #include "native_client/src/trusted/desc/nrd_xfer.h"
11 :
12 : namespace nacl {
13 : // Forward declarations.
14 : class DescWrapper;
15 : class DescWrapperCommon;
16 :
17 : // We also create a utility class that allows creation of wrappers for the
18 : // NaClDescs.
19 : class DescWrapperFactory {
20 : public:
21 : DescWrapperFactory();
22 : ~DescWrapperFactory();
23 :
24 : // Create a bound socket, socket address pair.
25 : int MakeBoundSock(DescWrapper* pair[2]);
26 : // Create a pair of DescWrappers for a connnected (data-only) socket.
27 : int MakeSocketPair(DescWrapper* pair[2]);
28 : // Create an IMC socket object.
29 : DescWrapper* MakeImcSock(NaClHandle handle);
30 : // Create a shared memory object.
31 : DescWrapper* MakeShm(size_t size);
32 : // Create a file descriptor object.
33 : DescWrapper* MakeFileDesc(int host_os_desc, int mode);
34 : // As with MakeFileDesc, but with quota management.
35 : DescWrapper* MakeFileDescQuota(int host_os_desc, int mode,
36 : const uint8_t* file_id);
37 : // Create a DescWrapper from opening a host file.
38 : DescWrapper* OpenHostFile(const char* fname, int flags, int mode);
39 : // As with OpenHostFile, but with quota management.
40 : DescWrapper* OpenHostFileQuota(const char* fname, int flags, int mode,
41 : const uint8_t* file_id);
42 : // Create a DescWrapper for a random number generator.
43 : DescWrapper* OpenRng();
44 : // Create a DescWrapper for the designated invalid descriptor
45 : DescWrapper* MakeInvalid();
46 :
47 : // Create a DescWrapper from a generic Pepper shared memory descriptor.
48 : DescWrapper* ImportPepperSharedMemory(intptr_t shm, size_t size);
49 : // Create a DescWrapper from a Pepper2D shared memory descriptor.
50 : DescWrapper* ImportPepper2DSharedMemory(intptr_t dib);
51 : // Create a DescWrapper from a Pepper synchronization object.
52 : DescWrapper* ImportPepperSync(intptr_t sock);
53 :
54 : // We will doubtless want more specific factory methods. For now,
55 : // we provide a wide-open method.
56 : DescWrapper* MakeGeneric(struct NaClDesc* desc);
57 : // Same as above but unrefs desc in case of failure
58 : DescWrapper* MakeGenericCleanup(struct NaClDesc* desc);
59 : // Utility routine for importing sync socket
60 : DescWrapper* ImportSyncSocketHandle(NaClHandle handle);
61 : // Utility routine for importing Linux/Mac (posix) and Windows shared memory.
62 : DescWrapper* ImportShmHandle(NaClHandle handle, size_t size);
63 : // Utility routine for importing SysV shared memory.
64 : DescWrapper* ImportSysvShm(int key, size_t size);
65 :
66 : private:
67 : // The common data from this instance of the wrapper.
68 : DescWrapperCommon* common_data_;
69 :
70 : DISALLOW_COPY_AND_ASSIGN(DescWrapperFactory);
71 : };
72 :
73 : // A wrapper around NaClDesc to provide slightly higher level abstractions for
74 : // common operations.
75 : class DescWrapper {
76 : friend class DescWrapperFactory;
77 :
78 : public:
79 : struct MsgIoVec {
80 : void* base;
81 : nacl_abi_size_t length;
82 : };
83 :
84 : struct MsgHeader {
85 : struct MsgIoVec* iov;
86 : nacl_abi_size_t iov_length;
87 : DescWrapper** ndescv; // Pointer to array of pointers.
88 : nacl_abi_size_t ndescv_length;
89 : int32_t flags;
90 : // flags may contain 0 or any combination of the following.
91 : static const int32_t kRecvMsgDataTruncated =
92 : NACL_ABI_RECVMSG_DATA_TRUNCATED;
93 : static const int32_t kRecvMsgDescTruncated =
94 : NACL_ABI_RECVMSG_DESC_TRUNCATED;
95 : };
96 :
97 : ~DescWrapper();
98 :
99 : // Extract a NaClDesc from the wrapper.
100 48 : struct NaClDesc* desc() const { return desc_; }
101 :
102 : // Get the type of the wrapped NaClDesc.
103 : enum NaClDescTypeTag type_tag() const {
104 : return reinterpret_cast<struct NaClDescVtbl const *>(desc_->base.vtbl)->
105 : typeTag;
106 : }
107 :
108 : // We do not replicate the underlying NaClDesc object hierarchy, so there
109 : // are obviously many more methods than a particular derived class
110 : // implements.
111 :
112 : // Map a shared memory descriptor.
113 : // Sets the address to the place mapped to and the size to the rounded result.
114 : // Returns zero on success, negative NaCl ABI errno on failure.
115 : int Map(void** addr, size_t* size);
116 :
117 : // Unmaps a region of shared memory.
118 : // Returns zero on success, negative NaCl ABI errno on failure.
119 : int Unmap(void* start_addr, size_t len);
120 :
121 : // Read len bytes into buf.
122 : // Returns bytes read on success, negative NaCl ABI errno on failure.
123 : ssize_t Read(void* buf, size_t len);
124 :
125 : // Write len bytes from buf.
126 : // Returns bytes written on success, negative NaCl ABI errno on failure.
127 : ssize_t Write(const void* buf, size_t len);
128 :
129 : // Move the file pointer.
130 : // Returns updated position on success, negative NaCl ABI errno on failure.
131 : nacl_off64_t Seek(nacl_off64_t offset, int whence);
132 :
133 : // The generic I/O control function.
134 : // Returns zero on success, negative NaCl ABI errno on failure.
135 : int Ioctl(int request, void* arg);
136 :
137 : // Get descriptor information.
138 : // Returns zero on success, negative NaCl ABI errno on failure.
139 : int Fstat(struct nacl_abi_stat* statbuf);
140 :
141 : // Close the descriptor.
142 : // Returns zero on success, negative NaCl ABI errno on failure.
143 : int Close();
144 :
145 : // Read count directory entries into dirp.
146 : // Returns count on success, negative NaCl ABI errno on failure.
147 : ssize_t Getdents(void* dirp, size_t count);
148 :
149 : // Lock a mutex.
150 : // Returns zero on success, negative NaCl ABI errno on failure.
151 : int Lock();
152 :
153 : // TryLock on a mutex.
154 : // Returns zero on success, negative NaCl ABI errno on failure.
155 : int TryLock();
156 :
157 : // Unlock a mutex.
158 : // Returns zero on success, negative NaCl ABI errno on failure.
159 : int Unlock();
160 :
161 : // Wait on a condition variable guarded by the specified mutex.
162 : // Returns zero on success, negative NaCl ABI errno on failure.
163 : int Wait(DescWrapper* mutex);
164 :
165 : // Timed wait on a condition variable guarded by the specified mutex.
166 : // Returns zero on success, negative NaCl ABI errno on failure.
167 : int TimedWaitAbs(DescWrapper* mutex, struct nacl_abi_timespec* ts);
168 :
169 : // Signal a condition variable.
170 : // Returns zero on success, negative NaCl ABI errno on failure.
171 : int Signal();
172 :
173 : // Broadcast to a condition variable.
174 : // Returns zero on success, negative NaCl ABI errno on failure.
175 : int Broadcast();
176 :
177 : // Send a message.
178 : // Returns bytes sent on success, negative NaCl ABI errno on failure.
179 : ssize_t SendMsg(const MsgHeader* dgram, int flags);
180 :
181 : // Receive a message.
182 : // Returns bytes received on success, negative NaCl ABI errno on failure.
183 : ssize_t RecvMsg(MsgHeader* dgram, int flags,
184 : struct NaClDescQuotaInterface *quota_interface);
185 :
186 : // Connect to a socket address.
187 : // Returns a valid DescWrapper on success, NULL on failure.
188 : DescWrapper* Connect();
189 :
190 : // Accept connection on a bound socket.
191 : // Returns a valid DescWrapper on success, NULL on failure.
192 : DescWrapper* Accept();
193 :
194 : // Post on a semaphore.
195 : // Returns zero on success, negative NaCl ABI errno on failure.
196 : int Post();
197 :
198 : // Wait on a semaphore.
199 : // Returns zero on success, negative NaCl ABI errno on failure.
200 : int SemWait();
201 :
202 : // Get a semaphore's value.
203 : // Returns zero on success, negative NaCl ABI errno on failure.
204 : int GetValue();
205 :
206 : private:
207 : DescWrapper(DescWrapperCommon* common_data, struct NaClDesc* desc);
208 :
209 : DescWrapperCommon* common_data_;
210 : struct NaClDesc* desc_;
211 :
212 : DISALLOW_COPY_AND_ASSIGN(DescWrapper);
213 : };
214 :
215 : } // namespace nacl
216 :
217 : #endif // NATIVE_CLIENT_SRC_TRUSTED_DESC_NACL_DESC_WRAPPER_H_
|