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