~ubuntu-branches/ubuntu/intrepid/xulrunner-1.9/intrepid

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/src/md/unix/uxshm.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack, Alexander Sack, Fabien Tassin
  • Date: 2008-02-13 11:47:21 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080213114721-7om0mgzngvuk9czv
Tags: 1.9~b3+nobinonly-0ubuntu1
* release FIREFOX_3_0b3_RELEASE

[ Alexander Sack ]
* submit patch that ships xpcshell to bugzilla
  - rename debian/patches/ship_xpcshell.patch =>
           debian/patches/bz410617_att295212_ship_xpcshell.patch
  - update debian/patches/series
* fix tooltip in epiphany: previously displayed out of screen bounds
  (LP: #37507)
  - add debian/patches/bz233371_att297343_fix_outofscreen_embed_tooltip.patch
  - update debian/patches/series
* use default upstream gcc tweaks for improved performance - especially of the
  javascript engine
  - update debian/rules
* update global extension/plugin patch to apply cleanly against latest code
  - update debian/patches/bzXXX_gre_extension_plugin_support.patch
* fix pyxpcom build failure introduced by recent commit
  - add debian/patches/bzXXX_fix_pyxpcom_build_failure.patch
  - update debian/patches/series
* add distro independent global install locations for extensions,
  /usr/lib/mozilla/extensions and /usr/share/mozilla/extensions
  - update debian/xulrunner-1.9.dirs
* support embedded tarball layout when either there is a *.tar.bz2 in orig tarball
  or if DEBIAN_MOZCLIENT_EMBEDDED is not unset (you will need this to produce embedded
  tarballs during |get-orig-source|
  - update debian/rules
* bump minimum libnss3-dev build requirements to >= 3.12.0~1.9b3
  - update debian/control
* bump minimum libnspr4-dev build requirements to >= 4.7.0~1.9b3
  - update debian/control

[ Fabien Tassin ]
* Drop patches applied upstream
  - drop debian/patches/bz410617_att295212_ship_xpcshell.patch
  - drop debian/patches/bz404634_att294921.patch
  - drop debian/patches/bz386610_python2.5_ftbfs_amd64.patch
  - drop debian/patches/bz373918_att295042.patch
  - drop debian/patches/bz408062_unstable_pc.patch
  - drop debian/patches/bz384304_fix_recursive_symlinks.patch
  - update debian/patches/series
* Refresh diverged patches:
  - update debian/patches/bzXXX_pc_honour_system_nspr_nss.patch
  - update debian/patches/rename_venkman_addon.patch
  - update debian/patches/bz344818_cairo_xrender.patch
* Install links for all .so libs in the -dev package
  - update debian/patches/dont_install_so_in_dev.patch
* Bump gtk requirement to 2.12 as per Mozilla bug 412432
  - update debian/control
* Add #DEBHELPER# token to postinst/prerm scripts
  - update debian/xulrunner-1.9.{postinst,prerm}
* Install previously missed libdbusservice.so
  - update debian/xulrunner-1.9.install
* Update venkman patch to also rename locales
  - update debian/patches/rename_venkman_addon.patch
* Bump requirement for system cairo to >= 1.5.8 as we now need
  the newly added cairo_path_extents()
  - update debian/rules
* Include mozilla-devscripts file using -include so ifneq could be omitted
  - update debian/rules
* Fix missing .so symlinks regression
  - update debian/patches/dont_install_so_in_dev.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
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) 1998-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
 
** uxshm.c -- Unix Implementations NSPR Named Shared Memory
40
 
**
41
 
**
42
 
** lth. Jul-1999.
43
 
**
44
 
*/
45
 
#include <string.h>
46
 
#include <prshm.h>
47
 
#include <prerr.h>
48
 
#include <prmem.h>
49
 
#include "primpl.h"       
50
 
#include <fcntl.h>
51
 
 
52
 
extern PRLogModuleInfo *_pr_shm_lm;
53
 
 
54
 
 
55
 
#define NSPR_IPC_SHM_KEY 'b'
56
 
/*
57
 
** Implementation for System V
58
 
*/
59
 
#if defined PR_HAVE_SYSV_NAMED_SHARED_MEMORY
60
 
#include <sys/ipc.h>
61
 
#include <sys/shm.h>
62
 
#include <sys/types.h>
63
 
#include <sys/stat.h>
64
 
 
65
 
#define _MD_OPEN_SHARED_MEMORY _MD_OpenSharedMemory
66
 
#define _MD_ATTACH_SHARED_MEMORY _MD_AttachSharedMemory
67
 
#define _MD_DETACH_SHARED_MEMORY _MD_DetachSharedMemory
68
 
#define _MD_CLOSE_SHARED_MEMORY _MD_CloseSharedMemory
69
 
#define _MD_DELETE_SHARED_MEMORY  _MD_DeleteSharedMemory
70
 
 
71
 
extern PRSharedMemory * _MD_OpenSharedMemory( 
72
 
    const char *name,
73
 
    PRSize      size,
74
 
    PRIntn      flags,
75
 
    PRIntn      mode
76
 
)
77
 
{
78
 
    PRStatus rc = PR_SUCCESS;
79
 
    key_t   key;
80
 
    PRSharedMemory *shm;
81
 
    char        ipcname[PR_IPC_NAME_SIZE];
82
 
 
83
 
    rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm );
84
 
    if ( PR_FAILURE == rc )
85
 
    {
86
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
87
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
88
 
            ("_MD_OpenSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name ));
89
 
        return( NULL );
90
 
    }
91
 
 
92
 
    shm = PR_NEWZAP( PRSharedMemory );
93
 
    if ( NULL == shm ) 
94
 
    {
95
 
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 );
96
 
        PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New PRSharedMemory out of memory")); 
97
 
        return( NULL );
98
 
    }
99
 
 
100
 
    shm->ipcname = (char*)PR_MALLOC( strlen( ipcname ) + 1 );
101
 
    if ( NULL == shm->ipcname )
102
 
    {
103
 
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 );
104
 
        PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New shm->ipcname out of memory")); 
105
 
        PR_DELETE( shm );
106
 
        return( NULL );
107
 
    }
