PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/googleclient/third_party/native_client/src/trusted/desc/nacl_desc_wrapper.h

http://o3d.googlecode.com/
C Header | 213 lines | 88 code | 45 blank | 80 comment | 2 complexity | af08a396c868bfd8492987e90797296d MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, GPL-2.0, Apache-2.0, MIT, CPL-1.0
  1. // Copyright (c) 2009 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. #ifndef NATIVE_CLIENT_SRC_TRUSTED_DESC_NACL_DESC_WRAPPER_H_
  5. #define NATIVE_CLIENT_SRC_TRUSTED_DESC_NACL_DESC_WRAPPER_H_
  6. #include "native_client/src/shared/platform/nacl_log.h"
  7. #include "native_client/src/trusted/desc/nacl_desc_base.h"
  8. #include "native_client/src/trusted/desc/nrd_xfer.h"
  9. namespace nacl {
  10. // Forward declarations.
  11. class DescWrapper;
  12. class DescWrapperCommon;
  13. // We also create a utility class that allows creation of wrappers for the
  14. // NaClDescs.
  15. class DescWrapperFactory {
  16. public:
  17. DescWrapperFactory();
  18. ~DescWrapperFactory();
  19. // Create a bound socket, socket address pair.
  20. int MakeBoundSock(DescWrapper* pair[2]);
  21. // Create a pair of DescWrappers for a connnected (data-only) socket.
  22. int MakeSocketPair(DescWrapper* pair[2]);
  23. // Create an IMC socket object.
  24. DescWrapper* MakeImcSock(NaClHandle handle);
  25. // Create a shared memory object.
  26. DescWrapper* MakeShm(size_t size);
  27. // Create a DescWrapper from a socket address string.
  28. DescWrapper* MakeSocketAddress(const char* addr);
  29. // Create a DescWrapper from opening a host file.
  30. DescWrapper* OpenHostFile(const char* fname, int flags, int mode);
  31. // Create a DescWrapper for the designated invalid descriptor
  32. DescWrapper* MakeInvalid();
  33. // Create a DescWrapper from a generic Pepper shared memory descriptor.
  34. DescWrapper* ImportPepperSharedMemory(intptr_t shm, size_t size);
  35. // Create a DescWrapper from a Pepper2D shared memory descriptor.
  36. DescWrapper* ImportPepper2DSharedMemory(intptr_t dib);
  37. // Create a DescWrapper from a Pepper synchronization object.
  38. DescWrapper* ImportPepperSync(intptr_t sock);
  39. // We will doubtless want more specific factory methods. For now,
  40. // we provide a wide-open method.
  41. DescWrapper* MakeGeneric(struct NaClDesc* desc);
  42. private:
  43. // Utility routine for importing Linux/Mac (posix) and Windows shared memory.
  44. DescWrapper* ImportShmHandle(NaClHandle handle, size_t size);
  45. // Utility routine for importing SysV shared memory.
  46. DescWrapper* ImportSysvShm(int key, size_t size);
  47. // The common data from this instance of the wrapper.
  48. DescWrapperCommon* common_data_;
  49. DISALLOW_COPY_AND_ASSIGN(DescWrapperFactory);
  50. };
  51. // A wrapper around NaClDesc to provide slightly higher level abstractions for
  52. // common operations.
  53. class DescWrapper {
  54. friend class DescWrapperFactory;
  55. public:
  56. struct MsgIoVec {
  57. void* base;
  58. nacl_abi_size_t length;
  59. };
  60. struct MsgHeader {
  61. struct MsgIoVec* iov;
  62. nacl_abi_size_t iov_length;
  63. DescWrapper** ndescv; // Pointer to array of pointers.
  64. nacl_abi_size_t ndescv_length;
  65. int32_t flags;
  66. // flags may contain 0 or any combination of the following.
  67. static const int32_t kRecvMsgDataTruncated =
  68. NACL_ABI_RECVMSG_DATA_TRUNCATED;
  69. static const int32_t kRecvMsgDescTruncated =
  70. NACL_ABI_RECVMSG_DESC_TRUNCATED;
  71. };
  72. // Delete this wrapper.
  73. void Delete() { delete this; }
  74. // Safely delete a specified wrapper. (No-op if wrapper is NULL.)
  75. static void SafeDelete(DescWrapper* wrapper) {
  76. if (NULL != wrapper) {
  77. wrapper->Delete();
  78. }
  79. }
  80. // Extract a NaClDesc from the wrapper.
  81. struct NaClDesc* desc() const { return desc_; }
  82. // Get the type of the wrapped NaClDesc.
  83. enum NaClDescTypeTag type_tag() const { return desc_->vtbl->typeTag; }
  84. // Get the path string from a NaClDescConnCap. Returns NULL if the
  85. // type of the NaClDesc is not NACL_DESC_CONN_CAP (a socket address).
  86. const char* conn_cap_path() const;
  87. // We do not replicate the underlying NaClDesc object hierarchy, so there
  88. // are obviously many more methods than a particular derived class
  89. // implements.
  90. // Map a shared memory descriptor.
  91. // Sets the address to the place mapped to and the size to the rounded result.
  92. // Returns zero on success, negative NaCl ABI errno on failure.
  93. int Map(void** addr, size_t* size);
  94. // Unmaps a region of shared memory.
  95. // Returns zero on success, negative NaCl ABI errno on failure.
  96. int Unmap(void* start_addr, size_t len);
  97. // Read len bytes into buf.
  98. // Returns bytes read on success, negative NaCl ABI errno on failure.
  99. ssize_t Read(void* buf, size_t len);
  100. // Write len bytes from buf.
  101. // Returns bytes written on success, negative NaCl ABI errno on failure.
  102. ssize_t Write(const void* buf, size_t len);
  103. // Move the file pointer.
  104. // Returns updated position on success, negative NaCl ABI errno on failure.
  105. nacl_off64_t Seek(nacl_off64_t offset, int whence);
  106. // The generic I/O control function.
  107. // Returns zero on success, negative NaCl ABI errno on failure.
  108. int Ioctl(int request, void* arg);
  109. // Get descriptor information.
  110. // Returns zero on success, negative NaCl ABI errno on failure.
  111. int Fstat(struct nacl_abi_stat* statbuf);
  112. // Close the descriptor.
  113. // Returns zero on success, negative NaCl ABI errno on failure.
  114. int Close();
  115. // Read count directory entries into dirp.
  116. // Returns count on success, negative NaCl ABI errno on failure.
  117. ssize_t Getdents(void* dirp, size_t count);
  118. // Lock a mutex.
  119. // Returns zero on success, negative NaCl ABI errno on failure.
  120. int Lock();
  121. // TryLock on a mutex.
  122. // Returns zero on success, negative NaCl ABI errno on failure.
  123. int TryLock();
  124. // Unlock a mutex.
  125. // Returns zero on success, negative NaCl ABI errno on failure.
  126. int Unlock();
  127. // Wait on a condition variable guarded by the specified mutex.
  128. // Returns zero on success, negative NaCl ABI errno on failure.
  129. int Wait(DescWrapper* mutex);
  130. // Timed wait on a condition variable guarded by the specified mutex.
  131. // Returns zero on success, negative NaCl ABI errno on failure.
  132. int TimedWaitAbs(DescWrapper* mutex, struct nacl_abi_timespec* ts);
  133. // Signal a condition variable.
  134. // Returns zero on success, negative NaCl ABI errno on failure.
  135. int Signal();
  136. // Broadcast to a condition variable.
  137. // Returns zero on success, negative NaCl ABI errno on failure.
  138. int Broadcast();
  139. // Send a message.
  140. // Returns bytes sent on success, negative NaCl ABI errno on failure.
  141. ssize_t SendMsg(const MsgHeader* dgram, int flags);
  142. // Receive a message.
  143. // Returns bytes received on success, negative NaCl ABI errno on failure.
  144. ssize_t RecvMsg(MsgHeader* dgram, int flags);
  145. // Connect to a socket address.
  146. // Returns a valid DescWrapper on success, NULL on failure.
  147. DescWrapper* Connect();
  148. // Accept connection on a bound socket.
  149. // Returns a valid DescWrapper on success, NULL on failure.
  150. DescWrapper* Accept();
  151. // Post on a semaphore.
  152. // Returns zero on success, negative NaCl ABI errno on failure.
  153. int Post();
  154. // Wait on a semaphore.
  155. // Returns zero on success, negative NaCl ABI errno on failure.
  156. int SemWait();
  157. // Get a semaphore's value.
  158. // Returns zero on success, negative NaCl ABI errno on failure.
  159. int GetValue();
  160. private:
  161. DescWrapper(DescWrapperCommon* common_data, struct NaClDesc* desc);
  162. ~DescWrapper();
  163. DescWrapperCommon* common_data_;
  164. struct NaClDesc* desc_;
  165. DISALLOW_COPY_AND_ASSIGN(DescWrapper);
  166. };
  167. } // namespace nacl
  168. #endif // NATIVE_CLIENT_SRC_TRUSTED_DESC_NACL_DESC_WRAPPER_H_