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