nsprpub/pr/include/prshma.h

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

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

mercurial