108
 
 
109
 
    /* copy args to struct */
110
 
    strcpy( shm->ipcname, ipcname );
111
 
    shm->size = size; 
112
 
    shm->mode = mode; 
113
 
    shm->flags = flags;
114
 
    shm->ident = _PR_SHM_IDENT;
115
 
 
116
 
    /* create the file first */
117
 
    if ( flags & PR_SHM_CREATE )  {
118
 
        int osfd = open( shm->ipcname, (O_RDWR | O_CREAT), shm->mode );
119
 
        if ( -1 == osfd ) {
120
 
            _PR_MD_MAP_OPEN_ERROR( errno );
121
 
            PR_FREEIF( shm->ipcname );
122
 
            PR_DELETE( shm );
123
 
            return( NULL );
124
 
        } 
125
 
        if ( close(osfd) == -1 ) {
126
 
            _PR_MD_MAP_CLOSE_ERROR( errno );
127
 
            PR_FREEIF( shm->ipcname );
128
 
            PR_DELETE( shm );
129
 
            return( NULL );
130
 
        }
131
 
    }
132
 
 
133
 
    /* hash the shm.name to an ID */
134
 
    key = ftok( shm->ipcname, NSPR_IPC_SHM_KEY );
135
 
    if ( -1 == key )
136
 
    {
137
 
        rc = PR_FAILURE;
138
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
139
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
140
 
            ("_MD_OpenSharedMemory(): ftok() failed on name: %s", shm->ipcname));
141
 
        PR_FREEIF( shm->ipcname );
142
 
        PR_DELETE( shm );
143
 
        return( NULL );
144
 
    }
145
 
 
146
 
    /* get the shared memory */
147
 
    if ( flags & PR_SHM_CREATE )  {
148
 
        shm->id = shmget( key, shm->size, ( shm->mode | IPC_CREAT|IPC_EXCL));
149
 
        if ( shm->id >= 0 ) {
150
 
            return( shm );
151
 
        }
152
 
        if ((errno == EEXIST) && (flags & PR_SHM_EXCL)) {
153
 
            PR_SetError( PR_FILE_EXISTS_ERROR, errno );
154
 
            PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
155
 
                ("_MD_OpenSharedMemory(): shmget() exclusive failed, errno: %d", errno));
156
 
            PR_FREEIF(shm->ipcname);
157
 
            PR_DELETE(shm);
158
 
            return(NULL);
159
 
        }
