prshma.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. ** prshma.h -- NSPR Anonymous Shared Memory
  7. **
  8. ** NSPR provides an anonymous shared memory based on NSPR's PRFileMap
  9. ** type. The anonymous file-mapped shared memory provides an inheritable
  10. ** shared memory, as in: the child process inherits the shared memory.
  11. ** Compare the file-mapped anonymous shared memory to to a named shared
  12. ** memory described in prshm.h. The intent is to provide a shared
  13. ** memory that is accessable only by parent and child processes. ...
  14. ** It's a security thing.
  15. **
  16. ** Depending on the underlying platform, the file-mapped shared memory
  17. ** may be backed by a file. ... surprise! ... On some platforms, no
  18. ** real file backs the shared memory. On platforms where the shared
  19. ** memory is backed by a file, the file's name in the filesystem is
  20. ** visible to other processes for only the duration of the creation of
  21. ** the file, hopefully a very short time. This restricts processess
  22. ** that do not inherit the shared memory from opening the file and
  23. ** reading or writing its contents. Further, when all processes
  24. ** using an anonymous shared memory terminate, the backing file is
  25. ** deleted. ... If you are not paranoid, you're not paying attention.
  26. **
  27. ** The file-mapped shared memory requires a protocol for the parent
  28. ** process and child process to share the memory. NSPR provides two
  29. ** protocols. Use one or the other; don't mix and match.
  30. **
  31. ** In the first protocol, the job of passing the inheritable shared
  32. ** memory is done via helper-functions with PR_CreateProcess(). In the
  33. ** second protocol, the parent process is responsible for creating the
  34. ** child process; the parent and child are mutually responsible for
  35. ** passing a FileMap string. NSPR provides helper functions for
  36. ** extracting data from the PRFileMap object. ... See the examples
  37. ** below.
  38. **
  39. ** Both sides should adhere strictly to the protocol for proper
  40. ** operation. The pseudo-code below shows the use of a file-mapped
  41. ** shared memory by a parent and child processes. In the examples, the
  42. ** server creates the file-mapped shared memory, the client attaches to
  43. ** it.
  44. **
  45. ** First protocol.
  46. ** Server:
  47. **
  48. ** fm = PR_OpenAnonFileMap(dirName, size, FilemapProt);
  49. ** addr = PR_MemMap(fm);
  50. ** attr = PR_NewProcessAttr();
  51. ** PR_ProcessAttrSetInheritableFileMap( attr, fm, shmname );
  52. ** PR_CreateProcess(Client);
  53. ** PR_DestroyProcessAttr(attr);
  54. ** ... yadda ...
  55. ** PR_MemUnmap( addr );
  56. ** PR_CloseFileMap(fm);
  57. **
  58. **
  59. ** Client:
  60. ** ... started by server via PR_CreateProcess()
  61. ** fm = PR_GetInheritedFileMap( shmname );
  62. ** addr = PR_MemMap(fm);
  63. ** ... yadda ...
  64. ** PR_MemUnmap(addr);
  65. ** PR_CloseFileMap(fm);
  66. **
  67. **
  68. ** Second Protocol:
  69. ** Server:
  70. **
  71. ** fm = PR_OpenAnonFileMap(dirName, size, FilemapProt);
  72. ** fmstring = PR_ExportFileMapAsString( fm );
  73. ** addr = PR_MemMap(fm);
  74. ** ... application specific technique to pass fmstring to child
  75. ** ... yadda ... Server uses his own magic to create child
  76. ** PR_MemUnmap( addr );
  77. ** PR_CloseFileMap(fm);
  78. **
  79. **
  80. ** Client:
  81. ** ... started by server via his own magic
  82. ** ... application specific technique to find fmstring from parent
  83. ** fm = PR_ImportFileMapFromString( fmstring )
  84. ** addr = PR_MemMap(fm);
  85. ** ... yadda ...
  86. ** PR_MemUnmap(addr);
  87. ** PR_CloseFileMap(fm);
  88. **
  89. **
  90. ** lth. 2-Jul-1999.
  91. **
  92. ** Note: The second protocol was requested by NelsonB (7/1999); this is
  93. ** to accomodate servers which already create their own child processes
  94. ** using platform native methods.
  95. **
  96. */
  97. #ifndef prshma_h___
  98. #define prshma_h___
  99. #include "prtypes.h"
  100. #include "prio.h"
  101. #include "prproces.h"
  102. PR_BEGIN_EXTERN_C
  103. /*
  104. ** PR_OpenAnonFileMap() -- Creates an anonymous file-mapped shared memory
  105. **
  106. ** Description:
  107. ** PR_OpenAnonFileMap() creates an anonymous shared memory. If the
  108. ** shared memory already exists, a handle is returned to that shared
  109. ** memory object.
  110. **
  111. ** On Unix platforms, PR_OpenAnonFileMap() uses 'dirName' as a
  112. ** directory name, without the trailing '/', to contain the anonymous
  113. ** file. A filename is generated for the name.
  114. **
  115. ** On Windows platforms, dirName is ignored.
  116. **
  117. ** Inputs:
  118. ** dirName -- A directory name to contain the anonymous file.
  119. ** size -- The size of the shared memory
  120. ** prot -- How the shared memory is mapped. See prio.h
  121. **
  122. ** Outputs:
  123. ** PRFileMap *
  124. **
  125. ** Returns:
  126. ** Pointer to PRFileMap or NULL on error.
  127. **
  128. */
  129. NSPR_API( PRFileMap *)
  130. PR_OpenAnonFileMap(
  131. const char *dirName,
  132. PRSize size,
  133. PRFileMapProtect prot
  134. );
  135. /*
  136. ** PR_ProcessAttrSetInheritableFileMap() -- Prepare FileMap for export
  137. ** to my children processes via PR_CreateProcess()
  138. **
  139. ** Description:
  140. ** PR_ProcessAttrSetInheritableFileMap() connects the PRFileMap to
  141. ** PRProcessAttr with shmname. A subsequent call to PR_CreateProcess()
  142. ** makes the PRFileMap importable by the child process.
  143. **
  144. ** Inputs:
  145. ** attr -- PRProcessAttr, used to pass data to PR_CreateProcess()
  146. ** fm -- PRFileMap structure to be passed to the child process
  147. ** shmname -- The name for the PRFileMap; used by child.
  148. **
  149. ** Outputs:
  150. ** PRFileMap *
  151. **
  152. ** Returns:
  153. ** PRStatus
  154. **
  155. */
  156. NSPR_API(PRStatus)
  157. PR_ProcessAttrSetInheritableFileMap(
  158. PRProcessAttr *attr,
  159. PRFileMap *fm,
  160. const char *shmname
  161. );
  162. /*
  163. ** PR_GetInheritedFileMap() -- Import a PRFileMap previously exported
  164. ** by my parent process via PR_CreateProcess()
  165. **
  166. ** Description:
  167. ** PR_GetInheritedFileMap() retrieves a PRFileMap object exported from
  168. ** its parent process via PR_CreateProcess().
  169. **
  170. ** Inputs:
  171. ** shmname -- The name provided to PR_ProcessAttrSetInheritableFileMap()
  172. **
  173. ** Outputs:
  174. ** PRFileMap *
  175. **
  176. ** Returns:
  177. ** PRFileMap pointer or NULL.
  178. **
  179. */
  180. NSPR_API( PRFileMap *)
  181. PR_GetInheritedFileMap(
  182. const char *shmname
  183. );
  184. /*
  185. ** PR_ExportFileMapAsString() -- Creates a string identifying a PRFileMap
  186. **
  187. ** Description:
  188. ** Creates an identifier, as a string, from a PRFileMap object
  189. ** previously created with PR_OpenAnonFileMap().
  190. **
  191. ** Inputs:
  192. ** fm -- PRFileMap pointer to be represented as a string.
  193. ** bufsize -- sizeof(buf)
  194. ** buf -- a buffer of length PR_FILEMAP_STRING_BUFSIZE
  195. **
  196. ** Outputs:
  197. ** buf contains the stringized PRFileMap identifier
  198. **
  199. ** Returns:
  200. ** PRStatus
  201. **
  202. */
  203. NSPR_API( PRStatus )
  204. PR_ExportFileMapAsString(
  205. PRFileMap *fm,
  206. PRSize bufsize,
  207. char *buf
  208. );
  209. #define PR_FILEMAP_STRING_BUFSIZE 128
  210. /*
  211. ** PR_ImportFileMapFromString() -- Creates a PRFileMap from the identifying string
  212. **
  213. ** Description:
  214. ** PR_ImportFileMapFromString() creates a PRFileMap object from a
  215. ** string previously created by PR_ExportFileMapAsString().
  216. **
  217. ** Inputs:
  218. ** fmstring -- string created by PR_ExportFileMapAsString()
  219. **
  220. ** Returns:
  221. ** PRFileMap pointer or NULL.
  222. **
  223. */
  224. NSPR_API( PRFileMap * )
  225. PR_ImportFileMapFromString(
  226. const char *fmstring
  227. );
  228. PR_END_EXTERN_C
  229. #endif /* prshma_h___ */