1 : // Copyright (c) 2012 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 file descriptor object.
31 : DescWrapper* MakeFileDesc(int host_os_desc, int mode);
32 : // As with MakeFileDesc, but with quota management.
33 : DescWrapper* MakeFileDescQuota(int host_os_desc, int mode,
34 : const uint8_t* file_id);
35 : // Create a DescWrapper from opening a host file.
36 : DescWrapper* OpenHostFile(const char* fname, int flags, int mode);
37 : // As with OpenHostFile, but with quota management.
38 : DescWrapper* OpenHostFileQuota(const char* fname, int flags, int mode,
39 : const uint8_t* file_id);
40 : // Create a DescWrapper for a random number generator.
41 : DescWrapper* OpenRng();
42 : // Create a DescWrapper for the designated invalid descriptor
43 : DescWrapper* MakeInvalid();
44 :
45 : // We will doubtless want more specific factory methods. For now,
46 : // we provide a wide-open method.
47 : DescWrapper* MakeGeneric(struct NaClDesc* desc);
48 : // Same as above but unrefs desc in case of failure
49 : DescWrapper* MakeGenericCleanup(struct NaClDesc* desc);
50 : // Utility routine for importing sync socket
51 : DescWrapper* ImportSyncSocketHandle(NaClHandle handle);
52 : // Utility routine for importing Linux/Mac (posix) and Windows shared memory.
53 : DescWrapper* ImportShmHandle(NaClHandle handle, size_t size);
54 :
55 : private:
56 : // The common data from this instance of the wrapper.
57 : DescWrapperCommon* common_data_;
58 :
59 : DISALLOW_COPY_AND_ASSIGN(DescWrapperFactory);
60 : };
61 :
62 : // A wrapper around NaClDesc to provide slightly higher level abstractions for
63 : // common operations.
64 : class DescWrapper {
65 : friend class DescWrapperFactory;
66 :
67 : public:
68 : struct MsgIoVec {
69 : void* base;
70 : nacl_abi_size_t length;
71 : };
72 :
73 : struct MsgHeader {
74 : struct MsgIoVec* iov;
75 : nacl_abi_size_t iov_length;
76 : DescWrapper** ndescv; // Pointer to array of pointers.
77 : nacl_abi_size_t ndescv_length;
78 : int32_t flags;
79 : // flags may contain 0 or any combination of the following.
80 : static const int32_t kRecvMsgDataTruncated =
81 : NACL_ABI_RECVMSG_DATA_TRUNCATED;
82 : static const int32_t kRecvMsgDescTruncated =
83 : NACL_ABI_RECVMSG_DESC_TRUNCATED;
84 : };
85 :
86 : ~DescWrapper();
87 :
88 : // Extract a NaClDesc from the wrapper.
89 81 : struct NaClDesc* desc() const { return desc_; }
90 :
91 : // Get the type of the wrapped NaClDesc.
92 : enum NaClDescTypeTag type_tag() const {
93 : return reinterpret_cast<struct NaClDescVtbl const *>(desc_->base.vtbl)->
94 : typeTag;
95 : }
96 :
97 : // We do not replicate the underlying NaClDesc object hierarchy, so there
98 : // are obviously many more methods than a particular derived class
99 : // implements.
100 :
101 : // Read len bytes into buf.
102 : // Returns bytes read on success, negative NaCl ABI errno on failure.
103 : ssize_t Read(void* buf, size_t len);
104 :
105 : // Write len bytes from buf.
106 : // Returns bytes written on success, negative NaCl ABI errno on failure.
107 : ssize_t Write(const void* buf, size_t len);
108 :
109 : // Move the file pointer.
110 : // Returns updated position on success, negative NaCl ABI errno on failure.
111 : nacl_off64_t Seek(nacl_off64_t offset, int whence);
112 :
113 : // Get descriptor information.
114 : // Returns zero on success, negative NaCl ABI errno on failure.
115 : int Fstat(struct nacl_abi_stat* statbuf);
116 :
117 : // Close the descriptor.
118 : // Returns zero on success, negative NaCl ABI errno on failure.
119 : int Close();
120 :
121 : // Read count directory entries into dirp.
122 : // Returns count on success, negative NaCl ABI errno on failure.
123 : ssize_t Getdents(void* dirp, size_t count);
124 :
125 : // Lock a mutex.
126 : // Returns zero on success, negative NaCl ABI errno on failure.
127 : int Lock();
128 :
129 : // TryLock on a mutex.
130 : // Returns zero on success, negative NaCl ABI errno on failure.
131 : int TryLock();
132 :
133 : // Unlock a mutex.
134 : // Returns zero on success, negative NaCl ABI errno on failure.
135 : int Unlock();
136 :
137 : // Wait on a condition variable guarded by the specified mutex.
138 : // Returns zero on success, negative NaCl ABI errno on failure.
139 : int Wait(DescWrapper* mutex);
140 :
141 : // Timed wait on a condition variable guarded by the specified mutex.
142 : // Returns zero on success, negative NaCl ABI errno on failure.
143 : int TimedWaitAbs(DescWrapper* mutex, struct nacl_abi_timespec* ts);
144 :
145 : // Signal a condition variable.
146 : // Returns zero on success, negative NaCl ABI errno on failure.
147 : int Signal();
148 :
149 : // Broadcast to a condition variable.
150 : // Returns zero on success, negative NaCl ABI errno on failure.
151 : int Broadcast();
152 :
153 : // Send a message.
154 : // Returns bytes sent on success, negative NaCl ABI errno on failure.
155 : ssize_t SendMsg(const MsgHeader* dgram, int flags);
156 :
157 : // Receive a message.
158 : // Returns bytes received on success, negative NaCl ABI errno on failure.
159 : ssize_t RecvMsg(MsgHeader* dgram, int flags,
160 : struct NaClDescQuotaInterface *quota_interface);
161 :
162 : // Connect to a socket address.
163 : // Returns a valid DescWrapper on success, NULL on failure.
164 : DescWrapper* Connect();
165 :
166 : // Accept connection on a bound socket.
167 : // Returns a valid DescWrapper on success, NULL on failure.
168 : DescWrapper* Accept();
169 :
170 : // Post on a semaphore.
171 : // Returns zero on success, negative NaCl ABI errno on failure.
172 : int Post();
173 :
174 : // Wait on a semaphore.
175 : // Returns zero on success, negative NaCl ABI errno on failure.
176 : int SemWait();
177 :
178 : // Get a semaphore's value.
179 : // Returns zero on success, negative NaCl ABI errno on failure.
180 : int GetValue();
181 :
182 : private:
183 : DescWrapper(DescWrapperCommon* common_data, struct NaClDesc* desc);
184 :
185 : DescWrapperCommon* common_data_;
186 : struct NaClDesc* desc_;
187 :
188 : DISALLOW_COPY_AND_ASSIGN(DescWrapper);
189 : };
190 :
191 : } // namespace nacl
192 :
193 : #endif // NATIVE_CLIENT_SRC_TRUSTED_DESC_NACL_DESC_WRAPPER_H_
|