160
 
    } 
161
 
 
162
 
    shm->id = shmget( key, shm->size, shm->mode );
163
 
    if ( -1 == shm->id ) {
164
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
165
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
166
 
            ("_MD_OpenSharedMemory(): shmget() failed, errno: %d", errno));
167
 
        PR_FREEIF(shm->ipcname);
168
 
        PR_DELETE(shm);
169
 
        return(NULL);
170
 
    }
171
 
 
172
 
    return( shm );
173
 
} /* end _MD_OpenSharedMemory() */
174
 
 
175
 
extern void * _MD_AttachSharedMemory( PRSharedMemory *shm, PRIntn flags )
176
 
{
177
 
    void        *addr;
178
 
    PRUint32    aFlags = shm->mode;
179
 
 
180
 
    PR_ASSERT( shm->ident == _PR_SHM_IDENT );
181
 
 
182
 
    aFlags |= (flags & PR_SHM_READONLY )? SHM_RDONLY : 0;
183
 
 
184
 
    addr = shmat( shm->id, NULL, aFlags );
185
 
    if ( (void*)-1 == addr )
186
 
    {
187
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
188
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
189
 
            ("_MD_AttachSharedMemory(): shmat() failed on name: %s, OsError: %d", 
190
 
                shm->ipcname, PR_GetOSError() ));
191
 
        addr = NULL;
192
 
    }
193
 
 
194
 
    return addr;
195
 
}    
196
 
 
197
 
extern PRStatus _MD_DetachSharedMemory( PRSharedMemory *shm, void *addr )
198
 
{
199
 
    PRStatus rc = PR_SUCCESS;
200
 
    PRIntn   urc;
201
 
 
202
 
    PR_ASSERT( shm->ident == _PR_SHM_IDENT );
203
 
 
204
 
    urc = shmdt( addr );
205
 
    if ( -1 == urc )
206
 
    {
207
 
        rc = PR_FAILURE;
208
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
209
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
210
 
            ("_MD_DetachSharedMemory(): shmdt() failed on name: %s", shm->ipcname ));
211
 
    }
212
 
 
213
 
    return rc;
214
 
}    
215
 
 
216
 
extern PRStatus _MD_CloseSharedMemory( PRSharedMemory *shm )
217
 
{
218
 
    PR_ASSERT( shm->ident == _PR_SHM_IDENT );
219
 
 
220
 
    PR_FREEIF(shm->ipcname);
221
 
    PR_DELETE(shm);
222
 
 
223
 
    return PR_SUCCESS;
224
 
}    
225
 
 
226
 
extern PRStatus _MD_DeleteSharedMemory( const char *name )
227
 
{
228
 
    PRStatus rc = PR_SUCCESS;
229
 
    key_t   key;
230
 
    int     id;
231
 
    PRIntn  urc;
232
 
    char        ipcname[PR_IPC_NAME_SIZE];
233
 
 
234
 
    rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm );
235
 
    if ( PR_FAILURE == rc )
236
 
    {
237
 
        PR_SetError( PR_UNKNOWN_ERROR , errno );
238
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
239
 
            ("_MD_DeleteSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name ));
240
 
        return(PR_FAILURE);
241
 
    }
242
 
 
243
 
    /* create the file first */ 
244
 
    {
245
 
        int osfd = open( ipcname, (O_RDWR | O_CREAT), 0666 );
246
 
        if ( -1 == osfd ) {
247
 
            _PR_MD_MAP_OPEN_ERROR( errno );
248
 
            return( PR_FAILURE );
249
 
        } 
250
 
        if ( close(osfd) == -1 ) {
251
 
            _PR_MD_MAP_CLOSE_ERROR( errno );
252
 
            return( PR_FAILURE );
253
 
        }
254
 
    }
255
 
 
256
 
    /* hash the shm.name to an ID */
257
 
    key = ftok( ipcname, NSPR_IPC_SHM_KEY );
258
 
    if ( -1 == key )
259
 
    {
260
 
        rc = PR_FAILURE;
261
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
262
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
263
 
            ("_MD_DeleteSharedMemory(): ftok() failed on name: %s", ipcname));
264
 
    }
