Back to index

lightning-sunbird  0.9+nobinonly
prshma.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** prshma.h -- NSPR Anonymous Shared Memory
00040 **
00041 ** NSPR provides an anonymous shared memory based on NSPR's PRFileMap
00042 ** type. The anonymous file-mapped shared memory provides an inheritable
00043 ** shared memory, as in: the child process inherits the shared memory.
00044 ** Compare the file-mapped anonymous shared memory to to a named shared
00045 ** memory described in prshm.h. The intent is to provide a shared
00046 ** memory that is accessable only by parent and child processes. ...
00047 ** It's a security thing.
00048 ** 
00049 ** Depending on the underlying platform, the file-mapped shared memory
00050 ** may be backed by a file. ... surprise! ... On some platforms, no
00051 ** real file backs the shared memory. On platforms where the shared
00052 ** memory is backed by a file, the file's name in the filesystem is
00053 ** visible to other processes for only the duration of the creation of
00054 ** the file, hopefully a very short time. This restricts processess
00055 ** that do not inherit the shared memory from opening the file and
00056 ** reading or writing its contents. Further, when all processes
00057 ** using an anonymous shared memory terminate, the backing file is
00058 ** deleted. ... If you are not paranoid, you're not paying attention.
00059 ** 
00060 ** The file-mapped shared memory requires a protocol for the parent
00061 ** process and child process to share the memory. NSPR provides two
00062 ** protocols. Use one or the other; don't mix and match.
00063 ** 
00064 ** In the first protocol, the job of passing the inheritable shared
00065 ** memory is done via helper-functions with PR_CreateProcess(). In the
00066 ** second protocol, the parent process is responsible for creating the
00067 ** child process; the parent and child are mutually responsible for
00068 ** passing a FileMap string. NSPR provides helper functions for
00069 ** extracting data from the PRFileMap object. ... See the examples
00070 ** below.
00071 ** 
00072 ** Both sides should adhere strictly to the protocol for proper
00073 ** operation. The pseudo-code below shows the use of a file-mapped
00074 ** shared memory by a parent and child processes. In the examples, the
00075 ** server creates the file-mapped shared memory, the client attaches to
00076 ** it.
00077 **
00078 ** First protocol.
00079 ** Server:
00080 **
00081 **   fm = PR_OpenAnonFileMap(dirName, size, FilemapProt); 
00082 **   addr = PR_MemMap(fm); 
00083 **   attr = PR_NewProcessAttr();
00084 **   PR_ProcessAttrSetInheritableFileMap( attr, fm, shmname );
00085 **   PR_CreateProcess(Client); 
00086 **   PR_DestroyProcessAttr(attr);
00087 **   ... yadda ...
00088 **   PR_MemUnmap( addr );
00089 **   PR_CloseFileMap(fm);
00090 **
00091 **
00092 ** Client: 
00093 **   ... started by server via PR_CreateProcess()
00094 **   fm = PR_GetInheritedFileMap( shmname );
00095 **   addr = PR_MemMap(fm);
00096 **   ... yadda ...
00097 **   PR_MemUnmap(addr);
00098 **   PR_CloseFileMap(fm);
00099 **
00100 **
00101 ** Second Protocol:
00102 ** Server:
00103 **
00104 **   fm = PR_OpenAnonFileMap(dirName, size, FilemapProt); 
00105 **   fmstring = PR_ExportFileMapAsString( fm );
00106 **   addr = PR_MemMap(fm); 
00107 **    ... application specific technique to pass fmstring to child
00108 **    ... yadda ... Server uses his own magic to create child
00109 **   PR_MemUnmap( addr );
00110 **   PR_CloseFileMap(fm);
00111 **
00112 **
00113 ** Client: 
00114 **   ... started by server via his own magic
00115 **   ... application specific technique to find fmstring from parent
00116 **   fm = PR_ImportFileMapFromString( fmstring )
00117 **   addr = PR_MemMap(fm);
00118 **   ... yadda ...
00119 **   PR_MemUnmap(addr);
00120 **   PR_CloseFileMap(fm);
00121 **
00122 **
00123 ** lth. 2-Jul-1999.
00124 **
00125 ** Note: The second protocol was requested by NelsonB (7/1999); this is
00126 ** to accomodate servers which already create their own child processes
00127 ** using platform native methods.
00128 ** 
00129 */
00130 
00131 #ifndef prshma_h___
00132 #define prshma_h___
00133 
00134 #include "prtypes.h"
00135 #include "prio.h"
00136 #include "prproces.h"
00137 
00138 PR_BEGIN_EXTERN_C
00139 
00140 /*
00141 ** PR_OpenAnonFileMap() -- Creates an anonymous file-mapped shared memory
00142 **
00143 ** Description:
00144 ** PR_OpenAnonFileMap() creates an anonymous shared memory. If the
00145 ** shared memory already exists, a handle is returned to that shared
00146 ** memory object.
00147 **
00148 ** On Unix platforms, PR_OpenAnonFileMap() uses 'dirName' as a
00149 ** directory name, without the trailing '/', to contain the anonymous
00150 ** file. A filename is generated for the name.
00151 **
00152 ** On Windows platforms, dirName is ignored.
00153 **
00154 ** Inputs:
00155 **   dirName -- A directory name to contain the anonymous file.
00156 **   size -- The size of the shared memory
00157 **   prot -- How the shared memory is mapped. See prio.h
00158 **   
00159 ** Outputs:
00160 **   PRFileMap *
00161 **
00162 ** Returns:
00163 **   Pointer to PRFileMap or NULL on error.
00164 **
00165 */
00166 NSPR_API( PRFileMap *)
00167 PR_OpenAnonFileMap(
00168     const char *dirName,
00169     PRSize      size, 
00170     PRFileMapProtect prot
00171 );  
00172 
00173 /*
00174 ** PR_ProcessAttrSetInheritableFileMap() -- Prepare FileMap for export  
00175 **   to my children processes via PR_CreateProcess()
00176 **
00177 ** Description:
00178 ** PR_ProcessAttrSetInheritableFileMap() connects the PRFileMap to
00179 ** PRProcessAttr with shmname. A subsequent call to PR_CreateProcess()
00180 ** makes the PRFileMap importable by the child process.
00181 **
00182 ** Inputs:
00183 **   attr -- PRProcessAttr, used to pass data to PR_CreateProcess()
00184 **   fm -- PRFileMap structure to be passed to the child process
00185 **   shmname -- The name for the PRFileMap; used by child.
00186 **
00187 ** Outputs:
00188 **   PRFileMap *
00189 **
00190 ** Returns:
00191 **   PRStatus
00192 **
00193 */
00194 NSPR_API(PRStatus) 
00195 PR_ProcessAttrSetInheritableFileMap( 
00196     PRProcessAttr   *attr,
00197     PRFileMap       *fm, 
00198     const char      *shmname
00199 );
00200 
00201 /*
00202 ** PR_GetInheritedFileMap() -- Import a PRFileMap previously exported
00203 **   by my parent process via PR_CreateProcess()
00204 **
00205 ** Description:
00206 ** PR_GetInheritedFileMap() retrieves a PRFileMap object exported from
00207 ** its parent process via PR_CreateProcess().
00208 **
00209 ** Inputs:
00210 **    shmname -- The name provided to PR_ProcessAttrSetInheritableFileMap()
00211 ** 
00212 ** Outputs:
00213 **   PRFileMap *
00214 **
00215 ** Returns:
00216 **   PRFileMap pointer or NULL.
00217 **
00218 */
00219 NSPR_API( PRFileMap *)
00220 PR_GetInheritedFileMap( 
00221     const char *shmname 
00222 );
00223 
00224 /*
00225 ** PR_ExportFileMapAsString() -- Creates a string identifying a PRFileMap
00226 **
00227 ** Description:
00228 ** Creates an identifier, as a string, from a PRFileMap object
00229 ** previously created with PR_OpenAnonFileMap().
00230 **
00231 ** Inputs:
00232 **   fm -- PRFileMap pointer to be represented as a string.
00233 **   bufsize -- sizeof(buf)
00234 **   buf -- a buffer of length PR_FILEMAP_STRING_BUFSIZE
00235 **
00236 ** Outputs:
00237 **   buf contains the stringized PRFileMap identifier
00238 **
00239 ** Returns:
00240 **   PRStatus
00241 **
00242 */
00243 NSPR_API( PRStatus )
00244 PR_ExportFileMapAsString( 
00245     PRFileMap *fm,
00246     PRSize    bufsize,
00247     char      *buf
00248 );
00249 #define PR_FILEMAP_STRING_BUFSIZE 128
00250 
00251 /*
00252 ** PR_ImportFileMapFromString() -- Creates a PRFileMap from the identifying string
00253 **
00254 ** Description:
00255 ** PR_ImportFileMapFromString() creates a PRFileMap object from a
00256 ** string previously created by PR_ExportFileMapAsString().
00257 **
00258 ** Inputs:
00259 **   fmstring -- string created by PR_ExportFileMapAsString()
00260 **
00261 ** Returns:
00262 **   PRFileMap pointer or NULL.
00263 **
00264 */
00265 NSPR_API( PRFileMap * )
00266 PR_ImportFileMapFromString( 
00267     const char *fmstring
00268 );
00269 
00270 PR_END_EXTERN_C
00271 #endif /* prshma_h___ */