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