265
 
 
266
 
    id = shmget( key, 0, 0 );
267
 
    if ( -1 == id ) {
268
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
269
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
270
 
            ("_MD_DeleteSharedMemory(): shmget() failed, errno: %d", errno));
271
 
        return(PR_FAILURE);
272
 
    }
273
 
 
274
 
    urc = shmctl( id, IPC_RMID, NULL );
275
 
    if ( -1 == urc )
276
 
    {
277
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
278
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
279
 
            ("_MD_DeleteSharedMemory(): shmctl() failed on name: %s", ipcname ));
280
 
        return(PR_FAILURE);
281
 
    }
282
 
 
283
 
    urc = unlink( ipcname );
284
 
    if ( -1 == urc ) {
285
 
        _PR_MD_MAP_UNLINK_ERROR( errno );
286
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
287
 
            ("_MD_DeleteSharedMemory(): unlink() failed: %s", ipcname ));
288
 
        return(PR_FAILURE);
289
 
    }
290
 
 
291
 
    return rc;
292
 
}  /* end _MD_DeleteSharedMemory() */
293
 
 
294
 
/*
295
 
** Implementation for Posix
296
 
*/
297
 
#elif defined PR_HAVE_POSIX_NAMED_SHARED_MEMORY
298
 
#include <sys/mman.h>
299
 
 
300
 
#define _MD_OPEN_SHARED_MEMORY _MD_OpenSharedMemory
301
 
#define _MD_ATTACH_SHARED_MEMORY _MD_AttachSharedMemory
302
 
#define _MD_DETACH_SHARED_MEMORY _MD_DetachSharedMemory
303
 
#define _MD_CLOSE_SHARED_MEMORY _MD_CloseSharedMemory
304
 
#define _MD_DELETE_SHARED_MEMORY  _MD_DeleteSharedMemory
305
 
 
306
 
struct _MDSharedMemory {
307
 
    int     handle;
308
 
};
309
 
 
310
 
extern PRSharedMemory * _MD_OpenSharedMemory( 
311
 
    const char *name,
312
 
    PRSize      size,
313
 
    PRIntn      flags,
314
 
    PRIntn      mode
315
 
)
316
 
{
317
 
    PRStatus    rc = PR_SUCCESS;
318
 
    PRInt32     end;
319
 
    PRSharedMemory *shm;
320
 
    char        ipcname[PR_IPC_NAME_SIZE];
321
 
 
322
 
    rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm );
323
 
    if ( PR_FAILURE == rc )
324
 
    {
325
 
        PR_SetError( PR_UNKNOWN_ERROR , errno );
326
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
327
 
            ("_MD_OpenSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name ));
328
 
        return( NULL );
329
 
    }
330
 
 
331
 
    shm = PR_NEWZAP( PRSharedMemory );
332
 
    if ( NULL == shm ) 
333
 
    {
334
 
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 );
335
 
        PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New PRSharedMemory out of memory")); 
336
 
        return( NULL );
337
 
    }
338
 
 
339
 
    shm->ipcname = PR_MALLOC( strlen( ipcname ) + 1 );
340
 
    if ( NULL == shm->ipcname )
341
 
    {
342
 
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 );
343
 
        PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, ( "PR_OpenSharedMemory: New shm->ipcname out of memory")); 
344
 
        return( NULL );
345
 
    }
346
 
 
347
 
    /* copy args to struct */
348
 
    strcpy( shm->ipcname, ipcname );
349
 
    shm->size = size; 
350
 
    shm->mode = mode;
351
 
    shm->flags = flags;
352
 
    shm->ident = _PR_SHM_IDENT;
353
 
 
354
 
    /*
355
 
    ** Create the shared memory
356
 
    */
357
 
    if ( flags & PR_SHM_CREATE )  {
358
 
        int oflag = (O_CREAT | O_RDWR);
359
 
        
360
 
        if ( flags & PR_SHM_EXCL )
361
 
            oflag |= O_EXCL;
362
 
        shm->id = shm_open( shm->ipcname, oflag, shm->mode );
363
 
    } else {
364
 
        shm->id = shm_open( shm->ipcname, O_RDWR, shm->mode );
365
 
    }
