1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * The contents of this file are subject to the Mozilla Public
4 * License Version 1.1 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of
6 * the License at http://www.mozilla.org/MPL/
8 * Software distributed under the License is distributed on an "AS
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the License for the specific language governing
11 * rights and limitations under the License.
13 * The Original Code is the Netscape Portable Runtime (NSPR).
15 * The Initial Developer of the Original Code is Netscape
16 * Communications Corporation. Portions created by Netscape are
17 * Copyright (C) 1999-2000 Netscape Communications Corporation. All
22 * Alternatively, the contents of this file may be used under the
23 * terms of the GNU General Public License Version 2 or later (the
24 * "GPL"), in which case the provisions of the GPL are applicable
25 * instead of those above. If you wish to allow use of your
26 * version of this file only under the terms of the GPL and not to
27 * allow others to use your version of this file under the MPL,
28 * indicate your decision by deleting the provisions above and
29 * replace them with the notice and other provisions required by
30 * the GPL. If you do not delete the provisions above, a recipient
31 * may use your version of this file under either the MPL or the
36 ** prshma.h -- NSPR Anonymous Shared Memory
38 ** NSPR provides an anonymous shared memory based on NSPR's PRFileMap
39 ** type. The anonymous file-mapped shared memory provides an inheritable
40 ** shared memory, as in: the child process inherits the shared memory.
41 ** Compare the file-mapped anonymous shared memory to to a named shared
42 ** memory described in prshm.h. The intent is to provide a shared
43 ** memory that is accessable only by parent and child processes. ...
44 ** It's a security thing.
46 ** Depending on the underlying platform, the file-mapped shared memory
47 ** may be backed by a file. ... surprise! ... On some platforms, no
48 ** real file backs the shared memory. On platforms where the shared
49 ** memory is backed by a file, the file's name in the filesystem is
50 ** visible to other processes for only the duration of the creation of
51 ** the file, hopefully a very short time. This restricts processess
52 ** that do not inherit the shared memory from opening the file and
53 ** reading or writing its contents. Further, when all processes
54 ** using an anonymous shared memory terminate, the backing file is
55 ** deleted. ... If you are not paranoid, you're not paying attention.
57 ** The file-mapped shared memory requires a protocol for the parent
58 ** process and child process to share the memory. NSPR provides two
59 ** protocols. Use one or the other; don't mix and match.
61 ** In the first protocol, the job of passing the inheritable shared
62 ** memory is done via helper-functions with PR_CreateProcess(). In the
63 ** second protocol, the parent process is responsible for creating the
64 ** child process; the parent and child are mutually responsible for
65 ** passing a FileMap string. NSPR provides helper functions for
66 ** extracting data from the PRFileMap object. ... See the examples
69 ** Both sides should adhere strictly to the protocol for proper
70 ** operation. The pseudo-code below shows the use of a file-mapped
71 ** shared memory by a parent and child processes. In the examples, the
72 ** server creates the file-mapped shared memory, the client attaches to
78 ** fm = PR_OpenAnonFileMap(dirName, size, FilemapProt);
79 ** addr = PR_MemMap(fm);
80 ** attr = PR_NewProcessAttr();
81 ** PR_ProcessAttrSetInheritableFileMap( attr, fm, shmname );
82 ** PR_CreateProcess(Client);
83 ** PR_DestroyProcessAttr(attr);
85 ** PR_MemUnmap( addr );
86 ** PR_CloseFileMap(fm);
90 ** ... started by server via PR_CreateProcess()
91 ** fm = PR_GetInheritedFileMap( shmname );
92 ** addr = PR_MemMap(fm);
95 ** PR_CloseFileMap(fm);
101 ** fm = PR_OpenAnonFileMap(dirName, size, FilemapProt);
102 ** fmstring = PR_ExportFileMapAsString( fm );
103 ** addr = PR_MemMap(fm);
104 ** ... application specific technique to pass fmstring to child
105 ** ... yadda ... Server uses his own magic to create child
106 ** PR_MemUnmap( addr );
107 ** PR_CloseFileMap(fm);
111 ** ... started by server via his own magic
112 ** ... application specific technique to find fmstring from parent
113 ** fm = PR_ImportFileMapFromString( fmstring )
114 ** addr = PR_MemMap(fm);
116 ** PR_MemUnmap(addr);
117 ** PR_CloseFileMap(fm);
122 ** Note: The second protocol was requested by NelsonB (7/1999); this is
123 ** to accomodate servers which already create their own child processes
124 ** using platform native methods.
133 #include "prproces.h"
138 ** PR_OpenAnonFileMap() -- Creates an anonymous file-mapped shared memory
141 ** PR_OpenAnonFileMap() creates an anonymous shared memory. If the
142 ** shared memory already exists, a handle is returned to that shared
145 ** On Unix platforms, PR_OpenAnonFileMap() uses 'dirName' as a
146 ** directory name, without the trailing '/', to contain the anonymous
147 ** file. A filename is generated for the name.
149 ** On Windows platforms, dirName is ignored.
152 ** dirName -- A directory name to contain the anonymous file.
153 ** size -- The size of the shared memory
154 ** prot -- How the shared memory is mapped. See prio.h
160 ** Pointer to PRFileMap or NULL on error.
163 NSPR_API( PRFileMap
*)
167 PRFileMapProtect prot
171 ** PR_ProcessAttrSetInheritableFileMap() -- Prepare FileMap for export
172 ** to my children processes via PR_CreateProcess()
175 ** PR_ProcessAttrSetInheritableFileMap() connects the PRFileMap to
176 ** PRProcessAttr with shmname. A subsequent call to PR_CreateProcess()
177 ** makes the PRFileMap importable by the child process.
180 ** attr -- PRProcessAttr, used to pass data to PR_CreateProcess()
181 ** fm -- PRFileMap structure to be passed to the child process
182 ** shmname -- The name for the PRFileMap; used by child.
192 PR_ProcessAttrSetInheritableFileMap(
199 ** PR_GetInheritedFileMap() -- Import a PRFileMap previously exported
200 ** by my parent process via PR_CreateProcess()
203 ** PR_GetInheritedFileMap() retrieves a PRFileMap object exported from
204 ** its parent process via PR_CreateProcess().
207 ** shmname -- The name provided to PR_ProcessAttrSetInheritableFileMap()
213 ** PRFileMap pointer or NULL.
216 NSPR_API( PRFileMap
*)
217 PR_GetInheritedFileMap(
222 ** PR_ExportFileMapAsString() -- Creates a string identifying a PRFileMap
225 ** Creates an identifier, as a string, from a PRFileMap object
226 ** previously created with PR_OpenAnonFileMap().
229 ** fm -- PRFileMap pointer to be represented as a string.
230 ** bufsize -- sizeof(buf)
231 ** buf -- a buffer of length PR_FILEMAP_STRING_BUFSIZE
234 ** buf contains the stringized PRFileMap identifier
241 PR_ExportFileMapAsString(
246 #define PR_FILEMAP_STRING_BUFSIZE 128
249 ** PR_ImportFileMapFromString() -- Creates a PRFileMap from the identifying string
252 ** PR_ImportFileMapFromString() creates a PRFileMap object from a
253 ** string previously created by PR_ExportFileMapAsString().
256 ** fmstring -- string created by PR_ExportFileMapAsString()
259 ** PRFileMap pointer or NULL.
262 NSPR_API( PRFileMap
* )
263 PR_ImportFileMapFromString(
268 #endif /* prshma_h___ */