xref: /onnv-gate/usr/src/lib/madv/common/madv.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <strings.h>
32*0Sstevel@tonic-gate #include <sys/shm.h>
33*0Sstevel@tonic-gate #include <sys/mman.h>
34*0Sstevel@tonic-gate #include <fcntl.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <sys/stat.h>
40*0Sstevel@tonic-gate #include <sys/auxv.h>
41*0Sstevel@tonic-gate #include <stdarg.h>
42*0Sstevel@tonic-gate #include <syslog.h>
43*0Sstevel@tonic-gate #include <sys/param.h>
44*0Sstevel@tonic-gate #include <sys/sysmacros.h>
45*0Sstevel@tonic-gate #include <procfs.h>
46*0Sstevel@tonic-gate #include <dlfcn.h>
47*0Sstevel@tonic-gate #include <assert.h>
48*0Sstevel@tonic-gate #include <libintl.h>
49*0Sstevel@tonic-gate #include <locale.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate extern int	gmatch(const char *s, const char *p);
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #pragma init(__madvmain)
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static FILE *errfp = NULL;
56*0Sstevel@tonic-gate static const char *madvident = "madv.so.1";
57*0Sstevel@tonic-gate static int pagesize;
58*0Sstevel@tonic-gate static int advice_all = -1;
59*0Sstevel@tonic-gate static int advice_heap = -1;
60*0Sstevel@tonic-gate static int advice_shm = -1;
61*0Sstevel@tonic-gate static int advice_ism = -1;
62*0Sstevel@tonic-gate static int advice_dism = -1;
63*0Sstevel@tonic-gate static int advice_map = -1;
64*0Sstevel@tonic-gate static int advice_mapshared = -1;
65*0Sstevel@tonic-gate static int advice_mapprivate = -1;
66*0Sstevel@tonic-gate static int advice_mapanon = -1;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /* environment variables */
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #define	ENV_MADV		"MADV"
71*0Sstevel@tonic-gate #define	ENV_MADVCFGFILE		"MADVCFGFILE"
72*0Sstevel@tonic-gate #define	ENV_MADVERRFILE		"MADVERRFILE"
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /* config file */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate #define	DEF_MADVCFGFILE		"/etc/madv.conf"
77*0Sstevel@tonic-gate #define	MAXLINELEN	MAXPATHLEN + 64
78*0Sstevel@tonic-gate #define	CFGDELIMITER	':'
79*0Sstevel@tonic-gate #define	ARGDELIMITER	' '
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate  * avoid malloc which causes certain applications to crash
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate static char		lbuf[MAXLINELEN];
85*0Sstevel@tonic-gate static char		pbuf[MAXPATHLEN];
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate #ifdef MADVDEBUG
88*0Sstevel@tonic-gate #define	ENV_MADVDEBUG	"MADVDEBUG"
89*0Sstevel@tonic-gate #define	MADVPRINT(x, y)	if (madvdebug & x) (void) fprintf y;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate static int madvdebug = 0;
92*0Sstevel@tonic-gate #else
93*0Sstevel@tonic-gate #define	MADVPRINT(x, y)
94*0Sstevel@tonic-gate #endif
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * advice options
98*0Sstevel@tonic-gate  */
99*0Sstevel@tonic-gate static char *legal_optstr[] = {
100*0Sstevel@tonic-gate 	"madv",
101*0Sstevel@tonic-gate 	"heap",
102*0Sstevel@tonic-gate 	"shm",
103*0Sstevel@tonic-gate 	"ism",
104*0Sstevel@tonic-gate 	"dism",
105*0Sstevel@tonic-gate 	"map",
106*0Sstevel@tonic-gate 	"mapshared",
107*0Sstevel@tonic-gate 	"mapprivate",
108*0Sstevel@tonic-gate 	"mapanon",
109*0Sstevel@tonic-gate 	NULL
110*0Sstevel@tonic-gate };
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate enum optenum {
113*0Sstevel@tonic-gate 	OPT_MADV,
114*0Sstevel@tonic-gate 	OPT_HEAP,
115*0Sstevel@tonic-gate 	OPT_SHM,
116*0Sstevel@tonic-gate 	OPT_ISM,
117*0Sstevel@tonic-gate 	OPT_DISM,
118*0Sstevel@tonic-gate 	OPT_MAP,
119*0Sstevel@tonic-gate 	OPT_MAPSHARED,
120*0Sstevel@tonic-gate 	OPT_MAPPRIVATE,
121*0Sstevel@tonic-gate 	OPT_MAPANON
122*0Sstevel@tonic-gate };
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate /*
125*0Sstevel@tonic-gate  * Advice values
126*0Sstevel@tonic-gate  * These need to correspond to the order of the MADV_ flags in mman.h
127*0Sstevel@tonic-gate  * since the position infers the value for the flag.
128*0Sstevel@tonic-gate  */
129*0Sstevel@tonic-gate static char *legal_madvice[] = {
130*0Sstevel@tonic-gate 	"normal",
131*0Sstevel@tonic-gate 	"random",
132*0Sstevel@tonic-gate 	"sequential",
133*0Sstevel@tonic-gate 	"willneed_NOT_SUPPORTED!",
134*0Sstevel@tonic-gate 	"dontneed_NOT_SUPPORTED!",
135*0Sstevel@tonic-gate 	"free_NOT_SUPPORTED!",
136*0Sstevel@tonic-gate 	"access_default",
137*0Sstevel@tonic-gate 	"access_lwp",
138*0Sstevel@tonic-gate 	"access_many",
139*0Sstevel@tonic-gate 	NULL
140*0Sstevel@tonic-gate };
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
143*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
144*0Sstevel@tonic-gate #endif
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate /*PRINTFLIKE2*/
147*0Sstevel@tonic-gate static void
148*0Sstevel@tonic-gate madverr(FILE *fp, char *fmt, ...)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate 	va_list		ap;
151*0Sstevel@tonic-gate 	va_start(ap, fmt);
152*0Sstevel@tonic-gate 	if (fp)
153*0Sstevel@tonic-gate 		(void) vfprintf(fp, fmt, ap);
154*0Sstevel@tonic-gate 	else
155*0Sstevel@tonic-gate 		vsyslog(LOG_ERR, fmt, ap);
156*0Sstevel@tonic-gate 	va_end(ap);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate  * Return the pointer to the fully-resolved path name of the process's
161*0Sstevel@tonic-gate  * executable file obtained from the AT_SUN_EXECNAME aux vector entry.
162*0Sstevel@tonic-gate  */
163*0Sstevel@tonic-gate static const char *
164*0Sstevel@tonic-gate mygetexecname(void)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	const char	*execname = NULL;
167*0Sstevel@tonic-gate 	static auxv_t	auxb;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/*
170*0Sstevel@tonic-gate 	 * The first time through, read the initial aux vector that was
171*0Sstevel@tonic-gate 	 * passed to the process at exec(2).  Only do this once.
172*0Sstevel@tonic-gate 	 */
173*0Sstevel@tonic-gate 	int fd = open("/proc/self/auxv", O_RDONLY);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (fd >= 0) {
176*0Sstevel@tonic-gate 		while (read(fd, &auxb, sizeof (auxv_t)) == sizeof (auxv_t)) {
177*0Sstevel@tonic-gate 			if (auxb.a_type == AT_SUN_EXECNAME) {
178*0Sstevel@tonic-gate 				execname = auxb.a_un.a_ptr;
179*0Sstevel@tonic-gate 				break;
180*0Sstevel@tonic-gate 			}
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 		(void) close(fd);
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 	return (execname);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate /*
188*0Sstevel@tonic-gate  * Return the process's current brk base and size.
189*0Sstevel@tonic-gate  */
190*0Sstevel@tonic-gate static int
191*0Sstevel@tonic-gate mygetbrk(uintptr_t *base, size_t *size)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	int fd;
194*0Sstevel@tonic-gate 	pstatus_t ps;
195*0Sstevel@tonic-gate 	int rc;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	fd = open("/proc/self/status", O_RDONLY);
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (fd >= 0) {
200*0Sstevel@tonic-gate 		if (read(fd, &ps, sizeof (ps)) == sizeof (ps)) {
201*0Sstevel@tonic-gate 			*base = ps.pr_brkbase;
202*0Sstevel@tonic-gate 			*size = ps.pr_brksize;
203*0Sstevel@tonic-gate 			rc = 0;
204*0Sstevel@tonic-gate 		} else {
205*0Sstevel@tonic-gate 			rc = errno;
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate 		(void) close(fd);
208*0Sstevel@tonic-gate 	} else {
209*0Sstevel@tonic-gate 		rc = errno;
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 	return (rc);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate /*
215*0Sstevel@tonic-gate  * Check if exec name matches cfgname found in madv cfg file.
216*0Sstevel@tonic-gate  */
217*0Sstevel@tonic-gate static int
218*0Sstevel@tonic-gate fnmatch(const char *execname, char *cfgname, char *cwd)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	const char	*ename;
221*0Sstevel@tonic-gate 	int		rc;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	/* cfgname should not have a '/' unless it begins with one */
224*0Sstevel@tonic-gate 	if (cfgname[0] == '/') {
225*0Sstevel@tonic-gate 		/*
226*0Sstevel@tonic-gate 		 * if execname does not begin with a '/', prepend the
227*0Sstevel@tonic-gate 		 * current directory.
228*0Sstevel@tonic-gate 		 */
229*0Sstevel@tonic-gate 		if (execname[0] != '/') {
230*0Sstevel@tonic-gate 			ename = (const char *)strcat(cwd, execname);
231*0Sstevel@tonic-gate 		} else
232*0Sstevel@tonic-gate 			ename = execname;
233*0Sstevel@tonic-gate 	} else {	/* simple cfg name */
234*0Sstevel@tonic-gate 		if (ename = strrchr(execname, '/'))
235*0Sstevel@tonic-gate 			/* execname is a path name - get the base name */
236*0Sstevel@tonic-gate 			ename++;
237*0Sstevel@tonic-gate 		else
238*0Sstevel@tonic-gate 			ename = execname;
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 	rc = gmatch(ename, cfgname);
241*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "gmatch: %s %s %s %d\n",
242*0Sstevel@tonic-gate 	    cfgname, ename, execname, rc));
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	return (rc);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * Check if string matches any of exec arguments.
249*0Sstevel@tonic-gate  */
250*0Sstevel@tonic-gate static int
251*0Sstevel@tonic-gate argmatch(char *str)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 	int fd;
254*0Sstevel@tonic-gate 	psinfo_t pi;
255*0Sstevel@tonic-gate 	int rc = 0;
256*0Sstevel@tonic-gate 	int arg;
257*0Sstevel@tonic-gate 	char **argv;
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	fd = open("/proc/self/psinfo", O_RDONLY);
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	if (fd >= 0) {
262*0Sstevel@tonic-gate 		if (read(fd, &pi, sizeof (pi)) == sizeof (pi)) {
263*0Sstevel@tonic-gate 			argv = (char **)pi.pr_argv;
264*0Sstevel@tonic-gate 			argv++;
265*0Sstevel@tonic-gate 			MADVPRINT(2, (stderr, "argmatch: %s ", str));
266*0Sstevel@tonic-gate 			for (arg = 1; arg < pi.pr_argc; arg++, argv++) {
267*0Sstevel@tonic-gate 				if (rc = gmatch(*argv, str)) {
268*0Sstevel@tonic-gate 					MADVPRINT(2, (stderr, "%s ", *argv));
269*0Sstevel@tonic-gate 					break;
270*0Sstevel@tonic-gate 				}
271*0Sstevel@tonic-gate 			}
272*0Sstevel@tonic-gate 			MADVPRINT(2, (stderr, "%d\n", rc));
273*0Sstevel@tonic-gate 		} else {
274*0Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
275*0Sstevel@tonic-gate 			    "%s: /proc/self/psinfo read failed [%s]\n"),
276*0Sstevel@tonic-gate 			    madvident, strerror(errno));
277*0Sstevel@tonic-gate 		}
278*0Sstevel@tonic-gate 		(void) close(fd);
279*0Sstevel@tonic-gate 	} else {
280*0Sstevel@tonic-gate 		madverr(errfp, dgettext(TEXT_DOMAIN,
281*0Sstevel@tonic-gate 		    "%s: /proc/self/psinfo open failed [%s]\n"),
282*0Sstevel@tonic-gate 		    madvident, strerror(errno));
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 	return (rc);
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate static int
288*0Sstevel@tonic-gate empty(char *str)
289*0Sstevel@tonic-gate {
290*0Sstevel@tonic-gate 	char	c;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	while ((c = *str) == '\n' || c == ' ' || c == '\t')
293*0Sstevel@tonic-gate 		str++;
294*0Sstevel@tonic-gate 	return (*str == '\0');
295*0Sstevel@tonic-gate }
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate static int
298*0Sstevel@tonic-gate strtoadv(char *advstr)
299*0Sstevel@tonic-gate {
300*0Sstevel@tonic-gate 	char *dummy, *locstr = advstr;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	return (getsubopt(&locstr, legal_madvice, &dummy));
303*0Sstevel@tonic-gate }
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate static void
306*0Sstevel@tonic-gate advice_opts(char *optstr, const char *execname, char *cfgfile, int lineno)
307*0Sstevel@tonic-gate {
308*0Sstevel@tonic-gate 	char *value;
309*0Sstevel@tonic-gate 	int opt;
310*0Sstevel@tonic-gate 	int advice = 0;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	while (*optstr != '\0') {
313*0Sstevel@tonic-gate 		opt = getsubopt(&optstr, legal_optstr, &value);
314*0Sstevel@tonic-gate 		if (opt < 0) {
315*0Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
316*0Sstevel@tonic-gate 			    "%s: invalid advice option (%s)"
317*0Sstevel@tonic-gate 			    " for %s - cfgfile: %s, line: %d\n"),
318*0Sstevel@tonic-gate 			    madvident, value, execname, cfgfile, lineno);
319*0Sstevel@tonic-gate 			break;
320*0Sstevel@tonic-gate 		} else if (!value) {
321*0Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
322*0Sstevel@tonic-gate 			    "%s: option missing advice"
323*0Sstevel@tonic-gate 			    " for %s - cfgfile: %s, line: %d\n"),
324*0Sstevel@tonic-gate 			    madvident, execname, cfgfile, lineno);
325*0Sstevel@tonic-gate 			break;
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 		advice = strtoadv(value);
328*0Sstevel@tonic-gate 		if (advice < 0) {
329*0Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
330*0Sstevel@tonic-gate 			    "%s: invalid advice specified (%s)"
331*0Sstevel@tonic-gate 			    " for %s - cfgfile: %s, line: %d\n"),
332*0Sstevel@tonic-gate 			    madvident, value, execname, cfgfile, lineno);
333*0Sstevel@tonic-gate 			break;
334*0Sstevel@tonic-gate 		}
335*0Sstevel@tonic-gate 		switch (opt) {
336*0Sstevel@tonic-gate 		case OPT_MADV:
337*0Sstevel@tonic-gate 			advice_all = advice;
338*0Sstevel@tonic-gate 			break;
339*0Sstevel@tonic-gate 		case OPT_HEAP:
340*0Sstevel@tonic-gate 			if (advice_heap < 0) {
341*0Sstevel@tonic-gate 				advice_heap = advice;
342*0Sstevel@tonic-gate 			} else {
343*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
344*0Sstevel@tonic-gate 				    "%s: duplicate advice specified "
345*0Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
346*0Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
347*0Sstevel@tonic-gate 				    lineno);
348*0Sstevel@tonic-gate 			}
349*0Sstevel@tonic-gate 			break;
350*0Sstevel@tonic-gate 		case OPT_SHM:
351*0Sstevel@tonic-gate 			if (advice_shm < 0) {
352*0Sstevel@tonic-gate 				advice_shm = advice;
353*0Sstevel@tonic-gate 			} else {
354*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
355*0Sstevel@tonic-gate 				    "%s: duplicate advice specified "
356*0Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
357*0Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
358*0Sstevel@tonic-gate 				    lineno);
359*0Sstevel@tonic-gate 			}
360*0Sstevel@tonic-gate 			break;
361*0Sstevel@tonic-gate 		case OPT_ISM:
362*0Sstevel@tonic-gate 			if (advice_ism < 0) {
363*0Sstevel@tonic-gate 				advice_ism = advice;
364*0Sstevel@tonic-gate 			} else {
365*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
366*0Sstevel@tonic-gate 				    "%s: duplicate advice specified "
367*0Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
368*0Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
369*0Sstevel@tonic-gate 				    lineno);
370*0Sstevel@tonic-gate 			}
371*0Sstevel@tonic-gate 			break;
372*0Sstevel@tonic-gate 		case OPT_DISM:
373*0Sstevel@tonic-gate 			if (advice_dism < 0) {
374*0Sstevel@tonic-gate 				advice_dism = advice;
375*0Sstevel@tonic-gate 			} else {
376*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
377*0Sstevel@tonic-gate 				    "%s: duplicate advice specified "
378*0Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
379*0Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
380*0Sstevel@tonic-gate 				    lineno);
381*0Sstevel@tonic-gate 			}
382*0Sstevel@tonic-gate 			break;
383*0Sstevel@tonic-gate 		case OPT_MAP:
384*0Sstevel@tonic-gate 			if (advice_map < 0) {
385*0Sstevel@tonic-gate 				advice_map = advice;
386*0Sstevel@tonic-gate 			} else {
387*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
388*0Sstevel@tonic-gate 				    "%s: duplicate advice specified "
389*0Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
390*0Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
391*0Sstevel@tonic-gate 				    lineno);
392*0Sstevel@tonic-gate 			}
393*0Sstevel@tonic-gate 			break;
394*0Sstevel@tonic-gate 		case OPT_MAPSHARED:
395*0Sstevel@tonic-gate 			if (advice_mapshared < 0) {
396*0Sstevel@tonic-gate 				advice_mapshared = advice;
397*0Sstevel@tonic-gate 			} else {
398*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
399*0Sstevel@tonic-gate 				    "%s: duplicate advice specified "
400*0Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
401*0Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
402*0Sstevel@tonic-gate 				    lineno);
403*0Sstevel@tonic-gate 			}
404*0Sstevel@tonic-gate 			break;
405*0Sstevel@tonic-gate 		case OPT_MAPPRIVATE:
406*0Sstevel@tonic-gate 			if (advice_mapprivate < 0) {
407*0Sstevel@tonic-gate 				advice_mapprivate = advice;
408*0Sstevel@tonic-gate 			} else {
409*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
410*0Sstevel@tonic-gate 				    "%s: duplicate advice specified "
411*0Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
412*0Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
413*0Sstevel@tonic-gate 				    lineno);
414*0Sstevel@tonic-gate 			}
415*0Sstevel@tonic-gate 			break;
416*0Sstevel@tonic-gate 		case OPT_MAPANON:
417*0Sstevel@tonic-gate 			if (advice_mapanon < 0) {
418*0Sstevel@tonic-gate 				advice_mapanon = advice;
419*0Sstevel@tonic-gate 			} else {
420*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
421*0Sstevel@tonic-gate 				    "%s: duplicate advice specified "
422*0Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
423*0Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
424*0Sstevel@tonic-gate 				    lineno);
425*0Sstevel@tonic-gate 			}
426*0Sstevel@tonic-gate 			break;
427*0Sstevel@tonic-gate 		default:
428*0Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
429*0Sstevel@tonic-gate 			    "%s: invalid advice option (%s)"
430*0Sstevel@tonic-gate 			    " for %s - cfgfile: %s, line: %d\n"),
431*0Sstevel@tonic-gate 			    madvident, value, execname, cfgfile, lineno);
432*0Sstevel@tonic-gate 			break;
433*0Sstevel@tonic-gate 		}
434*0Sstevel@tonic-gate 	}
435*0Sstevel@tonic-gate }
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate static void
438*0Sstevel@tonic-gate __madvmain()
439*0Sstevel@tonic-gate {
440*0Sstevel@tonic-gate 	char		*cfgfile, *errfile;
441*0Sstevel@tonic-gate 	FILE		*fp = NULL;
442*0Sstevel@tonic-gate 	const char	*execname;
443*0Sstevel@tonic-gate 	char		*cwd;
444*0Sstevel@tonic-gate 	int		cwdlen;
445*0Sstevel@tonic-gate 	char		*tok, *tokadv, *tokarg;
446*0Sstevel@tonic-gate 	char		*str, *envadv;
447*0Sstevel@tonic-gate 	int		lineno = 0;
448*0Sstevel@tonic-gate 	int		advice;
449*0Sstevel@tonic-gate 	uintptr_t	brkbase, brkend;
450*0Sstevel@tonic-gate 	size_t		brksize;
451*0Sstevel@tonic-gate 	int		rc;
452*0Sstevel@tonic-gate 	char		*locale;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	/*
455*0Sstevel@tonic-gate 	 * If a private error file is indicated then set the locale
456*0Sstevel@tonic-gate 	 * for error messages for the duration of this routine.
457*0Sstevel@tonic-gate 	 * Error messages destined for syslog should not be translated
458*0Sstevel@tonic-gate 	 * and thus come from the default C locale.
459*0Sstevel@tonic-gate 	 */
460*0Sstevel@tonic-gate 	if ((errfile = getenv(ENV_MADVERRFILE)) != NULL) {
461*0Sstevel@tonic-gate 		errfp = fopen(errfile, "a");
462*0Sstevel@tonic-gate 		if (errfp) {
463*0Sstevel@tonic-gate 			locale = setlocale(LC_MESSAGES, "");
464*0Sstevel@tonic-gate 		} else {
465*0Sstevel@tonic-gate 			madverr(NULL, dgettext(TEXT_DOMAIN,
466*0Sstevel@tonic-gate 			    "%s: cannot open error file: %s [%s]\n"),
467*0Sstevel@tonic-gate 			    madvident, errfile, strerror(errno));
468*0Sstevel@tonic-gate 		}
469*0Sstevel@tonic-gate 	}
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate #ifdef MADVDEBUG
472*0Sstevel@tonic-gate 	if (str = getenv(ENV_MADVDEBUG))
473*0Sstevel@tonic-gate 		madvdebug = atoi(str);
474*0Sstevel@tonic-gate #endif
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	if (envadv = getenv(ENV_MADV)) {
477*0Sstevel@tonic-gate 		if ((advice = strtoadv(envadv)) >= 0)
478*0Sstevel@tonic-gate 			advice_all = advice;
479*0Sstevel@tonic-gate 		else
480*0Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
481*0Sstevel@tonic-gate 			    "%s: invalid advice specified: MADV=%s\n"),
482*0Sstevel@tonic-gate 			    madvident, envadv);
483*0Sstevel@tonic-gate 	}
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	/*
486*0Sstevel@tonic-gate 	 * Open specified cfg file or default one.
487*0Sstevel@tonic-gate 	 */
488*0Sstevel@tonic-gate 	if (cfgfile = getenv(ENV_MADVCFGFILE)) {
489*0Sstevel@tonic-gate 		fp = fopen(cfgfile, "r");
490*0Sstevel@tonic-gate 		if (!fp) {
491*0Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
492*0Sstevel@tonic-gate 			    "%s: cannot open configuration file: %s [%s]\n"),
493*0Sstevel@tonic-gate 			    madvident, cfgfile, strerror(errno));
494*0Sstevel@tonic-gate 		}
495*0Sstevel@tonic-gate 	} else {
496*0Sstevel@tonic-gate 		cfgfile = DEF_MADVCFGFILE;
497*0Sstevel@tonic-gate 		fp = fopen(cfgfile, "r");
498*0Sstevel@tonic-gate 	}
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	if (fp) {
501*0Sstevel@tonic-gate 		execname = mygetexecname();
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 		cwd = getcwd(pbuf, MAXPATHLEN);
504*0Sstevel@tonic-gate 		if (!cwd)
505*0Sstevel@tonic-gate 			return;
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 		cwd = strcat(cwd, "/");
508*0Sstevel@tonic-gate 		cwdlen = strlen(cwd);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 		while (fgets(lbuf, MAXLINELEN, fp)) {
511*0Sstevel@tonic-gate 			lineno++;
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 			/*
514*0Sstevel@tonic-gate 			 * Make sure line wasn't truncated.
515*0Sstevel@tonic-gate 			 */
516*0Sstevel@tonic-gate 			if (strlen(lbuf) >= MAXLINELEN - 1) {
517*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
518*0Sstevel@tonic-gate 				    "%s: invalid entry, "
519*0Sstevel@tonic-gate 				    "line too long - cfgfile:"
520*0Sstevel@tonic-gate 				    " %s, line: %d\n"),
521*0Sstevel@tonic-gate 				    madvident, cfgfile, lineno);
522*0Sstevel@tonic-gate 				continue;
523*0Sstevel@tonic-gate 			}
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 			if (empty(lbuf))
526*0Sstevel@tonic-gate 				continue;
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 			/*
529*0Sstevel@tonic-gate 			 * Get advice options.
530*0Sstevel@tonic-gate 			 * Parse right to left in case delimiter is in name.
531*0Sstevel@tonic-gate 			 */
532*0Sstevel@tonic-gate 			if (!(tokadv = strrchr(lbuf, CFGDELIMITER))) {
533*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
534*0Sstevel@tonic-gate 				    "%s: no delimiter specified - cfgfile:"
535*0Sstevel@tonic-gate 				    " %s, line: %d\n"),
536*0Sstevel@tonic-gate 				    madvident, cfgfile, lineno);
537*0Sstevel@tonic-gate 				continue;
538*0Sstevel@tonic-gate 			}
539*0Sstevel@tonic-gate 			*tokadv++ = '\0';
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 			/*
542*0Sstevel@tonic-gate 			 * Remove newline from end of advice options.
543*0Sstevel@tonic-gate 			 */
544*0Sstevel@tonic-gate 			if (str = strrchr(tokadv, '\n'))
545*0Sstevel@tonic-gate 				*str = '\0';
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 			/*
548*0Sstevel@tonic-gate 			 * Get optional argument string.
549*0Sstevel@tonic-gate 			 */
550*0Sstevel@tonic-gate 			if (tokarg = strrchr(lbuf, ARGDELIMITER)) {
551*0Sstevel@tonic-gate 				*tokarg++ = '\0';
552*0Sstevel@tonic-gate 			}
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 			/*
555*0Sstevel@tonic-gate 			 * Compare exec name.
556*0Sstevel@tonic-gate 			 */
557*0Sstevel@tonic-gate 			tok = lbuf;
558*0Sstevel@tonic-gate 			if (!fnmatch(execname, tok, cwd)) {
559*0Sstevel@tonic-gate 				tokadv = tokarg = NULL;
560*0Sstevel@tonic-gate 				cwd[cwdlen] = '\0';
561*0Sstevel@tonic-gate 				continue;
562*0Sstevel@tonic-gate 			}
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 			/*
565*0Sstevel@tonic-gate 			 * Compare arguments if argument string specified.
566*0Sstevel@tonic-gate 			 */
567*0Sstevel@tonic-gate 			if (tokarg &&
568*0Sstevel@tonic-gate 			    !empty(tokarg) &&
569*0Sstevel@tonic-gate 			    !argmatch(tokarg)) {
570*0Sstevel@tonic-gate 				tokadv = tokarg = NULL;
571*0Sstevel@tonic-gate 				cwd[cwdlen] = '\0';
572*0Sstevel@tonic-gate 				continue;
573*0Sstevel@tonic-gate 			}
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 			/*
576*0Sstevel@tonic-gate 			 * Parse advice options.
577*0Sstevel@tonic-gate 			 * If empty, any advice from ENV_MADV is reset.
578*0Sstevel@tonic-gate 			 */
579*0Sstevel@tonic-gate 			if (empty(tokadv)) {
580*0Sstevel@tonic-gate 				advice_all = -1;
581*0Sstevel@tonic-gate 			} else {
582*0Sstevel@tonic-gate 				advice_opts(tokadv, execname, cfgfile, lineno);
583*0Sstevel@tonic-gate 			}
584*0Sstevel@tonic-gate 			break;
585*0Sstevel@tonic-gate 		}
586*0Sstevel@tonic-gate 		(void) fclose(fp);
587*0Sstevel@tonic-gate 	}
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	/*
590*0Sstevel@tonic-gate 	 * Pagesize needed for proper aligning by brk interpose.
591*0Sstevel@tonic-gate 	 */
592*0Sstevel@tonic-gate 	pagesize = sysconf(_SC_PAGESIZE);
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	/*
595*0Sstevel@tonic-gate 	 * Apply global advice if set.
596*0Sstevel@tonic-gate 	 * Specific options in the cfgfile take precedence.
597*0Sstevel@tonic-gate 	 */
598*0Sstevel@tonic-gate 	if (advice_all >= 0) {
599*0Sstevel@tonic-gate 		if (advice_heap < 0)
600*0Sstevel@tonic-gate 			advice_heap = advice_all;
601*0Sstevel@tonic-gate 		if (advice_shm < 0)
602*0Sstevel@tonic-gate 			advice_shm = advice_all;
603*0Sstevel@tonic-gate 		if (advice_map < 0)
604*0Sstevel@tonic-gate 			advice_map = advice_all;
605*0Sstevel@tonic-gate 	}
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_all %d\n", advice_all));
608*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_heap %d\n", advice_heap));
609*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_shm %d\n", advice_shm));
610*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_ism %d\n", advice_ism));
611*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_dism %d\n", advice_dism));
612*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_map %d\n", advice_map));
613*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_mapshared %d\n", advice_mapshared));
614*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_mapprivate %d\n", advice_mapprivate));
615*0Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_mapanon %d\n", advice_mapanon));
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	/*
618*0Sstevel@tonic-gate 	 * If heap advice is specified, apply it to the existing heap.
619*0Sstevel@tonic-gate 	 * As the heap grows the kernel applies the advice automatically
620*0Sstevel@tonic-gate 	 * to new portions of the heap.
621*0Sstevel@tonic-gate 	 */
622*0Sstevel@tonic-gate 	if (advice_heap >= 0) {
623*0Sstevel@tonic-gate 		if (rc = mygetbrk(&brkbase, &brksize)) {
624*0Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
625*0Sstevel@tonic-gate 			    "%s: /proc/self/status read failed [%s]\n"),
626*0Sstevel@tonic-gate 			    madvident, strerror(rc));
627*0Sstevel@tonic-gate 		} else {
628*0Sstevel@tonic-gate 			MADVPRINT(4, (stderr, "brkbase 0x%x brksize 0x%x\n",
629*0Sstevel@tonic-gate 			    brkbase, brksize));
630*0Sstevel@tonic-gate 			/*
631*0Sstevel@tonic-gate 			 * Align start address for memcntl and apply advice
632*0Sstevel@tonic-gate 			 * on full pages of heap.  Create a page of heap if
633*0Sstevel@tonic-gate 			 * it does not already exist.
634*0Sstevel@tonic-gate 			 */
635*0Sstevel@tonic-gate 			brkend = roundup(brkbase+brksize, pagesize);
636*0Sstevel@tonic-gate 			brkbase = roundup(brkbase, pagesize);
637*0Sstevel@tonic-gate 			brksize = brkend - brkbase;
638*0Sstevel@tonic-gate 			if (brksize < pagesize) {
639*0Sstevel@tonic-gate 				if (sbrk(pagesize) == (void *)-1) {
640*0Sstevel@tonic-gate 					madverr(errfp, dgettext(TEXT_DOMAIN,
641*0Sstevel@tonic-gate 					    "%s: sbrk failed [%s]\n"),
642*0Sstevel@tonic-gate 					    madvident, strerror(errno));
643*0Sstevel@tonic-gate 					goto out;
644*0Sstevel@tonic-gate 				}
645*0Sstevel@tonic-gate 				brksize = pagesize;
646*0Sstevel@tonic-gate 			}
647*0Sstevel@tonic-gate 			MADVPRINT(1, (stderr, "heap advice: 0x%x 0x%x %d\n",
648*0Sstevel@tonic-gate 			    brkbase, brksize, advice_heap));
649*0Sstevel@tonic-gate 			if (memcntl((caddr_t)brkbase, brksize, MC_ADVISE,
650*0Sstevel@tonic-gate 			    (caddr_t)(intptr_t)advice_heap, 0, 0) < 0) {
651*0Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
652*0Sstevel@tonic-gate 				    "%s: memcntl() failed [%s]: heap advice\n"),
653*0Sstevel@tonic-gate 				    madvident, strerror(errno));
654*0Sstevel@tonic-gate 			}
655*0Sstevel@tonic-gate 		}
656*0Sstevel@tonic-gate 	}
657*0Sstevel@tonic-gate out:
658*0Sstevel@tonic-gate 	if (errfp) {
659*0Sstevel@tonic-gate 		(void) fclose(errfp);
660*0Sstevel@tonic-gate 		(void) setlocale(LC_MESSAGES, locale);
661*0Sstevel@tonic-gate 	} else {
662*0Sstevel@tonic-gate 		/* close log file: no-op if nothing logged to syslog */
663*0Sstevel@tonic-gate 		closelog();
664*0Sstevel@tonic-gate 	}
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate }
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate /*
669*0Sstevel@tonic-gate  * shmat interpose
670*0Sstevel@tonic-gate  */
671*0Sstevel@tonic-gate void *
672*0Sstevel@tonic-gate shmat(int shmid, const void *shmaddr, int shmflag)
673*0Sstevel@tonic-gate {
674*0Sstevel@tonic-gate 	static caddr_t (*shmatfunc)() = NULL;
675*0Sstevel@tonic-gate 	void *result;
676*0Sstevel@tonic-gate 	int advice = -1;
677*0Sstevel@tonic-gate 	struct shmid_ds	mds;
678*0Sstevel@tonic-gate #ifdef MADVDEBUG
679*0Sstevel@tonic-gate 	int rc;
680*0Sstevel@tonic-gate #else
681*0Sstevel@tonic-gate 	/* LINTED */
682*0Sstevel@tonic-gate 	int rc;
683*0Sstevel@tonic-gate #endif
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 	if (!shmatfunc) {
686*0Sstevel@tonic-gate 		shmatfunc = (caddr_t (*)()) dlsym(RTLD_NEXT, "shmat");
687*0Sstevel@tonic-gate 		assert(shmatfunc);
688*0Sstevel@tonic-gate 	}
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 	result = shmatfunc(shmid, shmaddr, shmflag);
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	/*
693*0Sstevel@tonic-gate 	 * Options ism, dism take precedence over option shm.
694*0Sstevel@tonic-gate 	 */
695*0Sstevel@tonic-gate 	if (advice_ism >= 0 && (shmflag & SHM_SHARE_MMU)) {
696*0Sstevel@tonic-gate 		advice = advice_ism;
697*0Sstevel@tonic-gate 	} else if (advice_dism >= 0 && (shmflag & SHM_PAGEABLE)) {
698*0Sstevel@tonic-gate 		advice = advice_dism;
699*0Sstevel@tonic-gate 	} else if (advice_shm >= 0) {
700*0Sstevel@tonic-gate 		advice = advice_shm;
701*0Sstevel@tonic-gate 	}
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 	/*
704*0Sstevel@tonic-gate 	 * Apply advice if specified and shmat succeeded.
705*0Sstevel@tonic-gate 	 */
706*0Sstevel@tonic-gate 	if (advice >= 0 && result != (void *)-1) {
707*0Sstevel@tonic-gate 		/* First determine segment size */
708*0Sstevel@tonic-gate 		rc = shmctl(shmid, IPC_STAT, &mds);
709*0Sstevel@tonic-gate 		MADVPRINT(4, (stderr, "shmctl rc %d errno %d\n",
710*0Sstevel@tonic-gate 		    strerror(errno)));
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 		rc = memcntl(result, mds.shm_segsz, MC_ADVISE,
713*0Sstevel@tonic-gate 		    (caddr_t)(intptr_t)advice, 0, 0);
714*0Sstevel@tonic-gate 		MADVPRINT(1, (stderr,
715*0Sstevel@tonic-gate 		    "shmat advice: 0x%x 0x%x %d, rc %d errno %d\n",
716*0Sstevel@tonic-gate 		    result, mds.shm_segsz, advice, rc, errno));
717*0Sstevel@tonic-gate 	}
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	return (result);
720*0Sstevel@tonic-gate }
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate /*
723*0Sstevel@tonic-gate  * mmap interpose
724*0Sstevel@tonic-gate  */
725*0Sstevel@tonic-gate caddr_t
726*0Sstevel@tonic-gate mmap(caddr_t addr, size_t len, int prot, int flags, int fd, off_t pos)
727*0Sstevel@tonic-gate {
728*0Sstevel@tonic-gate 	static caddr_t (*mmapfunc)() = NULL;
729*0Sstevel@tonic-gate 	caddr_t result;
730*0Sstevel@tonic-gate 	int advice = -1;
731*0Sstevel@tonic-gate #ifdef MADVDEBUG
732*0Sstevel@tonic-gate 	int rc;
733*0Sstevel@tonic-gate #else
734*0Sstevel@tonic-gate 	/* LINTED */
735*0Sstevel@tonic-gate 	int rc;
736*0Sstevel@tonic-gate #endif
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	if (!mmapfunc) {
739*0Sstevel@tonic-gate 		mmapfunc = (caddr_t (*)()) dlsym(RTLD_NEXT, "mmap");
740*0Sstevel@tonic-gate 		assert(mmapfunc);
741*0Sstevel@tonic-gate 	}
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	result = mmapfunc(addr, len, prot, flags, fd, pos);
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	/*
746*0Sstevel@tonic-gate 	 * Option mapanon has highest precedence while option map
747*0Sstevel@tonic-gate 	 * has lowest precedence.
748*0Sstevel@tonic-gate 	 */
749*0Sstevel@tonic-gate 	if (advice_mapanon >= 0 && (flags & MAP_ANON)) {
750*0Sstevel@tonic-gate 		advice = advice_mapanon;
751*0Sstevel@tonic-gate 	} else if (advice_mapshared >= 0 && (flags & MAP_SHARED)) {
752*0Sstevel@tonic-gate 		advice = advice_mapshared;
753*0Sstevel@tonic-gate 	} else if (advice_mapprivate >= 0 && (flags & MAP_PRIVATE)) {
754*0Sstevel@tonic-gate 		advice = advice_mapprivate;
755*0Sstevel@tonic-gate 	} else if (advice_map >= 0) {
756*0Sstevel@tonic-gate 		advice = advice_map;
757*0Sstevel@tonic-gate 	}
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	/*
760*0Sstevel@tonic-gate 	 * Apply advice if specified and mmap succeeded.
761*0Sstevel@tonic-gate 	 */
762*0Sstevel@tonic-gate 	if (advice >= 0 && result != MAP_FAILED) {
763*0Sstevel@tonic-gate 		rc = memcntl(result, len, MC_ADVISE,
764*0Sstevel@tonic-gate 		    (caddr_t)(intptr_t)advice, 0, 0);
765*0Sstevel@tonic-gate 		MADVPRINT(1, (stderr,
766*0Sstevel@tonic-gate 		    "mmap advice: 0x%x 0x%x %d, rc %d errno %d\n",
767*0Sstevel@tonic-gate 		    result, len, advice, rc, errno));
768*0Sstevel@tonic-gate 	}
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 	return (result);
771*0Sstevel@tonic-gate }
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate #if !defined(_LP64)
774*0Sstevel@tonic-gate /*
775*0Sstevel@tonic-gate  * mmap64 interpose
776*0Sstevel@tonic-gate  */
777*0Sstevel@tonic-gate caddr_t
778*0Sstevel@tonic-gate mmap64(caddr_t addr, size_t len, int prot, int flags, int fd, off64_t pos)
779*0Sstevel@tonic-gate {
780*0Sstevel@tonic-gate 	static caddr_t (*mmap64func)();
781*0Sstevel@tonic-gate 	caddr_t result;
782*0Sstevel@tonic-gate 	int advice = -1;
783*0Sstevel@tonic-gate #ifdef MADVDEBUG
784*0Sstevel@tonic-gate 	int rc;
785*0Sstevel@tonic-gate #else
786*0Sstevel@tonic-gate 	/* LINTED */
787*0Sstevel@tonic-gate 	int rc;
788*0Sstevel@tonic-gate #endif
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	if (!mmap64func) {
791*0Sstevel@tonic-gate 		mmap64func = (caddr_t (*)()) dlsym(RTLD_NEXT, "mmap64");
792*0Sstevel@tonic-gate 		assert(mmap64func);
793*0Sstevel@tonic-gate 	}
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 	result = mmap64func(addr, len, prot, flags, fd, pos);
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	/*
798*0Sstevel@tonic-gate 	 * Option mapanon has highest precedence while option map
799*0Sstevel@tonic-gate 	 * has lowest precedence.
800*0Sstevel@tonic-gate 	 */
801*0Sstevel@tonic-gate 	if (advice_mapanon >= 0 && (flags & MAP_ANON)) {
802*0Sstevel@tonic-gate 		advice = advice_mapanon;
803*0Sstevel@tonic-gate 	} else if (advice_mapshared >= 0 && (flags & MAP_SHARED)) {
804*0Sstevel@tonic-gate 		advice = advice_mapshared;
805*0Sstevel@tonic-gate 	} else if (advice_mapprivate >= 0 && (flags & MAP_PRIVATE)) {
806*0Sstevel@tonic-gate 		advice = advice_mapprivate;
807*0Sstevel@tonic-gate 	} else if (advice_map >= 0) {
808*0Sstevel@tonic-gate 		advice = advice_map;
809*0Sstevel@tonic-gate 	}
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 	/*
812*0Sstevel@tonic-gate 	 * Apply advice if specified and mmap succeeded.
813*0Sstevel@tonic-gate 	 */
814*0Sstevel@tonic-gate 	if (advice >= 0 && result != MAP_FAILED) {
815*0Sstevel@tonic-gate 		rc = memcntl(result, len, MC_ADVISE, (caddr_t)advice, 0, 0);
816*0Sstevel@tonic-gate 		MADVPRINT(1, (stderr,
817*0Sstevel@tonic-gate 		    "mmap64 advice: 0x%x 0x%x %d, rc %d errno %d\n",
818*0Sstevel@tonic-gate 		    result, len, advice, rc, errno));
819*0Sstevel@tonic-gate 	}
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 	return (result);
822*0Sstevel@tonic-gate }
823*0Sstevel@tonic-gate #endif	/* !_LP64 */
824