366
 
 
367
 
    if ( -1 == shm->id )  {
368
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
369
 
        PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, 
370
 
            ("_MD_OpenSharedMemory(): shm_open failed: %s, OSError: %d",
371
 
                shm->ipcname, PR_GetOSError())); 
372
 
        PR_DELETE( shm->ipcname );
373
 
        PR_DELETE( shm );
374
 
        return(NULL);
375
 
    }
376
 
 
377
 
    end = ftruncate( shm->id, shm->size );
378
 
    if ( -1 == end ) {
379
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
380
 
        PR_LOG(_pr_shm_lm, PR_LOG_DEBUG, 
381
 
            ("_MD_OpenSharedMemory(): ftruncate failed, OSError: %d",
382
 
                PR_GetOSError()));
383
 
        PR_DELETE( shm->ipcname );
384
 
        PR_DELETE( shm );
385
 
        return(NULL);
386
 
    }
387
 
 
388
 
    return(shm);
389
 
} /* end _MD_OpenSharedMemory() */
390
 
 
391
 
extern void * _MD_AttachSharedMemory( PRSharedMemory *shm, PRIntn flags )
392
 
{
393
 
    void        *addr;
394
 
    PRIntn      prot = (PROT_READ | PROT_WRITE);
395
 
 
396
 
    PR_ASSERT( shm->ident == _PR_SHM_IDENT );
397
 
 
398
 
    if ( PR_SHM_READONLY == flags)
399
 
        prot ^= PROT_WRITE;
400
 
 
401
 
    addr = mmap( (void*)0, shm->size, prot, MAP_SHARED, shm->id, 0 );
402
 
    if ((void*)-1 == addr )
403
 
    {
404
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
405
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
406
 
            ("_MD_AttachSharedMemory(): mmap failed: %s, errno: %d",
407
 
                shm->ipcname, PR_GetOSError()));
408
 
        addr = NULL;
409
 
    } else {
410
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
411
 
            ("_MD_AttachSharedMemory(): name: %s, attached at: %p", shm->ipcname, addr));
412
 
    }
413
 
    
414
 
    return addr;
415
 
}    
416
 
 
417
 
extern PRStatus _MD_DetachSharedMemory( PRSharedMemory *shm, void *addr )
418
 
{
419
 
    PRStatus    rc = PR_SUCCESS;
420
 
    PRIntn      urc;
421
 
 
422
 
    PR_ASSERT( shm->ident == _PR_SHM_IDENT );
423
 
 
424
 
    urc = munmap( addr, shm->size );
425
 
    if ( -1 == urc )
426
 
    {
427
 
        rc = PR_FAILURE;
428
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
429
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
430
 
            ("_MD_DetachSharedMemory(): munmap failed: %s, errno: %d", 
431
 
                shm->ipcname, PR_GetOSError()));
432
 
    }
433
 
    return rc;
434
 
}    
435
 
 
436
 
extern PRStatus _MD_CloseSharedMemory( PRSharedMemory *shm )
437
 
{
438
 
    int urc;
439
 
    
440
 
    PR_ASSERT( shm->ident == _PR_SHM_IDENT );
441
 
 
442
 
    urc = close( shm->id );
443
 
    if ( -1 == urc ) {
444
 
        _PR_MD_MAP_CLOSE_ERROR( errno );
445
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
446
 
            ("_MD_CloseSharedMemory(): close() failed, error: %d", PR_GetOSError()));
447
 
        return(PR_FAILURE);
448
 
    }
449
 
    PR_DELETE( shm->ipcname );
450
 
    PR_DELETE( shm );
451
 
    return PR_SUCCESS;
452
 
}    
453
 
 
454
 
extern PRStatus _MD_DeleteSharedMemory( const char *name )
455
 
{
456
 
    PRStatus    rc = PR_SUCCESS;
457
 
    PRUintn     urc;
458
 
    char        ipcname[PR_IPC_NAME_SIZE];
459
 
 
460
 
    rc = _PR_MakeNativeIPCName( name, ipcname, PR_IPC_NAME_SIZE, _PRIPCShm );
461
 
    if ( PR_FAILURE == rc )
462
 
    {
463
 
        PR_SetError( PR_UNKNOWN_ERROR , errno );
464
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
465
 
            ("_MD_OpenSharedMemory(): _PR_MakeNativeIPCName() failed: %s", name ));
466
 
        return rc;
467
 
    }
