1*7c478bd9Sstevel@tonic-gate /*- 2*7c478bd9Sstevel@tonic-gate * See the file LICENSE for redistribution information. 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998 5*7c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved. 6*7c478bd9Sstevel@tonic-gate */ 7*7c478bd9Sstevel@tonic-gate 8*7c478bd9Sstevel@tonic-gate #include "config.h" 9*7c478bd9Sstevel@tonic-gate 10*7c478bd9Sstevel@tonic-gate #ifndef lint 11*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)os_map.c 10.24 (Sleepycat) 10/12/98"; 12*7c478bd9Sstevel@tonic-gate #endif /* not lint */ 13*7c478bd9Sstevel@tonic-gate 14*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES 15*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 16*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 17*7c478bd9Sstevel@tonic-gate #include <sys/mman.h> 18*7c478bd9Sstevel@tonic-gate #endif 19*7c478bd9Sstevel@tonic-gate 20*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SHMGET 21*7c478bd9Sstevel@tonic-gate #include <sys/ipc.h> 22*7c478bd9Sstevel@tonic-gate #include <sys/shm.h> 23*7c478bd9Sstevel@tonic-gate #endif 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate #include <errno.h> 26*7c478bd9Sstevel@tonic-gate #include <string.h> 27*7c478bd9Sstevel@tonic-gate #endif 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include "db_int.h" 30*7c478bd9Sstevel@tonic-gate #include "os_jump.h" 31*7c478bd9Sstevel@tonic-gate #include "common_ext.h" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 34*7c478bd9Sstevel@tonic-gate static int __os_map __P((char *, int, size_t, int, int, int, void **)); 35*7c478bd9Sstevel@tonic-gate #endif 36*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SHMGET 37*7c478bd9Sstevel@tonic-gate static int __os_shmget __P((REGINFO *)); 38*7c478bd9Sstevel@tonic-gate #endif 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate /* 41*7c478bd9Sstevel@tonic-gate * __db_mapanon_ok -- 42*7c478bd9Sstevel@tonic-gate * Return if this OS can support anonymous memory regions. 43*7c478bd9Sstevel@tonic-gate * 44*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_mapanon_ok __P((int)); 45*7c478bd9Sstevel@tonic-gate */ 46*7c478bd9Sstevel@tonic-gate int 47*7c478bd9Sstevel@tonic-gate __db_mapanon_ok(need_names) 48*7c478bd9Sstevel@tonic-gate int need_names; 49*7c478bd9Sstevel@tonic-gate { 50*7c478bd9Sstevel@tonic-gate int ret; 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate ret = EINVAL; 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate /* 55*7c478bd9Sstevel@tonic-gate * If we don't have spinlocks, we have to have a file descriptor 56*7c478bd9Sstevel@tonic-gate * for fcntl(2) locking, which implies using mmap(2) to map in a 57*7c478bd9Sstevel@tonic-gate * regular file. Theoretically, we could probably find ways to 58*7c478bd9Sstevel@tonic-gate * get a file descriptor to lock other types of shared regions, 59*7c478bd9Sstevel@tonic-gate * but I don't see any reason to do so. 60*7c478bd9Sstevel@tonic-gate * 61*7c478bd9Sstevel@tonic-gate * If need_names is set, the application wants to share anonymous 62*7c478bd9Sstevel@tonic-gate * memory among multiple processes, so we have to have a way to 63*7c478bd9Sstevel@tonic-gate * name it. This requires shmget(2), on UNIX systems. 64*7c478bd9Sstevel@tonic-gate */ 65*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SPINLOCKS 66*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SHMGET 67*7c478bd9Sstevel@tonic-gate ret = 0; 68*7c478bd9Sstevel@tonic-gate #endif 69*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 70*7c478bd9Sstevel@tonic-gate #ifdef MAP_ANON 71*7c478bd9Sstevel@tonic-gate if (!need_names) 72*7c478bd9Sstevel@tonic-gate ret = 0; 73*7c478bd9Sstevel@tonic-gate #endif 74*7c478bd9Sstevel@tonic-gate #ifdef MAP_ANONYMOUS 75*7c478bd9Sstevel@tonic-gate if (!need_names) 76*7c478bd9Sstevel@tonic-gate ret = 0; 77*7c478bd9Sstevel@tonic-gate #endif 78*7c478bd9Sstevel@tonic-gate #else 79*7c478bd9Sstevel@tonic-gate COMPQUIET(need_names, 0); 80*7c478bd9Sstevel@tonic-gate #endif /* HAVE_MMAP */ 81*7c478bd9Sstevel@tonic-gate #endif /* HAVE_SPINLOCKS */ 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate return (ret); 84*7c478bd9Sstevel@tonic-gate } 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate /* 87*7c478bd9Sstevel@tonic-gate * __db_mapinit -- 88*7c478bd9Sstevel@tonic-gate * Return if shared regions need to be initialized. 89*7c478bd9Sstevel@tonic-gate * 90*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_mapinit __P((void)); 91*7c478bd9Sstevel@tonic-gate */ 92*7c478bd9Sstevel@tonic-gate int 93*7c478bd9Sstevel@tonic-gate __db_mapinit() 94*7c478bd9Sstevel@tonic-gate { 95*7c478bd9Sstevel@tonic-gate /* 96*7c478bd9Sstevel@tonic-gate * Historically, some systems required that all of the bytes of the 97*7c478bd9Sstevel@tonic-gate * region be written before it could be mmapped and accessed randomly. 98*7c478bd9Sstevel@tonic-gate * We have the option of setting REGION_INIT_NEEDED at configuration 99*7c478bd9Sstevel@tonic-gate * time if we're running on one of those systems. 100*7c478bd9Sstevel@tonic-gate */ 101*7c478bd9Sstevel@tonic-gate #ifdef REGION_INIT_NEEDED 102*7c478bd9Sstevel@tonic-gate return (1); 103*7c478bd9Sstevel@tonic-gate #else 104*7c478bd9Sstevel@tonic-gate return (0); 105*7c478bd9Sstevel@tonic-gate #endif 106*7c478bd9Sstevel@tonic-gate } 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate /* 109*7c478bd9Sstevel@tonic-gate * __db_mapregion -- 110*7c478bd9Sstevel@tonic-gate * Attach to a shared memory region. 111*7c478bd9Sstevel@tonic-gate * 112*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_mapregion __P((char *, REGINFO *)); 113*7c478bd9Sstevel@tonic-gate */ 114*7c478bd9Sstevel@tonic-gate int 115*7c478bd9Sstevel@tonic-gate __db_mapregion(path, infop) 116*7c478bd9Sstevel@tonic-gate char *path; 117*7c478bd9Sstevel@tonic-gate REGINFO *infop; 118*7c478bd9Sstevel@tonic-gate { 119*7c478bd9Sstevel@tonic-gate int called, ret; 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate called = 0; 122*7c478bd9Sstevel@tonic-gate ret = EINVAL; 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate /* If the user replaces the map call, call through their interface. */ 125*7c478bd9Sstevel@tonic-gate if (__db_jump.j_map != NULL) { 126*7c478bd9Sstevel@tonic-gate F_SET(infop, REGION_HOLDINGSYS); 127*7c478bd9Sstevel@tonic-gate return (__db_jump.j_map(path, infop->fd, infop->size, 128*7c478bd9Sstevel@tonic-gate 1, F_ISSET(infop, REGION_ANONYMOUS), 0, &infop->addr)); 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate if (F_ISSET(infop, REGION_ANONYMOUS)) { 132*7c478bd9Sstevel@tonic-gate /* 133*7c478bd9Sstevel@tonic-gate * !!! 134*7c478bd9Sstevel@tonic-gate * If we're creating anonymous regions: 135*7c478bd9Sstevel@tonic-gate * 136*7c478bd9Sstevel@tonic-gate * If it's private, we use mmap(2). The problem with using 137*7c478bd9Sstevel@tonic-gate * shmget(2) is that we may be creating a region of which the 138*7c478bd9Sstevel@tonic-gate * application isn't aware, and if the application crashes 139*7c478bd9Sstevel@tonic-gate * we'll have no way to remove the system resources for the 140*7c478bd9Sstevel@tonic-gate * region. 141*7c478bd9Sstevel@tonic-gate * 142*7c478bd9Sstevel@tonic-gate * If it's not private, we use the shmget(2) interface if it's 143*7c478bd9Sstevel@tonic-gate * available, because it allows us to name anonymous memory. 144*7c478bd9Sstevel@tonic-gate * If shmget(2) isn't available, use the mmap(2) calls. 145*7c478bd9Sstevel@tonic-gate * 146*7c478bd9Sstevel@tonic-gate * In the case of anonymous memory, using mmap(2) means the 147*7c478bd9Sstevel@tonic-gate * memory isn't named and only the single process and its 148*7c478bd9Sstevel@tonic-gate * threads can access the region. 149*7c478bd9Sstevel@tonic-gate */ 150*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 151*7c478bd9Sstevel@tonic-gate #ifdef MAP_ANON 152*7c478bd9Sstevel@tonic-gate #define HAVE_MMAP_ANONYMOUS 1 153*7c478bd9Sstevel@tonic-gate #else 154*7c478bd9Sstevel@tonic-gate #ifdef MAP_ANONYMOUS 155*7c478bd9Sstevel@tonic-gate #define HAVE_MMAP_ANONYMOUS 1 156*7c478bd9Sstevel@tonic-gate #endif 157*7c478bd9Sstevel@tonic-gate #endif 158*7c478bd9Sstevel@tonic-gate #endif 159*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP_ANONYMOUS 160*7c478bd9Sstevel@tonic-gate if (!called && F_ISSET(infop, REGION_PRIVATE)) { 161*7c478bd9Sstevel@tonic-gate called = 1; 162*7c478bd9Sstevel@tonic-gate ret = __os_map(path, 163*7c478bd9Sstevel@tonic-gate infop->fd, infop->size, 1, 1, 0, &infop->addr); 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate #endif 166*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SHMGET 167*7c478bd9Sstevel@tonic-gate if (!called) { 168*7c478bd9Sstevel@tonic-gate called = 1; 169*7c478bd9Sstevel@tonic-gate ret = __os_shmget(infop); 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate #endif 172*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 173*7c478bd9Sstevel@tonic-gate /* 174*7c478bd9Sstevel@tonic-gate * If we're trying to join an unnamed anonymous region, fail -- 175*7c478bd9Sstevel@tonic-gate * that's not possible. 176*7c478bd9Sstevel@tonic-gate */ 177*7c478bd9Sstevel@tonic-gate if (!called) { 178*7c478bd9Sstevel@tonic-gate called = 1; 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate if (!F_ISSET(infop, REGION_CREATED)) { 181*7c478bd9Sstevel@tonic-gate __db_err(infop->dbenv, 182*7c478bd9Sstevel@tonic-gate "cannot join region in unnamed anonymous memory"); 183*7c478bd9Sstevel@tonic-gate return (EINVAL); 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate ret = __os_map(path, 187*7c478bd9Sstevel@tonic-gate infop->fd, infop->size, 1, 1, 0, &infop->addr); 188*7c478bd9Sstevel@tonic-gate } 189*7c478bd9Sstevel@tonic-gate #endif 190*7c478bd9Sstevel@tonic-gate } else { 191*7c478bd9Sstevel@tonic-gate /* 192*7c478bd9Sstevel@tonic-gate * !!! 193*7c478bd9Sstevel@tonic-gate * If we're creating normal regions, we use the mmap(2) 194*7c478bd9Sstevel@tonic-gate * interface if it's available because it's POSIX 1003.1 195*7c478bd9Sstevel@tonic-gate * standard and we trust it more than we do shmget(2). 196*7c478bd9Sstevel@tonic-gate */ 197*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 198*7c478bd9Sstevel@tonic-gate if (!called) { 199*7c478bd9Sstevel@tonic-gate called = 1; 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate /* Mmap(2) regions that aren't anonymous can grow. */ 202*7c478bd9Sstevel@tonic-gate F_SET(infop, REGION_CANGROW); 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate ret = __os_map(path, 205*7c478bd9Sstevel@tonic-gate infop->fd, infop->size, 1, 0, 0, &infop->addr); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate #endif 208*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SHMGET 209*7c478bd9Sstevel@tonic-gate if (!called) { 210*7c478bd9Sstevel@tonic-gate called = 1; 211*7c478bd9Sstevel@tonic-gate ret = __os_shmget(infop); 212*7c478bd9Sstevel@tonic-gate } 213*7c478bd9Sstevel@tonic-gate #endif 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate return (ret); 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate /* 219*7c478bd9Sstevel@tonic-gate * __db_unmapregion -- 220*7c478bd9Sstevel@tonic-gate * Detach from the shared memory region. 221*7c478bd9Sstevel@tonic-gate * 222*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_unmapregion __P((REGINFO *)); 223*7c478bd9Sstevel@tonic-gate */ 224*7c478bd9Sstevel@tonic-gate int 225*7c478bd9Sstevel@tonic-gate __db_unmapregion(infop) 226*7c478bd9Sstevel@tonic-gate REGINFO *infop; 227*7c478bd9Sstevel@tonic-gate { 228*7c478bd9Sstevel@tonic-gate int called, ret; 229*7c478bd9Sstevel@tonic-gate 230*7c478bd9Sstevel@tonic-gate called = 0; 231*7c478bd9Sstevel@tonic-gate ret = EINVAL; 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate if (__db_jump.j_unmap != NULL) 234*7c478bd9Sstevel@tonic-gate return (__db_jump.j_unmap(infop->addr, infop->size)); 235*7c478bd9Sstevel@tonic-gate 236*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SHMGET 237*7c478bd9Sstevel@tonic-gate if (infop->segid != INVALID_SEGID) { 238*7c478bd9Sstevel@tonic-gate called = 1; 239*7c478bd9Sstevel@tonic-gate ret = shmdt(infop->addr) ? errno : 0; 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate #endif 242*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 243*7c478bd9Sstevel@tonic-gate if (!called) { 244*7c478bd9Sstevel@tonic-gate called = 1; 245*7c478bd9Sstevel@tonic-gate ret = munmap(infop->addr, infop->size) ? errno : 0; 246*7c478bd9Sstevel@tonic-gate } 247*7c478bd9Sstevel@tonic-gate #endif 248*7c478bd9Sstevel@tonic-gate return (ret); 249*7c478bd9Sstevel@tonic-gate } 250*7c478bd9Sstevel@tonic-gate 251*7c478bd9Sstevel@tonic-gate /* 252*7c478bd9Sstevel@tonic-gate * __db_unlinkregion -- 253*7c478bd9Sstevel@tonic-gate * Remove the shared memory region. 254*7c478bd9Sstevel@tonic-gate * 255*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_unlinkregion __P((char *, REGINFO *)); 256*7c478bd9Sstevel@tonic-gate */ 257*7c478bd9Sstevel@tonic-gate int 258*7c478bd9Sstevel@tonic-gate __db_unlinkregion(name, infop) 259*7c478bd9Sstevel@tonic-gate char *name; 260*7c478bd9Sstevel@tonic-gate REGINFO *infop; 261*7c478bd9Sstevel@tonic-gate { 262*7c478bd9Sstevel@tonic-gate int called, ret; 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate called = 0; 265*7c478bd9Sstevel@tonic-gate ret = EINVAL; 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate if (__db_jump.j_runlink != NULL) 268*7c478bd9Sstevel@tonic-gate return (__db_jump.j_runlink(name)); 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SHMGET 271*7c478bd9Sstevel@tonic-gate if (infop->segid != INVALID_SEGID) { 272*7c478bd9Sstevel@tonic-gate called = 1; 273*7c478bd9Sstevel@tonic-gate ret = shmctl(infop->segid, IPC_RMID, NULL) ? errno : 0; 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate #endif 276*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 277*7c478bd9Sstevel@tonic-gate COMPQUIET(infop, NULL); 278*7c478bd9Sstevel@tonic-gate if (!called) { 279*7c478bd9Sstevel@tonic-gate called = 1; 280*7c478bd9Sstevel@tonic-gate ret = 0; 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate #endif 283*7c478bd9Sstevel@tonic-gate return (ret); 284*7c478bd9Sstevel@tonic-gate } 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate /* 287*7c478bd9Sstevel@tonic-gate * __db_mapfile -- 288*7c478bd9Sstevel@tonic-gate * Map in a shared memory file. 289*7c478bd9Sstevel@tonic-gate * 290*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_mapfile __P((char *, int, size_t, int, void **)); 291*7c478bd9Sstevel@tonic-gate */ 292*7c478bd9Sstevel@tonic-gate int 293*7c478bd9Sstevel@tonic-gate __db_mapfile(path, fd, len, is_rdonly, addr) 294*7c478bd9Sstevel@tonic-gate char *path; 295*7c478bd9Sstevel@tonic-gate int fd, is_rdonly; 296*7c478bd9Sstevel@tonic-gate size_t len; 297*7c478bd9Sstevel@tonic-gate void **addr; 298*7c478bd9Sstevel@tonic-gate { 299*7c478bd9Sstevel@tonic-gate if (__db_jump.j_map != NULL) 300*7c478bd9Sstevel@tonic-gate return (__db_jump.j_map(path, fd, len, 0, 0, is_rdonly, addr)); 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 303*7c478bd9Sstevel@tonic-gate return (__os_map(path, fd, len, 0, 0, is_rdonly, addr)); 304*7c478bd9Sstevel@tonic-gate #else 305*7c478bd9Sstevel@tonic-gate return (EINVAL); 306*7c478bd9Sstevel@tonic-gate #endif 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate /* 310*7c478bd9Sstevel@tonic-gate * __db_unmapfile -- 311*7c478bd9Sstevel@tonic-gate * Unmap the shared memory file. 312*7c478bd9Sstevel@tonic-gate * 313*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_unmapfile __P((void *, size_t)); 314*7c478bd9Sstevel@tonic-gate */ 315*7c478bd9Sstevel@tonic-gate int 316*7c478bd9Sstevel@tonic-gate __db_unmapfile(addr, len) 317*7c478bd9Sstevel@tonic-gate void *addr; 318*7c478bd9Sstevel@tonic-gate size_t len; 319*7c478bd9Sstevel@tonic-gate { 320*7c478bd9Sstevel@tonic-gate if (__db_jump.j_unmap != NULL) 321*7c478bd9Sstevel@tonic-gate return (__db_jump.j_unmap(addr, len)); 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 324*7c478bd9Sstevel@tonic-gate return (munmap(addr, len) ? errno : 0); 325*7c478bd9Sstevel@tonic-gate #else 326*7c478bd9Sstevel@tonic-gate return (EINVAL); 327*7c478bd9Sstevel@tonic-gate #endif 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MMAP 331*7c478bd9Sstevel@tonic-gate /* 332*7c478bd9Sstevel@tonic-gate * __os_map -- 333*7c478bd9Sstevel@tonic-gate * Call the mmap(2) function. 334*7c478bd9Sstevel@tonic-gate */ 335*7c478bd9Sstevel@tonic-gate static int 336*7c478bd9Sstevel@tonic-gate __os_map(path, fd, len, is_region, is_anonymous, is_rdonly, addr) 337*7c478bd9Sstevel@tonic-gate char *path; 338*7c478bd9Sstevel@tonic-gate int fd, is_region, is_anonymous, is_rdonly; 339*7c478bd9Sstevel@tonic-gate size_t len; 340*7c478bd9Sstevel@tonic-gate void **addr; 341*7c478bd9Sstevel@tonic-gate { 342*7c478bd9Sstevel@tonic-gate void *p; 343*7c478bd9Sstevel@tonic-gate int flags, prot; 344*7c478bd9Sstevel@tonic-gate 345*7c478bd9Sstevel@tonic-gate COMPQUIET(path, NULL); 346*7c478bd9Sstevel@tonic-gate 347*7c478bd9Sstevel@tonic-gate /* 348*7c478bd9Sstevel@tonic-gate * If it's read-only, it's private, and if it's not, it's shared. 349*7c478bd9Sstevel@tonic-gate * Don't bother with an additional parameter. 350*7c478bd9Sstevel@tonic-gate */ 351*7c478bd9Sstevel@tonic-gate flags = is_rdonly ? MAP_PRIVATE : MAP_SHARED; 352*7c478bd9Sstevel@tonic-gate 353*7c478bd9Sstevel@tonic-gate if (is_region && is_anonymous) { 354*7c478bd9Sstevel@tonic-gate /* 355*7c478bd9Sstevel@tonic-gate * BSD derived systems use MAP_ANON; Digital Unix and HP/UX 356*7c478bd9Sstevel@tonic-gate * use MAP_ANONYMOUS. 357*7c478bd9Sstevel@tonic-gate */ 358*7c478bd9Sstevel@tonic-gate #ifdef MAP_ANON 359*7c478bd9Sstevel@tonic-gate flags |= MAP_ANON; 360*7c478bd9Sstevel@tonic-gate #endif 361*7c478bd9Sstevel@tonic-gate #ifdef MAP_ANONYMOUS 362*7c478bd9Sstevel@tonic-gate flags |= MAP_ANONYMOUS; 363*7c478bd9Sstevel@tonic-gate #endif 364*7c478bd9Sstevel@tonic-gate fd = -1; 365*7c478bd9Sstevel@tonic-gate } 366*7c478bd9Sstevel@tonic-gate #ifdef MAP_FILE 367*7c478bd9Sstevel@tonic-gate if (!is_region || !is_anonymous) { 368*7c478bd9Sstevel@tonic-gate /* 369*7c478bd9Sstevel@tonic-gate * Historically, MAP_FILE was required for mapping regular 370*7c478bd9Sstevel@tonic-gate * files, even though it was the default. Some systems have 371*7c478bd9Sstevel@tonic-gate * it, some don't, some that have it set it to 0. 372*7c478bd9Sstevel@tonic-gate */ 373*7c478bd9Sstevel@tonic-gate flags |= MAP_FILE; 374*7c478bd9Sstevel@tonic-gate } 375*7c478bd9Sstevel@tonic-gate #endif 376*7c478bd9Sstevel@tonic-gate 377*7c478bd9Sstevel@tonic-gate /* 378*7c478bd9Sstevel@tonic-gate * I know of no systems that implement the flag to tell the system 379*7c478bd9Sstevel@tonic-gate * that the region contains semaphores, but it's not an unreasonable 380*7c478bd9Sstevel@tonic-gate * thing to do, and has been part of the design since forever. I 381*7c478bd9Sstevel@tonic-gate * don't think anyone will object, but don't set it for read-only 382*7c478bd9Sstevel@tonic-gate * files, it doesn't make sense. 383*7c478bd9Sstevel@tonic-gate */ 384*7c478bd9Sstevel@tonic-gate #ifdef MAP_HASSEMAPHORE 385*7c478bd9Sstevel@tonic-gate if (!is_rdonly) 386*7c478bd9Sstevel@tonic-gate flags |= MAP_HASSEMAPHORE; 387*7c478bd9Sstevel@tonic-gate #endif 388*7c478bd9Sstevel@tonic-gate 389*7c478bd9Sstevel@tonic-gate prot = PROT_READ | (is_rdonly ? 0 : PROT_WRITE); 390*7c478bd9Sstevel@tonic-gate 391*7c478bd9Sstevel@tonic-gate /* 392*7c478bd9Sstevel@tonic-gate * XXX 393*7c478bd9Sstevel@tonic-gate * Work around a bug in the VMS V7.1 mmap() implementation. To map a file 394*7c478bd9Sstevel@tonic-gate * into memory on VMS it needs to be opened in a certain way, originally. 395*7c478bd9Sstevel@tonic-gate * To get the file opened in that certain way, the VMS mmap() closes the 396*7c478bd9Sstevel@tonic-gate * file and re-opens it. When it does this, it doesn't flush any caches 397*7c478bd9Sstevel@tonic-gate * out to disk before closing. The problem this causes us is that when the 398*7c478bd9Sstevel@tonic-gate * memory cache doesn't get written out, the file isn't big enough to match 399*7c478bd9Sstevel@tonic-gate * the memory chunk and the mmap() call fails. This call to fsync() fixes 400*7c478bd9Sstevel@tonic-gate * the problem. DEC thinks this isn't a bug because of language in XPG5 401*7c478bd9Sstevel@tonic-gate * discussing user responsibility for on-disk and in-memory synchronization. 402*7c478bd9Sstevel@tonic-gate */ 403*7c478bd9Sstevel@tonic-gate #ifdef VMS 404*7c478bd9Sstevel@tonic-gate if (__os_fsync(fd) == -1) 405*7c478bd9Sstevel@tonic-gate return(errno); 406*7c478bd9Sstevel@tonic-gate #endif 407*7c478bd9Sstevel@tonic-gate 408*7c478bd9Sstevel@tonic-gate /* MAP_FAILED was not defined in early mmap implementations. */ 409*7c478bd9Sstevel@tonic-gate #ifndef MAP_FAILED 410*7c478bd9Sstevel@tonic-gate #define MAP_FAILED -1 411*7c478bd9Sstevel@tonic-gate #endif 412*7c478bd9Sstevel@tonic-gate if ((p = 413*7c478bd9Sstevel@tonic-gate mmap(NULL, len, prot, flags, fd, (off_t)0)) == (void *)MAP_FAILED) 414*7c478bd9Sstevel@tonic-gate return (errno); 415*7c478bd9Sstevel@tonic-gate 416*7c478bd9Sstevel@tonic-gate *addr = p; 417*7c478bd9Sstevel@tonic-gate return (0); 418*7c478bd9Sstevel@tonic-gate } 419*7c478bd9Sstevel@tonic-gate #endif 420*7c478bd9Sstevel@tonic-gate 421*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SHMGET 422*7c478bd9Sstevel@tonic-gate /* 423*7c478bd9Sstevel@tonic-gate * __os_shmget -- 424*7c478bd9Sstevel@tonic-gate * Call the shmget(2) family of functions. 425*7c478bd9Sstevel@tonic-gate */ 426*7c478bd9Sstevel@tonic-gate static int 427*7c478bd9Sstevel@tonic-gate __os_shmget(infop) 428*7c478bd9Sstevel@tonic-gate REGINFO *infop; 429*7c478bd9Sstevel@tonic-gate { 430*7c478bd9Sstevel@tonic-gate if (F_ISSET(infop, REGION_CREATED) && 431*7c478bd9Sstevel@tonic-gate (infop->segid = shmget(0, infop->size, IPC_PRIVATE | 0600)) == -1) 432*7c478bd9Sstevel@tonic-gate return (errno); 433*7c478bd9Sstevel@tonic-gate 434*7c478bd9Sstevel@tonic-gate if ((infop->addr = shmat(infop->segid, NULL, 0)) == (void *)-1) { 435*7c478bd9Sstevel@tonic-gate /* 436*7c478bd9Sstevel@tonic-gate * If we're trying to join the region and failing, assume 437*7c478bd9Sstevel@tonic-gate * that there was a reboot and the region no longer exists. 438*7c478bd9Sstevel@tonic-gate */ 439*7c478bd9Sstevel@tonic-gate if (!F_ISSET(infop, REGION_CREATED)) 440*7c478bd9Sstevel@tonic-gate errno = EAGAIN; 441*7c478bd9Sstevel@tonic-gate return (errno); 442*7c478bd9Sstevel@tonic-gate } 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate F_SET(infop, REGION_HOLDINGSYS); 445*7c478bd9Sstevel@tonic-gate return (0); 446*7c478bd9Sstevel@tonic-gate } 447*7c478bd9Sstevel@tonic-gate #endif 448