/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <syslog.h>
#include <sys/sysmacros.h>
#include <procfs.h>
#include <dlfcn.h>
#include <assert.h>
#include <libintl.h>
#include <locale.h>
extern int gmatch(const char *s, const char *p);
#pragma init(__madvmain)
static int pagesize;
/* environment variables */
/* config file */
/*
* avoid malloc which causes certain applications to crash
*/
#ifdef MADVDEBUG
static int madvdebug = 0;
#else
#define MADVPRINT(x, y)
#endif
/*
* advice options
*/
static char *legal_optstr[] = {
"madv",
"heap",
"shm",
"ism",
"dism",
"map",
"mapshared",
"mapprivate",
"mapanon",
};
enum optenum {
};
/*
* Advice values
* These need to correspond to the order of the MADV_ flags in mman.h
* since the position infers the value for the flag.
*/
static char *legal_madvice[] = {
"normal",
"random",
"sequential",
"willneed_NOT_SUPPORTED!",
"dontneed_NOT_SUPPORTED!",
"free_NOT_SUPPORTED!",
"access_default",
"access_lwp",
"access_many",
"access_many_pset",
};
#if !defined(TEXT_DOMAIN)
#endif
/*PRINTFLIKE2*/
static void
{
if (fp)
else
}
/*
* Return the pointer to the fully-resolved path name of the process's
* executable file obtained from the AT_SUN_EXECNAME aux vector entry.
*/
static const char *
mygetexecname(void)
{
/*
* The first time through, read the initial aux vector that was
* passed to the process at exec(2). Only do this once.
*/
if (fd >= 0) {
break;
}
}
}
return (execname);
}
/*
* Return the process's current brk base and size.
*/
static int
{
int fd;
int rc;
if (fd >= 0) {
rc = 0;
} else {
}
} else {
}
return (rc);
}
/*
* Check if exec name matches cfgname found in madv cfg file.
*/
static int
{
const char *ename;
int rc;
/* cfgname should not have a '/' unless it begins with one */
if (cfgname[0] == '/') {
/*
* if execname does not begin with a '/', prepend the
* current directory.
*/
if (execname[0] != '/') {
} else
} else { /* simple cfg name */
/* execname is a path name - get the base name */
ename++;
else
}
return (rc);
}
/*
* Check if string matches any of exec arguments.
*/
static int
{
int fd;
int rc = 0;
int arg;
char **argv;
if (fd >= 0) {
argv++;
break;
}
}
} else {
}
} else {
}
return (rc);
}
static int
{
char c;
str++;
return (*str == '\0');
}
static int
{
}
static void
{
char *value;
int opt;
int advice = 0;
while (*optstr != '\0') {
if (opt < 0) {
"%s: invalid advice option (%s)"
" for %s - cfgfile: %s, line: %d\n"),
break;
} else if (!value) {
"%s: option missing advice"
" for %s - cfgfile: %s, line: %d\n"),
break;
}
if (advice < 0) {
"%s: invalid advice specified (%s)"
" for %s - cfgfile: %s, line: %d\n"),
break;
}
switch (opt) {
case OPT_MADV:
advice_all = advice;
break;
case OPT_HEAP:
if (advice_heap < 0) {
} else {
"%s: duplicate advice specified "
"(%s) for %s - cfgfile: %s, line: %d\n"),
lineno);
}
break;
case OPT_SHM:
if (advice_shm < 0) {
advice_shm = advice;
} else {
"%s: duplicate advice specified "
"(%s) for %s - cfgfile: %s, line: %d\n"),
lineno);
}
break;
case OPT_ISM:
if (advice_ism < 0) {
advice_ism = advice;
} else {
"%s: duplicate advice specified "
"(%s) for %s - cfgfile: %s, line: %d\n"),
lineno);
}
break;
case OPT_DISM:
if (advice_dism < 0) {
} else {
"%s: duplicate advice specified "
"(%s) for %s - cfgfile: %s, line: %d\n"),
lineno);
}
break;
case OPT_MAP:
if (advice_map < 0) {
advice_map = advice;
} else {
"%s: duplicate advice specified "
"(%s) for %s - cfgfile: %s, line: %d\n"),
lineno);
}
break;
case OPT_MAPSHARED:
if (advice_mapshared < 0) {
} else {
"%s: duplicate advice specified "
"(%s) for %s - cfgfile: %s, line: %d\n"),
lineno);
}
break;
case OPT_MAPPRIVATE:
if (advice_mapprivate < 0) {
} else {
"%s: duplicate advice specified "
"(%s) for %s - cfgfile: %s, line: %d\n"),
lineno);
}
break;
case OPT_MAPANON:
if (advice_mapanon < 0) {
} else {
"%s: duplicate advice specified "
"(%s) for %s - cfgfile: %s, line: %d\n"),
lineno);
}
break;
default:
"%s: invalid advice option (%s)"
" for %s - cfgfile: %s, line: %d\n"),
break;
}
}
}
static void
{
const char *execname;
char *cwd;
int cwdlen;
int lineno = 0;
int advice;
int rc;
char *locale;
/*
* If a private error file is indicated then set the locale
* for error messages for the duration of this routine.
* Error messages destined for syslog should not be translated
* and thus come from the default C locale.
*/
if (errfp) {
} else {
"%s: cannot open error file: %s [%s]\n"),
}
}
#ifdef MADVDEBUG
#endif
advice_all = advice;
else
"%s: invalid advice specified: MADV=%s\n"),
}
/*
* Open specified cfg file or default one.
*/
if (!fp) {
"%s: cannot open configuration file: %s [%s]\n"),
}
} else {
}
if (fp) {
execname = mygetexecname();
if (!cwd)
return;
lineno++;
/*
* Make sure line wasn't truncated.
*/
"%s: invalid entry, "
"line too long - cfgfile:"
" %s, line: %d\n"),
continue;
}
continue;
/*
* Get advice options.
* Parse right to left in case delimiter is in name.
*/
"%s: no delimiter specified - cfgfile:"
" %s, line: %d\n"),
continue;
}
*tokadv++ = '\0';
/*
* Remove newline from end of advice options.
*/
*str = '\0';
/*
* Get optional argument string.
*/
*tokarg++ = '\0';
}
/*
* Compare exec name.
*/
continue;
}
/*
* Compare arguments if argument string specified.
*/
if (tokarg &&
continue;
}
/*
* Parse advice options.
* If empty, any advice from ENV_MADV is reset.
*/
advice_all = -1;
} else {
}
break;
}
}
/*
* Pagesize needed for proper aligning by brk interpose.
*/
/*
* Apply global advice if set.
* Specific options in the cfgfile take precedence.
*/
if (advice_all >= 0) {
if (advice_heap < 0)
if (advice_shm < 0)
if (advice_map < 0)
}
/*
* If heap advice is specified, apply it to the existing heap.
* As the heap grows the kernel applies the advice automatically
* to new portions of the heap.
*/
if (advice_heap >= 0) {
} else {
/*
* Align start address for memcntl and apply advice
* on full pages of heap. Create a page of heap if
* it does not already exist.
*/
"%s: sbrk failed [%s]\n"),
goto out;
}
}
"%s: memcntl() failed [%s]: heap advice\n"),
}
}
}
out:
if (errfp) {
} else {
/* close log file: no-op if nothing logged to syslog */
closelog();
}
}
/*
* shmat interpose
*/
void *
{
void *result;
#ifdef MADVDEBUG
int rc;
#else
/* LINTED */
int rc;
#endif
if (!shmatfunc) {
}
/*
* Options ism, dism take precedence over option shm.
*/
advice = advice_ism;
} else if (advice_shm >= 0) {
advice = advice_shm;
}
/*
* Apply advice if specified and shmat succeeded.
*/
/* First determine segment size */
"shmat advice: 0x%x 0x%x %d, rc %d errno %d\n",
}
return (result);
}
/*
* mmap interpose
*/
void *
{
void *result;
#ifdef MADVDEBUG
int rc;
#else
/* LINTED */
int rc;
#endif
if (!mmapfunc) {
}
/*
* Option mapanon has highest precedence while option map
* has lowest precedence.
*/
} else if (advice_map >= 0) {
advice = advice_map;
}
/*
* Apply advice if specified and mmap succeeded.
*/
"mmap advice: 0x%x 0x%x %d, rc %d errno %d\n",
}
return (result);
}
#if !defined(_LP64)
/*
* mmap64 interpose
*/
void *
{
static void *(*mmap64func)();
void *result;
#ifdef MADVDEBUG
int rc;
#else
/* LINTED */
int rc;
#endif
if (!mmap64func) {
}
/*
* Option mapanon has highest precedence while option map
* has lowest precedence.
*/
} else if (advice_map >= 0) {
advice = advice_map;
}
/*
* Apply advice if specified and mmap succeeded.
*/
"mmap64 advice: 0x%x 0x%x %d, rc %d errno %d\n",
}
return (result);
}
#endif /* !_LP64 */