468
 
 
469
 
    urc = shm_unlink( ipcname );
470
 
    if ( -1 == urc ) {
471
 
        rc = PR_FAILURE;
472
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
473
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
474
 
            ("_MD_DeleteSharedMemory(): shm_unlink failed: %s, errno: %d", 
475
 
                ipcname, PR_GetOSError()));
476
 
    } else {
477
 
        PR_LOG( _pr_shm_lm, PR_LOG_DEBUG, 
478
 
            ("_MD_DeleteSharedMemory(): %s, success", ipcname));
479
 
    }
480
 
 
481
 
    return rc;
482
 
} /* end _MD_DeleteSharedMemory() */
483
 
#endif
484
 
 
485
 
 
486
 
 
487
 
/*
488
 
** Unix implementation for anonymous memory (file) mapping
489
 
*/
490
 
extern PRLogModuleInfo *_pr_shma_lm;
491
 
 
492
 
#include <unistd.h>
493
 
 
494
 
extern PRFileMap* _md_OpenAnonFileMap( 
495
 
    const char *dirName,
496
 
    PRSize      size,
497
 
    PRFileMapProtect prot
498
 
)
499
 
{
500
 
    PRFileMap   *fm = NULL;
501
 
    PRFileDesc  *fd;
502
 
    int         osfd;
503
 
    PRIntn      urc;
504
 
    PRIntn      mode = 0600;
505
 
    char        *genName;
506
 
    pid_t       pid = getpid(); /* for generating filename */
507
 
    PRThread    *tid = PR_GetCurrentThread(); /* for generating filename */
508
 
    int         incr; /* for generating filename */
509
 
    const int   maxTries = 20; /* maximum # attempts at a unique filename */
510
 
    PRInt64     size64; /* 64-bit version of 'size' */
511
 
 
512
 
    /*
513
 
    ** generate a filename from input and runtime environment
514
 
    ** open the file, unlink the file.
515
 
    ** make maxTries number of attempts at uniqueness in the filename
516
 
    */
517
 
    for ( incr = 0; incr < maxTries ; incr++ ) {
518
 
        genName = PR_smprintf( "%s/.NSPR-AFM-%d-%p.%d", 
519
 
            dirName, (int) pid, tid, incr );
520
 
        if ( NULL == genName ) {
521
 
            PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
522
 
                ("_md_OpenAnonFileMap(): PR_snprintf(): failed, generating filename"));
523
 
            goto Finished;
524
 
        }
525
 
        
526
 
        /* create the file */
527
 
        osfd = open( genName, (O_CREAT | O_EXCL | O_RDWR), mode );
528
 
        if ( -1 == osfd ) {
529
 
            if ( EEXIST == errno )  {
530
 
                PR_smprintf_free( genName );
531
 
                continue; /* name exists, try again */
532
 
            } else {
533
 
                _PR_MD_MAP_OPEN_ERROR( errno );
534
 
                PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
535
 
                    ("_md_OpenAnonFileMap(): open(): failed, filename: %s, errno: %d", 
536
 
                        genName, PR_GetOSError()));
537
 
                PR_smprintf_free( genName );
538
 
                goto Finished;
539
 
            }
540
 
        }
541
 
        break; /* name generation and open successful, break; */
542
 
    } /* end for() */
543
 
 
544
 
    if ( incr == maxTries ) {
545
 
        PR_ASSERT( -1 == osfd );
546
 
        PR_ASSERT( EEXIST == errno );
547
 
        _PR_MD_MAP_OPEN_ERROR( errno );
548
 
        goto Finished;
549
 
    }
550
 
 
551
 
    urc = unlink( genName );
552
 
    if ( -1 == urc ) {
553
 
        _PR_MD_MAP_UNLINK_ERROR( errno );
554
 
        PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
555
 
            ("_md_OpenAnonFileMap(): failed on unlink(), errno: %d", errno));
556
 
        PR_smprintf_free( genName );
557
 
        close( osfd );
558
 
        goto Finished;        
559
 
    }
560
 
    PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
561
 
        ("_md_OpenAnonFileMap(): unlink(): %s", genName ));
