xref: /onnv-gate/usr/src/lib/madv/common/madv.c (revision 1914:8a8c5f225b1b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1914Scasper  * Common Development and Distribution License (the "License").
6*1914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1914Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <sys/shm.h>
320Sstevel@tonic-gate #include <sys/mman.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <sys/auxv.h>
400Sstevel@tonic-gate #include <stdarg.h>
410Sstevel@tonic-gate #include <syslog.h>
420Sstevel@tonic-gate #include <sys/param.h>
430Sstevel@tonic-gate #include <sys/sysmacros.h>
440Sstevel@tonic-gate #include <procfs.h>
450Sstevel@tonic-gate #include <dlfcn.h>
460Sstevel@tonic-gate #include <assert.h>
470Sstevel@tonic-gate #include <libintl.h>
480Sstevel@tonic-gate #include <locale.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate extern int	gmatch(const char *s, const char *p);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #pragma init(__madvmain)
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static FILE *errfp = NULL;
550Sstevel@tonic-gate static const char *madvident = "madv.so.1";
560Sstevel@tonic-gate static int pagesize;
570Sstevel@tonic-gate static int advice_all = -1;
580Sstevel@tonic-gate static int advice_heap = -1;
590Sstevel@tonic-gate static int advice_shm = -1;
600Sstevel@tonic-gate static int advice_ism = -1;
610Sstevel@tonic-gate static int advice_dism = -1;
620Sstevel@tonic-gate static int advice_map = -1;
630Sstevel@tonic-gate static int advice_mapshared = -1;
640Sstevel@tonic-gate static int advice_mapprivate = -1;
650Sstevel@tonic-gate static int advice_mapanon = -1;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /* environment variables */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	ENV_MADV		"MADV"
700Sstevel@tonic-gate #define	ENV_MADVCFGFILE		"MADVCFGFILE"
710Sstevel@tonic-gate #define	ENV_MADVERRFILE		"MADVERRFILE"
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* config file */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define	DEF_MADVCFGFILE		"/etc/madv.conf"
760Sstevel@tonic-gate #define	MAXLINELEN	MAXPATHLEN + 64
770Sstevel@tonic-gate #define	CFGDELIMITER	':'
780Sstevel@tonic-gate #define	ARGDELIMITER	' '
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * avoid malloc which causes certain applications to crash
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate static char		lbuf[MAXLINELEN];
840Sstevel@tonic-gate static char		pbuf[MAXPATHLEN];
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #ifdef MADVDEBUG
870Sstevel@tonic-gate #define	ENV_MADVDEBUG	"MADVDEBUG"
880Sstevel@tonic-gate #define	MADVPRINT(x, y)	if (madvdebug & x) (void) fprintf y;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static int madvdebug = 0;
910Sstevel@tonic-gate #else
920Sstevel@tonic-gate #define	MADVPRINT(x, y)
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * advice options
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate static char *legal_optstr[] = {
990Sstevel@tonic-gate 	"madv",
1000Sstevel@tonic-gate 	"heap",
1010Sstevel@tonic-gate 	"shm",
1020Sstevel@tonic-gate 	"ism",
1030Sstevel@tonic-gate 	"dism",
1040Sstevel@tonic-gate 	"map",
1050Sstevel@tonic-gate 	"mapshared",
1060Sstevel@tonic-gate 	"mapprivate",
1070Sstevel@tonic-gate 	"mapanon",
1080Sstevel@tonic-gate 	NULL
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate enum optenum {
1120Sstevel@tonic-gate 	OPT_MADV,
1130Sstevel@tonic-gate 	OPT_HEAP,
1140Sstevel@tonic-gate 	OPT_SHM,
1150Sstevel@tonic-gate 	OPT_ISM,
1160Sstevel@tonic-gate 	OPT_DISM,
1170Sstevel@tonic-gate 	OPT_MAP,
1180Sstevel@tonic-gate 	OPT_MAPSHARED,
1190Sstevel@tonic-gate 	OPT_MAPPRIVATE,
1200Sstevel@tonic-gate 	OPT_MAPANON
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * Advice values
1250Sstevel@tonic-gate  * These need to correspond to the order of the MADV_ flags in mman.h
1260Sstevel@tonic-gate  * since the position infers the value for the flag.
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate static char *legal_madvice[] = {
1290Sstevel@tonic-gate 	"normal",
1300Sstevel@tonic-gate 	"random",
1310Sstevel@tonic-gate 	"sequential",
1320Sstevel@tonic-gate 	"willneed_NOT_SUPPORTED!",
1330Sstevel@tonic-gate 	"dontneed_NOT_SUPPORTED!",
1340Sstevel@tonic-gate 	"free_NOT_SUPPORTED!",
1350Sstevel@tonic-gate 	"access_default",
1360Sstevel@tonic-gate 	"access_lwp",
1370Sstevel@tonic-gate 	"access_many",
1380Sstevel@tonic-gate 	NULL
1390Sstevel@tonic-gate };
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1420Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1430Sstevel@tonic-gate #endif
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*PRINTFLIKE2*/
1460Sstevel@tonic-gate static void
madverr(FILE * fp,char * fmt,...)1470Sstevel@tonic-gate madverr(FILE *fp, char *fmt, ...)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	va_list		ap;
1500Sstevel@tonic-gate 	va_start(ap, fmt);
1510Sstevel@tonic-gate 	if (fp)
1520Sstevel@tonic-gate 		(void) vfprintf(fp, fmt, ap);
1530Sstevel@tonic-gate 	else
1540Sstevel@tonic-gate 		vsyslog(LOG_ERR, fmt, ap);
1550Sstevel@tonic-gate 	va_end(ap);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * Return the pointer to the fully-resolved path name of the process's
1600Sstevel@tonic-gate  * executable file obtained from the AT_SUN_EXECNAME aux vector entry.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate static const char *
mygetexecname(void)1630Sstevel@tonic-gate mygetexecname(void)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	const char	*execname = NULL;
1660Sstevel@tonic-gate 	static auxv_t	auxb;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
1690Sstevel@tonic-gate 	 * The first time through, read the initial aux vector that was
1700Sstevel@tonic-gate 	 * passed to the process at exec(2).  Only do this once.
1710Sstevel@tonic-gate 	 */
1720Sstevel@tonic-gate 	int fd = open("/proc/self/auxv", O_RDONLY);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if (fd >= 0) {
1750Sstevel@tonic-gate 		while (read(fd, &auxb, sizeof (auxv_t)) == sizeof (auxv_t)) {
1760Sstevel@tonic-gate 			if (auxb.a_type == AT_SUN_EXECNAME) {
1770Sstevel@tonic-gate 				execname = auxb.a_un.a_ptr;
1780Sstevel@tonic-gate 				break;
1790Sstevel@tonic-gate 			}
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 		(void) close(fd);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 	return (execname);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * Return the process's current brk base and size.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate static int
mygetbrk(uintptr_t * base,size_t * size)1900Sstevel@tonic-gate mygetbrk(uintptr_t *base, size_t *size)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	int fd;
1930Sstevel@tonic-gate 	pstatus_t ps;
1940Sstevel@tonic-gate 	int rc;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	fd = open("/proc/self/status", O_RDONLY);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (fd >= 0) {
1990Sstevel@tonic-gate 		if (read(fd, &ps, sizeof (ps)) == sizeof (ps)) {
2000Sstevel@tonic-gate 			*base = ps.pr_brkbase;
2010Sstevel@tonic-gate 			*size = ps.pr_brksize;
2020Sstevel@tonic-gate 			rc = 0;
2030Sstevel@tonic-gate 		} else {
2040Sstevel@tonic-gate 			rc = errno;
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 		(void) close(fd);
2070Sstevel@tonic-gate 	} else {
2080Sstevel@tonic-gate 		rc = errno;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	return (rc);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * Check if exec name matches cfgname found in madv cfg file.
2150Sstevel@tonic-gate  */
2160Sstevel@tonic-gate static int
fnmatch(const char * execname,char * cfgname,char * cwd)2170Sstevel@tonic-gate fnmatch(const char *execname, char *cfgname, char *cwd)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	const char	*ename;
2200Sstevel@tonic-gate 	int		rc;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/* cfgname should not have a '/' unless it begins with one */
2230Sstevel@tonic-gate 	if (cfgname[0] == '/') {
2240Sstevel@tonic-gate 		/*
2250Sstevel@tonic-gate 		 * if execname does not begin with a '/', prepend the
2260Sstevel@tonic-gate 		 * current directory.
2270Sstevel@tonic-gate 		 */
2280Sstevel@tonic-gate 		if (execname[0] != '/') {
2290Sstevel@tonic-gate 			ename = (const char *)strcat(cwd, execname);
2300Sstevel@tonic-gate 		} else
2310Sstevel@tonic-gate 			ename = execname;
2320Sstevel@tonic-gate 	} else {	/* simple cfg name */
2330Sstevel@tonic-gate 		if (ename = strrchr(execname, '/'))
2340Sstevel@tonic-gate 			/* execname is a path name - get the base name */
2350Sstevel@tonic-gate 			ename++;
2360Sstevel@tonic-gate 		else
2370Sstevel@tonic-gate 			ename = execname;
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 	rc = gmatch(ename, cfgname);
2400Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "gmatch: %s %s %s %d\n",
2410Sstevel@tonic-gate 	    cfgname, ename, execname, rc));
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	return (rc);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * Check if string matches any of exec arguments.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate static int
argmatch(char * str)2500Sstevel@tonic-gate argmatch(char *str)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	int fd;
2530Sstevel@tonic-gate 	psinfo_t pi;
2540Sstevel@tonic-gate 	int rc = 0;
2550Sstevel@tonic-gate 	int arg;
2560Sstevel@tonic-gate 	char **argv;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	fd = open("/proc/self/psinfo", O_RDONLY);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (fd >= 0) {
2610Sstevel@tonic-gate 		if (read(fd, &pi, sizeof (pi)) == sizeof (pi)) {
2620Sstevel@tonic-gate 			argv = (char **)pi.pr_argv;
2630Sstevel@tonic-gate 			argv++;
2640Sstevel@tonic-gate 			MADVPRINT(2, (stderr, "argmatch: %s ", str));
2650Sstevel@tonic-gate 			for (arg = 1; arg < pi.pr_argc; arg++, argv++) {
2660Sstevel@tonic-gate 				if (rc = gmatch(*argv, str)) {
2670Sstevel@tonic-gate 					MADVPRINT(2, (stderr, "%s ", *argv));
2680Sstevel@tonic-gate 					break;
2690Sstevel@tonic-gate 				}
2700Sstevel@tonic-gate 			}
2710Sstevel@tonic-gate 			MADVPRINT(2, (stderr, "%d\n", rc));
2720Sstevel@tonic-gate 		} else {
2730Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
2740Sstevel@tonic-gate 			    "%s: /proc/self/psinfo read failed [%s]\n"),
2750Sstevel@tonic-gate 			    madvident, strerror(errno));
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 		(void) close(fd);
2780Sstevel@tonic-gate 	} else {
2790Sstevel@tonic-gate 		madverr(errfp, dgettext(TEXT_DOMAIN,
2800Sstevel@tonic-gate 		    "%s: /proc/self/psinfo open failed [%s]\n"),
2810Sstevel@tonic-gate 		    madvident, strerror(errno));
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 	return (rc);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static int
empty(char * str)2870Sstevel@tonic-gate empty(char *str)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	char	c;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	while ((c = *str) == '\n' || c == ' ' || c == '\t')
2920Sstevel@tonic-gate 		str++;
2930Sstevel@tonic-gate 	return (*str == '\0');
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate static int
strtoadv(char * advstr)2970Sstevel@tonic-gate strtoadv(char *advstr)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	char *dummy, *locstr = advstr;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	return (getsubopt(&locstr, legal_madvice, &dummy));
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate static void
advice_opts(char * optstr,const char * execname,char * cfgfile,int lineno)3050Sstevel@tonic-gate advice_opts(char *optstr, const char *execname, char *cfgfile, int lineno)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	char *value;
3080Sstevel@tonic-gate 	int opt;
3090Sstevel@tonic-gate 	int advice = 0;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	while (*optstr != '\0') {
3120Sstevel@tonic-gate 		opt = getsubopt(&optstr, legal_optstr, &value);
3130Sstevel@tonic-gate 		if (opt < 0) {
3140Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
3150Sstevel@tonic-gate 			    "%s: invalid advice option (%s)"
3160Sstevel@tonic-gate 			    " for %s - cfgfile: %s, line: %d\n"),
3170Sstevel@tonic-gate 			    madvident, value, execname, cfgfile, lineno);
3180Sstevel@tonic-gate 			break;
3190Sstevel@tonic-gate 		} else if (!value) {
3200Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
3210Sstevel@tonic-gate 			    "%s: option missing advice"
3220Sstevel@tonic-gate 			    " for %s - cfgfile: %s, line: %d\n"),
3230Sstevel@tonic-gate 			    madvident, execname, cfgfile, lineno);
3240Sstevel@tonic-gate 			break;
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 		advice = strtoadv(value);
3270Sstevel@tonic-gate 		if (advice < 0) {
3280Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
3290Sstevel@tonic-gate 			    "%s: invalid advice specified (%s)"
3300Sstevel@tonic-gate 			    " for %s - cfgfile: %s, line: %d\n"),
3310Sstevel@tonic-gate 			    madvident, value, execname, cfgfile, lineno);
3320Sstevel@tonic-gate 			break;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		switch (opt) {
3350Sstevel@tonic-gate 		case OPT_MADV:
3360Sstevel@tonic-gate 			advice_all = advice;
3370Sstevel@tonic-gate 			break;
3380Sstevel@tonic-gate 		case OPT_HEAP:
3390Sstevel@tonic-gate 			if (advice_heap < 0) {
3400Sstevel@tonic-gate 				advice_heap = advice;
3410Sstevel@tonic-gate 			} else {
3420Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
3430Sstevel@tonic-gate 				    "%s: duplicate advice specified "
3440Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
3450Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
3460Sstevel@tonic-gate 				    lineno);
3470Sstevel@tonic-gate 			}
3480Sstevel@tonic-gate 			break;
3490Sstevel@tonic-gate 		case OPT_SHM:
3500Sstevel@tonic-gate 			if (advice_shm < 0) {
3510Sstevel@tonic-gate 				advice_shm = advice;
3520Sstevel@tonic-gate 			} else {
3530Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
3540Sstevel@tonic-gate 				    "%s: duplicate advice specified "
3550Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
3560Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
3570Sstevel@tonic-gate 				    lineno);
3580Sstevel@tonic-gate 			}
3590Sstevel@tonic-gate 			break;
3600Sstevel@tonic-gate 		case OPT_ISM:
3610Sstevel@tonic-gate 			if (advice_ism < 0) {
3620Sstevel@tonic-gate 				advice_ism = advice;
3630Sstevel@tonic-gate 			} else {
3640Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
3650Sstevel@tonic-gate 				    "%s: duplicate advice specified "
3660Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
3670Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
3680Sstevel@tonic-gate 				    lineno);
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 			break;
3710Sstevel@tonic-gate 		case OPT_DISM:
3720Sstevel@tonic-gate 			if (advice_dism < 0) {
3730Sstevel@tonic-gate 				advice_dism = advice;
3740Sstevel@tonic-gate 			} else {
3750Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
3760Sstevel@tonic-gate 				    "%s: duplicate advice specified "
3770Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
3780Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
3790Sstevel@tonic-gate 				    lineno);
3800Sstevel@tonic-gate 			}
3810Sstevel@tonic-gate 			break;
3820Sstevel@tonic-gate 		case OPT_MAP:
3830Sstevel@tonic-gate 			if (advice_map < 0) {
3840Sstevel@tonic-gate 				advice_map = advice;
3850Sstevel@tonic-gate 			} else {
3860Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
3870Sstevel@tonic-gate 				    "%s: duplicate advice specified "
3880Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
3890Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
3900Sstevel@tonic-gate 				    lineno);
3910Sstevel@tonic-gate 			}
3920Sstevel@tonic-gate 			break;
3930Sstevel@tonic-gate 		case OPT_MAPSHARED:
3940Sstevel@tonic-gate 			if (advice_mapshared < 0) {
3950Sstevel@tonic-gate 				advice_mapshared = advice;
3960Sstevel@tonic-gate 			} else {
3970Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
3980Sstevel@tonic-gate 				    "%s: duplicate advice specified "
3990Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
4000Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
4010Sstevel@tonic-gate 				    lineno);
4020Sstevel@tonic-gate 			}
4030Sstevel@tonic-gate 			break;
4040Sstevel@tonic-gate 		case OPT_MAPPRIVATE:
4050Sstevel@tonic-gate 			if (advice_mapprivate < 0) {
4060Sstevel@tonic-gate 				advice_mapprivate = advice;
4070Sstevel@tonic-gate 			} else {
4080Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
4090Sstevel@tonic-gate 				    "%s: duplicate advice specified "
4100Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
4110Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
4120Sstevel@tonic-gate 				    lineno);
4130Sstevel@tonic-gate 			}
4140Sstevel@tonic-gate 			break;
4150Sstevel@tonic-gate 		case OPT_MAPANON:
4160Sstevel@tonic-gate 			if (advice_mapanon < 0) {
4170Sstevel@tonic-gate 				advice_mapanon = advice;
4180Sstevel@tonic-gate 			} else {
4190Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
4200Sstevel@tonic-gate 				    "%s: duplicate advice specified "
4210Sstevel@tonic-gate 				    "(%s) for %s - cfgfile: %s, line: %d\n"),
4220Sstevel@tonic-gate 				    madvident, value, execname, cfgfile,
4230Sstevel@tonic-gate 				    lineno);
4240Sstevel@tonic-gate 			}
4250Sstevel@tonic-gate 			break;
4260Sstevel@tonic-gate 		default:
4270Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
4280Sstevel@tonic-gate 			    "%s: invalid advice option (%s)"
4290Sstevel@tonic-gate 			    " for %s - cfgfile: %s, line: %d\n"),
4300Sstevel@tonic-gate 			    madvident, value, execname, cfgfile, lineno);
4310Sstevel@tonic-gate 			break;
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate static void
__madvmain()4370Sstevel@tonic-gate __madvmain()
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	char		*cfgfile, *errfile;
4400Sstevel@tonic-gate 	FILE		*fp = NULL;
4410Sstevel@tonic-gate 	const char	*execname;
4420Sstevel@tonic-gate 	char		*cwd;
4430Sstevel@tonic-gate 	int		cwdlen;
4440Sstevel@tonic-gate 	char		*tok, *tokadv, *tokarg;
4450Sstevel@tonic-gate 	char		*str, *envadv;
4460Sstevel@tonic-gate 	int		lineno = 0;
4470Sstevel@tonic-gate 	int		advice;
4480Sstevel@tonic-gate 	uintptr_t	brkbase, brkend;
4490Sstevel@tonic-gate 	size_t		brksize;
4500Sstevel@tonic-gate 	int		rc;
4510Sstevel@tonic-gate 	char		*locale;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * If a private error file is indicated then set the locale
4550Sstevel@tonic-gate 	 * for error messages for the duration of this routine.
4560Sstevel@tonic-gate 	 * Error messages destined for syslog should not be translated
4570Sstevel@tonic-gate 	 * and thus come from the default C locale.
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	if ((errfile = getenv(ENV_MADVERRFILE)) != NULL) {
460*1914Scasper 		errfp = fopen(errfile, "aF");
4610Sstevel@tonic-gate 		if (errfp) {
4620Sstevel@tonic-gate 			locale = setlocale(LC_MESSAGES, "");
4630Sstevel@tonic-gate 		} else {
4640Sstevel@tonic-gate 			madverr(NULL, dgettext(TEXT_DOMAIN,
4650Sstevel@tonic-gate 			    "%s: cannot open error file: %s [%s]\n"),
4660Sstevel@tonic-gate 			    madvident, errfile, strerror(errno));
4670Sstevel@tonic-gate 		}
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate #ifdef MADVDEBUG
4710Sstevel@tonic-gate 	if (str = getenv(ENV_MADVDEBUG))
4720Sstevel@tonic-gate 		madvdebug = atoi(str);
4730Sstevel@tonic-gate #endif
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (envadv = getenv(ENV_MADV)) {
4760Sstevel@tonic-gate 		if ((advice = strtoadv(envadv)) >= 0)
4770Sstevel@tonic-gate 			advice_all = advice;
4780Sstevel@tonic-gate 		else
4790Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
4800Sstevel@tonic-gate 			    "%s: invalid advice specified: MADV=%s\n"),
4810Sstevel@tonic-gate 			    madvident, envadv);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/*
4850Sstevel@tonic-gate 	 * Open specified cfg file or default one.
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	if (cfgfile = getenv(ENV_MADVCFGFILE)) {
488*1914Scasper 		fp = fopen(cfgfile, "rF");
4890Sstevel@tonic-gate 		if (!fp) {
4900Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
4910Sstevel@tonic-gate 			    "%s: cannot open configuration file: %s [%s]\n"),
4920Sstevel@tonic-gate 			    madvident, cfgfile, strerror(errno));
4930Sstevel@tonic-gate 		}
4940Sstevel@tonic-gate 	} else {
4950Sstevel@tonic-gate 		cfgfile = DEF_MADVCFGFILE;
496*1914Scasper 		fp = fopen(cfgfile, "rF");
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (fp) {
5000Sstevel@tonic-gate 		execname = mygetexecname();
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		cwd = getcwd(pbuf, MAXPATHLEN);
5030Sstevel@tonic-gate 		if (!cwd)
5040Sstevel@tonic-gate 			return;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		cwd = strcat(cwd, "/");
5070Sstevel@tonic-gate 		cwdlen = strlen(cwd);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		while (fgets(lbuf, MAXLINELEN, fp)) {
5100Sstevel@tonic-gate 			lineno++;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 			/*
5130Sstevel@tonic-gate 			 * Make sure line wasn't truncated.
5140Sstevel@tonic-gate 			 */
5150Sstevel@tonic-gate 			if (strlen(lbuf) >= MAXLINELEN - 1) {
5160Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
5170Sstevel@tonic-gate 				    "%s: invalid entry, "
5180Sstevel@tonic-gate 				    "line too long - cfgfile:"
5190Sstevel@tonic-gate 				    " %s, line: %d\n"),
5200Sstevel@tonic-gate 				    madvident, cfgfile, lineno);
5210Sstevel@tonic-gate 				continue;
5220Sstevel@tonic-gate 			}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 			if (empty(lbuf))
5250Sstevel@tonic-gate 				continue;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 			/*
5280Sstevel@tonic-gate 			 * Get advice options.
5290Sstevel@tonic-gate 			 * Parse right to left in case delimiter is in name.
5300Sstevel@tonic-gate 			 */
5310Sstevel@tonic-gate 			if (!(tokadv = strrchr(lbuf, CFGDELIMITER))) {
5320Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
5330Sstevel@tonic-gate 				    "%s: no delimiter specified - cfgfile:"
5340Sstevel@tonic-gate 				    " %s, line: %d\n"),
5350Sstevel@tonic-gate 				    madvident, cfgfile, lineno);
5360Sstevel@tonic-gate 				continue;
5370Sstevel@tonic-gate 			}
5380Sstevel@tonic-gate 			*tokadv++ = '\0';
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 			/*
5410Sstevel@tonic-gate 			 * Remove newline from end of advice options.
5420Sstevel@tonic-gate 			 */
5430Sstevel@tonic-gate 			if (str = strrchr(tokadv, '\n'))
5440Sstevel@tonic-gate 				*str = '\0';
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 			/*
5470Sstevel@tonic-gate 			 * Get optional argument string.
5480Sstevel@tonic-gate 			 */
5490Sstevel@tonic-gate 			if (tokarg = strrchr(lbuf, ARGDELIMITER)) {
5500Sstevel@tonic-gate 				*tokarg++ = '\0';
5510Sstevel@tonic-gate 			}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 			/*
5540Sstevel@tonic-gate 			 * Compare exec name.
5550Sstevel@tonic-gate 			 */
5560Sstevel@tonic-gate 			tok = lbuf;
5570Sstevel@tonic-gate 			if (!fnmatch(execname, tok, cwd)) {
5580Sstevel@tonic-gate 				tokadv = tokarg = NULL;
5590Sstevel@tonic-gate 				cwd[cwdlen] = '\0';
5600Sstevel@tonic-gate 				continue;
5610Sstevel@tonic-gate 			}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 			/*
5640Sstevel@tonic-gate 			 * Compare arguments if argument string specified.
5650Sstevel@tonic-gate 			 */
5660Sstevel@tonic-gate 			if (tokarg &&
5670Sstevel@tonic-gate 			    !empty(tokarg) &&
5680Sstevel@tonic-gate 			    !argmatch(tokarg)) {
5690Sstevel@tonic-gate 				tokadv = tokarg = NULL;
5700Sstevel@tonic-gate 				cwd[cwdlen] = '\0';
5710Sstevel@tonic-gate 				continue;
5720Sstevel@tonic-gate 			}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 			/*
5750Sstevel@tonic-gate 			 * Parse advice options.
5760Sstevel@tonic-gate 			 * If empty, any advice from ENV_MADV is reset.
5770Sstevel@tonic-gate 			 */
5780Sstevel@tonic-gate 			if (empty(tokadv)) {
5790Sstevel@tonic-gate 				advice_all = -1;
5800Sstevel@tonic-gate 			} else {
5810Sstevel@tonic-gate 				advice_opts(tokadv, execname, cfgfile, lineno);
5820Sstevel@tonic-gate 			}
5830Sstevel@tonic-gate 			break;
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate 		(void) fclose(fp);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/*
5890Sstevel@tonic-gate 	 * Pagesize needed for proper aligning by brk interpose.
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	pagesize = sysconf(_SC_PAGESIZE);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * Apply global advice if set.
5950Sstevel@tonic-gate 	 * Specific options in the cfgfile take precedence.
5960Sstevel@tonic-gate 	 */
5970Sstevel@tonic-gate 	if (advice_all >= 0) {
5980Sstevel@tonic-gate 		if (advice_heap < 0)
5990Sstevel@tonic-gate 			advice_heap = advice_all;
6000Sstevel@tonic-gate 		if (advice_shm < 0)
6010Sstevel@tonic-gate 			advice_shm = advice_all;
6020Sstevel@tonic-gate 		if (advice_map < 0)
6030Sstevel@tonic-gate 			advice_map = advice_all;
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_all %d\n", advice_all));
6070Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_heap %d\n", advice_heap));
6080Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_shm %d\n", advice_shm));
6090Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_ism %d\n", advice_ism));
6100Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_dism %d\n", advice_dism));
6110Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_map %d\n", advice_map));
6120Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_mapshared %d\n", advice_mapshared));
6130Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_mapprivate %d\n", advice_mapprivate));
6140Sstevel@tonic-gate 	MADVPRINT(2, (stderr, "advice_mapanon %d\n", advice_mapanon));
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	/*
6170Sstevel@tonic-gate 	 * If heap advice is specified, apply it to the existing heap.
6180Sstevel@tonic-gate 	 * As the heap grows the kernel applies the advice automatically
6190Sstevel@tonic-gate 	 * to new portions of the heap.
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 	if (advice_heap >= 0) {
6220Sstevel@tonic-gate 		if (rc = mygetbrk(&brkbase, &brksize)) {
6230Sstevel@tonic-gate 			madverr(errfp, dgettext(TEXT_DOMAIN,
6240Sstevel@tonic-gate 			    "%s: /proc/self/status read failed [%s]\n"),
6250Sstevel@tonic-gate 			    madvident, strerror(rc));
6260Sstevel@tonic-gate 		} else {
6270Sstevel@tonic-gate 			MADVPRINT(4, (stderr, "brkbase 0x%x brksize 0x%x\n",
6280Sstevel@tonic-gate 			    brkbase, brksize));
6290Sstevel@tonic-gate 			/*
6300Sstevel@tonic-gate 			 * Align start address for memcntl and apply advice
6310Sstevel@tonic-gate 			 * on full pages of heap.  Create a page of heap if
6320Sstevel@tonic-gate 			 * it does not already exist.
6330Sstevel@tonic-gate 			 */
6340Sstevel@tonic-gate 			brkend = roundup(brkbase+brksize, pagesize);
6350Sstevel@tonic-gate 			brkbase = roundup(brkbase, pagesize);
6360Sstevel@tonic-gate 			brksize = brkend - brkbase;
6370Sstevel@tonic-gate 			if (brksize < pagesize) {
6380Sstevel@tonic-gate 				if (sbrk(pagesize) == (void *)-1) {
6390Sstevel@tonic-gate 					madverr(errfp, dgettext(TEXT_DOMAIN,
6400Sstevel@tonic-gate 					    "%s: sbrk failed [%s]\n"),
6410Sstevel@tonic-gate 					    madvident, strerror(errno));
6420Sstevel@tonic-gate 					goto out;
6430Sstevel@tonic-gate 				}
6440Sstevel@tonic-gate 				brksize = pagesize;
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 			MADVPRINT(1, (stderr, "heap advice: 0x%x 0x%x %d\n",
6470Sstevel@tonic-gate 			    brkbase, brksize, advice_heap));
6480Sstevel@tonic-gate 			if (memcntl((caddr_t)brkbase, brksize, MC_ADVISE,
6490Sstevel@tonic-gate 			    (caddr_t)(intptr_t)advice_heap, 0, 0) < 0) {
6500Sstevel@tonic-gate 				madverr(errfp, dgettext(TEXT_DOMAIN,
6510Sstevel@tonic-gate 				    "%s: memcntl() failed [%s]: heap advice\n"),
6520Sstevel@tonic-gate 				    madvident, strerror(errno));
6530Sstevel@tonic-gate 			}
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate out:
6570Sstevel@tonic-gate 	if (errfp) {
6580Sstevel@tonic-gate 		(void) fclose(errfp);
6590Sstevel@tonic-gate 		(void) setlocale(LC_MESSAGES, locale);
6600Sstevel@tonic-gate 	} else {
6610Sstevel@tonic-gate 		/* close log file: no-op if nothing logged to syslog */
6620Sstevel@tonic-gate 		closelog();
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate  * shmat interpose
6690Sstevel@tonic-gate  */
6700Sstevel@tonic-gate void *
shmat(int shmid,const void * shmaddr,int shmflag)6710Sstevel@tonic-gate shmat(int shmid, const void *shmaddr, int shmflag)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	static caddr_t (*shmatfunc)() = NULL;
6740Sstevel@tonic-gate 	void *result;
6750Sstevel@tonic-gate 	int advice = -1;
6760Sstevel@tonic-gate 	struct shmid_ds	mds;
6770Sstevel@tonic-gate #ifdef MADVDEBUG
6780Sstevel@tonic-gate 	int rc;
6790Sstevel@tonic-gate #else
6800Sstevel@tonic-gate 	/* LINTED */
6810Sstevel@tonic-gate 	int rc;
6820Sstevel@tonic-gate #endif
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	if (!shmatfunc) {
6850Sstevel@tonic-gate 		shmatfunc = (caddr_t (*)()) dlsym(RTLD_NEXT, "shmat");
6860Sstevel@tonic-gate 		assert(shmatfunc);
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	result = shmatfunc(shmid, shmaddr, shmflag);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * Options ism, dism take precedence over option shm.
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	if (advice_ism >= 0 && (shmflag & SHM_SHARE_MMU)) {
6950Sstevel@tonic-gate 		advice = advice_ism;
6960Sstevel@tonic-gate 	} else if (advice_dism >= 0 && (shmflag & SHM_PAGEABLE)) {
6970Sstevel@tonic-gate 		advice = advice_dism;
6980Sstevel@tonic-gate 	} else if (advice_shm >= 0) {
6990Sstevel@tonic-gate 		advice = advice_shm;
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * Apply advice if specified and shmat succeeded.
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	if (advice >= 0 && result != (void *)-1) {
7060Sstevel@tonic-gate 		/* First determine segment size */
7070Sstevel@tonic-gate 		rc = shmctl(shmid, IPC_STAT, &mds);
7080Sstevel@tonic-gate 		MADVPRINT(4, (stderr, "shmctl rc %d errno %d\n",
7090Sstevel@tonic-gate 		    strerror(errno)));
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		rc = memcntl(result, mds.shm_segsz, MC_ADVISE,
7120Sstevel@tonic-gate 		    (caddr_t)(intptr_t)advice, 0, 0);
7130Sstevel@tonic-gate 		MADVPRINT(1, (stderr,
7140Sstevel@tonic-gate 		    "shmat advice: 0x%x 0x%x %d, rc %d errno %d\n",
7150Sstevel@tonic-gate 		    result, mds.shm_segsz, advice, rc, errno));
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	return (result);
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate  * mmap interpose
7230Sstevel@tonic-gate  */
7240Sstevel@tonic-gate caddr_t
mmap(caddr_t addr,size_t len,int prot,int flags,int fd,off_t pos)7250Sstevel@tonic-gate mmap(caddr_t addr, size_t len, int prot, int flags, int fd, off_t pos)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	static caddr_t (*mmapfunc)() = NULL;
7280Sstevel@tonic-gate 	caddr_t result;
7290Sstevel@tonic-gate 	int advice = -1;
7300Sstevel@tonic-gate #ifdef MADVDEBUG
7310Sstevel@tonic-gate 	int rc;
7320Sstevel@tonic-gate #else
7330Sstevel@tonic-gate 	/* LINTED */
7340Sstevel@tonic-gate 	int rc;
7350Sstevel@tonic-gate #endif
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (!mmapfunc) {
7380Sstevel@tonic-gate 		mmapfunc = (caddr_t (*)()) dlsym(RTLD_NEXT, "mmap");
7390Sstevel@tonic-gate 		assert(mmapfunc);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	result = mmapfunc(addr, len, prot, flags, fd, pos);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * Option mapanon has highest precedence while option map
7460Sstevel@tonic-gate 	 * has lowest precedence.
7470Sstevel@tonic-gate 	 */
7480Sstevel@tonic-gate 	if (advice_mapanon >= 0 && (flags & MAP_ANON)) {
7490Sstevel@tonic-gate 		advice = advice_mapanon;
7500Sstevel@tonic-gate 	} else if (advice_mapshared >= 0 && (flags & MAP_SHARED)) {
7510Sstevel@tonic-gate 		advice = advice_mapshared;
7520Sstevel@tonic-gate 	} else if (advice_mapprivate >= 0 && (flags & MAP_PRIVATE)) {
7530Sstevel@tonic-gate 		advice = advice_mapprivate;
7540Sstevel@tonic-gate 	} else if (advice_map >= 0) {
7550Sstevel@tonic-gate 		advice = advice_map;
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/*
7590Sstevel@tonic-gate 	 * Apply advice if specified and mmap succeeded.
7600Sstevel@tonic-gate 	 */
7610Sstevel@tonic-gate 	if (advice >= 0 && result != MAP_FAILED) {
7620Sstevel@tonic-gate 		rc = memcntl(result, len, MC_ADVISE,
7630Sstevel@tonic-gate 		    (caddr_t)(intptr_t)advice, 0, 0);
7640Sstevel@tonic-gate 		MADVPRINT(1, (stderr,
7650Sstevel@tonic-gate 		    "mmap advice: 0x%x 0x%x %d, rc %d errno %d\n",
7660Sstevel@tonic-gate 		    result, len, advice, rc, errno));
7670Sstevel@tonic-gate 	}
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	return (result);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate #if !defined(_LP64)
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * mmap64 interpose
7750Sstevel@tonic-gate  */
7760Sstevel@tonic-gate caddr_t
mmap64(caddr_t addr,size_t len,int prot,int flags,int fd,off64_t pos)7770Sstevel@tonic-gate mmap64(caddr_t addr, size_t len, int prot, int flags, int fd, off64_t pos)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	static caddr_t (*mmap64func)();
7800Sstevel@tonic-gate 	caddr_t result;
7810Sstevel@tonic-gate 	int advice = -1;
7820Sstevel@tonic-gate #ifdef MADVDEBUG
7830Sstevel@tonic-gate 	int rc;
7840Sstevel@tonic-gate #else
7850Sstevel@tonic-gate 	/* LINTED */
7860Sstevel@tonic-gate 	int rc;
7870Sstevel@tonic-gate #endif
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	if (!mmap64func) {
7900Sstevel@tonic-gate 		mmap64func = (caddr_t (*)()) dlsym(RTLD_NEXT, "mmap64");
7910Sstevel@tonic-gate 		assert(mmap64func);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	result = mmap64func(addr, len, prot, flags, fd, pos);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/*
7970Sstevel@tonic-gate 	 * Option mapanon has highest precedence while option map
7980Sstevel@tonic-gate 	 * has lowest precedence.
7990Sstevel@tonic-gate 	 */
8000Sstevel@tonic-gate 	if (advice_mapanon >= 0 && (flags & MAP_ANON)) {
8010Sstevel@tonic-gate 		advice = advice_mapanon;
8020Sstevel@tonic-gate 	} else if (advice_mapshared >= 0 && (flags & MAP_SHARED)) {
8030Sstevel@tonic-gate 		advice = advice_mapshared;
8040Sstevel@tonic-gate 	} else if (advice_mapprivate >= 0 && (flags & MAP_PRIVATE)) {
8050Sstevel@tonic-gate 		advice = advice_mapprivate;
8060Sstevel@tonic-gate 	} else if (advice_map >= 0) {
8070Sstevel@tonic-gate 		advice = advice_map;
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * Apply advice if specified and mmap succeeded.
8120Sstevel@tonic-gate 	 */
8130Sstevel@tonic-gate 	if (advice >= 0 && result != MAP_FAILED) {
8140Sstevel@tonic-gate 		rc = memcntl(result, len, MC_ADVISE, (caddr_t)advice, 0, 0);
8150Sstevel@tonic-gate 		MADVPRINT(1, (stderr,
8160Sstevel@tonic-gate 		    "mmap64 advice: 0x%x 0x%x %d, rc %d errno %d\n",
8170Sstevel@tonic-gate 		    result, len, advice, rc, errno));
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	return (result);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate #endif	/* !_LP64 */
823