562
 
 
563
 
    PR_smprintf_free( genName );
564
 
 
565
 
    fd = PR_ImportFile( osfd );
566
 
    if ( NULL == fd ) {
567
 
        PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
568
 
            ("_md_OpenAnonFileMap(): PR_ImportFile(): failed"));
569
 
        goto Finished;        
570
 
    }
571
 
    PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
572
 
        ("_md_OpenAnonFileMap(): fd: %p", fd ));
573
 
 
574
 
    urc = ftruncate( fd->secret->md.osfd, size );
575
 
    if ( -1 == urc ) {
576
 
        _PR_MD_MAP_DEFAULT_ERROR( errno );
577
 
        PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
578
 
            ("_md_OpenAnonFileMap(): failed on ftruncate(), errno: %d", errno));
579
 
        PR_Close( fd );
580
 
        goto Finished;        
581
 
    }
582
 
    PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
583
 
        ("_md_OpenAnonFileMap(): ftruncate(): size: %d", size ));
584
 
 
585
 
    LL_UI2L(size64, size);  /* PRSize (size_t) is unsigned */
586
 
    fm = PR_CreateFileMap( fd, size64, prot );
587
 
    if ( NULL == fm )  {
588
 
        PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
589
 
            ("PR_OpenAnonFileMap(): failed"));
590
 
        PR_Close( fd );
591
 
        goto Finished;        
592
 
    }
593
 
    fm->md.isAnonFM = PR_TRUE; /* set fd close */
594
 
 
595
 
    PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
596
 
        ("_md_OpenAnonFileMap(): PR_CreateFileMap(): fm: %p", fm ));
597
 
 
598
 
Finished:    
599
 
    return(fm);
600
 
} /* end md_OpenAnonFileMap() */
601
 
 
602
 
/*
603
 
** _md_ExportFileMapAsString()
604
 
**
605
 
**
606
 
*/
607
 
extern PRStatus _md_ExportFileMapAsString(
608
 
    PRFileMap *fm,
609
 
    PRSize    bufSize,
610
 
    char      *buf
611
 
)
612
 
{
613
 
    PRIntn  written;
614
 
    PRIntn  prot = (PRIntn)fm->prot;
615
 
    
616
 
    written = PR_snprintf( buf, bufSize, "%ld:%d",
617
 
        fm->fd->secret->md.osfd, prot );
618
 
        
619
 
    return((written == -1)? PR_FAILURE : PR_SUCCESS);
620
 
} /* end _md_ExportFileMapAsString() */
621
 
 
622
 
 
623
 
extern PRFileMap * _md_ImportFileMapFromString(
624
 
    const char *fmstring
625
 
)
626
 
{
627
 
    PRStatus    rc;
628
 
    PRInt32     osfd;
629
 
    PRIntn      prot; /* really: a PRFileMapProtect */
630
 
    PRFileDesc  *fd;
631
 
    PRFileMap   *fm = NULL; /* default return value */
632
 
    PRFileInfo64 info;
633
 
 
634
 
    PR_sscanf( fmstring, "%ld:%d", &osfd, &prot );
635
 
 
636
 
    /* import the os file descriptor */
637
 
    fd = PR_ImportFile( osfd );
638
 
    if ( NULL == fd ) {
639
 
        PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
640
 
            ("_md_ImportFileMapFromString(): PR_ImportFile() failed"));
641
 
        goto Finished;
642
 
    }
643
 
 
644
 
    rc = PR_GetOpenFileInfo64( fd, &info );
645
 
    if ( PR_FAILURE == rc )  {
646
 
        PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
647
 
            ("_md_ImportFileMapFromString(): PR_GetOpenFileInfo64() failed"));    
648
 
        goto Finished;
649
 
    }
650
 
 
651
 
    fm = PR_CreateFileMap( fd, info.size, (PRFileMapProtect)prot );
652
 
    if ( NULL == fm ) {
653
 
        PR_LOG( _pr_shma_lm, PR_LOG_DEBUG,
654
 
            ("_md_ImportFileMapFromString(): PR_CreateFileMap() failed"));    
655
 
    }
656
 
 
657
 
Finished:
658
 
    return(fm);
659
 
} /* end _md_ImportFileMapFromString() */